controller_test.go 15 KB

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