provisioner_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package provisioner
  2. import (
  3. "context"
  4. "errors"
  5. "net/http"
  6. "testing"
  7. "golang.org/x/crypto/ssh"
  8. "github.com/smallstep/assert"
  9. "github.com/smallstep/certificates/api/render"
  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. usernames []string
  63. err error
  64. identity *Identity
  65. }
  66. tests := map[string]func(*testing.T) test{
  67. "fail/unsupported-provisioner": func(t *testing.T) test {
  68. return test{
  69. p: &X5C{},
  70. err: errors.New("provisioner type '*provisioner.X5C' not supported by identity function"),
  71. }
  72. },
  73. "fail/bad-ssh-regex": func(t *testing.T) test {
  74. return test{
  75. p: &OIDC{},
  76. email: "$%^#_>@smallstep.com",
  77. err: errors.New("invalid principal '______' from email '$%^#_>@smallstep.com'"),
  78. }
  79. },
  80. "ok": func(t *testing.T) test {
  81. return test{
  82. p: &OIDC{},
  83. email: "max.furman@smallstep.com",
  84. identity: &Identity{Usernames: []string{"maxfurman", "max.furman", "max.furman@smallstep.com"}},
  85. }
  86. },
  87. "ok letter case": func(t *testing.T) test {
  88. return test{
  89. p: &OIDC{},
  90. email: "Max.Furman@smallstep.com",
  91. identity: &Identity{Usernames: []string{"maxfurman", "Max.Furman", "Max.Furman@smallstep.com"}},
  92. }
  93. },
  94. "ok simple": func(t *testing.T) test {
  95. return test{
  96. p: &OIDC{},
  97. email: "john@smallstep.com",
  98. identity: &Identity{Usernames: []string{"john", "john@smallstep.com"}},
  99. }
  100. },
  101. "ok simple letter case": func(t *testing.T) test {
  102. return test{
  103. p: &OIDC{},
  104. email: "John@smallstep.com",
  105. identity: &Identity{Usernames: []string{"john", "John", "John@smallstep.com"}},
  106. }
  107. },
  108. "ok symbol": func(t *testing.T) test {
  109. return test{
  110. p: &OIDC{},
  111. email: "John+Doe@smallstep.com",
  112. identity: &Identity{Usernames: []string{"john_doe", "John+Doe", "John+Doe@smallstep.com"}},
  113. }
  114. },
  115. "ok username": func(t *testing.T) test {
  116. return test{
  117. p: &OIDC{},
  118. email: "john@smallstep.com",
  119. usernames: []string{"johnny"},
  120. identity: &Identity{Usernames: []string{"john", "john@smallstep.com"}},
  121. }
  122. },
  123. "ok usernames": func(t *testing.T) test {
  124. return test{
  125. p: &OIDC{},
  126. email: "john@smallstep.com",
  127. usernames: []string{"johnny", "js", "", "johnny", ""},
  128. identity: &Identity{Usernames: []string{"john", "john@smallstep.com"}},
  129. }
  130. },
  131. "ok empty username": func(t *testing.T) test {
  132. return test{
  133. p: &OIDC{},
  134. email: "john@smallstep.com",
  135. usernames: []string{""},
  136. identity: &Identity{Usernames: []string{"john", "john@smallstep.com"}},
  137. }
  138. },
  139. }
  140. for name, get := range tests {
  141. t.Run(name, func(t *testing.T) {
  142. tc := get(t)
  143. identity, err := DefaultIdentityFunc(context.Background(), tc.p, tc.email)
  144. if err != nil {
  145. if assert.NotNil(t, tc.err) {
  146. assert.Equals(t, tc.err.Error(), err.Error())
  147. }
  148. } else {
  149. if assert.Nil(t, tc.err) {
  150. assert.Equals(t, identity.Usernames, tc.identity.Usernames)
  151. }
  152. }
  153. })
  154. }
  155. }
  156. func TestUnimplementedMethods(t *testing.T) {
  157. tests := []struct {
  158. name string
  159. p Interface
  160. method Method
  161. }{
  162. {"jwk/sshRekey", &JWK{}, SSHRekeyMethod},
  163. {"jwk/sshRenew", &JWK{}, SSHRenewMethod},
  164. {"aws/revoke", &AWS{}, RevokeMethod},
  165. {"aws/sshRenew", &AWS{}, SSHRenewMethod},
  166. {"aws/rekey", &AWS{}, SSHRekeyMethod},
  167. {"aws/sshRevoke", &AWS{}, SSHRevokeMethod},
  168. {"azure/revoke", &Azure{}, RevokeMethod},
  169. {"azure/sshRenew", &Azure{}, SSHRenewMethod},
  170. {"azure/sshRekey", &Azure{}, SSHRekeyMethod},
  171. {"azure/sshRevoke", &Azure{}, SSHRevokeMethod},
  172. {"gcp/revoke", &GCP{}, RevokeMethod},
  173. {"gcp/sshRenew", &GCP{}, SSHRenewMethod},
  174. {"gcp/sshRekey", &GCP{}, SSHRekeyMethod},
  175. {"gcp/sshRevoke", &GCP{}, SSHRevokeMethod},
  176. {"oidc/sshRenew", &OIDC{}, SSHRenewMethod},
  177. {"oidc/sshRekey", &OIDC{}, SSHRekeyMethod},
  178. {"x5c/sshRenew", &X5C{}, SSHRenewMethod},
  179. {"x5c/sshRekey", &X5C{}, SSHRekeyMethod},
  180. {"x5c/sshRevoke", &X5C{}, SSHRekeyMethod},
  181. {"acme/sshSign", &ACME{}, SSHSignMethod},
  182. {"acme/sshRekey", &ACME{}, SSHRekeyMethod},
  183. {"acme/sshRenew", &ACME{}, SSHRenewMethod},
  184. {"acme/sshRevoke", &ACME{}, SSHRevokeMethod},
  185. {"sshpop/sign", &SSHPOP{}, SignMethod},
  186. {"sshpop/renew", &SSHPOP{}, RenewMethod},
  187. {"sshpop/revoke", &SSHPOP{}, RevokeMethod},
  188. {"sshpop/sshSign", &SSHPOP{}, SSHSignMethod},
  189. {"k8ssa/sshRekey", &K8sSA{}, SSHRekeyMethod},
  190. {"k8ssa/sshRenew", &K8sSA{}, SSHRenewMethod},
  191. {"k8ssa/sshRevoke", &K8sSA{}, SSHRevokeMethod},
  192. }
  193. for _, tt := range tests {
  194. t.Run(tt.name, func(t *testing.T) {
  195. var (
  196. err error
  197. msg string
  198. )
  199. switch tt.method {
  200. case SignMethod:
  201. var signOpts []SignOption
  202. signOpts, err = tt.p.AuthorizeSign(context.Background(), "")
  203. assert.Nil(t, signOpts)
  204. msg = "provisioner.AuthorizeSign not implemented"
  205. case RenewMethod:
  206. err = tt.p.AuthorizeRenew(context.Background(), nil)
  207. msg = "provisioner.AuthorizeRenew not implemented"
  208. case RevokeMethod:
  209. err = tt.p.AuthorizeRevoke(context.Background(), "")
  210. msg = "provisioner.AuthorizeRevoke not implemented"
  211. case SSHSignMethod:
  212. var signOpts []SignOption
  213. signOpts, err = tt.p.AuthorizeSSHSign(context.Background(), "")
  214. assert.Nil(t, signOpts)
  215. msg = "provisioner.AuthorizeSSHSign not implemented"
  216. case SSHRenewMethod:
  217. var cert *ssh.Certificate
  218. cert, err = tt.p.AuthorizeSSHRenew(context.Background(), "")
  219. assert.Nil(t, cert)
  220. msg = "provisioner.AuthorizeSSHRenew not implemented"
  221. case SSHRekeyMethod:
  222. var (
  223. cert *ssh.Certificate
  224. signOpts []SignOption
  225. )
  226. cert, signOpts, err = tt.p.AuthorizeSSHRekey(context.Background(), "")
  227. assert.Nil(t, cert)
  228. assert.Nil(t, signOpts)
  229. msg = "provisioner.AuthorizeSSHRekey not implemented"
  230. case SSHRevokeMethod:
  231. err = tt.p.AuthorizeSSHRevoke(context.Background(), "")
  232. msg = "provisioner.AuthorizeSSHRevoke not implemented"
  233. default:
  234. t.Errorf("unexpected method %s", tt.method)
  235. }
  236. sc, ok := err.(render.StatusCodedError)
  237. assert.Fatal(t, ok, "error does not implement StatusCodedError interface")
  238. assert.Equals(t, sc.StatusCode(), http.StatusUnauthorized)
  239. assert.Equals(t, err.Error(), msg)
  240. })
  241. }
  242. }