controller_test.go 14 KB

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