provisioners_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package authority
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/assert"
  7. "github.com/smallstep/certificates/authority/provisioner"
  8. )
  9. func TestGetEncryptedKey(t *testing.T) {
  10. type ek struct {
  11. a *Authority
  12. kid string
  13. err *apiError
  14. }
  15. tests := map[string]func(t *testing.T) *ek{
  16. "ok": func(t *testing.T) *ek {
  17. c, err := LoadConfiguration("../ca/testdata/ca.json")
  18. assert.FatalError(t, err)
  19. a, err := New(c)
  20. assert.FatalError(t, err)
  21. return &ek{
  22. a: a,
  23. kid: c.AuthorityConfig.Provisioners[1].(*provisioner.JWK).Key.KeyID,
  24. }
  25. },
  26. "fail-not-found": func(t *testing.T) *ek {
  27. c, err := LoadConfiguration("../ca/testdata/ca.json")
  28. assert.FatalError(t, err)
  29. a, err := New(c)
  30. assert.FatalError(t, err)
  31. return &ek{
  32. a: a,
  33. kid: "foo",
  34. err: &apiError{errors.Errorf("encrypted key with kid foo was not found"),
  35. http.StatusNotFound, context{}},
  36. }
  37. },
  38. // "fail-invalid-type-found": func(t *testing.T) *ek {
  39. // c, err := LoadConfiguration("../ca/testdata/ca.json")
  40. // assert.FatalError(t, err)
  41. // a, err := New(c)
  42. // assert.FatalError(t, err)
  43. // a.encryptedKeyIndex.Store("foo", 5)
  44. // return &ek{
  45. // a: a,
  46. // kid: "foo",
  47. // err: &apiError{errors.Errorf("stored value is not a string"),
  48. // http.StatusInternalServerError, context{}},
  49. // }
  50. // },
  51. }
  52. for name, genTestCase := range tests {
  53. t.Run(name, func(t *testing.T) {
  54. tc := genTestCase(t)
  55. ek, err := tc.a.GetEncryptedKey(tc.kid)
  56. if err != nil {
  57. if assert.NotNil(t, tc.err) {
  58. switch v := err.(type) {
  59. case *apiError:
  60. assert.HasPrefix(t, v.err.Error(), tc.err.Error())
  61. assert.Equals(t, v.code, tc.err.code)
  62. assert.Equals(t, v.context, tc.err.context)
  63. default:
  64. t.Errorf("unexpected error type: %T", v)
  65. }
  66. }
  67. } else {
  68. if assert.Nil(t, tc.err) {
  69. val, ok := tc.a.provisioners.Load("max:" + tc.kid)
  70. assert.Fatal(t, ok)
  71. p, ok := val.(*provisioner.JWK)
  72. assert.Fatal(t, ok)
  73. assert.Equals(t, p.EncryptedKey, ek)
  74. }
  75. }
  76. })
  77. }
  78. }
  79. func TestGetProvisioners(t *testing.T) {
  80. type gp struct {
  81. a *Authority
  82. err *apiError
  83. }
  84. tests := map[string]func(t *testing.T) *gp{
  85. "ok": func(t *testing.T) *gp {
  86. c, err := LoadConfiguration("../ca/testdata/ca.json")
  87. assert.FatalError(t, err)
  88. a, err := New(c)
  89. assert.FatalError(t, err)
  90. return &gp{a: a}
  91. },
  92. }
  93. for name, genTestCase := range tests {
  94. t.Run(name, func(t *testing.T) {
  95. tc := genTestCase(t)
  96. ps, next, err := tc.a.GetProvisioners("", 0)
  97. if err != nil {
  98. if assert.NotNil(t, tc.err) {
  99. switch v := err.(type) {
  100. case *apiError:
  101. assert.HasPrefix(t, v.err.Error(), tc.err.Error())
  102. assert.Equals(t, v.code, tc.err.code)
  103. assert.Equals(t, v.context, tc.err.context)
  104. default:
  105. t.Errorf("unexpected error type: %T", v)
  106. }
  107. }
  108. } else {
  109. if assert.Nil(t, tc.err) {
  110. assert.Equals(t, ps, tc.a.config.AuthorityConfig.Provisioners)
  111. assert.Equals(t, "", next)
  112. }
  113. }
  114. })
  115. }
  116. }