scep_test.go 9.9 KB

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