4
0

oidc_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. "github.com/smallstep/cli/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(2)
  136. defer srv.Close()
  137. var keys jose.JSONWebKeySet
  138. assert.FatalError(t, getAndDecode(srv.URL+"/private", &keys))
  139. // Create test provisioners
  140. p1, err := generateOIDC()
  141. assert.FatalError(t, err)
  142. p2, err := generateOIDC()
  143. assert.FatalError(t, err)
  144. p3, err := generateOIDC()
  145. assert.FatalError(t, err)
  146. // Admin + Domains
  147. p3.Admins = []string{"name@smallstep.com", "root@example.com"}
  148. p3.Domains = []string{"smallstep.com"}
  149. // Update configuration endpoints and initialize
  150. config := Config{Claims: globalProvisionerClaims}
  151. p1.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  152. p2.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  153. p3.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  154. assert.FatalError(t, p1.Init(config))
  155. assert.FatalError(t, p2.Init(config))
  156. assert.FatalError(t, p3.Init(config))
  157. t1, err := generateSimpleToken("the-issuer", p1.ClientID, &keys.Keys[0])
  158. assert.FatalError(t, err)
  159. t2, err := generateSimpleToken("the-issuer", p2.ClientID, &keys.Keys[1])
  160. assert.FatalError(t, err)
  161. // Invalid email
  162. failEmail, err := generateToken("subject", "the-issuer", p3.ClientID, "", []string{}, time.Now(), &keys.Keys[0])
  163. assert.FatalError(t, err)
  164. failDomain, err := generateToken("subject", "the-issuer", p3.ClientID, "name@example.com", []string{}, time.Now(), &keys.Keys[0])
  165. assert.FatalError(t, err)
  166. // Invalid tokens
  167. parts := strings.Split(t1, ".")
  168. key, err := generateJSONWebKey()
  169. assert.FatalError(t, err)
  170. // missing key
  171. failKey, err := generateSimpleToken("the-issuer", p1.ClientID, key)
  172. assert.FatalError(t, err)
  173. // invalid token
  174. failTok := "foo." + parts[1] + "." + parts[2]
  175. // invalid claims
  176. failClaims := parts[0] + ".foo." + parts[1]
  177. // invalid issuer
  178. failIss, err := generateSimpleToken("bad-issuer", p1.ClientID, &keys.Keys[0])
  179. assert.FatalError(t, err)
  180. // invalid audience
  181. failAud, err := generateSimpleToken("the-issuer", "foobar", &keys.Keys[0])
  182. assert.FatalError(t, err)
  183. // invalid signature
  184. failSig := t1[0 : len(t1)-2]
  185. // expired
  186. failExp, err := generateToken("subject", "the-issuer", p1.ClientID, "name@smallstep.com", []string{}, time.Now().Add(-360*time.Second), &keys.Keys[0])
  187. assert.FatalError(t, err)
  188. // not before
  189. failNbf, err := generateToken("subject", "the-issuer", p1.ClientID, "name@smallstep.com", []string{}, time.Now().Add(360*time.Second), &keys.Keys[0])
  190. assert.FatalError(t, err)
  191. type args struct {
  192. token string
  193. }
  194. tests := []struct {
  195. name string
  196. prov *OIDC
  197. args args
  198. code int
  199. wantErr bool
  200. }{
  201. {"ok1", p1, args{t1}, http.StatusOK, false},
  202. {"ok2", p2, args{t2}, http.StatusOK, false},
  203. {"fail-email", p3, args{failEmail}, http.StatusUnauthorized, true},
  204. {"fail-domain", p3, args{failDomain}, http.StatusUnauthorized, true},
  205. {"fail-key", p1, args{failKey}, http.StatusUnauthorized, true},
  206. {"fail-token", p1, args{failTok}, http.StatusUnauthorized, true},
  207. {"fail-claims", p1, args{failClaims}, http.StatusUnauthorized, true},
  208. {"fail-issuer", p1, args{failIss}, http.StatusUnauthorized, true},
  209. {"fail-audience", p1, args{failAud}, http.StatusUnauthorized, true},
  210. {"fail-signature", p1, args{failSig}, http.StatusUnauthorized, true},
  211. {"fail-expired", p1, args{failExp}, http.StatusUnauthorized, true},
  212. {"fail-not-before", p1, args{failNbf}, http.StatusUnauthorized, true},
  213. }
  214. for _, tt := range tests {
  215. t.Run(tt.name, func(t *testing.T) {
  216. got, err := tt.prov.authorizeToken(tt.args.token)
  217. if (err != nil) != tt.wantErr {
  218. fmt.Println(tt)
  219. t.Errorf("OIDC.Authorize() error = %v, wantErr %v", err, tt.wantErr)
  220. return
  221. }
  222. if err != nil {
  223. sc, ok := err.(errs.StatusCoder)
  224. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  225. assert.Equals(t, sc.StatusCode(), tt.code)
  226. assert.Nil(t, got)
  227. } else {
  228. assert.NotNil(t, got)
  229. assert.Equals(t, got.Issuer, "the-issuer")
  230. }
  231. })
  232. }
  233. }
  234. func TestOIDC_AuthorizeSign(t *testing.T) {
  235. srv := generateJWKServer(2)
  236. defer srv.Close()
  237. var keys jose.JSONWebKeySet
  238. assert.FatalError(t, getAndDecode(srv.URL+"/private", &keys))
  239. // Create test provisioners
  240. p1, err := generateOIDC()
  241. assert.FatalError(t, err)
  242. p2, err := generateOIDC()
  243. assert.FatalError(t, err)
  244. p3, err := generateOIDC()
  245. assert.FatalError(t, err)
  246. // Admin + Domains
  247. p3.Admins = []string{"name@smallstep.com", "root@example.com"}
  248. p3.Domains = []string{"smallstep.com"}
  249. // Update configuration endpoints and initialize
  250. config := Config{Claims: globalProvisionerClaims}
  251. p1.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  252. p2.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  253. p3.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  254. assert.FatalError(t, p1.Init(config))
  255. assert.FatalError(t, p2.Init(config))
  256. assert.FatalError(t, p3.Init(config))
  257. t1, err := generateSimpleToken("the-issuer", p1.ClientID, &keys.Keys[0])
  258. assert.FatalError(t, err)
  259. // Admin email not in domains
  260. okAdmin, err := generateToken("subject", "the-issuer", p3.ClientID, "root@example.com", []string{"test.smallstep.com"}, time.Now(), &keys.Keys[0])
  261. assert.FatalError(t, err)
  262. // Invalid email
  263. failEmail, err := generateToken("subject", "the-issuer", p3.ClientID, "", []string{}, time.Now(), &keys.Keys[0])
  264. assert.FatalError(t, err)
  265. type args struct {
  266. token string
  267. }
  268. tests := []struct {
  269. name string
  270. prov *OIDC
  271. args args
  272. code int
  273. wantErr bool
  274. }{
  275. {"ok1", p1, args{t1}, http.StatusOK, false},
  276. {"admin", p3, args{okAdmin}, http.StatusOK, false},
  277. {"fail-email", p3, args{failEmail}, http.StatusUnauthorized, true},
  278. }
  279. for _, tt := range tests {
  280. t.Run(tt.name, func(t *testing.T) {
  281. got, err := tt.prov.AuthorizeSign(context.Background(), tt.args.token)
  282. if (err != nil) != tt.wantErr {
  283. t.Errorf("OIDC.Authorize() error = %v, wantErr %v", err, tt.wantErr)
  284. return
  285. }
  286. if err != nil {
  287. sc, ok := err.(errs.StatusCoder)
  288. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  289. assert.Equals(t, sc.StatusCode(), tt.code)
  290. assert.Nil(t, got)
  291. } else {
  292. if assert.NotNil(t, got) {
  293. if tt.name == "admin" {
  294. assert.Len(t, 4, got)
  295. } else {
  296. assert.Len(t, 5, got)
  297. }
  298. for _, o := range got {
  299. switch v := o.(type) {
  300. case *provisionerExtensionOption:
  301. assert.Equals(t, v.Type, int(TypeOIDC))
  302. assert.Equals(t, v.Name, tt.prov.GetName())
  303. assert.Equals(t, v.CredentialID, tt.prov.ClientID)
  304. assert.Len(t, 0, v.KeyValuePairs)
  305. case profileDefaultDuration:
  306. assert.Equals(t, time.Duration(v), tt.prov.claimer.DefaultTLSCertDuration())
  307. case defaultPublicKeyValidator:
  308. case *validityValidator:
  309. assert.Equals(t, v.min, tt.prov.claimer.MinTLSCertDuration())
  310. assert.Equals(t, v.max, tt.prov.claimer.MaxTLSCertDuration())
  311. case emailOnlyIdentity:
  312. assert.Equals(t, string(v), "name@smallstep.com")
  313. default:
  314. assert.FatalError(t, errors.Errorf("unexpected sign option of type %T", v))
  315. }
  316. }
  317. }
  318. }
  319. })
  320. }
  321. }
  322. func TestOIDC_AuthorizeRevoke(t *testing.T) {
  323. srv := generateJWKServer(2)
  324. defer srv.Close()
  325. var keys jose.JSONWebKeySet
  326. assert.FatalError(t, getAndDecode(srv.URL+"/private", &keys))
  327. // Create test provisioners
  328. p1, err := generateOIDC()
  329. assert.FatalError(t, err)
  330. p3, err := generateOIDC()
  331. assert.FatalError(t, err)
  332. // Admin + Domains
  333. p3.Admins = []string{"name@smallstep.com", "root@example.com"}
  334. p3.Domains = []string{"smallstep.com"}
  335. // Update configuration endpoints and initialize
  336. config := Config{Claims: globalProvisionerClaims}
  337. p1.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  338. p3.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  339. assert.FatalError(t, p1.Init(config))
  340. assert.FatalError(t, p3.Init(config))
  341. t1, err := generateSimpleToken("the-issuer", p1.ClientID, &keys.Keys[0])
  342. assert.FatalError(t, err)
  343. // Admin email not in domains
  344. okAdmin, err := generateToken("subject", "the-issuer", p3.ClientID, "root@example.com", []string{"test.smallstep.com"}, time.Now(), &keys.Keys[0])
  345. assert.FatalError(t, err)
  346. // Invalid email
  347. failEmail, err := generateToken("subject", "the-issuer", p3.ClientID, "", []string{}, time.Now(), &keys.Keys[0])
  348. assert.FatalError(t, err)
  349. type args struct {
  350. token string
  351. }
  352. tests := []struct {
  353. name string
  354. prov *OIDC
  355. args args
  356. code int
  357. wantErr bool
  358. }{
  359. {"ok1", p1, args{t1}, http.StatusUnauthorized, true},
  360. {"admin", p3, args{okAdmin}, http.StatusOK, false},
  361. {"fail-email", p3, args{failEmail}, http.StatusUnauthorized, true},
  362. }
  363. for _, tt := range tests {
  364. t.Run(tt.name, func(t *testing.T) {
  365. err := tt.prov.AuthorizeRevoke(context.Background(), tt.args.token)
  366. if (err != nil) != tt.wantErr {
  367. fmt.Println(tt)
  368. t.Errorf("OIDC.Authorize() error = %v, wantErr %v", err, tt.wantErr)
  369. return
  370. } else if err != nil {
  371. sc, ok := err.(errs.StatusCoder)
  372. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  373. assert.Equals(t, sc.StatusCode(), tt.code)
  374. }
  375. })
  376. }
  377. }
  378. func TestOIDC_AuthorizeRenew(t *testing.T) {
  379. p1, err := generateOIDC()
  380. assert.FatalError(t, err)
  381. p2, err := generateOIDC()
  382. assert.FatalError(t, err)
  383. // disable renewal
  384. disable := true
  385. p2.Claims = &Claims{DisableRenewal: &disable}
  386. p2.claimer, err = NewClaimer(p2.Claims, globalProvisionerClaims)
  387. assert.FatalError(t, err)
  388. type args struct {
  389. cert *x509.Certificate
  390. }
  391. tests := []struct {
  392. name string
  393. prov *OIDC
  394. args args
  395. code int
  396. wantErr bool
  397. }{
  398. {"ok", p1, args{nil}, http.StatusOK, false},
  399. {"fail/renew-disabled", p2, args{nil}, http.StatusUnauthorized, true},
  400. }
  401. for _, tt := range tests {
  402. t.Run(tt.name, func(t *testing.T) {
  403. err := tt.prov.AuthorizeRenew(context.Background(), tt.args.cert)
  404. if (err != nil) != tt.wantErr {
  405. t.Errorf("OIDC.AuthorizeRenew() error = %v, wantErr %v", err, tt.wantErr)
  406. } else if err != nil {
  407. sc, ok := err.(errs.StatusCoder)
  408. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  409. assert.Equals(t, sc.StatusCode(), tt.code)
  410. }
  411. })
  412. }
  413. }
  414. func TestOIDC_AuthorizeSSHSign(t *testing.T) {
  415. tm, fn := mockNow()
  416. defer fn()
  417. srv := generateJWKServer(2)
  418. defer srv.Close()
  419. var keys jose.JSONWebKeySet
  420. assert.FatalError(t, getAndDecode(srv.URL+"/private", &keys))
  421. // Create test provisioners
  422. p1, err := generateOIDC()
  423. assert.FatalError(t, err)
  424. p2, err := generateOIDC()
  425. assert.FatalError(t, err)
  426. p3, err := generateOIDC()
  427. assert.FatalError(t, err)
  428. p4, err := generateOIDC()
  429. assert.FatalError(t, err)
  430. p5, err := generateOIDC()
  431. assert.FatalError(t, err)
  432. p6, err := generateOIDC()
  433. assert.FatalError(t, err)
  434. // Admin + Domains
  435. p3.Admins = []string{"name@smallstep.com", "root@example.com"}
  436. p3.Domains = []string{"smallstep.com"}
  437. // disable sshCA
  438. disable := false
  439. p6.Claims = &Claims{EnableSSHCA: &disable}
  440. p6.claimer, err = NewClaimer(p6.Claims, globalProvisionerClaims)
  441. assert.FatalError(t, err)
  442. // Update configuration endpoints and initialize
  443. config := Config{Claims: globalProvisionerClaims}
  444. p1.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  445. p2.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  446. p3.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  447. p4.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  448. p5.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  449. assert.FatalError(t, p1.Init(config))
  450. assert.FatalError(t, p2.Init(config))
  451. assert.FatalError(t, p3.Init(config))
  452. assert.FatalError(t, p4.Init(config))
  453. assert.FatalError(t, p5.Init(config))
  454. p4.getIdentityFunc = func(p Interface, email string) (*Identity, error) {
  455. return &Identity{Usernames: []string{"max", "mariano"}}, nil
  456. }
  457. p5.getIdentityFunc = func(p Interface, email string) (*Identity, error) {
  458. return nil, errors.New("force")
  459. }
  460. t1, err := generateSimpleToken("the-issuer", p1.ClientID, &keys.Keys[0])
  461. assert.FatalError(t, err)
  462. okGetIdentityToken, err := generateSimpleToken("the-issuer", p4.ClientID, &keys.Keys[0])
  463. assert.FatalError(t, err)
  464. failGetIdentityToken, err := generateSimpleToken("the-issuer", p5.ClientID, &keys.Keys[0])
  465. assert.FatalError(t, err)
  466. // Admin email not in domains
  467. okAdmin, err := generateToken("subject", "the-issuer", p3.ClientID, "root@example.com", []string{}, time.Now(), &keys.Keys[0])
  468. assert.FatalError(t, err)
  469. // Invalid email
  470. failEmail, err := generateToken("subject", "the-issuer", p3.ClientID, "", []string{}, time.Now(), &keys.Keys[0])
  471. assert.FatalError(t, err)
  472. key, err := generateJSONWebKey()
  473. assert.FatalError(t, err)
  474. signer, err := generateJSONWebKey()
  475. assert.FatalError(t, err)
  476. pub := key.Public().Key
  477. rsa2048, err := rsa.GenerateKey(rand.Reader, 2048)
  478. assert.FatalError(t, err)
  479. rsa1024, err := rsa.GenerateKey(rand.Reader, 1024)
  480. assert.FatalError(t, err)
  481. userDuration := p1.claimer.DefaultUserSSHCertDuration()
  482. hostDuration := p1.claimer.DefaultHostSSHCertDuration()
  483. expectedUserOptions := &SSHOptions{
  484. CertType: "user", Principals: []string{"name", "name@smallstep.com"},
  485. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration)),
  486. }
  487. expectedAdminOptions := &SSHOptions{
  488. CertType: "user", Principals: []string{"root", "root@example.com"},
  489. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration)),
  490. }
  491. expectedHostOptions := &SSHOptions{
  492. CertType: "host", Principals: []string{"smallstep.com"},
  493. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(hostDuration)),
  494. }
  495. type args struct {
  496. token string
  497. sshOpts SSHOptions
  498. key interface{}
  499. }
  500. tests := []struct {
  501. name string
  502. prov *OIDC
  503. args args
  504. expected *SSHOptions
  505. code int
  506. wantErr bool
  507. wantSignErr bool
  508. }{
  509. {"ok", p1, args{t1, SSHOptions{}, pub}, expectedUserOptions, http.StatusOK, false, false},
  510. {"ok-rsa2048", p1, args{t1, SSHOptions{}, rsa2048.Public()}, expectedUserOptions, http.StatusOK, false, false},
  511. {"ok-user", p1, args{t1, SSHOptions{CertType: "user"}, pub}, expectedUserOptions, http.StatusOK, false, false},
  512. {"ok-principals", p1, args{t1, SSHOptions{Principals: []string{"name"}}, pub},
  513. &SSHOptions{CertType: "user", Principals: []string{"name"},
  514. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration))}, http.StatusOK, false, false},
  515. {"ok-principals-getIdentity", p4, args{okGetIdentityToken, SSHOptions{Principals: []string{"mariano"}}, pub},
  516. &SSHOptions{CertType: "user", Principals: []string{"mariano"},
  517. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration))}, http.StatusOK, false, false},
  518. {"ok-emptyPrincipals-getIdentity", p4, args{okGetIdentityToken, SSHOptions{}, pub},
  519. &SSHOptions{CertType: "user", Principals: []string{"max", "mariano"},
  520. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration))}, http.StatusOK, false, false},
  521. {"ok-options", p1, args{t1, SSHOptions{CertType: "user", Principals: []string{"name"}}, pub},
  522. &SSHOptions{CertType: "user", Principals: []string{"name"},
  523. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration))}, http.StatusOK, false, false},
  524. {"admin", p3, args{okAdmin, SSHOptions{}, pub}, expectedAdminOptions, http.StatusOK, false, false},
  525. {"admin-user", p3, args{okAdmin, SSHOptions{CertType: "user"}, pub}, expectedAdminOptions, http.StatusOK, false, false},
  526. {"admin-principals", p3, args{okAdmin, SSHOptions{Principals: []string{"root"}}, pub},
  527. &SSHOptions{CertType: "user", Principals: []string{"root"},
  528. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration))}, http.StatusOK, false, false},
  529. {"admin-options", p3, args{okAdmin, SSHOptions{CertType: "user", Principals: []string{"name"}}, pub},
  530. &SSHOptions{CertType: "user", Principals: []string{"name"},
  531. ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration))}, http.StatusOK, false, false},
  532. {"admin-host", p3, args{okAdmin, SSHOptions{CertType: "host", Principals: []string{"smallstep.com"}}, pub},
  533. expectedHostOptions, http.StatusOK, false, false},
  534. {"fail-rsa1024", p1, args{t1, SSHOptions{}, rsa1024.Public()}, expectedUserOptions, http.StatusOK, false, true},
  535. {"fail-user-host", p1, args{t1, SSHOptions{CertType: "host"}, pub}, nil, http.StatusOK, false, true},
  536. {"fail-user-principals", p1, args{t1, SSHOptions{Principals: []string{"root"}}, pub}, nil, http.StatusOK, false, true},
  537. {"fail-email", p3, args{failEmail, SSHOptions{}, pub}, nil, http.StatusUnauthorized, true, false},
  538. {"fail-getIdentity", p5, args{failGetIdentityToken, SSHOptions{}, pub}, nil, http.StatusInternalServerError, true, false},
  539. {"fail-sshCA-disabled", p6, args{"foo", SSHOptions{}, pub}, nil, http.StatusUnauthorized, true, false},
  540. }
  541. for _, tt := range tests {
  542. t.Run(tt.name, func(t *testing.T) {
  543. got, err := tt.prov.AuthorizeSSHSign(context.Background(), tt.args.token)
  544. if (err != nil) != tt.wantErr {
  545. t.Errorf("OIDC.AuthorizeSSHSign() error = %v, wantErr %v", err, tt.wantErr)
  546. return
  547. }
  548. if err != nil {
  549. sc, ok := err.(errs.StatusCoder)
  550. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  551. assert.Equals(t, sc.StatusCode(), tt.code)
  552. assert.Nil(t, got)
  553. } else if assert.NotNil(t, got) {
  554. cert, err := signSSHCertificate(tt.args.key, tt.args.sshOpts, got, signer.Key.(crypto.Signer))
  555. if (err != nil) != tt.wantSignErr {
  556. t.Errorf("SignSSH error = %v, wantSignErr %v", err, tt.wantSignErr)
  557. } else {
  558. if tt.wantSignErr {
  559. assert.Nil(t, cert)
  560. } else {
  561. assert.NoError(t, validateSSHCertificate(cert, tt.expected))
  562. }
  563. }
  564. }
  565. })
  566. }
  567. }
  568. func TestOIDC_AuthorizeSSHRevoke(t *testing.T) {
  569. p1, err := generateOIDC()
  570. assert.FatalError(t, err)
  571. p2, err := generateOIDC()
  572. assert.FatalError(t, err)
  573. p2.Admins = []string{"root@example.com"}
  574. srv := generateJWKServer(2)
  575. defer srv.Close()
  576. var keys jose.JSONWebKeySet
  577. assert.FatalError(t, getAndDecode(srv.URL+"/private", &keys))
  578. config := Config{Claims: globalProvisionerClaims}
  579. p1.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  580. p2.ConfigurationEndpoint = srv.URL + "/.well-known/openid-configuration"
  581. assert.FatalError(t, p1.Init(config))
  582. assert.FatalError(t, p2.Init(config))
  583. // Invalid email
  584. failEmail, err := generateToken("subject", "the-issuer", p1.ClientID, "", []string{}, time.Now(), &keys.Keys[0])
  585. assert.FatalError(t, err)
  586. // Admin email not in domains
  587. noAdmin, err := generateToken("subject", "the-issuer", p1.ClientID, "root@example.com", []string{"test.smallstep.com"}, time.Now(), &keys.Keys[0])
  588. assert.FatalError(t, err)
  589. // Admin email in domains
  590. okAdmin, err := generateToken("subject", "the-issuer", p2.ClientID, "root@example.com", []string{"test.smallstep.com"}, time.Now(), &keys.Keys[0])
  591. assert.FatalError(t, err)
  592. type args struct {
  593. token string
  594. }
  595. tests := []struct {
  596. name string
  597. prov *OIDC
  598. args args
  599. code int
  600. wantErr bool
  601. }{
  602. {"ok", p2, args{okAdmin}, http.StatusOK, false},
  603. {"fail/invalid-token", p1, args{failEmail}, http.StatusUnauthorized, true},
  604. {"fail/not-admin", p1, args{noAdmin}, http.StatusUnauthorized, true},
  605. }
  606. for _, tt := range tests {
  607. t.Run(tt.name, func(t *testing.T) {
  608. err := tt.prov.AuthorizeSSHRevoke(context.Background(), tt.args.token)
  609. if (err != nil) != tt.wantErr {
  610. t.Errorf("OIDC.AuthorizeSSHRevoke() error = %v, wantErr %v", err, tt.wantErr)
  611. } else if err != nil {
  612. sc, ok := err.(errs.StatusCoder)
  613. assert.Fatal(t, ok, "error does not implement StatusCoder interface")
  614. assert.Equals(t, sc.StatusCode(), tt.code)
  615. }
  616. })
  617. }
  618. }
  619. func Test_sanitizeEmail(t *testing.T) {
  620. tests := []struct {
  621. name string
  622. email string
  623. want string
  624. }{
  625. {"equal", "name@smallstep.com", "name@smallstep.com"},
  626. {"domain-insensitive", "name@SMALLSTEP.COM", "name@smallstep.com"},
  627. {"local-sensitive", "NaMe@smallSTEP.CoM", "NaMe@smallstep.com"},
  628. {"multiple-@", "NaMe@NaMe@smallSTEP.CoM", "NaMe@NaMe@smallstep.com"},
  629. }
  630. for _, tt := range tests {
  631. t.Run(tt.name, func(t *testing.T) {
  632. if got := sanitizeEmail(tt.email); got != tt.want {
  633. t.Errorf("sanitizeEmail() = %v, want %v", got, tt.want)
  634. }
  635. })
  636. }
  637. }