provisioner_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package provisioner
  2. import (
  3. "context"
  4. "net/http"
  5. "testing"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/assert"
  8. "github.com/smallstep/certificates/errs"
  9. "golang.org/x/crypto/ssh"
  10. )
  11. func TestType_String(t *testing.T) {
  12. tests := []struct {
  13. name string
  14. t Type
  15. want string
  16. }{
  17. {"JWK", TypeJWK, "JWK"},
  18. {"OIDC", TypeOIDC, "OIDC"},
  19. {"AWS", TypeAWS, "AWS"},
  20. {"Azure", TypeAzure, "Azure"},
  21. {"GCP", TypeGCP, "GCP"},
  22. {"noop", noopType, ""},
  23. {"notFound", 1000, ""},
  24. }
  25. for _, tt := range tests {
  26. t.Run(tt.name, func(t *testing.T) {
  27. if got := tt.t.String(); got != tt.want {
  28. t.Errorf("Type.String() = %v, want %v", got, tt.want)
  29. }
  30. })
  31. }
  32. }
  33. func TestSanitizeSSHUserPrincipal(t *testing.T) {
  34. type args struct {
  35. email string
  36. }
  37. tests := []struct {
  38. name string
  39. args args
  40. want string
  41. }{
  42. {"simple", args{"foobar"}, "foobar"},
  43. {"camelcase", args{"FooBar"}, "foobar"},
  44. {"email", args{"foo@example.com"}, "foo"},
  45. {"email with dots", args{"foo.bar.zar@example.com"}, "foobarzar"},
  46. {"email with dashes", args{"foo-bar-zar@example.com"}, "foo-bar-zar"},
  47. {"email with underscores", args{"foo_bar_zar@example.com"}, "foo_bar_zar"},
  48. {"email with symbols", args{"Foo.Bar0123456789!#$%&'*+-/=?^_`{|}~;@example.com"}, "foobar0123456789________-___________"},
  49. }
  50. for _, tt := range tests {
  51. t.Run(tt.name, func(t *testing.T) {
  52. if got := SanitizeSSHUserPrincipal(tt.args.email); got != tt.want {
  53. t.Errorf("SanitizeSSHUserPrincipal() = %v, want %v", got, tt.want)
  54. }
  55. })
  56. }
  57. }
  58. func TestDefaultIdentityFunc(t *testing.T) {
  59. type test struct {
  60. p Interface
  61. email string
  62. err error
  63. identity *Identity
  64. }
  65. tests := map[string]func(*testing.T) test{
  66. "fail/unsupported-provisioner": func(t *testing.T) test {
  67. return test{
  68. p: &X5C{},
  69. err: errors.New("provisioner type '*provisioner.X5C' not supported by identity function"),
  70. }
  71. },
  72. "fail/bad-ssh-regex": func(t *testing.T) test {
  73. return test{
  74. p: &OIDC{},
  75. email: "$%^#_>@smallstep.com",
  76. err: errors.New("invalid principal '______' from email '$%^#_>@smallstep.com'"),
  77. }
  78. },
  79. "ok": func(t *testing.T) test {
  80. return test{
  81. p: &OIDC{},
  82. email: "max.furman@smallstep.com",
  83. identity: &Identity{Usernames: []string{"maxfurman", "max.furman@smallstep.com"}},
  84. }
  85. },
  86. }
  87. for name, get := range tests {
  88. t.Run(name, func(t *testing.T) {
  89. tc := get(t)
  90. identity, err := DefaultIdentityFunc(tc.p, tc.email)
  91. if err != nil {
  92. if assert.NotNil(t, tc.err) {
  93. assert.Equals(t, tc.err.Error(), err.Error())
  94. }
  95. } else {
  96. if assert.Nil(t, tc.err) {
  97. assert.Equals(t, identity.Usernames, tc.identity.Usernames)
  98. }
  99. }
  100. })
  101. }
  102. }
  103. func TestUnimplementedMethods(t *testing.T) {
  104. tests := []struct {
  105. name string
  106. p Interface
  107. method Method
  108. }{
  109. {"jwk/sshRekey", &JWK{}, SSHRekeyMethod},
  110. {"jwk/sshRenew", &JWK{}, SSHRenewMethod},
  111. {"aws/revoke", &AWS{}, RevokeMethod},
  112. {"aws/sshRenew", &AWS{}, SSHRenewMethod},
  113. {"aws/rekey", &AWS{}, SSHRekeyMethod},
  114. {"aws/sshRevoke", &AWS{}, SSHRevokeMethod},
  115. {"azure/revoke", &Azure{}, RevokeMethod},
  116. {"azure/sshRenew", &Azure{}, SSHRenewMethod},
  117. {"azure/sshRekey", &Azure{}, SSHRekeyMethod},
  118. {"azure/sshRevoke", &Azure{}, SSHRevokeMethod},
  119. {"gcp/revoke", &GCP{}, RevokeMethod},
  120. {"gcp/sshRenew", &GCP{}, SSHRenewMethod},
  121. {"gcp/sshRekey", &GCP{}, SSHRekeyMethod},
  122. {"gcp/sshRevoke", &GCP{}, SSHRevokeMethod},
  123. {"oidc/sshRenew", &OIDC{}, SSHRenewMethod},
  124. {"oidc/sshRekey", &OIDC{}, SSHRekeyMethod},
  125. {"x5c/sshRenew", &X5C{}, SSHRenewMethod},
  126. {"x5c/sshRekey", &X5C{}, SSHRekeyMethod},
  127. {"x5c/sshRevoke", &X5C{}, SSHRekeyMethod},
  128. {"acme/revoke", &ACME{}, RevokeMethod},
  129. {"acme/sshSign", &ACME{}, SSHSignMethod},
  130. {"acme/sshRekey", &ACME{}, SSHRekeyMethod},
  131. {"acme/sshRenew", &ACME{}, SSHRenewMethod},
  132. {"acme/sshRevoke", &ACME{}, SSHRevokeMethod},
  133. {"sshpop/sign", &SSHPOP{}, SignMethod},
  134. {"sshpop/renew", &SSHPOP{}, RenewMethod},
  135. {"sshpop/revoke", &SSHPOP{}, RevokeMethod},
  136. {"sshpop/sshSign", &SSHPOP{}, SSHSignMethod},
  137. {"k8ssa/sshRekey", &K8sSA{}, SSHRekeyMethod},
  138. {"k8ssa/sshRenew", &K8sSA{}, SSHRenewMethod},
  139. {"k8ssa/sshRevoke", &K8sSA{}, SSHRevokeMethod},
  140. }
  141. for _, tt := range tests {
  142. t.Run(tt.name, func(t *testing.T) {
  143. var (
  144. err error
  145. msg string
  146. )
  147. switch tt.method {
  148. case SignMethod:
  149. var signOpts []SignOption
  150. signOpts, err = tt.p.AuthorizeSign(context.Background(), "")
  151. assert.Nil(t, signOpts)
  152. msg = "provisioner.AuthorizeSign not implemented"
  153. case RenewMethod:
  154. err = tt.p.AuthorizeRenew(context.Background(), nil)
  155. msg = "provisioner.AuthorizeRenew not implemented"
  156. case RevokeMethod:
  157. err = tt.p.AuthorizeRevoke(context.Background(), "")
  158. msg = "provisioner.AuthorizeRevoke not implemented"
  159. case SSHSignMethod:
  160. var signOpts []SignOption
  161. signOpts, err = tt.p.AuthorizeSSHSign(context.Background(), "")
  162. assert.Nil(t, signOpts)
  163. msg = "provisioner.AuthorizeSSHSign not implemented"
  164. case SSHRenewMethod:
  165. var cert *ssh.Certificate
  166. cert, err = tt.p.AuthorizeSSHRenew(context.Background(), "")
  167. assert.Nil(t, cert)
  168. msg = "provisioner.AuthorizeSSHRenew not implemented"
  169. case SSHRekeyMethod:
  170. var (
  171. cert *ssh.Certificate
  172. signOpts []SignOption
  173. )
  174. cert, signOpts, err = tt.p.AuthorizeSSHRekey(context.Background(), "")
  175. assert.Nil(t, cert)
  176. assert.Nil(t, signOpts)
  177. msg = "provisioner.AuthorizeSSHRekey not implemented"
  178. case SSHRevokeMethod:
  179. err = tt.p.AuthorizeSSHRevoke(context.Background(), "")
  180. msg = "provisioner.AuthorizeSSHRevoke not implemented"
  181. default:
  182. t.Errorf("unexpected method %s", tt.method)
  183. }
  184. sc, ok := err.(errs.StatusCoder)
  185. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  186. assert.Equals(t, sc.StatusCode(), http.StatusUnauthorized)
  187. assert.Equals(t, err.Error(), msg)
  188. })
  189. }
  190. }