acme_test.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "io"
  7. "net/http"
  8. "net/http/httptest"
  9. "strings"
  10. "testing"
  11. "github.com/go-chi/chi"
  12. "google.golang.org/protobuf/encoding/protojson"
  13. "google.golang.org/protobuf/proto"
  14. "go.step.sm/linkedca"
  15. "github.com/smallstep/assert"
  16. "github.com/smallstep/certificates/authority/admin"
  17. )
  18. func readProtoJSON(r io.ReadCloser, m proto.Message) error {
  19. defer r.Close()
  20. data, err := io.ReadAll(r)
  21. if err != nil {
  22. return err
  23. }
  24. return protojson.Unmarshal(data, m)
  25. }
  26. func TestHandler_requireEABEnabled(t *testing.T) {
  27. type test struct {
  28. ctx context.Context
  29. next http.HandlerFunc
  30. err *admin.Error
  31. statusCode int
  32. }
  33. var tests = map[string]func(t *testing.T) test{
  34. "fail/prov.GetDetails": func(t *testing.T) test {
  35. prov := &linkedca.Provisioner{
  36. Id: "provID",
  37. Name: "provName",
  38. }
  39. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  40. err := admin.NewErrorISE("error getting details for provisioner 'provName'")
  41. err.Message = "error getting details for provisioner 'provName'"
  42. return test{
  43. ctx: ctx,
  44. err: err,
  45. statusCode: 500,
  46. }
  47. },
  48. "fail/details.GetACME": func(t *testing.T) test {
  49. prov := &linkedca.Provisioner{
  50. Id: "provID",
  51. Name: "provName",
  52. Details: &linkedca.ProvisionerDetails{},
  53. }
  54. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  55. err := admin.NewErrorISE("error getting ACME details for provisioner 'provName'")
  56. err.Message = "error getting ACME details for provisioner 'provName'"
  57. return test{
  58. ctx: ctx,
  59. err: err,
  60. statusCode: 500,
  61. }
  62. },
  63. "ok/eab-disabled": func(t *testing.T) test {
  64. prov := &linkedca.Provisioner{
  65. Id: "provID",
  66. Name: "provName",
  67. Details: &linkedca.ProvisionerDetails{
  68. Data: &linkedca.ProvisionerDetails_ACME{
  69. ACME: &linkedca.ACMEProvisioner{
  70. RequireEab: false,
  71. },
  72. },
  73. },
  74. }
  75. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  76. err := admin.NewError(admin.ErrorBadRequestType, "ACME EAB not enabled for provisioner provName")
  77. err.Message = "ACME EAB not enabled for provisioner 'provName'"
  78. return test{
  79. ctx: ctx,
  80. err: err,
  81. statusCode: 400,
  82. }
  83. },
  84. "ok/eab-enabled": func(t *testing.T) test {
  85. prov := &linkedca.Provisioner{
  86. Id: "provID",
  87. Name: "provName",
  88. Details: &linkedca.ProvisionerDetails{
  89. Data: &linkedca.ProvisionerDetails_ACME{
  90. ACME: &linkedca.ACMEProvisioner{
  91. RequireEab: true,
  92. },
  93. },
  94. },
  95. }
  96. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  97. return test{
  98. ctx: ctx,
  99. next: func(w http.ResponseWriter, r *http.Request) {
  100. w.Write(nil) // mock response with status 200
  101. },
  102. statusCode: 200,
  103. }
  104. },
  105. }
  106. for name, prep := range tests {
  107. tc := prep(t)
  108. t.Run(name, func(t *testing.T) {
  109. h := &Handler{}
  110. req := httptest.NewRequest("GET", "/foo", nil)
  111. req = req.WithContext(tc.ctx)
  112. w := httptest.NewRecorder()
  113. h.requireEABEnabled(tc.next)(w, req)
  114. res := w.Result()
  115. assert.Equals(t, tc.statusCode, res.StatusCode)
  116. body, err := io.ReadAll(res.Body)
  117. res.Body.Close()
  118. assert.FatalError(t, err)
  119. if res.StatusCode >= 400 {
  120. err := admin.Error{}
  121. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &err))
  122. assert.Equals(t, tc.err.Type, err.Type)
  123. assert.Equals(t, tc.err.Message, err.Message)
  124. assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
  125. assert.Equals(t, tc.err.Detail, err.Detail)
  126. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  127. return
  128. }
  129. })
  130. }
  131. }
  132. func TestCreateExternalAccountKeyRequest_Validate(t *testing.T) {
  133. type fields struct {
  134. Reference string
  135. }
  136. tests := []struct {
  137. name string
  138. fields fields
  139. wantErr bool
  140. }{
  141. {
  142. name: "fail/reference-too-long",
  143. fields: fields{
  144. Reference: strings.Repeat("A", 257),
  145. },
  146. wantErr: true,
  147. },
  148. {
  149. name: "ok/empty-reference",
  150. fields: fields{
  151. Reference: "",
  152. },
  153. wantErr: false,
  154. },
  155. {
  156. name: "ok",
  157. fields: fields{
  158. Reference: "my-eab-reference",
  159. },
  160. wantErr: false,
  161. },
  162. }
  163. for _, tt := range tests {
  164. t.Run(tt.name, func(t *testing.T) {
  165. r := &CreateExternalAccountKeyRequest{
  166. Reference: tt.fields.Reference,
  167. }
  168. if err := r.Validate(); (err != nil) != tt.wantErr {
  169. t.Errorf("CreateExternalAccountKeyRequest.Validate() error = %v, wantErr %v", err, tt.wantErr)
  170. }
  171. })
  172. }
  173. }
  174. func TestHandler_CreateExternalAccountKey(t *testing.T) {
  175. type test struct {
  176. ctx context.Context
  177. statusCode int
  178. err *admin.Error
  179. }
  180. var tests = map[string]func(t *testing.T) test{
  181. "ok": func(t *testing.T) test {
  182. chiCtx := chi.NewRouteContext()
  183. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  184. return test{
  185. ctx: ctx,
  186. statusCode: 501,
  187. err: &admin.Error{
  188. Type: admin.ErrorNotImplementedType.String(),
  189. Status: http.StatusNotImplemented,
  190. Message: "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm",
  191. Detail: "not implemented",
  192. },
  193. }
  194. },
  195. }
  196. for name, prep := range tests {
  197. tc := prep(t)
  198. t.Run(name, func(t *testing.T) {
  199. req := httptest.NewRequest("POST", "/foo", nil) // chi routing is prepared in test setup
  200. req = req.WithContext(tc.ctx)
  201. w := httptest.NewRecorder()
  202. acmeResponder := NewACMEAdminResponder()
  203. acmeResponder.CreateExternalAccountKey(w, req)
  204. res := w.Result()
  205. assert.Equals(t, tc.statusCode, res.StatusCode)
  206. body, err := io.ReadAll(res.Body)
  207. res.Body.Close()
  208. assert.FatalError(t, err)
  209. adminErr := admin.Error{}
  210. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  211. assert.Equals(t, tc.err.Type, adminErr.Type)
  212. assert.Equals(t, tc.err.Message, adminErr.Message)
  213. assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
  214. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  215. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  216. })
  217. }
  218. }
  219. func TestHandler_DeleteExternalAccountKey(t *testing.T) {
  220. type test struct {
  221. ctx context.Context
  222. statusCode int
  223. err *admin.Error
  224. }
  225. var tests = map[string]func(t *testing.T) test{
  226. "ok": func(t *testing.T) test {
  227. chiCtx := chi.NewRouteContext()
  228. chiCtx.URLParams.Add("provisionerName", "provName")
  229. chiCtx.URLParams.Add("id", "keyID")
  230. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  231. return test{
  232. ctx: ctx,
  233. statusCode: 501,
  234. err: &admin.Error{
  235. Type: admin.ErrorNotImplementedType.String(),
  236. Status: http.StatusNotImplemented,
  237. Message: "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm",
  238. Detail: "not implemented",
  239. },
  240. }
  241. },
  242. }
  243. for name, prep := range tests {
  244. tc := prep(t)
  245. t.Run(name, func(t *testing.T) {
  246. req := httptest.NewRequest("DELETE", "/foo", nil) // chi routing is prepared in test setup
  247. req = req.WithContext(tc.ctx)
  248. w := httptest.NewRecorder()
  249. acmeResponder := NewACMEAdminResponder()
  250. acmeResponder.DeleteExternalAccountKey(w, req)
  251. res := w.Result()
  252. assert.Equals(t, tc.statusCode, res.StatusCode)
  253. body, err := io.ReadAll(res.Body)
  254. res.Body.Close()
  255. assert.FatalError(t, err)
  256. adminErr := admin.Error{}
  257. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  258. assert.Equals(t, tc.err.Type, adminErr.Type)
  259. assert.Equals(t, tc.err.Message, adminErr.Message)
  260. assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
  261. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  262. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  263. })
  264. }
  265. }
  266. func TestHandler_GetExternalAccountKeys(t *testing.T) {
  267. type test struct {
  268. ctx context.Context
  269. statusCode int
  270. req *http.Request
  271. err *admin.Error
  272. }
  273. var tests = map[string]func(t *testing.T) test{
  274. "ok": func(t *testing.T) test {
  275. chiCtx := chi.NewRouteContext()
  276. chiCtx.URLParams.Add("provisionerName", "provName")
  277. req := httptest.NewRequest("GET", "/foo", nil)
  278. ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
  279. return test{
  280. ctx: ctx,
  281. statusCode: 501,
  282. req: req,
  283. err: &admin.Error{
  284. Type: admin.ErrorNotImplementedType.String(),
  285. Status: http.StatusNotImplemented,
  286. Message: "this functionality is currently only available in Certificate Manager: https://u.step.sm/cm",
  287. Detail: "not implemented",
  288. },
  289. }
  290. },
  291. }
  292. for name, prep := range tests {
  293. tc := prep(t)
  294. t.Run(name, func(t *testing.T) {
  295. req := tc.req.WithContext(tc.ctx)
  296. w := httptest.NewRecorder()
  297. acmeResponder := NewACMEAdminResponder()
  298. acmeResponder.GetExternalAccountKeys(w, req)
  299. res := w.Result()
  300. assert.Equals(t, tc.statusCode, res.StatusCode)
  301. body, err := io.ReadAll(res.Body)
  302. res.Body.Close()
  303. assert.FatalError(t, err)
  304. adminErr := admin.Error{}
  305. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
  306. assert.Equals(t, tc.err.Type, adminErr.Type)
  307. assert.Equals(t, tc.err.Message, adminErr.Message)
  308. assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
  309. assert.Equals(t, tc.err.Detail, adminErr.Detail)
  310. assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
  311. })
  312. }
  313. }