provisioner_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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", "max.furman@smallstep.com"}},
  84. }
  85. },
  86. "ok letter case": func(t *testing.T) test {
  87. return test{
  88. p: &OIDC{},
  89. email: "Max.Furman@smallstep.com",
  90. identity: &Identity{Usernames: []string{"maxfurman", "Max.Furman", "Max.Furman@smallstep.com"}},
  91. }
  92. },
  93. "ok simple": func(t *testing.T) test {
  94. return test{
  95. p: &OIDC{},
  96. email: "john@smallstep.com",
  97. identity: &Identity{Usernames: []string{"john", "john@smallstep.com"}},
  98. }
  99. },
  100. "ok simple letter case": func(t *testing.T) test {
  101. return test{
  102. p: &OIDC{},
  103. email: "John@smallstep.com",
  104. identity: &Identity{Usernames: []string{"john", "John@smallstep.com"}},
  105. }
  106. },
  107. "ok symbol": func(t *testing.T) test {
  108. return test{
  109. p: &OIDC{},
  110. email: "John+Doe@smallstep.com",
  111. identity: &Identity{Usernames: []string{"john_doe", "John+Doe", "John+Doe@smallstep.com"}},
  112. }
  113. },
  114. }
  115. for name, get := range tests {
  116. t.Run(name, func(t *testing.T) {
  117. tc := get(t)
  118. identity, err := DefaultIdentityFunc(context.Background(), tc.p, tc.email)
  119. if err != nil {
  120. if assert.NotNil(t, tc.err) {
  121. assert.Equals(t, tc.err.Error(), err.Error())
  122. }
  123. } else {
  124. if assert.Nil(t, tc.err) {
  125. assert.Equals(t, identity.Usernames, tc.identity.Usernames)
  126. }
  127. }
  128. })
  129. }
  130. }
  131. func TestUnimplementedMethods(t *testing.T) {
  132. tests := []struct {
  133. name string
  134. p Interface
  135. method Method
  136. }{
  137. {"jwk/sshRekey", &JWK{}, SSHRekeyMethod},
  138. {"jwk/sshRenew", &JWK{}, SSHRenewMethod},
  139. {"aws/revoke", &AWS{}, RevokeMethod},
  140. {"aws/sshRenew", &AWS{}, SSHRenewMethod},
  141. {"aws/rekey", &AWS{}, SSHRekeyMethod},
  142. {"aws/sshRevoke", &AWS{}, SSHRevokeMethod},
  143. {"azure/revoke", &Azure{}, RevokeMethod},
  144. {"azure/sshRenew", &Azure{}, SSHRenewMethod},
  145. {"azure/sshRekey", &Azure{}, SSHRekeyMethod},
  146. {"azure/sshRevoke", &Azure{}, SSHRevokeMethod},
  147. {"gcp/revoke", &GCP{}, RevokeMethod},
  148. {"gcp/sshRenew", &GCP{}, SSHRenewMethod},
  149. {"gcp/sshRekey", &GCP{}, SSHRekeyMethod},
  150. {"gcp/sshRevoke", &GCP{}, SSHRevokeMethod},
  151. {"oidc/sshRenew", &OIDC{}, SSHRenewMethod},
  152. {"oidc/sshRekey", &OIDC{}, SSHRekeyMethod},
  153. {"x5c/sshRenew", &X5C{}, SSHRenewMethod},
  154. {"x5c/sshRekey", &X5C{}, SSHRekeyMethod},
  155. {"x5c/sshRevoke", &X5C{}, SSHRekeyMethod},
  156. {"acme/revoke", &ACME{}, RevokeMethod},
  157. {"acme/sshSign", &ACME{}, SSHSignMethod},
  158. {"acme/sshRekey", &ACME{}, SSHRekeyMethod},
  159. {"acme/sshRenew", &ACME{}, SSHRenewMethod},
  160. {"acme/sshRevoke", &ACME{}, SSHRevokeMethod},
  161. {"sshpop/sign", &SSHPOP{}, SignMethod},
  162. {"sshpop/renew", &SSHPOP{}, RenewMethod},
  163. {"sshpop/revoke", &SSHPOP{}, RevokeMethod},
  164. {"sshpop/sshSign", &SSHPOP{}, SSHSignMethod},
  165. {"k8ssa/sshRekey", &K8sSA{}, SSHRekeyMethod},
  166. {"k8ssa/sshRenew", &K8sSA{}, SSHRenewMethod},
  167. {"k8ssa/sshRevoke", &K8sSA{}, SSHRevokeMethod},
  168. }
  169. for _, tt := range tests {
  170. t.Run(tt.name, func(t *testing.T) {
  171. var (
  172. err error
  173. msg string
  174. )
  175. switch tt.method {
  176. case SignMethod:
  177. var signOpts []SignOption
  178. signOpts, err = tt.p.AuthorizeSign(context.Background(), "")
  179. assert.Nil(t, signOpts)
  180. msg = "provisioner.AuthorizeSign not implemented"
  181. case RenewMethod:
  182. err = tt.p.AuthorizeRenew(context.Background(), nil)
  183. msg = "provisioner.AuthorizeRenew not implemented"
  184. case RevokeMethod:
  185. err = tt.p.AuthorizeRevoke(context.Background(), "")
  186. msg = "provisioner.AuthorizeRevoke not implemented"
  187. case SSHSignMethod:
  188. var signOpts []SignOption
  189. signOpts, err = tt.p.AuthorizeSSHSign(context.Background(), "")
  190. assert.Nil(t, signOpts)
  191. msg = "provisioner.AuthorizeSSHSign not implemented"
  192. case SSHRenewMethod:
  193. var cert *ssh.Certificate
  194. cert, err = tt.p.AuthorizeSSHRenew(context.Background(), "")
  195. assert.Nil(t, cert)
  196. msg = "provisioner.AuthorizeSSHRenew not implemented"
  197. case SSHRekeyMethod:
  198. var (
  199. cert *ssh.Certificate
  200. signOpts []SignOption
  201. )
  202. cert, signOpts, err = tt.p.AuthorizeSSHRekey(context.Background(), "")
  203. assert.Nil(t, cert)
  204. assert.Nil(t, signOpts)
  205. msg = "provisioner.AuthorizeSSHRekey not implemented"
  206. case SSHRevokeMethod:
  207. err = tt.p.AuthorizeSSHRevoke(context.Background(), "")
  208. msg = "provisioner.AuthorizeSSHRevoke not implemented"
  209. default:
  210. t.Errorf("unexpected method %s", tt.method)
  211. }
  212. sc, ok := err.(errs.StatusCoder)
  213. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  214. assert.Equals(t, sc.StatusCode(), http.StatusUnauthorized)
  215. assert.Equals(t, err.Error(), msg)
  216. })
  217. }
  218. }