scep_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package provisioner
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "go.step.sm/linkedca"
  12. )
  13. func Test_challengeValidationController_Validate(t *testing.T) {
  14. type request struct {
  15. Challenge string `json:"scepChallenge"`
  16. TransactionID string `json:"scepTransactionID"`
  17. }
  18. type response struct {
  19. Allow bool `json:"allow"`
  20. }
  21. nokServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  22. req := &request{}
  23. err := json.NewDecoder(r.Body).Decode(req)
  24. require.NoError(t, err)
  25. assert.Equal(t, "not-allowed", req.Challenge)
  26. assert.Equal(t, "transaction-1", req.TransactionID)
  27. b, err := json.Marshal(response{Allow: false})
  28. require.NoError(t, err)
  29. w.WriteHeader(200)
  30. w.Write(b)
  31. }))
  32. okServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  33. req := &request{}
  34. err := json.NewDecoder(r.Body).Decode(req)
  35. require.NoError(t, err)
  36. assert.Equal(t, "challenge", req.Challenge)
  37. assert.Equal(t, "transaction-1", req.TransactionID)
  38. b, err := json.Marshal(response{Allow: true})
  39. require.NoError(t, err)
  40. w.WriteHeader(200)
  41. w.Write(b)
  42. }))
  43. type fields struct {
  44. client *http.Client
  45. webhooks []*Webhook
  46. }
  47. type args struct {
  48. challenge string
  49. transactionID string
  50. }
  51. tests := []struct {
  52. name string
  53. fields fields
  54. args args
  55. server *httptest.Server
  56. expErr error
  57. }{
  58. {
  59. name: "fail/no-webhook",
  60. fields: fields{http.DefaultClient, nil},
  61. args: args{"no-webhook", "transaction-1"},
  62. expErr: errors.New("webhook server did not allow request"),
  63. },
  64. {
  65. name: "fail/wrong-cert-type",
  66. fields: fields{http.DefaultClient, []*Webhook{
  67. {
  68. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  69. CertType: linkedca.Webhook_SSH.String(),
  70. },
  71. }},
  72. args: args{"wrong-cert-type", "transaction-1"},
  73. expErr: errors.New("webhook server did not allow request"),
  74. },
  75. {
  76. name: "fail/wrong-secret-value",
  77. fields: fields{http.DefaultClient, []*Webhook{
  78. {
  79. ID: "webhook-id-1",
  80. Name: "webhook-name-1",
  81. Secret: "{{}}",
  82. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  83. CertType: linkedca.Webhook_X509.String(),
  84. URL: okServer.URL,
  85. },
  86. }},
  87. args: args{
  88. challenge: "wrong-secret-value",
  89. transactionID: "transaction-1",
  90. },
  91. expErr: errors.New("failed executing webhook request: illegal base64 data at input byte 0"),
  92. },
  93. {
  94. name: "fail/not-allowed",
  95. fields: fields{http.DefaultClient, []*Webhook{
  96. {
  97. ID: "webhook-id-1",
  98. Name: "webhook-name-1",
  99. Secret: "MTIzNAo=",
  100. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  101. CertType: linkedca.Webhook_X509.String(),
  102. URL: nokServer.URL,
  103. },
  104. }},
  105. args: args{
  106. challenge: "not-allowed",
  107. transactionID: "transaction-1",
  108. },
  109. server: nokServer,
  110. expErr: errors.New("webhook server did not allow request"),
  111. },
  112. {
  113. name: "ok",
  114. fields: fields{http.DefaultClient, []*Webhook{
  115. {
  116. ID: "webhook-id-1",
  117. Name: "webhook-name-1",
  118. Secret: "MTIzNAo=",
  119. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  120. CertType: linkedca.Webhook_X509.String(),
  121. URL: okServer.URL,
  122. },
  123. }},
  124. args: args{
  125. challenge: "challenge",
  126. transactionID: "transaction-1",
  127. },
  128. server: okServer,
  129. },
  130. }
  131. for _, tt := range tests {
  132. t.Run(tt.name, func(t *testing.T) {
  133. c := newChallengeValidationController(tt.fields.client, tt.fields.webhooks)
  134. if tt.server != nil {
  135. defer tt.server.Close()
  136. }
  137. ctx := context.Background()
  138. err := c.Validate(ctx, tt.args.challenge, tt.args.transactionID)
  139. if tt.expErr != nil {
  140. assert.EqualError(t, err, tt.expErr.Error())
  141. return
  142. }
  143. assert.NoError(t, err)
  144. })
  145. }
  146. }
  147. func TestController_isCertTypeOK(t *testing.T) {
  148. assert.True(t, isCertTypeOK(&Webhook{CertType: linkedca.Webhook_X509.String()}))
  149. assert.True(t, isCertTypeOK(&Webhook{CertType: linkedca.Webhook_ALL.String()}))
  150. assert.True(t, isCertTypeOK(&Webhook{CertType: ""}))
  151. assert.False(t, isCertTypeOK(&Webhook{CertType: linkedca.Webhook_SSH.String()}))
  152. }
  153. func Test_selectValidationMethod(t *testing.T) {
  154. tests := []struct {
  155. name string
  156. p *SCEP
  157. want validationMethod
  158. }{
  159. {"webhooks", &SCEP{
  160. Name: "SCEP",
  161. Type: "SCEP",
  162. Options: &Options{
  163. Webhooks: []*Webhook{
  164. {
  165. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  166. },
  167. },
  168. },
  169. }, "webhook"},
  170. {"challenge", &SCEP{
  171. Name: "SCEP",
  172. Type: "SCEP",
  173. ChallengePassword: "pass",
  174. }, "static"},
  175. {"challenge-with-different-webhook", &SCEP{
  176. Name: "SCEP",
  177. Type: "SCEP",
  178. Options: &Options{
  179. Webhooks: []*Webhook{
  180. {
  181. Kind: linkedca.Webhook_AUTHORIZING.String(),
  182. },
  183. },
  184. },
  185. ChallengePassword: "pass",
  186. }, "static"},
  187. {"none", &SCEP{
  188. Name: "SCEP",
  189. Type: "SCEP",
  190. }, "none"},
  191. {"none-with-different-webhook", &SCEP{
  192. Name: "SCEP",
  193. Type: "SCEP",
  194. Options: &Options{
  195. Webhooks: []*Webhook{
  196. {
  197. Kind: linkedca.Webhook_AUTHORIZING.String(),
  198. },
  199. },
  200. },
  201. }, "none"},
  202. }
  203. for _, tt := range tests {
  204. t.Run(tt.name, func(t *testing.T) {
  205. err := tt.p.Init(Config{Claims: globalProvisionerClaims})
  206. require.NoError(t, err)
  207. got := tt.p.selectValidationMethod()
  208. assert.Equal(t, tt.want, got)
  209. })
  210. }
  211. }
  212. func TestSCEP_ValidateChallenge(t *testing.T) {
  213. type request struct {
  214. Challenge string `json:"scepChallenge"`
  215. TransactionID string `json:"scepTransactionID"`
  216. }
  217. type response struct {
  218. Allow bool `json:"allow"`
  219. }
  220. okServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  221. req := &request{}
  222. err := json.NewDecoder(r.Body).Decode(req)
  223. require.NoError(t, err)
  224. assert.Equal(t, "webhook-challenge", req.Challenge)
  225. assert.Equal(t, "webhook-transaction-1", req.TransactionID)
  226. b, err := json.Marshal(response{Allow: true})
  227. require.NoError(t, err)
  228. w.WriteHeader(200)
  229. w.Write(b)
  230. }))
  231. type args struct {
  232. challenge string
  233. transactionID string
  234. }
  235. tests := []struct {
  236. name string
  237. p *SCEP
  238. server *httptest.Server
  239. args args
  240. expErr error
  241. }{
  242. {"ok/webhooks", &SCEP{
  243. Name: "SCEP",
  244. Type: "SCEP",
  245. Options: &Options{
  246. Webhooks: []*Webhook{
  247. {
  248. ID: "webhook-id-1",
  249. Name: "webhook-name-1",
  250. Secret: "MTIzNAo=",
  251. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  252. CertType: linkedca.Webhook_X509.String(),
  253. URL: okServer.URL,
  254. },
  255. },
  256. },
  257. }, okServer, args{"webhook-challenge", "webhook-transaction-1"},
  258. nil,
  259. },
  260. {"fail/webhooks-secret-configuration", &SCEP{
  261. Name: "SCEP",
  262. Type: "SCEP",
  263. Options: &Options{
  264. Webhooks: []*Webhook{
  265. {
  266. ID: "webhook-id-1",
  267. Name: "webhook-name-1",
  268. Secret: "{{}}",
  269. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  270. CertType: linkedca.Webhook_X509.String(),
  271. URL: okServer.URL,
  272. },
  273. },
  274. },
  275. }, nil, args{"webhook-challenge", "webhook-transaction-1"},
  276. errors.New("failed executing webhook request: illegal base64 data at input byte 0"),
  277. },
  278. {"ok/static-challenge", &SCEP{
  279. Name: "SCEP",
  280. Type: "SCEP",
  281. Options: &Options{},
  282. ChallengePassword: "secret-static-challenge",
  283. }, nil, args{"secret-static-challenge", "static-transaction-1"},
  284. nil,
  285. },
  286. {"fail/wrong-static-challenge", &SCEP{
  287. Name: "SCEP",
  288. Type: "SCEP",
  289. Options: &Options{},
  290. ChallengePassword: "secret-static-challenge",
  291. }, nil, args{"the-wrong-challenge-secret", "static-transaction-1"},
  292. errors.New("invalid challenge password provided"),
  293. },
  294. {"ok/no-challenge", &SCEP{
  295. Name: "SCEP",
  296. Type: "SCEP",
  297. Options: &Options{},
  298. ChallengePassword: "",
  299. }, nil, args{"", "static-transaction-1"},
  300. nil,
  301. },
  302. {"fail/no-challenge-but-provided", &SCEP{
  303. Name: "SCEP",
  304. Type: "SCEP",
  305. Options: &Options{},
  306. ChallengePassword: "",
  307. }, nil, args{"a-challenge-value", "static-transaction-1"},
  308. errors.New("invalid challenge password provided"),
  309. },
  310. }
  311. for _, tt := range tests {
  312. t.Run(tt.name, func(t *testing.T) {
  313. if tt.server != nil {
  314. defer tt.server.Close()
  315. }
  316. err := tt.p.Init(Config{Claims: globalProvisionerClaims, WebhookClient: http.DefaultClient})
  317. require.NoError(t, err)
  318. ctx := context.Background()
  319. err = tt.p.ValidateChallenge(ctx, tt.args.challenge, tt.args.transactionID)
  320. if tt.expErr != nil {
  321. assert.EqualError(t, err, tt.expErr.Error())
  322. return
  323. }
  324. assert.NoError(t, err)
  325. })
  326. }
  327. }