provisioner_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package provisioner
  2. import (
  3. "context"
  4. "errors"
  5. "net/http"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "go.step.sm/crypto/jose"
  9. "golang.org/x/crypto/ssh"
  10. "github.com/smallstep/certificates/api/render"
  11. )
  12. func TestType_String(t *testing.T) {
  13. tests := []struct {
  14. name string
  15. t Type
  16. want string
  17. }{
  18. {"JWK", TypeJWK, "JWK"},
  19. {"OIDC", TypeOIDC, "OIDC"},
  20. {"AWS", TypeAWS, "AWS"},
  21. {"Azure", TypeAzure, "Azure"},
  22. {"GCP", TypeGCP, "GCP"},
  23. {"noop", noopType, ""},
  24. {"notFound", 1000, ""},
  25. }
  26. for _, tt := range tests {
  27. t.Run(tt.name, func(t *testing.T) {
  28. if got := tt.t.String(); got != tt.want {
  29. t.Errorf("Type.String() = %v, want %v", got, tt.want)
  30. }
  31. })
  32. }
  33. }
  34. func TestSanitizeSSHUserPrincipal(t *testing.T) {
  35. type args struct {
  36. email string
  37. }
  38. tests := []struct {
  39. name string
  40. args args
  41. want string
  42. }{
  43. {"simple", args{"foobar"}, "foobar"},
  44. {"camelcase", args{"FooBar"}, "foobar"},
  45. {"email", args{"foo@example.com"}, "foo"},
  46. {"email with dots", args{"foo.bar.zar@example.com"}, "foobarzar"},
  47. {"email with dashes", args{"foo-bar-zar@example.com"}, "foo-bar-zar"},
  48. {"email with underscores", args{"foo_bar_zar@example.com"}, "foo_bar_zar"},
  49. {"email with symbols", args{"Foo.Bar0123456789!#$%&'*+-/=?^_`{|}~;@example.com"}, "foobar0123456789________-___________"},
  50. }
  51. for _, tt := range tests {
  52. t.Run(tt.name, func(t *testing.T) {
  53. if got := SanitizeSSHUserPrincipal(tt.args.email); got != tt.want {
  54. t.Errorf("SanitizeSSHUserPrincipal() = %v, want %v", got, tt.want)
  55. }
  56. })
  57. }
  58. }
  59. func TestDefaultIdentityFunc(t *testing.T) {
  60. type test struct {
  61. p Interface
  62. email string
  63. usernames []string
  64. err error
  65. identity *Identity
  66. }
  67. tests := map[string]func(*testing.T) test{
  68. "fail/unsupported-provisioner": func(t *testing.T) test {
  69. return test{
  70. p: &X5C{},
  71. err: errors.New("provisioner type '*provisioner.X5C' not supported by identity function"),
  72. }
  73. },
  74. "ok": func(t *testing.T) test {
  75. return test{
  76. p: &OIDC{},
  77. email: "max.furman@smallstep.com",
  78. identity: &Identity{Usernames: []string{"maxfurman", "max.furman", "max.furman@smallstep.com"}},
  79. }
  80. },
  81. "ok letter case": func(t *testing.T) test {
  82. return test{
  83. p: &OIDC{},
  84. email: "Max.Furman@smallstep.com",
  85. identity: &Identity{Usernames: []string{"maxfurman", "Max.Furman", "Max.Furman@smallstep.com"}},
  86. }
  87. },
  88. "ok simple": func(t *testing.T) test {
  89. return test{
  90. p: &OIDC{},
  91. email: "john@smallstep.com",
  92. identity: &Identity{Usernames: []string{"john", "john@smallstep.com"}},
  93. }
  94. },
  95. "ok simple letter case": func(t *testing.T) test {
  96. return test{
  97. p: &OIDC{},
  98. email: "John@smallstep.com",
  99. identity: &Identity{Usernames: []string{"john", "John", "John@smallstep.com"}},
  100. }
  101. },
  102. "ok symbol": func(t *testing.T) test {
  103. return test{
  104. p: &OIDC{},
  105. email: "John+Doe@smallstep.com",
  106. identity: &Identity{Usernames: []string{"john_doe", "John+Doe", "John+Doe@smallstep.com"}},
  107. }
  108. },
  109. "ok username": func(t *testing.T) test {
  110. return test{
  111. p: &OIDC{},
  112. email: "john@smallstep.com",
  113. usernames: []string{"johnny"},
  114. identity: &Identity{Usernames: []string{"john", "john@smallstep.com"}},
  115. }
  116. },
  117. "ok usernames": func(t *testing.T) test {
  118. return test{
  119. p: &OIDC{},
  120. email: "john@smallstep.com",
  121. usernames: []string{"johnny", "js", "", "johnny", ""},
  122. identity: &Identity{Usernames: []string{"john", "john@smallstep.com"}},
  123. }
  124. },
  125. "ok empty username": func(t *testing.T) test {
  126. return test{
  127. p: &OIDC{},
  128. email: "john@smallstep.com",
  129. usernames: []string{""},
  130. identity: &Identity{Usernames: []string{"john", "john@smallstep.com"}},
  131. }
  132. },
  133. "ok/badname": func(t *testing.T) test {
  134. return test{
  135. p: &OIDC{},
  136. email: "$%^#_>@smallstep.com",
  137. identity: &Identity{Usernames: []string{"______", "$%^#_>", "$%^#_>@smallstep.com"}},
  138. }
  139. },
  140. }
  141. for name, get := range tests {
  142. t.Run(name, func(t *testing.T) {
  143. tc := get(t)
  144. identity, err := DefaultIdentityFunc(context.Background(), tc.p, tc.email)
  145. if err != nil {
  146. if assert.NotNil(t, tc.err) {
  147. assert.Equal(t, tc.err.Error(), err.Error())
  148. }
  149. } else {
  150. if assert.Nil(t, tc.err) {
  151. assert.Equal(t, identity.Usernames, tc.identity.Usernames)
  152. }
  153. }
  154. })
  155. }
  156. }
  157. func TestUnimplementedMethods(t *testing.T) {
  158. tests := []struct {
  159. name string
  160. p Interface
  161. method Method
  162. }{
  163. {"jwk/sshRekey", &JWK{}, SSHRekeyMethod},
  164. {"jwk/sshRenew", &JWK{}, SSHRenewMethod},
  165. {"aws/revoke", &AWS{}, RevokeMethod},
  166. {"aws/sshRenew", &AWS{}, SSHRenewMethod},
  167. {"aws/rekey", &AWS{}, SSHRekeyMethod},
  168. {"aws/sshRevoke", &AWS{}, SSHRevokeMethod},
  169. {"azure/revoke", &Azure{}, RevokeMethod},
  170. {"azure/sshRenew", &Azure{}, SSHRenewMethod},
  171. {"azure/sshRekey", &Azure{}, SSHRekeyMethod},
  172. {"azure/sshRevoke", &Azure{}, SSHRevokeMethod},
  173. {"gcp/revoke", &GCP{}, RevokeMethod},
  174. {"gcp/sshRenew", &GCP{}, SSHRenewMethod},
  175. {"gcp/sshRekey", &GCP{}, SSHRekeyMethod},
  176. {"gcp/sshRevoke", &GCP{}, SSHRevokeMethod},
  177. {"oidc/sshRenew", &OIDC{}, SSHRenewMethod},
  178. {"oidc/sshRekey", &OIDC{}, SSHRekeyMethod},
  179. {"x5c/sshRenew", &X5C{}, SSHRenewMethod},
  180. {"x5c/sshRekey", &X5C{}, SSHRekeyMethod},
  181. {"x5c/sshRevoke", &X5C{}, SSHRekeyMethod},
  182. {"acme/sshSign", &ACME{}, SSHSignMethod},
  183. {"acme/sshRekey", &ACME{}, SSHRekeyMethod},
  184. {"acme/sshRenew", &ACME{}, SSHRenewMethod},
  185. {"acme/sshRevoke", &ACME{}, SSHRevokeMethod},
  186. {"sshpop/sign", &SSHPOP{}, SignMethod},
  187. {"sshpop/renew", &SSHPOP{}, RenewMethod},
  188. {"sshpop/revoke", &SSHPOP{}, RevokeMethod},
  189. {"sshpop/sshSign", &SSHPOP{}, SSHSignMethod},
  190. {"k8ssa/sshRekey", &K8sSA{}, SSHRekeyMethod},
  191. {"k8ssa/sshRenew", &K8sSA{}, SSHRenewMethod},
  192. {"k8ssa/sshRevoke", &K8sSA{}, SSHRevokeMethod},
  193. }
  194. for _, tt := range tests {
  195. t.Run(tt.name, func(t *testing.T) {
  196. var (
  197. err error
  198. msg string
  199. )
  200. switch tt.method {
  201. case SignMethod:
  202. var signOpts []SignOption
  203. signOpts, err = tt.p.AuthorizeSign(context.Background(), "")
  204. assert.Nil(t, signOpts)
  205. msg = "provisioner.AuthorizeSign not implemented"
  206. case RenewMethod:
  207. err = tt.p.AuthorizeRenew(context.Background(), nil)
  208. msg = "provisioner.AuthorizeRenew not implemented"
  209. case RevokeMethod:
  210. err = tt.p.AuthorizeRevoke(context.Background(), "")
  211. msg = "provisioner.AuthorizeRevoke not implemented"
  212. case SSHSignMethod:
  213. var signOpts []SignOption
  214. signOpts, err = tt.p.AuthorizeSSHSign(context.Background(), "")
  215. assert.Nil(t, signOpts)
  216. msg = "provisioner.AuthorizeSSHSign not implemented"
  217. case SSHRenewMethod:
  218. var cert *ssh.Certificate
  219. cert, err = tt.p.AuthorizeSSHRenew(context.Background(), "")
  220. assert.Nil(t, cert)
  221. msg = "provisioner.AuthorizeSSHRenew not implemented"
  222. case SSHRekeyMethod:
  223. var (
  224. cert *ssh.Certificate
  225. signOpts []SignOption
  226. )
  227. cert, signOpts, err = tt.p.AuthorizeSSHRekey(context.Background(), "")
  228. assert.Nil(t, cert)
  229. assert.Nil(t, signOpts)
  230. msg = "provisioner.AuthorizeSSHRekey not implemented"
  231. case SSHRevokeMethod:
  232. err = tt.p.AuthorizeSSHRevoke(context.Background(), "")
  233. msg = "provisioner.AuthorizeSSHRevoke not implemented"
  234. default:
  235. t.Errorf("unexpected method %s", tt.method)
  236. }
  237. var sc render.StatusCodedError
  238. if assert.True(t, errors.As(err, &sc), "error does not implement StatusCodedError interface") {
  239. assert.Equal(t, http.StatusUnauthorized, sc.StatusCode())
  240. }
  241. assert.Equal(t, msg, err.Error())
  242. })
  243. }
  244. }
  245. func TestUninitialized_MarshalJSON(t *testing.T) {
  246. p := &JWK{
  247. Name: "bad-provisioner",
  248. Type: "JWK",
  249. Key: &jose.JSONWebKey{
  250. Key: []byte("foo"),
  251. },
  252. }
  253. type fields struct {
  254. Interface Interface
  255. Reason error
  256. }
  257. tests := []struct {
  258. name string
  259. fields fields
  260. want []byte
  261. assertion assert.ErrorAssertionFunc
  262. }{
  263. {"ok", fields{p, errors.New("bad key")}, []byte(`{"type":"JWK","name":"bad-provisioner","key":{"kty":"oct","k":"Zm9v"},"state":"Uninitialized","stateReason":"bad key"}`), assert.NoError},
  264. }
  265. for _, tt := range tests {
  266. t.Run(tt.name, func(t *testing.T) {
  267. p := Uninitialized{
  268. Interface: tt.fields.Interface,
  269. Reason: tt.fields.Reason,
  270. }
  271. got, err := p.MarshalJSON()
  272. tt.assertion(t, err)
  273. assert.Equal(t, tt.want, got)
  274. })
  275. }
  276. }