2018-10-09 05:40:07 +00:00
|
|
|
package authority
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/assert"
|
2019-03-08 03:30:17 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2020-01-24 06:04:34 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
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) {
|
2020-01-24 06:04:34 +00:00
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
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) {
|
2019-03-08 03:30:17 +00:00
|
|
|
val, ok := tc.a.provisioners.Load("max:" + 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetProvisioners(t *testing.T) {
|
|
|
|
type gp struct {
|
|
|
|
a *Authority
|
|
|
|
err *apiError
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
switch v := err.(type) {
|
|
|
|
case *apiError:
|
|
|
|
assert.HasPrefix(t, v.err.Error(), tc.err.Error())
|
|
|
|
assert.Equals(t, v.code, tc.err.code)
|
|
|
|
assert.Equals(t, v.context, tc.err.context)
|
|
|
|
default:
|
|
|
|
t.Errorf("unexpected error type: %T", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|