oidc_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. package provisioner
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/rand"
  6. "crypto/rsa"
  7. "crypto/x509"
  8. "fmt"
  9. "net/http"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/pkg/errors"
  14. "github.com/smallstep/assert"
  15. "github.com/smallstep/certificates/errs"
  16. "go.step.sm/crypto/jose"
  17. )
  18. func Test_openIDConfiguration_Validate(t *testing.T) {
  19. type fields struct {
  20. Issuer string
  21. JWKSetURI string
  22. }
  23. tests := []struct {
  24. name string
  25. fields fields
  26. wantErr bool
  27. }{
  28. {"ok", fields{"the-issuer", "the-jwks-uri"}, false},
  29. {"no-issuer", fields{"", "the-jwks-uri"}, true},
  30. {"no-jwks-uri", fields{"the-issuer", ""}, true},
  31. {"empty", fields{"", ""}, true},
  32. }
  33. for _, tt := range tests {
  34. t.Run(tt.name, func(t *testing.T) {
  35. c := openIDConfiguration{
  36. Issuer: tt.fields.Issuer,
  37. JWKSetURI: tt.fields.JWKSetURI,
  38. }
  39. if err := c.Validate(); (err != nil) != tt.wantErr {
  40. t.Errorf("openIDConfiguration.Validate() error = %v, wantErr %v", err, tt.wantErr)
  41. }
  42. })
  43. }
  44. }
  45. func TestOIDC_Getters(t *testing.T) {
  46. p, err := generateOIDC()
  47. assert.FatalError(t, err)
  48. if got := p.GetID(); got != p.ClientID {
  49. t.Errorf("OIDC.GetID() = %v, want %v", got, p.ClientID)
  50. }
  51. if got := p.GetName(); got != p.Name {
  52. t.Errorf("OIDC.GetName() = %v, want %v", got, p.Name)
  53. }
  54. if got := p.GetType(); got != TypeOIDC {
  55. t.Errorf("OIDC.GetType() = %v, want %v", got, TypeOIDC)
  56. }
  57. kid, key, ok := p.GetEncryptedKey()
  58. if kid != "" || key != "" || ok == true {
  59. t.Errorf("OIDC.GetEncryptedKey() = (%v, %v, %v), want (%v, %v, %v)",
  60. kid, key, ok, "", "", false)
  61. }
  62. }
  63. func TestOIDC_Init(t *testing.T) {
  64. srv := generateJWKServer(2)
  65. defer srv.Close()
  66. config := Config{
  67. Claims: globalProvisionerClaims,
  68. }
  69. badClaims := &Claims{
  70. DefaultTLSDur: &Duration{0},
  71. }
  72. type fields struct {
  73. Type string
  74. Name string
  75. ClientID string
  76. ClientSecret string
  77. ConfigurationEndpoint string
  78. Claims *Claims
  79. Admins []string
  80. Domains []string
  81. ListenAddress string
  82. }
  83. type args struct {
  84. config Config
  85. }
  86. tests := []struct {
  87. name string
  88. fields fields
  89. args args
  90. wantErr bool
  91. }{
  92. {"ok", fields{"oidc", "name", "client-id", "client-secret", srv.URL, nil, nil, nil, ""}, args{config}, false},
  93. {"ok-admins", fields{"oidc", "name", "client-id", "client-secret", srv.URL + "/.well-known/openid-configuration", nil, []string{"foo@smallstep.com"}, nil, ""}, args{config}, false},
  94. {"ok-domains", fields{"oidc", "name", "client-id", "client-secret", srv.URL, nil, nil, []string{"smallstep.com"}, ""}, args{config}, false},
  95. {"ok-listen-port", fields{"oidc", "name", "client-id", "client-secret", srv.URL, nil, nil, nil, ":10000"}, args{config}, false},
  96. {"ok-listen-host-port", fields{"oidc", "name", "client-id", "client-secret", srv.URL, nil, nil, nil, "127.0.0.1:10000"}, args{config}, false},
  97. {"ok-no-secret", fields{"oidc", "name", "client-id", "", srv.URL, nil, nil, nil, ""}, args{config}, false},
  98. {"no-name", fields{"oidc", "", "client-id", "client-secret", srv.URL, nil, nil, nil, ""}, args{config}, true},
  99. {"no-type", fields{"", "name", "client-id", "client-secret", srv.URL, nil, nil, nil, ""}, args{config}, true},
  100. {"no-client-id", fields{"oidc", "name", "", "client-secret", srv.URL, nil, nil, nil, ""}, args{config}, true},
  101. {"no-configuration", fields{"oidc", "name", "client-id", "client-secret", "", nil, nil, nil, ""}, args{config}, true},
  102. {"bad-configuration", fields{"oidc", "name", "client-id", "client-secret", srv.URL + "/random", nil, nil, nil, ""}, args{config}, true},
  103. {"bad-claims", fields{"oidc", "name", "client-id", "client-secret", srv.URL + "/.well-known/openid-configuration", badClaims, nil, nil, ""}, args{config}, true},
  104. {"bad-parse-url", fields{"oidc", "name", "client-id", "client-secret", ":", nil, nil, nil, ""}, args{config}, true},
  105. {"bad-get-url", fields{"oidc", "name", "client-id", "client-secret", "https://", nil, nil, nil, ""}, args{config}, true},
  106. {"bad-listen-address", fields{"oidc", "name", "client-id", "client-secret", srv.URL, nil, nil, nil, "127.0.0.1"}, args{config}, true},
  107. }
  108. for _, tt := range tests {
  109. t.Run(tt.name, func(t *testing.T) {
  110. p := &OIDC{
  111. Type: tt.fields.Type,
  112. Name: tt.fields.Name,
  113. ClientID: tt.fields.ClientID,
  114. ConfigurationEndpoint: tt.fields.ConfigurationEndpoint,
  115. Claims: tt.fields.Claims,
  116. Admins: tt.fields.Admins,
  117. Domains: tt.fields.Domains,
  118. ListenAddress: tt.fields.ListenAddress,
  119. }
  120. if err := p.Init(tt.args.config); (err != nil) != tt.wantErr {
  121. t.Errorf("OIDC.Init() error = %v, wantErr %v", err, tt.wantErr)
  122. return
  123. }
  124. if tt.wantErr == false {
  125. assert.Len(t, 2, p.keyStore.keySet.Keys)
  126. assert.Equals(t, openIDConfiguration{
  127. Issuer: "the-issuer",
  128. JWKSetURI: srv.URL + "/jwks_uri",
  129. }, p.configuration)
  130. }
  131. })
  132. }
  133. }
  134. func TestOIDC_authorizeToken(t *testing.T) {
  135. srv := generateJWKServer(3)
  136. defer srv.Close()
  137. var keys jose.JSONWebKeySet
  138. assert.FatalError(t, getAndDecode(srv.URL+"/private", &keys))
  139. issuer := "the-issuer"
  140. tenantID := "ab800f7d-2c87-45fb-b1d0-f90d0bc5ec25"
  141. tenantIssuer := "https://login.microsoftonline.com/" + tenantID + "/v2.0"
  142. // Create test provisioners
  143. p1, err := generateOIDC()
  144. assert.FatalError(t, err)
  145. p2, err := generateOIDC()
  146. assert.FatalError(t, err)
  147. p3, err := generateOIDC()
  148. assert.FatalError(t, err)
  149. // TenantID
  150. p2.TenantID = tenantID
  151. // Admin + Domains
  152. p3.Admins = []string{"name@smallstep.com", "root@example.com"}
  153. p3.Domains = []string{"smallstep.com"}
  154. // Update configuration endpoints and initialize
  155. config := Config{Claims: globalProvisionerClaims}
  156. p1.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  157. p2.ConfigurationEndpoint = srv.URL + "/common/.well-known/openid-configuration"
  158. p3.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  159. assert.FatalError(t, p1.Init(config))
  160. assert.FatalError(t, p2.Init(config))
  161. assert.FatalError(t, p3.Init(config))
  162. t1, err := generateSimpleToken(issuer, p1.ClientID, &keys.Keys[0])
  163. assert.FatalError(t, err)
  164. t2, err := generateSimpleToken(tenantIssuer, p2.ClientID, &keys.Keys[1])
  165. assert.FatalError(t, err)
  166. t3, err := generateToken("subject", issuer, p3.ClientID, "name@smallstep.com", []string{}, time.Now(), &keys.Keys[2])
  167. assert.FatalError(t, err)
  168. t4, err := generateToken("subject", issuer, p3.ClientID, "foo@smallstep.com", []string{}, time.Now(), &keys.Keys[2])
  169. assert.FatalError(t, err)
  170. t5, err := generateToken("subject", issuer, p3.ClientID, "", []string{}, time.Now(), &keys.Keys[2])
  171. assert.FatalError(t, err)
  172. // Invalid email
  173. failDomain, err := generateToken("subject", issuer, p3.ClientID, "name@example.com", []string{}, time.Now(), &keys.Keys[2])
  174. assert.FatalError(t, err)
  175. // Invalid tokens
  176. parts := strings.Split(t1, ".")
  177. key, err := generateJSONWebKey()
  178. assert.FatalError(t, err)
  179. // missing key
  180. failKey, err := generateSimpleToken(issuer, p1.ClientID, key)
  181. assert.FatalError(t, err)
  182. // invalid token
  183. failTok := "foo." + parts[1] + "." + parts[2]
  184. // invalid claims
  185. failClaims := parts[0] + ".foo." + parts[1]
  186. // invalid issuer
  187. failIss, err := generateSimpleToken("bad-issuer", p1.ClientID, &keys.Keys[0])
  188. assert.FatalError(t, err)
  189. // invalid audience
  190. failAud, err := generateSimpleToken(issuer, "foobar", &keys.Keys[0])
  191. assert.FatalError(t, err)
  192. // invalid signature
  193. failSig := t1[0 : len(t1)-2]
  194. // expired
  195. failExp, err := generateToken("subject", issuer, p1.ClientID, "name@smallstep.com", []string{}, time.Now().Add(-360*time.Second), &keys.Keys[0])
  196. assert.FatalError(t, err)
  197. // not before
  198. failNbf, err := generateToken("subject", issuer, p1.ClientID, "name@smallstep.com", []string{}, time.Now().Add(360*time.Second), &keys.Keys[0])
  199. assert.FatalError(t, err)
  200. type args struct {
  201. token string
  202. }
  203. tests := []struct {
  204. name string
  205. prov *OIDC
  206. args args
  207. code int
  208. wantIssuer string
  209. wantErr bool
  210. }{
  211. {"ok1", p1, args{t1}, http.StatusOK, issuer, false},
  212. {"ok tenantid", p2, args{t2}, http.StatusOK, tenantIssuer, false},
  213. {"ok admin", p3, args{t3}, http.StatusOK, issuer, false},
  214. {"ok domain", p3, args{t4}, http.StatusOK, issuer, false},
  215. {"ok no email", p3, args{t5}, http.StatusOK, issuer, false},
  216. {"fail-domain", p3, args{failDomain}, http.StatusUnauthorized, "", true},
  217. {"fail-key", p1, args{failKey}, http.StatusUnauthorized, "", true},
  218. {"fail-token", p1, args{failTok}, http.StatusUnauthorized, "", true},
  219. {"fail-claims", p1, args{failClaims}, http.StatusUnauthorized, "", true},
  220. {"fail-issuer", p1, args{failIss}, http.StatusUnauthorized, "", true},
  221. {"fail-audience", p1, args{failAud}, http.StatusUnauthorized, "", true},
  222. {"fail-signature", p1, args{failSig}, http.StatusUnauthorized, "", true},
  223. {"fail-expired", p1, args{failExp}, http.StatusUnauthorized, "", true},
  224. {"fail-not-before", p1, args{failNbf}, http.StatusUnauthorized, "", true},
  225. }
  226. for _, tt := range tests {
  227. t.Run(tt.name, func(t *testing.T) {
  228. got, err := tt.prov.authorizeToken(tt.args.token)
  229. if (err != nil) != tt.wantErr {
  230. fmt.Println(tt)
  231. t.Errorf("OIDC.Authorize() error = %v, wantErr %v", err, tt.wantErr)
  232. return
  233. }
  234. if err != nil {
  235. sc, ok := err.(errs.StatusCoder)
  236. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  237. assert.Equals(t, sc.StatusCode(), tt.code)
  238. assert.Nil(t, got)
  239. } else {
  240. assert.NotNil(t, got)
  241. assert.Equals(t, got.Issuer, tt.wantIssuer)
  242. }
  243. })
  244. }
  245. }
  246. func TestOIDC_AuthorizeSign(t *testing.T) {
  247. srv := generateJWKServer(2)
  248. defer srv.Close()
  249. var keys jose.JSONWebKeySet
  250. assert.FatalError(t, getAndDecode(srv.URL+"/private", &keys))
  251. // Create test provisioners
  252. p1, err := generateOIDC()
  253. assert.FatalError(t, err)
  254. p2, err := generateOIDC()
  255. assert.FatalError(t, err)
  256. p3, err := generateOIDC()
  257. assert.FatalError(t, err)
  258. // Admin + Domains
  259. p3.Admins = []string{"name@smallstep.com", "root@example.com"}
  260. p3.Domains = []string{"smallstep.com"}
  261. // Update configuration endpoints and initialize
  262. config := Config{Claims: globalProvisionerClaims}
  263. p1.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  264. p2.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  265. p3.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  266. assert.FatalError(t, p1.Init(config))
  267. assert.FatalError(t, p2.Init(config))
  268. assert.FatalError(t, p3.Init(config))
  269. t1, err := generateSimpleToken("the-issuer", p1.ClientID, &keys.Keys[0])
  270. assert.FatalError(t, err)
  271. // Admin email not in domains
  272. okAdmin, err := generateToken("subject", "the-issuer", p3.ClientID, "root@example.com", []string{"test.smallstep.com"}, time.Now(), &keys.Keys[0])
  273. assert.FatalError(t, err)
  274. // No email
  275. noEmail, err := generateToken("subject", "the-issuer", p3.ClientID, "", []string{}, time.Now(), &keys.Keys[0])
  276. assert.FatalError(t, err)
  277. type args struct {
  278. token string
  279. }
  280. tests := []struct {
  281. name string
  282. prov *OIDC
  283. args args
  284. code int
  285. wantErr bool
  286. }{
  287. {"ok1", p1, args{t1}, http.StatusOK, false},
  288. {"admin", p3, args{okAdmin}, http.StatusOK, false},
  289. {"no-email", p3, args{noEmail}, http.StatusOK, false},
  290. {"bad-token", p3, args{"foobar"}, http.StatusUnauthorized, true},
  291. }
  292. for _, tt := range tests {
  293. t.Run(tt.name, func(t *testing.T) {
  294. got, err := tt.prov.AuthorizeSign(context.Background(), tt.args.token)
  295. if (err != nil) != tt.wantErr {
  296. t.Errorf("OIDC.Authorize() error = %v, wantErr %v", err, tt.wantErr)
  297. return
  298. }
  299. if err != nil {
  300. sc, ok := err.(errs.StatusCoder)
  301. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  302. assert.Equals(t, sc.StatusCode(), tt.code)
  303. assert.Nil(t, got)
  304. } else {
  305. if assert.NotNil(t, got) {
  306. if tt.name == "admin" {
  307. assert.Len(t, 5, got)
  308. } else {
  309. assert.Len(t, 5, got)
  310. }
  311. for _, o := range got {
  312. switch v := o.(type) {
  313. case certificateOptionsFunc:
  314. case *provisionerExtensionOption:
  315. assert.Equals(t, v.Type, int(TypeOIDC))
  316. assert.Equals(t, v.Name, tt.prov.GetName())
  317. assert.Equals(t, v.CredentialID, tt.prov.ClientID)
  318. assert.Len(t, 0, v.KeyValuePairs)
  319. case profileDefaultDuration:
  320. assert.Equals(t, time.Duration(v), tt.prov.claimer.DefaultTLSCertDuration())
  321. case defaultPublicKeyValidator:
  322. case *validityValidator:
  323. assert.Equals(t, v.min, tt.prov.claimer.MinTLSCertDuration())
  324. assert.Equals(t, v.max, tt.prov.claimer.MaxTLSCertDuration())
  325. case emailOnlyIdentity:
  326. assert.Equals(t, string(v), "name@smallstep.com")
  327. default:
  328. assert.FatalError(t, errors.Errorf("unexpected sign option of type %T", v))
  329. }
  330. }
  331. }
  332. }
  333. })
  334. }
  335. }
  336. func TestOIDC_AuthorizeRevoke(t *testing.T) {
  337. srv := generateJWKServer(2)
  338. defer srv.Close()
  339. var keys jose.JSONWebKeySet
  340. assert.FatalError(t, getAndDecode(srv.URL+"/private", &keys))
  341. // Create test provisioners
  342. p1, err := generateOIDC()
  343. assert.FatalError(t, err)
  344. p3, err := generateOIDC()
  345. assert.FatalError(t, err)
  346. // Admin + Domains
  347. p3.Admins = []string{"name@smallstep.com", "root@example.com"}
  348. p3.Domains = []string{"smallstep.com"}
  349. // Update configuration endpoints and initialize
  350. config := Config{Claims: globalProvisionerClaims}
  351. p1.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  352. p3.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  353. assert.FatalError(t, p1.Init(config))
  354. assert.FatalError(t, p3.Init(config))
  355. t1, err := generateSimpleToken("the-issuer", p1.ClientID, &keys.Keys[0])
  356. assert.FatalError(t, err)
  357. // Admin email not in domains
  358. okAdmin, err := generateToken("subject", "the-issuer", p3.ClientID, "root@example.com", []string{"test.smallstep.com"}, time.Now(), &keys.Keys[0])
  359. assert.FatalError(t, err)
  360. // Invalid email
  361. failEmail, err := generateToken("subject", "the-issuer", p3.ClientID, "", []string{}, time.Now(), &keys.Keys[0])
  362. assert.FatalError(t, err)
  363. type args struct {
  364. token string
  365. }
  366. tests := []struct {
  367. name string
  368. prov *OIDC
  369. args args
  370. code int
  371. wantErr bool
  372. }{
  373. {"ok1", p1, args{t1}, http.StatusUnauthorized, true},
  374. {"admin", p3, args{okAdmin}, http.StatusOK, false},
  375. {"fail-email", p3, args{failEmail}, http.StatusUnauthorized, true},
  376. }
  377. for _, tt := range tests {
  378. t.Run(tt.name, func(t *testing.T) {
  379. err := tt.prov.AuthorizeRevoke(context.Background(), tt.args.token)
  380. if (err != nil) != tt.wantErr {
  381. fmt.Println(tt)
  382. t.Errorf("OIDC.Authorize() error = %v, wantErr %v", err, tt.wantErr)
  383. return
  384. } else if err != nil {
  385. sc, ok := err.(errs.StatusCoder)
  386. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  387. assert.Equals(t, sc.StatusCode(), tt.code)
  388. }
  389. })
  390. }
  391. }
  392. func TestOIDC_AuthorizeRenew(t *testing.T) {
  393. p1, err := generateOIDC()
  394. assert.FatalError(t, err)
  395. p2, err := generateOIDC()
  396. assert.FatalError(t, err)
  397. // disable renewal
  398. disable := true
  399. p2.Claims = &Claims{DisableRenewal: &disable}
  400. p2.claimer, err = NewClaimer(p2.Claims, globalProvisionerClaims)
  401. assert.FatalError(t, err)
  402. type args struct {
  403. cert *x509.Certificate
  404. }
  405. tests := []struct {
  406. name string
  407. prov *OIDC
  408. args args
  409. code int
  410. wantErr bool
  411. }{
  412. {"ok", p1, args{nil}, http.StatusOK, false},
  413. {"fail/renew-disabled", p2, args{nil}, http.StatusUnauthorized, true},
  414. }
  415. for _, tt := range tests {
  416. t.Run(tt.name, func(t *testing.T) {
  417. err := tt.prov.AuthorizeRenew(context.Background(), tt.args.cert)
  418. if (err != nil) != tt.wantErr {
  419. t.Errorf("OIDC.AuthorizeRenew() error = %v, wantErr %v", err, tt.wantErr)
  420. } else if err != nil {
  421. sc, ok := err.(errs.StatusCoder)
  422. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  423. assert.Equals(t, sc.StatusCode(), tt.code)
  424. }
  425. })
  426. }
  427. }
  428. func TestOIDC_AuthorizeSSHSign(t *testing.T) {
  429. tm, fn := mockNow()
  430. defer fn()
  431. srv := generateJWKServer(2)
  432. defer srv.Close()
  433. var keys jose.JSONWebKeySet
  434. assert.FatalError(t, getAndDecode(srv.URL+"/private", &keys))
  435. // Create test provisioners
  436. p1, err := generateOIDC()
  437. assert.FatalError(t, err)
  438. p2, err := generateOIDC()
  439. assert.FatalError(t, err)
  440. p3, err := generateOIDC()
  441. assert.FatalError(t, err)
  442. p4, err := generateOIDC()
  443. assert.FatalError(t, err)
  444. p5, err := generateOIDC()
  445. assert.FatalError(t, err)
  446. p6, err := generateOIDC()
  447. assert.FatalError(t, err)
  448. // Admin + Domains
  449. p3.Admins = []string{"name@smallstep.com", "root@example.com"}
  450. p3.Domains = []string{"smallstep.com"}
  451. // disable sshCA
  452. disable := false
  453. p6.Claims = &Claims{EnableSSHCA: &disable}
  454. p6.claimer, err = NewClaimer(p6.Claims, globalProvisionerClaims)
  455. assert.FatalError(t, err)
  456. // Update configuration endpoints and initialize
  457. config := Config{Claims: globalProvisionerClaims}
  458. p1.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  459. p2.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  460. p3.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  461. p4.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  462. p5.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  463. assert.FatalError(t, p1.Init(config))
  464. assert.FatalError(t, p2.Init(config))
  465. assert.FatalError(t, p3.Init(config))
  466. assert.FatalError(t, p4.Init(config))
  467. assert.FatalError(t, p5.Init(config))
  468. p4.getIdentityFunc = func(ctx context.Context, p Interface, email string) (*Identity, error) {
  469. return &Identity{Usernames: []string{"max", "mariano"}}, nil
  470. }
  471. p5.getIdentityFunc = func(ctx context.Context, p Interface, email string) (*Identity, error) {
  472. return nil, errors.New("force")
  473. }
  474. // Additional test needed for empty usernames and duplicate email and usernames
  475. t1, err := generateSimpleToken("the-issuer", p1.ClientID, &keys.Keys[0])
  476. assert.FatalError(t, err)
  477. okGetIdentityToken, err := generateSimpleToken("the-issuer", p4.ClientID, &keys.Keys[0])
  478. assert.FatalError(t, err)
  479. failGetIdentityToken, err := generateSimpleToken("the-issuer", p5.ClientID, &keys.Keys[0])
  480. assert.FatalError(t, err)
  481. // Admin email not in domains
  482. okAdmin, err := generateOIDCToken("subject", "the-issuer", p3.ClientID, "root@example.com", "", time.Now(), &keys.Keys[0])
  483. assert.FatalError(t, err)
  484. // Empty email
  485. failEmail, err := generateToken("subject", "the-issuer", p3.ClientID, "", []string{}, time.Now(), &keys.Keys[0])
  486. assert.FatalError(t, err)
  487. key, err := generateJSONWebKey()
  488. assert.FatalError(t, err)
  489. signer, err := generateJSONWebKey()
  490. assert.FatalError(t, err)
  491. pub := key.Public().Key
  492. rsa2048, err := rsa.GenerateKey(rand.Reader, 2048)
  493. assert.FatalError(t, err)
  494. rsa1024, err := rsa.GenerateKey(rand.Reader, 1024)
  495. assert.FatalError(t, err)
  496. userDuration := p1.claimer.DefaultUserSSHCertDuration()
  497. hostDuration := p1.claimer.DefaultHostSSHCertDuration()
  498. expectedUserOptions := &SignSSHOptions{
  499. CertType: "user", Principals: []string{"name", "name@smallstep.com"},
  500. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration)),
  501. }
  502. expectedAdminOptions := &SignSSHOptions{
  503. CertType: "user", Principals: []string{"root", "root@example.com"},
  504. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration)),
  505. }
  506. expectedHostOptions := &SignSSHOptions{
  507. CertType: "host", Principals: []string{"smallstep.com"},
  508. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(hostDuration)),
  509. }
  510. type args struct {
  511. token string
  512. sshOpts SignSSHOptions
  513. key interface{}
  514. }
  515. tests := []struct {
  516. name string
  517. prov *OIDC
  518. args args
  519. expected *SignSSHOptions
  520. code int
  521. wantErr bool
  522. wantSignErr bool
  523. }{
  524. {"ok", p1, args{t1, SignSSHOptions{}, pub}, expectedUserOptions, http.StatusOK, false, false},
  525. {"ok-rsa2048", p1, args{t1, SignSSHOptions{}, rsa2048.Public()}, expectedUserOptions, http.StatusOK, false, false},
  526. {"ok-user", p1, args{t1, SignSSHOptions{CertType: "user"}, pub}, expectedUserOptions, http.StatusOK, false, false},
  527. {"ok-principals", p1, args{t1, SignSSHOptions{Principals: []string{"name"}}, pub},
  528. &SignSSHOptions{CertType: "user", Principals: []string{"name", "name@smallstep.com"},
  529. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration))}, http.StatusOK, false, false},
  530. {"ok-principals-getIdentity", p4, args{okGetIdentityToken, SignSSHOptions{Principals: []string{"mariano"}}, pub},
  531. &SignSSHOptions{CertType: "user", Principals: []string{"max", "mariano"},
  532. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration))}, http.StatusOK, false, false},
  533. {"ok-emptyPrincipals-getIdentity", p4, args{okGetIdentityToken, SignSSHOptions{}, pub},
  534. &SignSSHOptions{CertType: "user", Principals: []string{"max", "mariano"},
  535. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration))}, http.StatusOK, false, false},
  536. {"ok-options", p1, args{t1, SignSSHOptions{CertType: "user", Principals: []string{"name"}}, pub},
  537. &SignSSHOptions{CertType: "user", Principals: []string{"name", "name@smallstep.com"},
  538. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration))}, http.StatusOK, false, false},
  539. {"ok-admin-user", p3, args{okAdmin, SignSSHOptions{CertType: "user", KeyID: "root@example.com", Principals: []string{"root", "root@example.com"}}, pub},
  540. expectedAdminOptions, http.StatusOK, false, false},
  541. {"ok-admin-host", p3, args{okAdmin, SignSSHOptions{CertType: "host", KeyID: "smallstep.com", Principals: []string{"smallstep.com"}}, pub},
  542. expectedHostOptions, http.StatusOK, false, false},
  543. {"ok-admin-options", p3, args{okAdmin, SignSSHOptions{CertType: "user", KeyID: "name", Principals: []string{"name"}}, pub},
  544. &SignSSHOptions{CertType: "user", Principals: []string{"name"},
  545. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration))}, http.StatusOK, false, false},
  546. {"fail-rsa1024", p1, args{t1, SignSSHOptions{}, rsa1024.Public()}, expectedUserOptions, http.StatusOK, false, true},
  547. {"fail-user-host", p1, args{t1, SignSSHOptions{CertType: "host"}, pub}, nil, http.StatusOK, false, true},
  548. {"fail-user-principals", p1, args{t1, SignSSHOptions{Principals: []string{"root"}}, pub}, nil, http.StatusOK, false, true},
  549. {"fail-email", p3, args{failEmail, SignSSHOptions{}, pub}, nil, http.StatusUnauthorized, true, false},
  550. {"fail-getIdentity", p5, args{failGetIdentityToken, SignSSHOptions{}, pub}, nil, http.StatusInternalServerError, true, false},
  551. {"fail-sshCA-disabled", p6, args{"foo", SignSSHOptions{}, pub}, nil, http.StatusUnauthorized, true, false},
  552. // Missing parametrs
  553. {"fail-admin-type", p3, args{okAdmin, SignSSHOptions{KeyID: "root@example.com", Principals: []string{"root@example.com"}}, pub}, nil, http.StatusUnauthorized, false, true},
  554. {"fail-admin-key-id", p3, args{okAdmin, SignSSHOptions{CertType: "user", Principals: []string{"root@example.com"}}, pub}, nil, http.StatusUnauthorized, false, true},
  555. {"fail-admin-principals", p3, args{okAdmin, SignSSHOptions{CertType: "user", KeyID: "root@example.com"}, pub}, nil, http.StatusUnauthorized, false, true},
  556. }
  557. for _, tt := range tests {
  558. t.Run(tt.name, func(t *testing.T) {
  559. got, err := tt.prov.AuthorizeSSHSign(context.Background(), tt.args.token)
  560. if (err != nil) != tt.wantErr {
  561. t.Errorf("OIDC.AuthorizeSSHSign() error = %v, wantErr %v", err, tt.wantErr)
  562. return
  563. }
  564. if err != nil {
  565. sc, ok := err.(errs.StatusCoder)
  566. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  567. assert.Equals(t, sc.StatusCode(), tt.code)
  568. assert.Nil(t, got)
  569. } else if assert.NotNil(t, got) {
  570. cert, err := signSSHCertificate(tt.args.key, tt.args.sshOpts, got, signer.Key.(crypto.Signer))
  571. if (err != nil) != tt.wantSignErr {
  572. t.Errorf("SignSSH error = %v, wantSignErr %v", err, tt.wantSignErr)
  573. } else {
  574. if tt.wantSignErr {
  575. assert.Nil(t, cert)
  576. } else {
  577. assert.NoError(t, validateSSHCertificate(cert, tt.expected))
  578. }
  579. }
  580. }
  581. })
  582. }
  583. }
  584. func TestOIDC_AuthorizeSSHRevoke(t *testing.T) {
  585. p1, err := generateOIDC()
  586. assert.FatalError(t, err)
  587. p2, err := generateOIDC()
  588. assert.FatalError(t, err)
  589. p2.Admins = []string{"root@example.com"}
  590. srv := generateJWKServer(2)
  591. defer srv.Close()
  592. var keys jose.JSONWebKeySet
  593. assert.FatalError(t, getAndDecode(srv.URL+"/private", &keys))
  594. config := Config{Claims: globalProvisionerClaims}
  595. p1.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  596. p2.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  597. assert.FatalError(t, p1.Init(config))
  598. assert.FatalError(t, p2.Init(config))
  599. // Invalid email
  600. failEmail, err := generateToken("subject", "the-issuer", p1.ClientID, "", []string{}, time.Now(), &keys.Keys[0])
  601. assert.FatalError(t, err)
  602. // Admin email not in domains
  603. noAdmin, err := generateToken("subject", "the-issuer", p1.ClientID, "root@example.com", []string{"test.smallstep.com"}, time.Now(), &keys.Keys[0])
  604. assert.FatalError(t, err)
  605. // Admin email in domains
  606. okAdmin, err := generateToken("subject", "the-issuer", p2.ClientID, "root@example.com", []string{"test.smallstep.com"}, time.Now(), &keys.Keys[0])
  607. assert.FatalError(t, err)
  608. type args struct {
  609. token string
  610. }
  611. tests := []struct {
  612. name string
  613. prov *OIDC
  614. args args
  615. code int
  616. wantErr bool
  617. }{
  618. {"ok", p2, args{okAdmin}, http.StatusOK, false},
  619. {"fail/invalid-token", p1, args{failEmail}, http.StatusUnauthorized, true},
  620. {"fail/not-admin", p1, args{noAdmin}, http.StatusUnauthorized, true},
  621. }
  622. for _, tt := range tests {
  623. t.Run(tt.name, func(t *testing.T) {
  624. err := tt.prov.AuthorizeSSHRevoke(context.Background(), tt.args.token)
  625. if (err != nil) != tt.wantErr {
  626. t.Errorf("OIDC.AuthorizeSSHRevoke() error = %v, wantErr %v", err, tt.wantErr)
  627. } else if err != nil {
  628. sc, ok := err.(errs.StatusCoder)
  629. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  630. assert.Equals(t, sc.StatusCode(), tt.code)
  631. }
  632. })
  633. }
  634. }
  635. func Test_sanitizeEmail(t *testing.T) {
  636. tests := []struct {
  637. name string
  638. email string
  639. want string
  640. }{
  641. {"equal", "name@smallstep.com", "name@smallstep.com"},
  642. {"domain-insensitive", "name@SMALLSTEP.COM", "name@smallstep.com"},
  643. {"local-sensitive", "NaMe@smallSTEP.CoM", "NaMe@smallstep.com"},
  644. {"multiple-@", "NaMe@NaMe@smallSTEP.CoM", "NaMe@NaMe@smallstep.com"},
  645. }
  646. for _, tt := range tests {
  647. t.Run(tt.name, func(t *testing.T) {
  648. if got := sanitizeEmail(tt.email); got != tt.want {
  649. t.Errorf("sanitizeEmail() = %v, want %v", got, tt.want)
  650. }
  651. })
  652. }
  653. }