|
@@ -1,8 +1,11 @@
|
|
|
package authority
|
|
package authority
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "crypto/sha256"
|
|
|
|
|
+ "encoding/hex"
|
|
|
"testing"
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
+ "github.com/pkg/errors"
|
|
|
"github.com/smallstep/assert"
|
|
"github.com/smallstep/assert"
|
|
|
stepJOSE "github.com/smallstep/cli/jose"
|
|
stepJOSE "github.com/smallstep/cli/jose"
|
|
|
)
|
|
)
|
|
@@ -39,3 +42,82 @@ func testAuthority(t *testing.T) *Authority {
|
|
|
assert.FatalError(t, err)
|
|
assert.FatalError(t, err)
|
|
|
return a
|
|
return a
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func TestAuthorityNew(t *testing.T) {
|
|
|
|
|
+ type newTest struct {
|
|
|
|
|
+ config *Config
|
|
|
|
|
+ err error
|
|
|
|
|
+ }
|
|
|
|
|
+ tests := map[string]func(t *testing.T) *newTest{
|
|
|
|
|
+ "ok": func(t *testing.T) *newTest {
|
|
|
|
|
+ c, err := LoadConfiguration("../ca/testdata/ca.json")
|
|
|
|
|
+ assert.FatalError(t, err)
|
|
|
|
|
+ return &newTest{
|
|
|
|
|
+ config: c,
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ "fail-bad-root": func(t *testing.T) *newTest {
|
|
|
|
|
+ c, err := LoadConfiguration("../ca/testdata/ca.json")
|
|
|
|
|
+ assert.FatalError(t, err)
|
|
|
|
|
+ c.Root = "foo"
|
|
|
|
|
+ return &newTest{
|
|
|
|
|
+ config: c,
|
|
|
|
|
+ err: errors.New("open foo failed: no such file or directory"),
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ "fail-bad-password": func(t *testing.T) *newTest {
|
|
|
|
|
+ c, err := LoadConfiguration("../ca/testdata/ca.json")
|
|
|
|
|
+ assert.FatalError(t, err)
|
|
|
|
|
+ c.Password = "wrong"
|
|
|
|
|
+ return &newTest{
|
|
|
|
|
+ config: c,
|
|
|
|
|
+ err: errors.New("error decrypting ../ca/testdata/secrets/intermediate_ca_key: x509: decryption password incorrect"),
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ "fail-loading-ca-cert": func(t *testing.T) *newTest {
|
|
|
|
|
+ c, err := LoadConfiguration("../ca/testdata/ca.json")
|
|
|
|
|
+ assert.FatalError(t, err)
|
|
|
|
|
+ c.IntermediateCert = "wrong"
|
|
|
|
|
+ return &newTest{
|
|
|
|
|
+ config: c,
|
|
|
|
|
+ err: errors.New("open wrong failed: no such file or directory"),
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for name, genTestCase := range tests {
|
|
|
|
|
+ t.Run(name, func(t *testing.T) {
|
|
|
|
|
+ tc := genTestCase(t)
|
|
|
|
|
+
|
|
|
|
|
+ auth, err := New(tc.config)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ if assert.NotNil(t, tc.err) {
|
|
|
|
|
+ assert.HasPrefix(t, err.Error(), tc.err.Error())
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if assert.Nil(t, tc.err) {
|
|
|
|
|
+ sum := sha256.Sum256(auth.rootX509Crt.Raw)
|
|
|
|
|
+ root, ok := auth.certificates.Load(hex.EncodeToString(sum[:]))
|
|
|
|
|
+ assert.Fatal(t, ok)
|
|
|
|
|
+ assert.Equals(t, auth.rootX509Crt, root)
|
|
|
|
|
+
|
|
|
|
|
+ assert.True(t, auth.initOnce)
|
|
|
|
|
+ assert.NotNil(t, auth.intermediateIdentity)
|
|
|
|
|
+ for _, p := range tc.config.AuthorityConfig.Provisioners {
|
|
|
|
|
+ _p, ok := auth.provisionerIDIndex.Load(p.Key.KeyID)
|
|
|
|
|
+ assert.True(t, ok)
|
|
|
|
|
+ assert.Equals(t, p, _p)
|
|
|
|
|
+ if len(p.EncryptedKey) > 0 {
|
|
|
|
|
+ key, ok := auth.encryptedKeyIndex.Load(p.Key.KeyID)
|
|
|
|
|
+ assert.True(t, ok)
|
|
|
|
|
+ assert.Equals(t, p.EncryptedKey, key)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // sanity check
|
|
|
|
|
+ _, ok = auth.provisionerIDIndex.Load("fooo")
|
|
|
|
|
+ assert.False(t, ok)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+}
|