| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package awskms |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"net/url" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/aws/aws-sdk-go-v2/aws" |
| 25 |
"github.com/aws/aws-sdk-go-v2/service/kms" |
| 26 |
"github.com/aws/aws-sdk-go-v2/service/kms/types" |
| 27 |
"github.com/aws/smithy-go" |
| 28 |
"github.com/google/go-cmp/cmp" |
| 29 |
"gocloud.dev/internal/testing/setup" |
| 30 |
"gocloud.dev/secrets" |
| 31 |
"gocloud.dev/secrets/driver" |
| 32 |
"gocloud.dev/secrets/drivertest" |
| 33 |
) |
| 34 |
|
| 35 |
const ( |
| 36 |
keyID1 = "alias/test-secrets" |
| 37 |
keyID2 = "alias/test-secrets2" |
| 38 |
region = "us-east-2" |
| 39 |
) |
| 40 |
|
| 41 |
type harness struct { |
| 42 |
client *kms.Client |
| 43 |
close func() |
| 44 |
} |
| 45 |
|
| 46 |
func (h *harness) MakeDriver(ctx context.Context) (driver.Keeper, driver.Keeper, error) { |
| 47 |
return &keeper{keyID: keyID1, client: h.client}, &keeper{keyID: keyID2, client: h.client}, nil |
| 48 |
} |
| 49 |
|
| 50 |
func (h *harness) Close() { |
| 51 |
h.close() |
| 52 |
} |
| 53 |
|
| 54 |
// Lists all existing aliases in the account, to see if we need to create one. |
| 55 |
func listExistingAliases(ctx context.Context, client *kms.Client) (map[string]bool, error) { |
| 56 |
existing := map[string]bool{} |
| 57 |
var nextMarker *string |
| 58 |
for { |
| 59 |
resp, err := client.ListAliases(ctx, &kms.ListAliasesInput{Marker: nextMarker}) |
| 60 |
if err != nil { |
| 61 |
return nil, fmt.Errorf("failed to list aliases: %w", err) |
| 62 |
} |
| 63 |
for _, alias := range resp.Aliases { |
| 64 |
existing[*alias.AliasName] = true |
| 65 |
} |
| 66 |
if !resp.Truncated { |
| 67 |
break |
| 68 |
} |
| 69 |
nextMarker = resp.NextMarker |
| 70 |
} |
| 71 |
return existing, nil |
| 72 |
} |
| 73 |
|
| 74 |
func createKey(ctx context.Context, client *kms.Client, keyID string) error { |
| 75 |
createKeyResult, err := client.CreateKey(ctx, &kms.CreateKeyInput{}) |
| 76 |
if err != nil { |
| 77 |
return fmt.Errorf("failed to create key %q: %w", keyID, err) |
| 78 |
} |
| 79 |
if _, err := client.CreateAlias(ctx, &kms.CreateAliasInput{ |
| 80 |
AliasName: aws.String(keyID), |
| 81 |
TargetKeyId: createKeyResult.KeyMetadata.KeyId, |
| 82 |
}); err != nil { |
| 83 |
return fmt.Errorf("failed to create alias for key %q: %w", keyID, err) |
| 84 |
} |
| 85 |
return nil |
| 86 |
} |
| 87 |
|
| 88 |
func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) { |
| 89 |
t.Helper() |
| 90 |
|
| 91 |
cfg, _, done, _ := setup.NewAWSv2Config(ctx, t, region, false) |
| 92 |
client := kms.NewFromConfig(cfg) |
| 93 |
|
| 94 |
// Create the keys we need if they don't exist. |
| 95 |
existing, err := listExistingAliases(ctx, client) |
| 96 |
if err != nil { |
| 97 |
return nil, err |
| 98 |
} |
| 99 |
if !existing[keyID1] { |
| 100 |
if err := createKey(ctx, client, keyID1); err != nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
} |
| 104 |
if !existing[keyID2] { |
| 105 |
if err := createKey(ctx, client, keyID2); err != nil { |
| 106 |
return nil, err |
| 107 |
} |
| 108 |
} |
| 109 |
return &harness{ |
| 110 |
client: client, |
| 111 |
close: done, |
| 112 |
}, nil |
| 113 |
} |
| 114 |
|
| 115 |
func TestConformance(t *testing.T) { |
| 116 |
drivertest.RunConformanceTests(t, newHarness, []drivertest.AsTest{verifyAs{}}) |
| 117 |
} |
| 118 |
|
| 119 |
type verifyAs struct { |
| 120 |
} |
| 121 |
|
| 122 |
func (v verifyAs) Name() string { |
| 123 |
return "verify As function" |
| 124 |
} |
| 125 |
|
| 126 |
func (v verifyAs) ErrorCheck(k *secrets.Keeper, err error) error { |
| 127 |
var e smithy.APIError |
| 128 |
if !k.ErrorAs(err, &e) { |
| 129 |
return errors.New("Keeper.ErrorAs failed") |
| 130 |
} |
| 131 |
code := e.ErrorCode() |
| 132 |
want := (&types.InvalidCiphertextException{}).ErrorCode() |
| 133 |
if code != want { |
| 134 |
return fmt.Errorf("got %q, want %q", code, want) |
| 135 |
} |
| 136 |
return nil |
| 137 |
} |
| 138 |
|
| 139 |
// KMS-specific tests. |
| 140 |
|
| 141 |
func TestEncryptionContext(t *testing.T) { |
| 142 |
tests := []struct { |
| 143 |
Existing map[string]string |
| 144 |
URL string |
| 145 |
WantErr bool |
| 146 |
Want map[string]string |
| 147 |
}{ |
| 148 |
// None before or after. |
| 149 |
{nil, "http://foo", false, nil}, |
| 150 |
// New parameter. |
| 151 |
{nil, "http://foo?context_foo=bar", false, map[string]string{"foo": "bar"}}, |
| 152 |
// 2 new parameters. |
| 153 |
{nil, "http://foo?context_foo=bar&context_abc=baz", false, map[string]string{"foo": "bar", "abc": "baz"}}, |
| 154 |
// Multiple values. |
| 155 |
{nil, "http://foo?context_foo=bar&context_foo=baz", true, nil}, |
| 156 |
// Existing, no new. |
| 157 |
{map[string]string{"foo": "bar"}, "http://foo", false, map[string]string{"foo": "bar"}}, |
| 158 |
// No-conflict merge. |
| 159 |
{map[string]string{"foo": "bar"}, "http://foo?context_abc=baz", false, map[string]string{"foo": "bar", "abc": "baz"}}, |
| 160 |
// Overwrite merge. |
| 161 |
{map[string]string{"foo": "bar"}, "http://foo?context_foo=baz", false, map[string]string{"foo": "baz"}}, |
| 162 |
} |
| 163 |
for _, test := range tests { |
| 164 |
t.Run(fmt.Sprintf("existing %v URL %v", test.Existing, test.URL), func(t *testing.T) { |
| 165 |
opts := KeeperOptions{ |
| 166 |
EncryptionContext: test.Existing, |
| 167 |
} |
| 168 |
u, err := url.Parse(test.URL) |
| 169 |
if err != nil { |
| 170 |
t.Fatal(err) |
| 171 |
} |
| 172 |
err = addEncryptionContextFromURLParams(&opts, u.Query()) |
| 173 |
if (err != nil) != test.WantErr { |
| 174 |
t.Fatalf("got err %v, want error? %v", err, test.WantErr) |
| 175 |
} |
| 176 |
if diff := cmp.Diff(opts.EncryptionContext, test.Want); diff != "" { |
| 177 |
t.Errorf("diff %v", diff) |
| 178 |
} |
| 179 |
}) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func TestOpenKeeper(t *testing.T) { |
| 184 |
tests := []struct { |
| 185 |
URL string |
| 186 |
WantErr bool |
| 187 |
}{ |
| 188 |
// OK, by alias. |
| 189 |
{"awskms://alias/my-key", false}, |
| 190 |
// OK, by ARN with empty Host. |
| 191 |
{"awskms:///arn:aws:kms:us-east-1:932528106278:alias/gocloud-test", false}, |
| 192 |
// OK, by ARN with empty Host. |
| 193 |
{"awskms:///arn:aws:kms:us-east-1:932528106278:key/8be0dcc5-da0a-4164-a99f-649015e344b5", false}, |
| 194 |
// OK, overriding region. |
| 195 |
{"awskms://alias/my-key?region=us-west1", false}, |
| 196 |
// OK, adding EncryptionContext. |
| 197 |
{"awskms://alias/my-key?context_abc=foo&context_def=bar", false}, |
| 198 |
// Multiple values for an EncryptionContext. |
| 199 |
{"awskms://alias/my-key?context_abc=foo&context_abc=bar", true}, |
| 200 |
// Unknown parameter. |
| 201 |
{"awskms://alias/my-key?param=value", true}, |
| 202 |
} |
| 203 |
|
| 204 |
ctx := context.Background() |
| 205 |
for _, test := range tests { |
| 206 |
keeper, err := secrets.OpenKeeper(ctx, test.URL) |
| 207 |
if (err != nil) != test.WantErr { |
| 208 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 209 |
} |
| 210 |
if err == nil { |
| 211 |
if err = keeper.Close(); err != nil { |
| 212 |
t.Errorf("%s: got error during close: %v", test.URL, err) |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |