| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- package provisioner
- import (
- "context"
- "crypto/x509"
- "fmt"
- "reflect"
- "testing"
- "time"
- "go.step.sm/crypto/x509util"
- "go.step.sm/linkedca"
- "golang.org/x/crypto/ssh"
- "github.com/smallstep/certificates/authority/policy"
- )
- var trueValue = true
- func mustClaimer(t *testing.T, claims *Claims, global Claims) *Claimer {
- t.Helper()
- c, err := NewClaimer(claims, global)
- if err != nil {
- t.Fatal(err)
- }
- return c
- }
- func mustDuration(t *testing.T, s string) *Duration {
- t.Helper()
- d, err := NewDuration(s)
- if err != nil {
- t.Fatal(err)
- }
- return d
- }
- func mustNewPolicyEngine(t *testing.T, options *Options) *policyEngine {
- t.Helper()
- c, err := newPolicyEngine(options)
- if err != nil {
- t.Fatal(err)
- }
- return c
- }
- func TestNewController(t *testing.T) {
- options := &Options{
- X509: &X509Options{
- AllowedNames: &policy.X509NameOptions{
- DNSDomains: []string{"*.local"},
- },
- },
- SSH: &SSHOptions{
- Host: &policy.SSHHostCertificateOptions{
- AllowedNames: &policy.SSHNameOptions{
- DNSDomains: []string{"*.local"},
- },
- },
- User: &policy.SSHUserCertificateOptions{
- AllowedNames: &policy.SSHNameOptions{
- EmailAddresses: []string{"@example.com"},
- },
- },
- },
- }
- type args struct {
- p Interface
- claims *Claims
- config Config
- options *Options
- }
- tests := []struct {
- name string
- args args
- want *Controller
- wantErr bool
- }{
- {"ok", args{&JWK{}, nil, Config{
- Claims: globalProvisionerClaims,
- Audiences: testAudiences,
- }, nil}, &Controller{
- Interface: &JWK{},
- Audiences: &testAudiences,
- Claimer: mustClaimer(t, nil, globalProvisionerClaims),
- }, false},
- {"ok with claims", args{&JWK{}, &Claims{
- DisableRenewal: &defaultDisableRenewal,
- }, Config{
- Claims: globalProvisionerClaims,
- Audiences: testAudiences,
- }, nil}, &Controller{
- Interface: &JWK{},
- Audiences: &testAudiences,
- Claimer: mustClaimer(t, &Claims{
- DisableRenewal: &defaultDisableRenewal,
- }, globalProvisionerClaims),
- }, false},
- {"ok with claims and options", args{&JWK{}, &Claims{
- DisableRenewal: &defaultDisableRenewal,
- }, Config{
- Claims: globalProvisionerClaims,
- Audiences: testAudiences,
- }, options}, &Controller{
- Interface: &JWK{},
- Audiences: &testAudiences,
- Claimer: mustClaimer(t, &Claims{
- DisableRenewal: &defaultDisableRenewal,
- }, globalProvisionerClaims),
- policy: mustNewPolicyEngine(t, options),
- }, false},
- {"fail claimer", args{&JWK{}, &Claims{
- MinTLSDur: mustDuration(t, "24h"),
- MaxTLSDur: mustDuration(t, "2h"),
- }, Config{
- Claims: globalProvisionerClaims,
- Audiences: testAudiences,
- }, nil}, nil, true},
- {"fail options", args{&JWK{}, &Claims{
- DisableRenewal: &defaultDisableRenewal,
- }, Config{
- Claims: globalProvisionerClaims,
- Audiences: testAudiences,
- }, &Options{
- X509: &X509Options{
- AllowedNames: &policy.X509NameOptions{
- DNSDomains: []string{"**.local"},
- },
- },
- }}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := NewController(tt.args.p, tt.args.claims, tt.args.config, tt.args.options)
- if (err != nil) != tt.wantErr {
- t.Errorf("NewController() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("NewController() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestController_GetIdentity(t *testing.T) {
- ctx := context.Background()
- type fields struct {
- Interface Interface
- IdentityFunc GetIdentityFunc
- }
- type args struct {
- ctx context.Context
- email string
- }
- tests := []struct {
- name string
- fields fields
- args args
- want *Identity
- wantErr bool
- }{
- {"ok", fields{&OIDC{}, nil}, args{ctx, "jane@doe.org"}, &Identity{
- Usernames: []string{"jane", "jane@doe.org"},
- }, false},
- {"ok custom", fields{&OIDC{}, func(ctx context.Context, p Interface, email string) (*Identity, error) {
- return &Identity{Usernames: []string{"jane"}}, nil
- }}, args{ctx, "jane@doe.org"}, &Identity{
- Usernames: []string{"jane"},
- }, false},
- {"fail provisioner", fields{&JWK{}, nil}, args{ctx, "jane@doe.org"}, nil, true},
- {"fail custom", fields{&OIDC{}, func(ctx context.Context, p Interface, email string) (*Identity, error) {
- return nil, fmt.Errorf("an error")
- }}, args{ctx, "jane@doe.org"}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &Controller{
- Interface: tt.fields.Interface,
- IdentityFunc: tt.fields.IdentityFunc,
- }
- got, err := c.GetIdentity(tt.args.ctx, tt.args.email)
- if (err != nil) != tt.wantErr {
- t.Errorf("Controller.GetIdentity() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("Controller.GetIdentity() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestController_AuthorizeRenew(t *testing.T) {
- ctx := context.Background()
- now := time.Now().Truncate(time.Second)
- type fields struct {
- Interface Interface
- Claimer *Claimer
- AuthorizeRenewFunc AuthorizeRenewFunc
- }
- type args struct {
- ctx context.Context
- cert *x509.Certificate
- }
- tests := []struct {
- name string
- fields fields
- args args
- wantErr bool
- }{
- {"ok", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
- NotBefore: now,
- NotAfter: now.Add(time.Hour),
- }}, false},
- {"ok custom", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *x509.Certificate) error {
- return nil
- }}, args{ctx, &x509.Certificate{
- NotBefore: now,
- NotAfter: now.Add(time.Hour),
- }}, false},
- {"ok custom disabled", fields{&JWK{}, mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *x509.Certificate) error {
- return nil
- }}, args{ctx, &x509.Certificate{
- NotBefore: now,
- NotAfter: now.Add(time.Hour),
- }}, false},
- {"ok renew after expiry", fields{&JWK{}, mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
- NotBefore: now.Add(-time.Hour),
- NotAfter: now.Add(-time.Minute),
- }}, false},
- {"fail disabled", fields{&JWK{}, mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
- NotBefore: now,
- NotAfter: now.Add(time.Hour),
- }}, true},
- {"fail not yet valid", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
- NotBefore: now.Add(time.Hour),
- NotAfter: now.Add(2 * time.Hour),
- }}, true},
- {"fail expired", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &x509.Certificate{
- NotBefore: now.Add(-time.Hour),
- NotAfter: now.Add(-time.Minute),
- }}, true},
- {"fail custom", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *x509.Certificate) error {
- return fmt.Errorf("an error")
- }}, args{ctx, &x509.Certificate{
- NotBefore: now,
- NotAfter: now.Add(time.Hour),
- }}, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &Controller{
- Interface: tt.fields.Interface,
- Claimer: tt.fields.Claimer,
- AuthorizeRenewFunc: tt.fields.AuthorizeRenewFunc,
- }
- if err := c.AuthorizeRenew(tt.args.ctx, tt.args.cert); (err != nil) != tt.wantErr {
- t.Errorf("Controller.AuthorizeRenew() error = %v, wantErr %v", err, tt.wantErr)
- }
- })
- }
- }
- func TestController_AuthorizeSSHRenew(t *testing.T) {
- ctx := context.Background()
- now := time.Now()
- type fields struct {
- Interface Interface
- Claimer *Claimer
- AuthorizeSSHRenewFunc AuthorizeSSHRenewFunc
- }
- type args struct {
- ctx context.Context
- cert *ssh.Certificate
- }
- tests := []struct {
- name string
- fields fields
- args args
- wantErr bool
- }{
- {"ok", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
- ValidAfter: uint64(now.Unix()),
- ValidBefore: uint64(now.Add(time.Hour).Unix()),
- }}, false},
- {"ok custom", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *ssh.Certificate) error {
- return nil
- }}, args{ctx, &ssh.Certificate{
- ValidAfter: uint64(now.Unix()),
- ValidBefore: uint64(now.Add(time.Hour).Unix()),
- }}, false},
- {"ok custom disabled", fields{&JWK{}, mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *ssh.Certificate) error {
- return nil
- }}, args{ctx, &ssh.Certificate{
- ValidAfter: uint64(now.Unix()),
- ValidBefore: uint64(now.Add(time.Hour).Unix()),
- }}, false},
- {"ok renew after expiry", fields{&JWK{}, mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
- ValidAfter: uint64(now.Add(-time.Hour).Unix()),
- ValidBefore: uint64(now.Add(-time.Minute).Unix()),
- }}, false},
- {"fail disabled", fields{&JWK{}, mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
- ValidAfter: uint64(now.Unix()),
- ValidBefore: uint64(now.Add(time.Hour).Unix()),
- }}, true},
- {"fail not yet valid", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
- ValidAfter: uint64(now.Add(time.Hour).Unix()),
- ValidBefore: uint64(now.Add(2 * time.Hour).Unix()),
- }}, true},
- {"fail expired", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), nil}, args{ctx, &ssh.Certificate{
- ValidAfter: uint64(now.Add(-time.Hour).Unix()),
- ValidBefore: uint64(now.Add(-time.Minute).Unix()),
- }}, true},
- {"fail custom", fields{&JWK{}, mustClaimer(t, nil, globalProvisionerClaims), func(ctx context.Context, p *Controller, cert *ssh.Certificate) error {
- return fmt.Errorf("an error")
- }}, args{ctx, &ssh.Certificate{
- ValidAfter: uint64(now.Unix()),
- ValidBefore: uint64(now.Add(time.Hour).Unix()),
- }}, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &Controller{
- Interface: tt.fields.Interface,
- Claimer: tt.fields.Claimer,
- AuthorizeSSHRenewFunc: tt.fields.AuthorizeSSHRenewFunc,
- }
- if err := c.AuthorizeSSHRenew(tt.args.ctx, tt.args.cert); (err != nil) != tt.wantErr {
- t.Errorf("Controller.AuthorizeSSHRenew() error = %v, wantErr %v", err, tt.wantErr)
- }
- })
- }
- }
- func TestDefaultAuthorizeRenew(t *testing.T) {
- ctx := context.Background()
- now := time.Now().Truncate(time.Second)
- type args struct {
- ctx context.Context
- p *Controller
- cert *x509.Certificate
- }
- tests := []struct {
- name string
- args args
- wantErr bool
- }{
- {"ok", args{ctx, &Controller{
- Interface: &JWK{},
- Claimer: mustClaimer(t, nil, globalProvisionerClaims),
- }, &x509.Certificate{
- NotBefore: now,
- NotAfter: now.Add(time.Hour),
- }}, false},
- {"ok renew after expiry", args{ctx, &Controller{
- Interface: &JWK{},
- Claimer: mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims),
- }, &x509.Certificate{
- NotBefore: now.Add(-time.Hour),
- NotAfter: now.Add(-time.Minute),
- }}, false},
- {"fail disabled", args{ctx, &Controller{
- Interface: &JWK{},
- Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
- }, &x509.Certificate{
- NotBefore: now,
- NotAfter: now.Add(time.Hour),
- }}, true},
- {"fail not yet valid", args{ctx, &Controller{
- Interface: &JWK{},
- Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
- }, &x509.Certificate{
- NotBefore: now.Add(time.Hour),
- NotAfter: now.Add(2 * time.Hour),
- }}, true},
- {"fail expired", args{ctx, &Controller{
- Interface: &JWK{},
- Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
- }, &x509.Certificate{
- NotBefore: now.Add(-time.Hour),
- NotAfter: now.Add(-time.Minute),
- }}, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if err := DefaultAuthorizeRenew(tt.args.ctx, tt.args.p, tt.args.cert); (err != nil) != tt.wantErr {
- t.Errorf("DefaultAuthorizeRenew() error = %v, wantErr %v", err, tt.wantErr)
- }
- })
- }
- }
- func TestDefaultAuthorizeSSHRenew(t *testing.T) {
- ctx := context.Background()
- now := time.Now()
- type args struct {
- ctx context.Context
- p *Controller
- cert *ssh.Certificate
- }
- tests := []struct {
- name string
- args args
- wantErr bool
- }{
- {"ok", args{ctx, &Controller{
- Interface: &JWK{},
- Claimer: mustClaimer(t, nil, globalProvisionerClaims),
- }, &ssh.Certificate{
- ValidAfter: uint64(now.Unix()),
- ValidBefore: uint64(now.Add(time.Hour).Unix()),
- }}, false},
- {"ok renew after expiry", args{ctx, &Controller{
- Interface: &JWK{},
- Claimer: mustClaimer(t, &Claims{AllowRenewalAfterExpiry: &trueValue}, globalProvisionerClaims),
- }, &ssh.Certificate{
- ValidAfter: uint64(now.Add(-time.Hour).Unix()),
- ValidBefore: uint64(now.Add(-time.Minute).Unix()),
- }}, false},
- {"fail disabled", args{ctx, &Controller{
- Interface: &JWK{},
- Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
- }, &ssh.Certificate{
- ValidAfter: uint64(now.Unix()),
- ValidBefore: uint64(now.Add(time.Hour).Unix()),
- }}, true},
- {"fail not yet valid", args{ctx, &Controller{
- Interface: &JWK{},
- Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
- }, &ssh.Certificate{
- ValidAfter: uint64(now.Add(time.Hour).Unix()),
- ValidBefore: uint64(now.Add(2 * time.Hour).Unix()),
- }}, true},
- {"fail expired", args{ctx, &Controller{
- Interface: &JWK{},
- Claimer: mustClaimer(t, &Claims{DisableRenewal: &trueValue}, globalProvisionerClaims),
- }, &ssh.Certificate{
- ValidAfter: uint64(now.Add(-time.Hour).Unix()),
- ValidBefore: uint64(now.Add(-time.Minute).Unix()),
- }}, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if err := DefaultAuthorizeSSHRenew(tt.args.ctx, tt.args.p, tt.args.cert); (err != nil) != tt.wantErr {
- t.Errorf("DefaultAuthorizeSSHRenew() error = %v, wantErr %v", err, tt.wantErr)
- }
- })
- }
- }
- func Test_newWebhookController(t *testing.T) {
- c := &Controller{}
- data := x509util.TemplateData{"foo": "bar"}
- ctl := c.newWebhookController(data, linkedca.Webhook_X509)
- if !reflect.DeepEqual(ctl.TemplateData, data) {
- t.Error("Failed to set templateData")
- }
- if ctl.certType != linkedca.Webhook_X509 {
- t.Error("Failed to set certType")
- }
- if ctl.client == nil {
- t.Error("Failed to set client")
- }
- }
|