acme_test.go 15 KB

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