controller_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. {"fail provisioner", fields{&JWK{}, nil}, args{ctx, "jane@doe.org"}, nil, true},
  166. {"fail custom", fields{&OIDC{}, func(ctx context.Context, p Interface, email string) (*Identity, error) {
  167. return nil, fmt.Errorf("an error")
  168. }}, args{ctx, "jane@doe.org"}, nil, true},
  169. }
  170. for _, tt := range tests {
  171. t.Run(tt.name, func(t *testing.T) {
  172. c := &Controller{
  173. Interface: tt.fields.Interface,
  174. IdentityFunc: tt.fields.IdentityFunc,
  175. }
  176. got, err := c.GetIdentity(tt.args.ctx, tt.args.email)
  177. if (err != nil) != tt.wantErr {
  178. t.Errorf("Controller.GetIdentity() error = %v, wantErr %v", err, tt.wantErr)
  179. return
  180. }
  181. if !reflect.DeepEqual(got, tt.want) {
  182. t.Errorf("Controller.GetIdentity() = %v, want %v", got, tt.want)
  183. }
  184. })
  185. }
  186. }
  187. func TestController_AuthorizeRenew(t *testing.T) {
  188. ctx := context.Background()
  189. now := time.Now().Truncate(time.Second)
  190. type fields struct {
  191. Interface Interface
  192. Claimer *Claimer
  193. AuthorizeRenewFunc AuthorizeRenewFunc
  194. }
  195. type args struct {
  196. ctx context.Context
  197. cert *x509.Certificate
  198. }
  199. tests := []struct {
  200. name string
  201. fields fields
  202. args args
  203. wantErr bool
  204. }{
  205. {"ok", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
  206. NotBefore: now,
  207. NotAfter: now.Add(time.Hour),
  208. }}, false},
  209. {"ok custom", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *x509.Certificate) error {
  210. return nil
  211. }}, args{ctx, &x509.Certificate{
  212. NotBefore: now,
  213. NotAfter: now.Add(time.Hour),
  214. }}, false},
  215. {"ok custom disabled", fields{&JWK{}, mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, 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 renew after expiry", fields{&JWK{}, mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
  222. NotBefore: now.Add(-time.Hour),
  223. NotAfter: now.Add(-time.Minute),
  224. }}, false},
  225. {"fail disabled", fields{&JWK{}, mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
  226. NotBefore: now,
  227. NotAfter: now.Add(time.Hour),
  228. }}, true},
  229. {"fail not yet valid", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
  230. NotBefore: now.Add(time.Hour),
  231. NotAfter: now.Add(2 * time.Hour),
  232. }}, true},
  233. {"fail expired", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
  234. NotBefore: now.Add(-time.Hour),
  235. NotAfter: now.Add(-time.Minute),
  236. }}, true},
  237. {"fail custom", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *x509.Certificate) error {
  238. return fmt.Errorf("an error")
  239. }}, args{ctx, &x509.Certificate{
  240. NotBefore: now,
  241. NotAfter: now.Add(time.Hour),
  242. }}, true},
  243. }
  244. for _, tt := range tests {
  245. t.Run(tt.name, func(t *testing.T) {
  246. c := &Controller{
  247. Interface: tt.fields.Interface,
  248. Claimer: tt.fields.Claimer,
  249. AuthorizeRenewFunc: tt.fields.AuthorizeRenewFunc,
  250. }
  251. if err := c.AuthorizeRenew(tt.args.ctx, tt.args.cert); (err != nil) != tt.wantErr {
  252. t.Errorf("Controller.AuthorizeRenew() error = %v, wantErr %v", err, tt.wantErr)
  253. }
  254. })
  255. }
  256. }
  257. func TestController_AuthorizeSSHRenew(t *testing.T) {
  258. ctx := context.Background()
  259. now := time.Now()
  260. type fields struct {
  261. Interface Interface
  262. Claimer *Claimer
  263. AuthorizeSSHRenewFunc AuthorizeSSHRenewFunc
  264. }
  265. type args struct {
  266. ctx context.Context
  267. cert *ssh.Certificate
  268. }
  269. tests := []struct {
  270. name string
  271. fields fields
  272. args args
  273. wantErr bool
  274. }{
  275. {"ok", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
  276. ValidAfter: uint64(now.Unix()),
  277. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  278. }}, false},
  279. {"ok custom", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *ssh.Certificate) error {
  280. return nil
  281. }}, args{ctx, &ssh.Certificate{
  282. ValidAfter: uint64(now.Unix()),
  283. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  284. }}, false},
  285. {"ok custom disabled", fields{&JWK{}, mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, 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 renew after expiry", fields{&JWK{}, mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
  292. ValidAfter: uint64(now.Add(-time.Hour).Unix()),
  293. ValidBefore: uint64(now.Add(-time.Minute).Unix()),
  294. }}, false},
  295. {"fail disabled", fields{&JWK{}, mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
  296. ValidAfter: uint64(now.Unix()),
  297. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  298. }}, true},
  299. {"fail not yet valid", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
  300. ValidAfter: uint64(now.Add(time.Hour).Unix()),
  301. ValidBefore: uint64(now.Add(2 * time.Hour).Unix()),
  302. }}, true},
  303. {"fail expired", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
  304. ValidAfter: uint64(now.Add(-time.Hour).Unix()),
  305. ValidBefore: uint64(now.Add(-time.Minute).Unix()),
  306. }}, true},
  307. {"fail custom", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *ssh.Certificate) error {
  308. return fmt.Errorf("an error")
  309. }}, args{ctx, &ssh.Certificate{
  310. ValidAfter: uint64(now.Unix()),
  311. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  312. }}, true},
  313. }
  314. for _, tt := range tests {
  315. t.Run(tt.name, func(t *testing.T) {
  316. c := &Controller{
  317. Interface: tt.fields.Interface,
  318. Claimer: tt.fields.Claimer,
  319. AuthorizeSSHRenewFunc: tt.fields.AuthorizeSSHRenewFunc,
  320. }
  321. if err := c.AuthorizeSSHRenew(tt.args.ctx, tt.args.cert); (err != nil) != tt.wantErr {
  322. t.Errorf("Controller.AuthorizeSSHRenew() error = %v, wantErr %v", err, tt.wantErr)
  323. }
  324. })
  325. }
  326. }
  327. func TestDefaultAuthorizeRenew(t *testing.T) {
  328. ctx := context.Background()
  329. now := time.Now().Truncate(time.Second)
  330. type args struct {
  331. ctx context.Context
  332. p *Controller
  333. cert *x509.Certificate
  334. }
  335. tests := []struct {
  336. name string
  337. args args
  338. wantErr bool
  339. }{
  340. {"ok", args{ctx, &Controller{
  341. Interface: &JWK{},
  342. Claimer: mustClaimer(t, nil, globalProvisionerClaims),
  343. }, &x509.Certificate{
  344. NotBefore: now,
  345. NotAfter: now.Add(time.Hour),
  346. }}, false},
  347. {"ok renew after expiry", args{ctx, &Controller{
  348. Interface: &JWK{},
  349. Claimer: mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims),
  350. }, &x509.Certificate{
  351. NotBefore: now.Add(-time.Hour),
  352. NotAfter: now.Add(-time.Minute),
  353. }}, false},
  354. {"fail disabled", args{ctx, &Controller{
  355. Interface: &JWK{},
  356. Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
  357. }, &x509.Certificate{
  358. NotBefore: now,
  359. NotAfter: now.Add(time.Hour),
  360. }}, true},
  361. {"fail not yet valid", args{ctx, &Controller{
  362. Interface: &JWK{},
  363. Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
  364. }, &x509.Certificate{
  365. NotBefore: now.Add(time.Hour),
  366. NotAfter: now.Add(2 * time.Hour),
  367. }}, true},
  368. {"fail expired", args{ctx, &Controller{
  369. Interface: &JWK{},
  370. Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
  371. }, &x509.Certificate{
  372. NotBefore: now.Add(-time.Hour),
  373. NotAfter: now.Add(-time.Minute),
  374. }}, true},
  375. }
  376. for _, tt := range tests {
  377. t.Run(tt.name, func(t *testing.T) {
  378. if err := DefaultAuthorizeRenew(tt.args.ctx, tt.args.p, tt.args.cert); (err != nil) != tt.wantErr {
  379. t.Errorf("DefaultAuthorizeRenew() error = %v, wantErr %v", err, tt.wantErr)
  380. }
  381. })
  382. }
  383. }
  384. func TestDefaultAuthorizeSSHRenew(t *testing.T) {
  385. ctx := context.Background()
  386. now := time.Now()
  387. type args struct {
  388. ctx context.Context
  389. p *Controller
  390. cert *ssh.Certificate
  391. }
  392. tests := []struct {
  393. name string
  394. args args
  395. wantErr bool
  396. }{
  397. {"ok", args{ctx, &Controller{
  398. Interface: &JWK{},
  399. Claimer: mustClaimer(t, nil, globalProvisionerClaims),
  400. }, &ssh.Certificate{
  401. ValidAfter: uint64(now.Unix()),
  402. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  403. }}, false},
  404. {"ok renew after expiry", args{ctx, &Controller{
  405. Interface: &JWK{},
  406. Claimer: mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims),
  407. }, &ssh.Certificate{
  408. ValidAfter: uint64(now.Add(-time.Hour).Unix()),
  409. ValidBefore: uint64(now.Add(-time.Minute).Unix()),
  410. }}, false},
  411. {"fail disabled", args{ctx, &Controller{
  412. Interface: &JWK{},
  413. Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
  414. }, &ssh.Certificate{
  415. ValidAfter: uint64(now.Unix()),
  416. ValidBefore: uint64(now.Add(time.Hour).Unix()),
  417. }}, true},
  418. {"fail not yet valid", args{ctx, &Controller{
  419. Interface: &JWK{},
  420. Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
  421. }, &ssh.Certificate{
  422. ValidAfter: uint64(now.Add(time.Hour).Unix()),
  423. ValidBefore: uint64(now.Add(2 * time.Hour).Unix()),
  424. }}, true},
  425. {"fail expired", args{ctx, &Controller{
  426. Interface: &JWK{},
  427. Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
  428. }, &ssh.Certificate{
  429. ValidAfter: uint64(now.Add(-time.Hour).Unix()),
  430. ValidBefore: uint64(now.Add(-time.Minute).Unix()),
  431. }}, true},
  432. }
  433. for _, tt := range tests {
  434. t.Run(tt.name, func(t *testing.T) {
  435. if err := DefaultAuthorizeSSHRenew(tt.args.ctx, tt.args.p, tt.args.cert); (err != nil) != tt.wantErr {
  436. t.Errorf("DefaultAuthorizeSSHRenew() error = %v, wantErr %v", err, tt.wantErr)
  437. }
  438. })
  439. }
  440. }
  441. func Test_newWebhookController(t *testing.T) {
  442. cert, err := pemutil.ReadCertificate("testdata/certs/x5c-leaf.crt", pemutil.WithFirstBlock())
  443. if err != nil {
  444. t.Fatal(err)
  445. }
  446. opts := []webhook.RequestBodyOption{webhook.WithX5CCertificate(cert)}
  447. type args struct {
  448. templateData WebhookSetter
  449. certType linkedca.Webhook_CertType
  450. opts []webhook.RequestBodyOption
  451. }
  452. tests := []struct {
  453. name string
  454. args args
  455. want *WebhookController
  456. }{
  457. {"ok", args{x509util.TemplateData{"foo": "bar"}, linkedca.Webhook_X509, nil}, &WebhookController{
  458. TemplateData: x509util.TemplateData{"foo": "bar"},
  459. certType: linkedca.Webhook_X509,
  460. client: http.DefaultClient,
  461. }},
  462. {"ok with options", args{x509util.TemplateData{"foo": "bar"}, linkedca.Webhook_SSH, opts}, &WebhookController{
  463. TemplateData: x509util.TemplateData{"foo": "bar"},
  464. certType: linkedca.Webhook_SSH,
  465. client: http.DefaultClient,
  466. options: opts,
  467. }},
  468. }
  469. for _, tt := range tests {
  470. c := &Controller{}
  471. got := c.newWebhookController(tt.args.templateData, tt.args.certType, tt.args.opts...)
  472. if !reflect.DeepEqual(got, tt.want) {
  473. t.Errorf("newWebhookController() = %v, want %v", got, tt.want)
  474. }
  475. }
  476. }