acme_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "net/http"
  9. "net/http/httptest"
  10. "strings"
  11. "testing"
  12. "github.com/go-chi/chi"
  13. "github.com/smallstep/assert"
  14. "github.com/smallstep/certificates/authority/admin"
  15. "github.com/smallstep/certificates/authority/provisioner"
  16. "go.step.sm/linkedca"
  17. "google.golang.org/protobuf/encoding/protojson"
  18. "google.golang.org/protobuf/proto"
  19. )
  20. func readProtoJSON(r io.ReadCloser, m proto.Message) error {
  21. defer r.Close()
  22. data, err := io.ReadAll(r)
  23. if err != nil {
  24. return err
  25. }
  26. return protojson.Unmarshal(data, m)
  27. }
  28. func TestHandler_requireEABEnabled(t *testing.T) {
  29. type test struct {
  30. ctx context.Context
  31. adminDB admin.DB
  32. auth adminAuthority
  33. next nextHTTP
  34. err *admin.Error
  35. statusCode int
  36. }
  37. var tests = map[string]func(t *testing.T) test{
  38. "fail/h.provisionerHasEABEnabled": func(t *testing.T) test {
  39. chiCtx := chi.NewRouteContext()
  40. chiCtx.URLParams.Add("provisionerName", "provName")
  41. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  42. auth := &mockAdminAuthority{
  43. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  44. assert.Equals(t, "provName", name)
  45. return nil, errors.New("force")
  46. },
  47. }
  48. err := admin.NewErrorISE("error loading provisioner provName: force")
  49. err.Message = "error loading provisioner provName: force"
  50. return test{
  51. ctx: ctx,
  52. auth: auth,
  53. err: err,
  54. statusCode: 500,
  55. }
  56. },
  57. "ok/eab-disabled": func(t *testing.T) test {
  58. chiCtx := chi.NewRouteContext()
  59. chiCtx.URLParams.Add("provisionerName", "provName")
  60. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  61. auth := &mockAdminAuthority{
  62. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  63. assert.Equals(t, "provName", name)
  64. return &provisioner.MockProvisioner{
  65. MgetID: func() string {
  66. return "provID"
  67. },
  68. }, nil
  69. },
  70. }
  71. db := &admin.MockDB{
  72. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  73. assert.Equals(t, "provID", id)
  74. return &linkedca.Provisioner{
  75. Id: "provID",
  76. Name: "provName",
  77. Details: &linkedca.ProvisionerDetails{
  78. Data: &linkedca.ProvisionerDetails_ACME{
  79. ACME: &linkedca.ACMEProvisioner{
  80. RequireEab: false,
  81. },
  82. },
  83. },
  84. }, nil
  85. },
  86. }
  87. err := admin.NewError(admin.ErrorBadRequestType, "ACME EAB not enabled for provisioner provName")
  88. err.Message = "ACME EAB not enabled for provisioner provName"
  89. return test{
  90. ctx: ctx,
  91. auth: auth,
  92. adminDB: db,
  93. err: err,
  94. statusCode: 400,
  95. }
  96. },
  97. "ok/eab-enabled": func(t *testing.T) test {
  98. chiCtx := chi.NewRouteContext()
  99. chiCtx.URLParams.Add("provisionerName", "provName")
  100. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  101. auth := &mockAdminAuthority{
  102. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  103. assert.Equals(t, "provName", name)
  104. return &provisioner.MockProvisioner{
  105. MgetID: func() string {
  106. return "provID"
  107. },
  108. }, nil
  109. },
  110. }
  111. db := &admin.MockDB{
  112. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  113. assert.Equals(t, "provID", id)
  114. return &linkedca.Provisioner{
  115. Id: "provID",
  116. Name: "provName",
  117. Details: &linkedca.ProvisionerDetails{
  118. Data: &linkedca.ProvisionerDetails_ACME{
  119. ACME: &linkedca.ACMEProvisioner{
  120. RequireEab: true,
  121. },
  122. },
  123. },
  124. }, nil
  125. },
  126. }
  127. return test{
  128. ctx: ctx,
  129. auth: auth,
  130. adminDB: db,
  131. next: func(w http.ResponseWriter, r *http.Request) {
  132. w.Write(nil) // mock response with status 200
  133. },
  134. statusCode: 200,
  135. }
  136. },
  137. }
  138. for name, prep := range tests {
  139. tc := prep(t)
  140. t.Run(name, func(t *testing.T) {
  141. h := &Handler{
  142. auth: tc.auth,
  143. adminDB: tc.adminDB,
  144. acmeDB: nil,
  145. }
  146. req := httptest.NewRequest("GET", "/foo", nil) // chi routing is prepared in test setup
  147. req = req.WithContext(tc.ctx)
  148. w := httptest.NewRecorder()
  149. h.requireEABEnabled(tc.next)(w, req)
  150. res := w.Result()
  151. assert.Equals(t, tc.statusCode, res.StatusCode)
  152. body, err := io.ReadAll(res.Body)
  153. res.Body.Close()
  154. assert.FatalError(t, err)
  155. if res.StatusCode >= 400 {
  156. err := admin.Error{}
  157. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &err))
  158. assert.Equals(t, tc.err.Type, err.Type)
  159. assert.Equals(t, tc.err.Message, err.Message)
  160. assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
  161. assert.Equals(t, tc.err.Detail, err.Detail)
  162. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  163. return
  164. }
  165. })
  166. }
  167. }
  168. func TestHandler_provisionerHasEABEnabled(t *testing.T) {
  169. type test struct {
  170. adminDB admin.DB
  171. auth adminAuthority
  172. provisionerName string
  173. want bool
  174. err *admin.Error
  175. }
  176. var tests = map[string]func(t *testing.T) test{
  177. "fail/auth.LoadProvisionerByName": func(t *testing.T) test {
  178. auth := &mockAdminAuthority{
  179. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  180. assert.Equals(t, "provName", name)
  181. return nil, errors.New("force")
  182. },
  183. }
  184. return test{
  185. auth: auth,
  186. provisionerName: "provName",
  187. want: false,
  188. err: admin.WrapErrorISE(errors.New("force"), "error loading provisioner provName"),
  189. }
  190. },
  191. "fail/db.GetProvisioner": func(t *testing.T) test {
  192. auth := &mockAdminAuthority{
  193. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  194. assert.Equals(t, "provName", name)
  195. return &provisioner.MockProvisioner{
  196. MgetID: func() string {
  197. return "provID"
  198. },
  199. }, nil
  200. },
  201. }
  202. db := &admin.MockDB{
  203. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  204. assert.Equals(t, "provID", id)
  205. return nil, errors.New("force")
  206. },
  207. }
  208. return test{
  209. auth: auth,
  210. adminDB: db,
  211. provisionerName: "provName",
  212. want: false,
  213. err: admin.WrapErrorISE(errors.New("force"), "error loading provisioner provName"),
  214. }
  215. },
  216. "fail/prov.GetDetails": func(t *testing.T) test {
  217. auth := &mockAdminAuthority{
  218. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  219. assert.Equals(t, "provName", name)
  220. return &provisioner.MockProvisioner{
  221. MgetID: func() string {
  222. return "provID"
  223. },
  224. }, nil
  225. },
  226. }
  227. db := &admin.MockDB{
  228. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  229. assert.Equals(t, "provID", id)
  230. return &linkedca.Provisioner{
  231. Id: "provID",
  232. Name: "provName",
  233. Details: nil,
  234. }, nil
  235. },
  236. }
  237. return test{
  238. auth: auth,
  239. adminDB: db,
  240. provisionerName: "provName",
  241. want: false,
  242. err: admin.WrapErrorISE(errors.New("force"), "error loading provisioner provName"),
  243. }
  244. },
  245. "fail/details.GetACME": func(t *testing.T) test {
  246. auth := &mockAdminAuthority{
  247. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  248. assert.Equals(t, "provName", name)
  249. return &provisioner.MockProvisioner{
  250. MgetID: func() string {
  251. return "provID"
  252. },
  253. }, nil
  254. },
  255. }
  256. db := &admin.MockDB{
  257. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  258. assert.Equals(t, "provID", id)
  259. return &linkedca.Provisioner{
  260. Id: "provID",
  261. Name: "provName",
  262. Details: &linkedca.ProvisionerDetails{
  263. Data: &linkedca.ProvisionerDetails_ACME{
  264. ACME: nil,
  265. },
  266. },
  267. }, nil
  268. },
  269. }
  270. return test{
  271. auth: auth,
  272. adminDB: db,
  273. provisionerName: "provName",
  274. want: false,
  275. err: admin.WrapErrorISE(errors.New("force"), "error loading provisioner provName"),
  276. }
  277. },
  278. "ok/eab-disabled": func(t *testing.T) test {
  279. auth := &mockAdminAuthority{
  280. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  281. assert.Equals(t, "eab-disabled", name)
  282. return &provisioner.MockProvisioner{
  283. MgetID: func() string {
  284. return "provID"
  285. },
  286. }, nil
  287. },
  288. }
  289. db := &admin.MockDB{
  290. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  291. assert.Equals(t, "provID", id)
  292. return &linkedca.Provisioner{
  293. Id: "provID",
  294. Name: "eab-disabled",
  295. Details: &linkedca.ProvisionerDetails{
  296. Data: &linkedca.ProvisionerDetails_ACME{
  297. ACME: &linkedca.ACMEProvisioner{
  298. RequireEab: false,
  299. },
  300. },
  301. },
  302. }, nil
  303. },
  304. }
  305. return test{
  306. adminDB: db,
  307. auth: auth,
  308. provisionerName: "eab-disabled",
  309. want: false,
  310. }
  311. },
  312. "ok/eab-enabled": func(t *testing.T) test {
  313. auth := &mockAdminAuthority{
  314. MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
  315. assert.Equals(t, "eab-enabled", name)
  316. return &provisioner.MockProvisioner{
  317. MgetID: func() string {
  318. return "provID"
  319. },
  320. }, nil
  321. },
  322. }
  323. db := &admin.MockDB{
  324. MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  325. assert.Equals(t, "provID", id)
  326. return &linkedca.Provisioner{
  327. Id: "provID",
  328. Name: "eab-enabled",
  329. Details: &linkedca.ProvisionerDetails{
  330. Data: &linkedca.ProvisionerDetails_ACME{
  331. ACME: &linkedca.ACMEProvisioner{
  332. RequireEab: true,
  333. },
  334. },
  335. },
  336. }, nil
  337. },
  338. }
  339. return test{
  340. adminDB: db,
  341. auth: auth,
  342. provisionerName: "eab-enabled",
  343. want: true,
  344. }
  345. },
  346. }
  347. for name, prep := range tests {
  348. tc := prep(t)
  349. t.Run(name, func(t *testing.T) {
  350. h := &Handler{
  351. auth: tc.auth,
  352. adminDB: tc.adminDB,
  353. acmeDB: nil,
  354. }
  355. got, prov, err := h.provisionerHasEABEnabled(context.TODO(), tc.provisionerName)
  356. if (err != nil) != (tc.err != nil) {
  357. t.Errorf("Handler.provisionerHasEABEnabled() error = %v, want err %v", err, tc.err)
  358. return
  359. }
  360. if tc.err != nil {
  361. assert.Type(t, &linkedca.Provisioner{}, prov)
  362. assert.Type(t, &admin.Error{}, err)
  363. adminError, _ := err.(*admin.Error)
  364. assert.Equals(t, tc.err.Type, adminError.Type)
  365. assert.Equals(t, tc.err.Status, adminError.Status)
  366. assert.Equals(t, tc.err.StatusCode(), adminError.StatusCode())
  367. assert.Equals(t, tc.err.Message, adminError.Message)
  368. assert.Equals(t, tc.err.Detail, adminError.Detail)
  369. return
  370. }
  371. if got != tc.want {
  372. t.Errorf("Handler.provisionerHasEABEnabled() = %v, want %v", got, tc.want)
  373. }
  374. })
  375. }
  376. }
  377. func TestCreateExternalAccountKeyRequest_Validate(t *testing.T) {
  378. type fields struct {
  379. Reference string
  380. }
  381. tests := []struct {
  382. name string
  383. fields fields
  384. wantErr bool
  385. }{
  386. {
  387. name: "fail/reference-too-long",
  388. fields: fields{
  389. Reference: strings.Repeat("A", 257),
  390. },
  391. wantErr: true,
  392. },
  393. {
  394. name: "ok/empty-reference",
  395. fields: fields{
  396. Reference: "",
  397. },
  398. wantErr: false,
  399. },
  400. {
  401. name: "ok",
  402. fields: fields{
  403. Reference: "my-eab-reference",
  404. },
  405. wantErr: false,
  406. },
  407. }
  408. for _, tt := range tests {
  409. t.Run(tt.name, func(t *testing.T) {
  410. r := &CreateExternalAccountKeyRequest{
  411. Reference: tt.fields.Reference,
  412. }
  413. if err := r.Validate(); (err != nil) != tt.wantErr {
  414. t.Errorf("CreateExternalAccountKeyRequest.Validate() error = %v, wantErr %v", err, tt.wantErr)
  415. }
  416. })
  417. }
  418. }
  419. func TestHandler_CreateExternalAccountKey(t *testing.T) {
  420. type test struct {
  421. ctx context.Context
  422. statusCode int
  423. err *admin.Error
  424. }
  425. var tests = map[string]func(t *testing.T) test{
  426. "ok": func(t *testing.T) test {
  427. chiCtx := chi.NewRouteContext()
  428. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  429. return test{
  430. ctx: ctx,
  431. statusCode: 501,
  432. err: &admin.Error{
  433. Type: admin.ErrorNotImplementedType.String(),
  434. Status: http.StatusNotImplemented,
  435. Message: "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm",
  436. Detail: "not implemented",
  437. },
  438. }
  439. },
  440. }
  441. for name, prep := range tests {
  442. tc := prep(t)
  443. t.Run(name, func(t *testing.T) {
  444. req := httptest.NewRequest("POST", "/foo", nil) // chi routing is prepared in test setup
  445. req = req.WithContext(tc.ctx)
  446. w := httptest.NewRecorder()
  447. acmeResponder := NewACMEAdminResponder()
  448. acmeResponder.CreateExternalAccountKey(w, req)
  449. res := w.Result()
  450. assert.Equals(t, tc.statusCode, res.StatusCode)
  451. body, err := io.ReadAll(res.Body)
  452. res.Body.Close()
  453. assert.FatalError(t, err)
  454. adminErr := admin.Error{}
  455. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  456. assert.Equals(t, tc.err.Type, adminErr.Type)
  457. assert.Equals(t, tc.err.Message, adminErr.Message)
  458. assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
  459. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  460. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  461. })
  462. }
  463. }
  464. func TestHandler_DeleteExternalAccountKey(t *testing.T) {
  465. type test struct {
  466. ctx context.Context
  467. statusCode int
  468. err *admin.Error
  469. }
  470. var tests = map[string]func(t *testing.T) test{
  471. "ok": func(t *testing.T) test {
  472. chiCtx := chi.NewRouteContext()
  473. chiCtx.URLParams.Add("provisionerName", "provName")
  474. chiCtx.URLParams.Add("id", "keyID")
  475. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  476. return test{
  477. ctx: ctx,
  478. statusCode: 501,
  479. err: &admin.Error{
  480. Type: admin.ErrorNotImplementedType.String(),
  481. Status: http.StatusNotImplemented,
  482. Message: "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm",
  483. Detail: "not implemented",
  484. },
  485. }
  486. },
  487. }
  488. for name, prep := range tests {
  489. tc := prep(t)
  490. t.Run(name, func(t *testing.T) {
  491. req := httptest.NewRequest("DELETE", "/foo", nil) // chi routing is prepared in test setup
  492. req = req.WithContext(tc.ctx)
  493. w := httptest.NewRecorder()
  494. acmeResponder := NewACMEAdminResponder()
  495. acmeResponder.DeleteExternalAccountKey(w, req)
  496. res := w.Result()
  497. assert.Equals(t, tc.statusCode, res.StatusCode)
  498. body, err := io.ReadAll(res.Body)
  499. res.Body.Close()
  500. assert.FatalError(t, err)
  501. adminErr := admin.Error{}
  502. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  503. assert.Equals(t, tc.err.Type, adminErr.Type)
  504. assert.Equals(t, tc.err.Message, adminErr.Message)
  505. assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
  506. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  507. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  508. })
  509. }
  510. }
  511. func TestHandler_GetExternalAccountKeys(t *testing.T) {
  512. type test struct {
  513. ctx context.Context
  514. statusCode int
  515. req *http.Request
  516. err *admin.Error
  517. }
  518. var tests = map[string]func(t *testing.T) test{
  519. "ok": func(t *testing.T) test {
  520. chiCtx := chi.NewRouteContext()
  521. chiCtx.URLParams.Add("provisionerName", "provName")
  522. req := httptest.NewRequest("GET", "/foo", nil)
  523. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  524. return test{
  525. ctx: ctx,
  526. statusCode: 501,
  527. req: req,
  528. err: &admin.Error{
  529. Type: admin.ErrorNotImplementedType.String(),
  530. Status: http.StatusNotImplemented,
  531. Message: "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm",
  532. Detail: "not implemented",
  533. },
  534. }
  535. },
  536. }
  537. for name, prep := range tests {
  538. tc := prep(t)
  539. t.Run(name, func(t *testing.T) {
  540. req := tc.req.WithContext(tc.ctx)
  541. w := httptest.NewRecorder()
  542. acmeResponder := NewACMEAdminResponder()
  543. acmeResponder.GetExternalAccountKeys(w, req)
  544. res := w.Result()
  545. assert.Equals(t, tc.statusCode, res.StatusCode)
  546. body, err := io.ReadAll(res.Body)
  547. res.Body.Close()
  548. assert.FatalError(t, err)
  549. adminErr := admin.Error{}
  550. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  551. assert.Equals(t, tc.err.Type, adminErr.Type)
  552. assert.Equals(t, tc.err.Message, adminErr.Message)
  553. assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
  554. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  555. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  556. })
  557. }
  558. }