controller_test.go 15 KB

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