controller_test.go 16 KB

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