controller_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. package provisioner
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. "time"
  10. "github.com/smallstep/certificates/authority/policy"
  11. "github.com/smallstep/certificates/webhook"
  12. "github.com/stretchr/testify/assert"
  13. "go.step.sm/crypto/pemutil"
  14. "go.step.sm/crypto/x509util"
  15. "go.step.sm/linkedca"
  16. "golang.org/x/crypto/ssh"
  17. )
  18. var trueValue = true
  19. func mustClaimer(t *testing.T, claims *Claims, global Claims) *Claimer {
  20. t.Helper()
  21. c, err := NewClaimer(claims, global)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. return c
  26. }
  27. func mustDuration(t *testing.T, s string) *Duration {
  28. t.Helper()
  29. d, err := NewDuration(s)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. return d
  34. }
  35. func mustNewPolicyEngine(t *testing.T, options *Options) *policyEngine {
  36. t.Helper()
  37. c, err := newPolicyEngine(options)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. return c
  42. }
  43. func TestNewController(t *testing.T) {
  44. options := &Options{
  45. X509: &X509Options{
  46. AllowedNames: &policy.X509NameOptions{
  47. DNSDomains: []string{"*.local"},
  48. },
  49. },
  50. SSH: &SSHOptions{
  51. Host: &policy.SSHHostCertificateOptions{
  52. AllowedNames: &policy.SSHNameOptions{
  53. DNSDomains: []string{"*.local"},
  54. },
  55. },
  56. User: &policy.SSHUserCertificateOptions{
  57. AllowedNames: &policy.SSHNameOptions{
  58. EmailAddresses: []string{"@example.com"},
  59. },
  60. },
  61. },
  62. }
  63. type args struct {
  64. p Interface
  65. claims *Claims
  66. config Config
  67. options *Options
  68. }
  69. tests := []struct {
  70. name string
  71. args args
  72. want *Controller
  73. wantErr bool
  74. }{
  75. {"ok", args{&JWK{}, nil, Config{
  76. Claims: globalProvisionerClaims,
  77. Audiences: testAudiences,
  78. HTTPClient: &http.Client{},
  79. }, nil}, &Controller{
  80. Interface: &JWK{},
  81. Audiences: &testAudiences,
  82. Claimer: mustClaimer(t, nil, globalProvisionerClaims),
  83. httpClient: &http.Client{},
  84. }, false},
  85. {"ok with claims", args{&JWK{}, &Claims{
  86. DisableRenewal: &defaultDisableRenewal,
  87. }, Config{
  88. Claims: globalProvisionerClaims,
  89. Audiences: testAudiences,
  90. }, nil}, &Controller{
  91. Interface: &JWK{},
  92. Audiences: &testAudiences,
  93. Claimer: mustClaimer(t, &Claims{
  94. DisableRenewal: &defaultDisableRenewal,
  95. }, globalProvisionerClaims),
  96. }, false},
  97. {"ok with claims and options", args{&JWK{}, &Claims{
  98. DisableRenewal: &defaultDisableRenewal,
  99. }, Config{
  100. Claims: globalProvisionerClaims,
  101. Audiences: testAudiences,
  102. }, options}, &Controller{
  103. Interface: &JWK{},
  104. Audiences: &testAudiences,
  105. Claimer: mustClaimer(t, &Claims{
  106. DisableRenewal: &defaultDisableRenewal,
  107. }, globalProvisionerClaims),
  108. policy: mustNewPolicyEngine(t, options),
  109. }, false},
  110. {"fail claimer", args{&JWK{}, &Claims{
  111. MinTLSDur: mustDuration(t, "24h"),
  112. MaxTLSDur: mustDuration(t, "2h"),
  113. }, Config{
  114. Claims: globalProvisionerClaims,
  115. Audiences: testAudiences,
  116. }, nil}, nil, true},
  117. {"fail options", args{&JWK{}, &Claims{
  118. DisableRenewal: &defaultDisableRenewal,
  119. }, Config{
  120. Claims: globalProvisionerClaims,
  121. Audiences: testAudiences,
  122. }, &Options{
  123. X509: &X509Options{
  124. AllowedNames: &policy.X509NameOptions{
  125. DNSDomains: []string{"**.local"},
  126. },
  127. },
  128. }}, nil, true},
  129. }
  130. for _, tt := range tests {
  131. t.Run(tt.name, func(t *testing.T) {
  132. got, err := NewController(tt.args.p, tt.args.claims, tt.args.config, tt.args.options)
  133. if (err != nil) != tt.wantErr {
  134. t.Errorf("NewController() error = %v, wantErr %v", err, tt.wantErr)
  135. return
  136. }
  137. if !reflect.DeepEqual(got, tt.want) {
  138. t.Errorf("NewController() = %v, want %v", got, tt.want)
  139. }
  140. })
  141. }
  142. }
  143. func TestController_GetHTTPClient(t *testing.T) {
  144. srv := generateTLSJWKServer(2)
  145. defer srv.Close()
  146. type fields struct {
  147. httpClient *http.Client
  148. }
  149. tests := []struct {
  150. name string
  151. fields fields
  152. want *http.Client
  153. }{
  154. {"ok custom", fields{srv.Client()}, srv.Client()},
  155. {"ok default", fields{http.DefaultClient}, http.DefaultClient},
  156. }
  157. for _, tt := range tests {
  158. t.Run(tt.name, func(t *testing.T) {
  159. c := &Controller{
  160. httpClient: tt.fields.httpClient,
  161. }
  162. assert.Equal(t, tt.want, c.GetHTTPClient())
  163. })
  164. }
  165. }
  166. func TestController_GetIdentity(t *testing.T) {
  167. ctx := context.Background()
  168. type fields struct {
  169. Interface Interface
  170. IdentityFunc GetIdentityFunc
  171. }
  172. type args struct {
  173. ctx context.Context
  174. email string
  175. }
  176. tests := []struct {
  177. name string
  178. fields fields
  179. args args
  180. want *Identity
  181. wantErr bool
  182. }{
  183. {"ok", fields{&OIDC{}, nil}, args{ctx, "jane@doe.org"}, &Identity{
  184. Usernames: []string{"jane", "jane@doe.org"},
  185. }, false},
  186. {"ok custom", fields{&OIDC{}, func(ctx context.Context, p Interface, email string) (*Identity, error) {
  187. return &Identity{Usernames: []string{"jane"}}, nil
  188. }}, args{ctx, "jane@doe.org"}, &Identity{
  189. Usernames: []string{"jane"},
  190. }, false},
  191. {"ok badname", fields{&OIDC{}, nil}, args{ctx, "1000@doe.org"}, &Identity{
  192. Usernames: []string{"1000", "1000@doe.org"},
  193. }, false},
  194. {"ok sanitized badname", fields{&OIDC{}, nil}, args{ctx, "1000+10@doe.org"}, &Identity{
  195. Usernames: []string{"1000_10", "1000+10", "1000+10@doe.org"},
  196. }, false},
  197. {"fail provisioner", fields{&JWK{}, nil}, args{ctx, "jane@doe.org"}, nil, true},
  198. {"fail custom", fields{&OIDC{}, func(ctx context.Context, p Interface, email string) (*Identity, error) {
  199. return nil, fmt.Errorf("an error")
  200. }}, args{ctx, "jane@doe.org"}, nil, true},
  201. }
  202. for _, tt := range tests {
  203. t.Run(tt.name, func(t *testing.T) {
  204. c := &Controller{
  205. Interface: tt.fields.Interface,
  206. IdentityFunc: tt.fields.IdentityFunc,
  207. }
  208. got, err := c.GetIdentity(tt.args.ctx, tt.args.email)
  209. if (err != nil) != tt.wantErr {
  210. t.Errorf("Controller.GetIdentity() error = %v, wantErr %v", err, tt.wantErr)
  211. return
  212. }
  213. if !reflect.DeepEqual(got, tt.want) {
  214. t.Errorf("Controller.GetIdentity() = %v, want %v", got, tt.want)
  215. }
  216. })
  217. }
  218. }
  219. func TestController_AuthorizeRenew(t *testing.T) {
  220. ctx := context.Background()
  221. now := time.Now().Truncate(time.Second)
  222. type fields struct {
  223. Interface Interface
  224. Claimer *Claimer
  225. AuthorizeRenewFunc AuthorizeRenewFunc
  226. }
  227. type args struct {
  228. ctx context.Context
  229. cert *x509.Certificate
  230. }
  231. tests := []struct {
  232. name string
  233. fields fields
  234. args args
  235. wantErr bool
  236. }{
  237. {"ok", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
  238. NotBefore: now,
  239. NotAfter: now.Add(time.Hour),
  240. }}, false},
  241. {"ok custom", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *x509.Certificate) error {
  242. return nil
  243. }}, args{ctx, &x509.Certificate{
  244. NotBefore: now,
  245. NotAfter: now.Add(time.Hour),
  246. }}, false},
  247. {"ok custom disabled", fields{&JWK{}, mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *x509.Certificate) error {
  248. return nil
  249. }}, args{ctx, &x509.Certificate{
  250. NotBefore: now,
  251. NotAfter: now.Add(time.Hour),
  252. }}, false},
  253. {"ok renew after expiry", fields{&JWK{}, mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
  254. NotBefore: now.Add(-time.Hour),
  255. NotAfter: now.Add(-time.Minute),
  256. }}, false},
  257. {"fail disabled", fields{&JWK{}, mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
  258. NotBefore: now,
  259. NotAfter: now.Add(time.Hour),
  260. }}, true},
  261. {"fail not yet valid", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
  262. NotBefore: now.Add(time.Hour),
  263. NotAfter: now.Add(2 * time.Hour),
  264. }}, true},
  265. {"fail expired", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
  266. NotBefore: now.Add(-time.Hour),
  267. NotAfter: now.Add(-time.Minute),
  268. }}, true},
  269. {"fail custom", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *x509.Certificate) error {
  270. return fmt.Errorf("an error")
  271. }}, args{ctx, &x509.Certificate{
  272. NotBefore: now,
  273. NotAfter: now.Add(time.Hour),
  274. }}, true},
  275. }
  276. for _, tt := range tests {
  277. t.Run(tt.name, func(t *testing.T) {
  278. c := &Controller{
  279. Interface: tt.fields.Interface,
  280. Claimer: tt.fields.Claimer,
  281. AuthorizeRenewFunc: tt.fields.AuthorizeRenewFunc,
  282. }
  283. if err := c.AuthorizeRenew(tt.args.ctx, tt.args.cert); (err != nil) != tt.wantErr {
  284. t.Errorf("Controller.AuthorizeRenew() error = %v, wantErr %v", err, tt.wantErr)
  285. }
  286. })
  287. }
  288. }
  289. func TestController_AuthorizeSSHRenew(t *testing.T) {
  290. ctx := context.Background()
  291. now := time.Now()
  292. type fields struct {
  293. Interface Interface
  294. Claimer *Claimer
  295. AuthorizeSSHRenewFunc AuthorizeSSHRenewFunc
  296. }
  297. type args struct {
  298. ctx context.Context
  299. cert *ssh.Certificate
  300. }
  301. tests := []struct {
  302. name string
  303. fields fields
  304. args args
  305. wantErr bool
  306. }{
  307. {"ok", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
  308. ValidAfter: uint64(now.Unix()),
  309. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  310. }}, false},
  311. {"ok custom", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *ssh.Certificate) error {
  312. return nil
  313. }}, args{ctx, &ssh.Certificate{
  314. ValidAfter: uint64(now.Unix()),
  315. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  316. }}, false},
  317. {"ok custom disabled", fields{&JWK{}, mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *ssh.Certificate) error {
  318. return nil
  319. }}, args{ctx, &ssh.Certificate{
  320. ValidAfter: uint64(now.Unix()),
  321. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  322. }}, false},
  323. {"ok renew after expiry", fields{&JWK{}, mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
  324. ValidAfter: uint64(now.Add(-time.Hour).Unix()),
  325. ValidBefore: uint64(now.Add(-time.Minute).Unix()),
  326. }}, false},
  327. {"fail disabled", fields{&JWK{}, mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
  328. ValidAfter: uint64(now.Unix()),
  329. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  330. }}, true},
  331. {"fail not yet valid", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
  332. ValidAfter: uint64(now.Add(time.Hour).Unix()),
  333. ValidBefore: uint64(now.Add(2 * time.Hour).Unix()),
  334. }}, true},
  335. {"fail expired", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
  336. ValidAfter: uint64(now.Add(-time.Hour).Unix()),
  337. ValidBefore: uint64(now.Add(-time.Minute).Unix()),
  338. }}, true},
  339. {"fail custom", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *ssh.Certificate) error {
  340. return fmt.Errorf("an error")
  341. }}, args{ctx, &ssh.Certificate{
  342. ValidAfter: uint64(now.Unix()),
  343. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  344. }}, true},
  345. }
  346. for _, tt := range tests {
  347. t.Run(tt.name, func(t *testing.T) {
  348. c := &Controller{
  349. Interface: tt.fields.Interface,
  350. Claimer: tt.fields.Claimer,
  351. AuthorizeSSHRenewFunc: tt.fields.AuthorizeSSHRenewFunc,
  352. }
  353. if err := c.AuthorizeSSHRenew(tt.args.ctx, tt.args.cert); (err != nil) != tt.wantErr {
  354. t.Errorf("Controller.AuthorizeSSHRenew() error = %v, wantErr %v", err, tt.wantErr)
  355. }
  356. })
  357. }
  358. }
  359. func TestDefaultAuthorizeRenew(t *testing.T) {
  360. ctx := context.Background()
  361. now := time.Now().Truncate(time.Second)
  362. type args struct {
  363. ctx context.Context
  364. p *Controller
  365. cert *x509.Certificate
  366. }
  367. tests := []struct {
  368. name string
  369. args args
  370. wantErr bool
  371. }{
  372. {"ok", args{ctx, &Controller{
  373. Interface: &JWK{},
  374. Claimer: mustClaimer(t, nil, globalProvisionerClaims),
  375. }, &x509.Certificate{
  376. NotBefore: now,
  377. NotAfter: now.Add(time.Hour),
  378. }}, false},
  379. {"ok renew after expiry", args{ctx, &Controller{
  380. Interface: &JWK{},
  381. Claimer: mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims),
  382. }, &x509.Certificate{
  383. NotBefore: now.Add(-time.Hour),
  384. NotAfter: now.Add(-time.Minute),
  385. }}, false},
  386. {"fail disabled", args{ctx, &Controller{
  387. Interface: &JWK{},
  388. Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
  389. }, &x509.Certificate{
  390. NotBefore: now,
  391. NotAfter: now.Add(time.Hour),
  392. }}, true},
  393. {"fail not yet valid", args{ctx, &Controller{
  394. Interface: &JWK{},
  395. Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
  396. }, &x509.Certificate{
  397. NotBefore: now.Add(time.Hour),
  398. NotAfter: now.Add(2 * time.Hour),
  399. }}, true},
  400. {"fail expired", args{ctx, &Controller{
  401. Interface: &JWK{},
  402. Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
  403. }, &x509.Certificate{
  404. NotBefore: now.Add(-time.Hour),
  405. NotAfter: now.Add(-time.Minute),
  406. }}, true},
  407. }
  408. for _, tt := range tests {
  409. t.Run(tt.name, func(t *testing.T) {
  410. if err := DefaultAuthorizeRenew(tt.args.ctx, tt.args.p, tt.args.cert); (err != nil) != tt.wantErr {
  411. t.Errorf("DefaultAuthorizeRenew() error = %v, wantErr %v", err, tt.wantErr)
  412. }
  413. })
  414. }
  415. }
  416. func TestDefaultAuthorizeSSHRenew(t *testing.T) {
  417. ctx := context.Background()
  418. now := time.Now()
  419. type args struct {
  420. ctx context.Context
  421. p *Controller
  422. cert *ssh.Certificate
  423. }
  424. tests := []struct {
  425. name string
  426. args args
  427. wantErr bool
  428. }{
  429. {"ok", args{ctx, &Controller{
  430. Interface: &JWK{},
  431. Claimer: mustClaimer(t, nil, globalProvisionerClaims),
  432. }, &ssh.Certificate{
  433. ValidAfter: uint64(now.Unix()),
  434. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  435. }}, false},
  436. {"ok renew after expiry", args{ctx, &Controller{
  437. Interface: &JWK{},
  438. Claimer: mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims),
  439. }, &ssh.Certificate{
  440. ValidAfter: uint64(now.Add(-time.Hour).Unix()),
  441. ValidBefore: uint64(now.Add(-time.Minute).Unix()),
  442. }}, false},
  443. {"fail disabled", args{ctx, &Controller{
  444. Interface: &JWK{},
  445. Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
  446. }, &ssh.Certificate{
  447. ValidAfter: uint64(now.Unix()),
  448. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  449. }}, true},
  450. {"fail not yet valid", args{ctx, &Controller{
  451. Interface: &JWK{},
  452. Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
  453. }, &ssh.Certificate{
  454. ValidAfter: uint64(now.Add(time.Hour).Unix()),
  455. ValidBefore: uint64(now.Add(2 * time.Hour).Unix()),
  456. }}, true},
  457. {"fail expired", args{ctx, &Controller{
  458. Interface: &JWK{},
  459. Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
  460. }, &ssh.Certificate{
  461. ValidAfter: uint64(now.Add(-time.Hour).Unix()),
  462. ValidBefore: uint64(now.Add(-time.Minute).Unix()),
  463. }}, true},
  464. }
  465. for _, tt := range tests {
  466. t.Run(tt.name, func(t *testing.T) {
  467. if err := DefaultAuthorizeSSHRenew(tt.args.ctx, tt.args.p, tt.args.cert); (err != nil) != tt.wantErr {
  468. t.Errorf("DefaultAuthorizeSSHRenew() error = %v, wantErr %v", err, tt.wantErr)
  469. }
  470. })
  471. }
  472. }
  473. func Test_newWebhookController(t *testing.T) {
  474. cert, err := pemutil.ReadCertificate("testdata/certs/x5c-leaf.crt", pemutil.WithFirstBlock())
  475. if err != nil {
  476. t.Fatal(err)
  477. }
  478. opts := []webhook.RequestBodyOption{webhook.WithX5CCertificate(cert)}
  479. type args struct {
  480. templateData WebhookSetter
  481. certType linkedca.Webhook_CertType
  482. opts []webhook.RequestBodyOption
  483. }
  484. tests := []struct {
  485. name string
  486. args args
  487. want *WebhookController
  488. }{
  489. {"ok", args{x509util.TemplateData{"foo": "bar"}, linkedca.Webhook_X509, nil}, &WebhookController{
  490. TemplateData: x509util.TemplateData{"foo": "bar"},
  491. certType: linkedca.Webhook_X509,
  492. client: http.DefaultClient,
  493. }},
  494. {"ok with options", args{x509util.TemplateData{"foo": "bar"}, linkedca.Webhook_SSH, opts}, &WebhookController{
  495. TemplateData: x509util.TemplateData{"foo": "bar"},
  496. certType: linkedca.Webhook_SSH,
  497. client: http.DefaultClient,
  498. options: opts,
  499. }},
  500. }
  501. for _, tt := range tests {
  502. c := &Controller{}
  503. got := c.newWebhookController(tt.args.templateData, tt.args.certType, tt.args.opts...)
  504. if !reflect.DeepEqual(got, tt.want) {
  505. t.Errorf("newWebhookController() = %v, want %v", got, tt.want)
  506. }
  507. }
  508. }