2018-10-09 05:40:07 +00:00
|
|
|
package authority
|
|
|
|
|
|
|
|
import (
|
2022-04-08 20:06:29 +00:00
|
|
|
"context"
|
|
|
|
"crypto/x509"
|
2022-03-30 08:22:22 +00:00
|
|
|
"errors"
|
2018-10-09 05:40:07 +00:00
|
|
|
"net/http"
|
2022-04-08 20:06:29 +00:00
|
|
|
"reflect"
|
2018-10-09 05:40:07 +00:00
|
|
|
"testing"
|
2022-04-08 20:06:29 +00:00
|
|
|
"time"
|
2018-10-09 05:40:07 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/assert"
|
2022-03-30 08:22:22 +00:00
|
|
|
"github.com/smallstep/certificates/api/render"
|
2022-04-08 20:06:29 +00:00
|
|
|
"github.com/smallstep/certificates/authority/admin"
|
2019-03-08 03:30:17 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2022-04-08 20:06:29 +00:00
|
|
|
"github.com/smallstep/certificates/db"
|
|
|
|
"go.step.sm/crypto/jose"
|
|
|
|
"go.step.sm/crypto/keyutil"
|
2018-10-09 05:40:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetEncryptedKey(t *testing.T) {
|
|
|
|
type ek struct {
|
2020-01-24 06:04:34 +00:00
|
|
|
a *Authority
|
|
|
|
kid string
|
|
|
|
err error
|
|
|
|
code int
|
2018-10-09 05:40:07 +00:00
|
|
|
}
|
|
|
|
tests := map[string]func(t *testing.T) *ek{
|
|
|
|
"ok": func(t *testing.T) *ek {
|
|
|
|
c, err := LoadConfiguration("../ca/testdata/ca.json")
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
a, err := New(c)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return &ek{
|
|
|
|
a: a,
|
2019-03-08 03:30:17 +00:00
|
|
|
kid: c.AuthorityConfig.Provisioners[1].(*provisioner.JWK).Key.KeyID,
|
2018-10-09 05:40:07 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail-not-found": func(t *testing.T) *ek {
|
|
|
|
c, err := LoadConfiguration("../ca/testdata/ca.json")
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
a, err := New(c)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return &ek{
|
2020-01-24 06:04:34 +00:00
|
|
|
a: a,
|
|
|
|
kid: "foo",
|
|
|
|
err: errors.New("encrypted key with kid foo was not found"),
|
|
|
|
code: http.StatusNotFound,
|
2018-10-09 05:40:07 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, genTestCase := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := genTestCase(t)
|
|
|
|
|
|
|
|
ek, err := tc.a.GetEncryptedKey(tc.kid)
|
|
|
|
if err != nil {
|
|
|
|
if assert.NotNil(t, tc.err) {
|
2022-03-30 08:22:22 +00:00
|
|
|
sc, ok := err.(render.StatusCodedError)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCodedError interface")
|
2020-01-24 06:04:34 +00:00
|
|
|
assert.Equals(t, sc.StatusCode(), tc.code)
|
|
|
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
2018-10-09 05:40:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if assert.Nil(t, tc.err) {
|
2021-05-03 19:48:20 +00:00
|
|
|
val, ok := tc.a.provisioners.Load("mike:" + tc.kid)
|
2018-10-09 05:40:07 +00:00
|
|
|
assert.Fatal(t, ok)
|
2019-03-08 03:30:17 +00:00
|
|
|
p, ok := val.(*provisioner.JWK)
|
2018-10-09 05:40:07 +00:00
|
|
|
assert.Fatal(t, ok)
|
|
|
|
assert.Equals(t, p.EncryptedKey, ek)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 20:06:29 +00:00
|
|
|
type mockAdminDB struct {
|
|
|
|
admin.MockDB
|
|
|
|
MGetCertificateData func(string) (*db.CertificateData, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockAdminDB) GetCertificateData(sn string) (*db.CertificateData, error) {
|
|
|
|
return c.MGetCertificateData(sn)
|
|
|
|
}
|
|
|
|
|
2018-10-09 05:40:07 +00:00
|
|
|
func TestGetProvisioners(t *testing.T) {
|
|
|
|
type gp struct {
|
2020-01-24 06:07:29 +00:00
|
|
|
a *Authority
|
|
|
|
err error
|
|
|
|
code int
|
2018-10-09 05:40:07 +00:00
|
|
|
}
|
|
|
|
tests := map[string]func(t *testing.T) *gp{
|
|
|
|
"ok": func(t *testing.T) *gp {
|
|
|
|
c, err := LoadConfiguration("../ca/testdata/ca.json")
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
a, err := New(c)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return &gp{a: a}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, genTestCase := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := genTestCase(t)
|
|
|
|
|
2018-10-26 01:53:13 +00:00
|
|
|
ps, next, err := tc.a.GetProvisioners("", 0)
|
2018-10-09 05:40:07 +00:00
|
|
|
if err != nil {
|
|
|
|
if assert.NotNil(t, tc.err) {
|
2022-03-30 08:22:22 +00:00
|
|
|
sc, ok := err.(render.StatusCodedError)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCodedError interface")
|
2020-01-24 06:07:29 +00:00
|
|
|
assert.Equals(t, sc.StatusCode(), tc.code)
|
|
|
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
2018-10-09 05:40:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if assert.Nil(t, tc.err) {
|
2018-10-12 06:03:00 +00:00
|
|
|
assert.Equals(t, ps, tc.a.config.AuthorityConfig.Provisioners)
|
2018-10-26 01:53:13 +00:00
|
|
|
assert.Equals(t, "", next)
|
2018-10-09 05:40:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-08 20:06:29 +00:00
|
|
|
|
|
|
|
func TestAuthority_LoadProvisionerByCertificate(t *testing.T) {
|
|
|
|
_, priv, err := keyutil.GenerateDefaultKeyPair()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
csr := getCSR(t, priv)
|
|
|
|
|
|
|
|
sign := func(a *Authority, extraOpts ...provisioner.SignOption) *x509.Certificate {
|
|
|
|
key, err := jose.ReadKey("testdata/secrets/step_cli_key_priv.jwk", jose.WithPassword([]byte("pass")))
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
token, err := generateToken("smallstep test", "step-cli", testAudiences.Sign[0], []string{"test.smallstep.com"}, time.Now(), key)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.SignMethod)
|
|
|
|
opts, err := a.Authorize(ctx, token)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
opts = append(opts, extraOpts...)
|
|
|
|
certs, err := a.Sign(csr, provisioner.SignOptions{}, opts...)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return certs[0]
|
|
|
|
}
|
|
|
|
getProvisioner := func(a *Authority, name string) provisioner.Interface {
|
|
|
|
p, ok := a.provisioners.LoadByName(name)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("provisioner %s does not exists", name)
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
removeExtension := provisioner.CertificateEnforcerFunc(func(cert *x509.Certificate) error {
|
|
|
|
for i, ext := range cert.ExtraExtensions {
|
|
|
|
if ext.Id.Equal(provisioner.StepOIDProvisioner) {
|
|
|
|
cert.ExtraExtensions = append(cert.ExtraExtensions[:i], cert.ExtraExtensions[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
a0 := testAuthority(t)
|
|
|
|
|
|
|
|
a1 := testAuthority(t)
|
|
|
|
a1.db = &db.MockAuthDB{
|
|
|
|
MUseToken: func(id, tok string) (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
},
|
|
|
|
MGetCertificateData: func(serialNumber string) (*db.CertificateData, error) {
|
|
|
|
p, err := a1.LoadProvisionerByName("dev")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return &db.CertificateData{
|
|
|
|
Provisioner: &db.ProvisionerData{
|
|
|
|
ID: p.GetID(),
|
|
|
|
Name: p.GetName(),
|
|
|
|
Type: p.GetType().String(),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
a2 := testAuthority(t)
|
|
|
|
a2.adminDB = &mockAdminDB{
|
|
|
|
MGetCertificateData: (func(s string) (*db.CertificateData, error) {
|
|
|
|
p, err := a2.LoadProvisionerByName("dev")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return &db.CertificateData{
|
|
|
|
Provisioner: &db.ProvisionerData{
|
|
|
|
ID: p.GetID(),
|
|
|
|
Name: p.GetName(),
|
|
|
|
Type: p.GetType().String(),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
a3 := testAuthority(t)
|
|
|
|
a3.db = &db.MockAuthDB{
|
|
|
|
MUseToken: func(id, tok string) (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
},
|
|
|
|
MGetCertificateData: func(serialNumber string) (*db.CertificateData, error) {
|
|
|
|
return &db.CertificateData{
|
|
|
|
Provisioner: &db.ProvisionerData{
|
|
|
|
ID: "foo", Name: "foo", Type: "foo",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
a4 := testAuthority(t)
|
|
|
|
a4.adminDB = &mockAdminDB{
|
|
|
|
MGetCertificateData: func(serialNumber string) (*db.CertificateData, error) {
|
|
|
|
return &db.CertificateData{
|
|
|
|
Provisioner: &db.ProvisionerData{
|
|
|
|
ID: "foo", Name: "foo", Type: "foo",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
crt *x509.Certificate
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
authority *Authority
|
|
|
|
args args
|
|
|
|
want provisioner.Interface
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{"ok from certificate", a0, args{sign(a0)}, getProvisioner(a0, "step-cli"), false},
|
|
|
|
{"ok from db", a1, args{sign(a1)}, getProvisioner(a1, "dev"), false},
|
|
|
|
{"ok from admindb", a2, args{sign(a2)}, getProvisioner(a2, "dev"), false},
|
|
|
|
{"fail from certificate", a0, args{sign(a0, removeExtension)}, nil, true},
|
|
|
|
{"fail from db", a3, args{sign(a3, removeExtension)}, nil, true},
|
|
|
|
{"fail from admindb", a4, args{sign(a4, removeExtension)}, nil, true},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := tt.authority.LoadProvisionerByCertificate(tt.args.crt)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Authority.LoadProvisionerByCertificate() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("Authority.LoadProvisionerByCertificate() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|