2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-24 13:00:17 -04:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-07-27 16:02:33 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
|
2022-03-24 13:00:17 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/keyvault/azkeys"
|
2023-01-11 04:08:57 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/keyservice/internal/storage"
|
|
|
|
"github.com/edgelesssys/constellation/v2/keyservice/kms"
|
2022-03-24 13:00:17 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stubHSMClient struct {
|
|
|
|
keyCreated bool
|
|
|
|
createOCTKeyErr error
|
|
|
|
importKeyErr error
|
|
|
|
getKeyErr error
|
2022-07-27 16:02:33 -04:00
|
|
|
keyID string
|
|
|
|
unwrapKeyErr error
|
|
|
|
unwrapKeyResult []byte
|
|
|
|
wrapKeyErr error
|
2022-03-24 13:00:17 -04:00
|
|
|
}
|
|
|
|
|
2022-07-27 16:02:33 -04:00
|
|
|
func (s *stubHSMClient) CreateKey(ctx context.Context, name string, parameters azkeys.CreateKeyParameters, options *azkeys.CreateKeyOptions) (azkeys.CreateKeyResponse, error) {
|
2022-03-24 13:00:17 -04:00
|
|
|
s.keyCreated = true
|
2022-07-27 16:02:33 -04:00
|
|
|
return azkeys.CreateKeyResponse{}, s.createOCTKeyErr
|
2022-03-24 13:00:17 -04:00
|
|
|
}
|
|
|
|
|
2022-07-27 16:02:33 -04:00
|
|
|
func (s *stubHSMClient) ImportKey(ctx context.Context, name string, parameters azkeys.ImportKeyParameters, options *azkeys.ImportKeyOptions) (azkeys.ImportKeyResponse, error) {
|
2022-03-24 13:00:17 -04:00
|
|
|
s.keyCreated = true
|
|
|
|
return azkeys.ImportKeyResponse{}, s.importKeyErr
|
|
|
|
}
|
|
|
|
|
2022-07-27 16:02:33 -04:00
|
|
|
func (s *stubHSMClient) GetKey(ctx context.Context, name string, version string, options *azkeys.GetKeyOptions) (azkeys.GetKeyResponse, error) {
|
2022-03-24 13:00:17 -04:00
|
|
|
return azkeys.GetKeyResponse{
|
|
|
|
KeyBundle: azkeys.KeyBundle{
|
|
|
|
Key: &azkeys.JSONWebKey{
|
2022-07-27 16:02:33 -04:00
|
|
|
KID: to.Ptr(azkeys.ID(s.keyID)),
|
2022-03-24 13:00:17 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, s.getKeyErr
|
|
|
|
}
|
|
|
|
|
2022-07-27 16:02:33 -04:00
|
|
|
func (s *stubHSMClient) UnwrapKey(ctx context.Context, name string, version string, parameters azkeys.KeyOperationsParameters, options *azkeys.UnwrapKeyOptions) (azkeys.UnwrapKeyResponse, error) {
|
|
|
|
return azkeys.UnwrapKeyResponse{
|
|
|
|
KeyOperationResult: azkeys.KeyOperationResult{
|
2022-03-24 13:00:17 -04:00
|
|
|
Result: s.unwrapKeyResult,
|
|
|
|
},
|
|
|
|
}, s.unwrapKeyErr
|
|
|
|
}
|
|
|
|
|
2022-07-27 16:02:33 -04:00
|
|
|
func (s *stubHSMClient) WrapKey(ctx context.Context, name string, version string, parameters azkeys.KeyOperationsParameters, options *azkeys.WrapKeyOptions) (azkeys.WrapKeyResponse, error) {
|
|
|
|
return azkeys.WrapKeyResponse{}, s.wrapKeyErr
|
2022-03-24 13:00:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type stubStorage struct {
|
|
|
|
key []byte
|
|
|
|
getErr error
|
|
|
|
putErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubStorage) Get(context.Context, string) ([]byte, error) {
|
|
|
|
return s.key, s.getErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubStorage) Put(context.Context, string, []byte) error {
|
|
|
|
return s.putErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHSMCreateKEK(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
importKey := []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client *stubHSMClient
|
|
|
|
importKey []byte
|
|
|
|
wantErr bool
|
2022-03-24 13:00:17 -04:00
|
|
|
}{
|
|
|
|
"create new kek successful": {
|
|
|
|
client: &stubHSMClient{},
|
|
|
|
},
|
|
|
|
"CreateOCTKey fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: &stubHSMClient{createOCTKeyErr: someErr},
|
|
|
|
wantErr: true,
|
2022-03-24 13:00:17 -04:00
|
|
|
},
|
|
|
|
"import key successful": {
|
|
|
|
client: &stubHSMClient{},
|
|
|
|
importKey: importKey,
|
|
|
|
},
|
|
|
|
"ImportKey fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: &stubHSMClient{importKeyErr: someErr},
|
|
|
|
importKey: importKey,
|
|
|
|
wantErr: true,
|
2022-03-24 13:00:17 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
client := HSMClient{
|
|
|
|
client: tc.client,
|
|
|
|
storage: storage.NewMemMapStorage(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.CreateKEK(context.Background(), "test-key", tc.importKey)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-24 13:00:17 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.True(tc.client.keyCreated)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHSMGetNewDEK(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
2022-07-27 16:02:33 -04:00
|
|
|
keyID := "https://test.managedhsm.azure.net/keys/test-key/test-key-version"
|
2022-03-24 13:00:17 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-07-27 16:02:33 -04:00
|
|
|
client hsmClientAPI
|
|
|
|
storage kms.Storage
|
|
|
|
wantErr bool
|
2022-03-24 13:00:17 -04:00
|
|
|
}{
|
|
|
|
"successful": {
|
2022-07-27 16:02:33 -04:00
|
|
|
client: &stubHSMClient{keyID: keyID},
|
|
|
|
storage: storage.NewMemMapStorage(),
|
2022-03-24 13:00:17 -04:00
|
|
|
},
|
|
|
|
"Get from storage fails": {
|
2022-07-27 16:02:33 -04:00
|
|
|
client: &stubHSMClient{keyID: keyID},
|
|
|
|
storage: &stubStorage{getErr: someErr},
|
|
|
|
wantErr: true,
|
2022-03-24 13:00:17 -04:00
|
|
|
},
|
|
|
|
"Put to storage fails": {
|
2022-07-27 16:02:33 -04:00
|
|
|
client: &stubHSMClient{keyID: keyID},
|
2022-03-24 13:00:17 -04:00
|
|
|
storage: &stubStorage{
|
|
|
|
getErr: storage.ErrDEKUnset,
|
|
|
|
putErr: someErr,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-24 13:00:17 -04:00
|
|
|
},
|
|
|
|
"WrapKey fails": {
|
2022-07-27 16:02:33 -04:00
|
|
|
client: &stubHSMClient{keyID: keyID, wrapKeyErr: someErr},
|
|
|
|
storage: storage.NewMemMapStorage(),
|
|
|
|
wantErr: true,
|
2022-03-24 13:00:17 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
client := HSMClient{
|
2022-07-27 16:02:33 -04:00
|
|
|
client: tc.client,
|
|
|
|
storage: tc.storage,
|
2022-03-24 13:00:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dek, err := client.GetDEK(context.Background(), "test-key", "volume-01", 32)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-24 13:00:17 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.Len(dek, 32)
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHSMGetExistingDEK(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
keyVersion := "https://test.managedhsm.azure.net/keys/test-key/test-key-version"
|
|
|
|
testKey := []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-07-27 16:02:33 -04:00
|
|
|
client hsmClientAPI
|
|
|
|
wantErr bool
|
2022-03-24 13:00:17 -04:00
|
|
|
}{
|
|
|
|
"successful": {
|
2022-07-27 16:02:33 -04:00
|
|
|
client: &stubHSMClient{keyID: keyVersion, unwrapKeyResult: testKey},
|
2022-03-24 13:00:17 -04:00
|
|
|
},
|
|
|
|
"UnwrapKey fails": {
|
2022-07-27 16:02:33 -04:00
|
|
|
client: &stubHSMClient{keyID: keyVersion, unwrapKeyErr: someErr},
|
|
|
|
wantErr: true,
|
2022-03-24 13:00:17 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
keyID := "volume-01"
|
|
|
|
storage := storage.NewMemMapStorage()
|
|
|
|
require.NoError(storage.Put(context.Background(), keyID, testKey))
|
|
|
|
|
|
|
|
client := HSMClient{
|
2022-07-27 16:02:33 -04:00
|
|
|
client: tc.client,
|
|
|
|
storage: storage,
|
2022-03-24 13:00:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dek, err := client.GetDEK(context.Background(), "test-key", keyID, len(testKey))
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-24 13:00:17 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.Len(dek, len(testKey))
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|