Skip to content
This repository has been archived by the owner on Aug 25, 2023. It is now read-only.

feat: add CreateAndExportPubKeyBytes func to aws #337

Merged
merged 1 commit into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pkg/aws/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,32 @@ func (s *Service) Create(kt arieskms.KeyType) (string, interface{}, error) {
return *result.KeyMetadata.KeyId, *result.KeyMetadata.KeyId, nil
}

// CreateAndExportPubKeyBytes create and export key.
func (s *Service) CreateAndExportPubKeyBytes(kt arieskms.KeyType, opts ...arieskms.KeyOpts) (string, []byte, error) {
keyID, _, err := s.Create(kt)
if err != nil {
return "", nil, err
}

pubKeyBytes, _, err := s.ExportPubKeyBytes(keyID)
if err != nil {
return "", nil, err
}

return keyID, pubKeyBytes, nil
}

// ImportPrivateKey private key.
func (s *Service) ImportPrivateKey(privKey interface{}, kt arieskms.KeyType,
opts ...arieskms.PrivateKeyOpts) (string, interface{}, error) {
return "", nil, fmt.Errorf("not implemented")
}

// SignMulti sign multi.
func (s *Service) SignMulti(messages [][]byte, kh interface{}) ([]byte, error) {
return nil, fmt.Errorf("not implemented")
}

func getKeyID(keyURI string) (string, error) {
// keyURI must have the following format: 'aws-kms://arn:<partition>:kms:<region>:[:path]'.
// See http://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html.
Expand Down
51 changes: 51 additions & 0 deletions pkg/aws/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,57 @@ func TestGet(t *testing.T) {
})
}

func TestCreateAndPubKeyBytes(t *testing.T) {
t.Run("success", func(t *testing.T) {
endpoint := localhost
awsSession, err := session.NewSession(&aws.Config{
Endpoint: &endpoint,
Region: aws.String("ca"),
CredentialsChainVerboseErrors: aws.Bool(true),
})
require.NoError(t, err)

keyID := "aws-kms://arn:aws:kms:ca-central-1:111122223333:key/800d5768-3fd7-4edd-a4b8-4c81c3e4c147"

svc := New(awsSession, &mockMetrics{}, "")

svc.client = &mockAWSClient{
getPublicKeyFunc: func(input *kms.GetPublicKeyInput) (*kms.GetPublicKeyOutput, error) {
signingAlgo := "ECDSA_SHA_256"

return &kms.GetPublicKeyOutput{
PublicKey: []byte("publickey"),
SigningAlgorithms: []*string{&signingAlgo},
}, nil
},
createKeyFunc: func(input *kms.CreateKeyInput) (req *request.Request, output *kms.CreateKeyOutput) {
return nil, &kms.CreateKeyOutput{KeyMetadata: &kms.KeyMetadata{KeyId: &keyID}}
},
}

keyID, publicKey, err := svc.CreateAndExportPubKeyBytes(arieskms.ECDSAP256DER)
require.NoError(t, err)
require.Contains(t, string(publicKey), "publickey")
require.Contains(t, keyID, keyID)
})
}

func TestSignMulti(t *testing.T) {
endpoint := localhost
awsSession, err := session.NewSession(&aws.Config{
Endpoint: &endpoint,
Region: aws.String("ca"),
CredentialsChainVerboseErrors: aws.Bool(true),
})
require.NoError(t, err)

svc := New(awsSession, &mockMetrics{}, "")

_, err = svc.SignMulti(nil, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "not implemented")
}

func TestPubKeyBytes(t *testing.T) {
t.Run("success", func(t *testing.T) {
endpoint := localhost
Expand Down