| 1 |
// Copyright 2018 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 gcpkms |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"testing" |
| 21 |
|
| 22 |
cloudkms "cloud.google.com/go/kms/apiv1" |
| 23 |
"gocloud.dev/internal/testing/setup" |
| 24 |
"gocloud.dev/secrets" |
| 25 |
"gocloud.dev/secrets/driver" |
| 26 |
"gocloud.dev/secrets/drivertest" |
| 27 |
"golang.org/x/oauth2" |
| 28 |
"google.golang.org/api/option" |
| 29 |
"google.golang.org/grpc/status" |
| 30 |
) |
| 31 |
|
| 32 |
// These constants capture values that were used during the last --record. |
| 33 |
// If you want to use --record mode, |
| 34 |
// 1. Update projectID to your GCP project name (not number!) |
| 35 |
// 2. Enable the Cloud KMS API. |
| 36 |
// 3. Create a key ring and a key, change their name below accordingly. |
| 37 |
const ( |
| 38 |
project = "go-cloud-test-216917" |
| 39 |
location = "global" |
| 40 |
keyRing = "test" |
| 41 |
keyID1 = "password" |
| 42 |
keyID2 = "password2" |
| 43 |
) |
| 44 |
|
| 45 |
type harness struct { |
| 46 |
client *cloudkms.KeyManagementClient |
| 47 |
close func() |
| 48 |
} |
| 49 |
|
| 50 |
func (h *harness) MakeDriver(ctx context.Context) (driver.Keeper, driver.Keeper, error) { |
| 51 |
return &keeper{KeyResourceID(project, location, keyRing, keyID1), h.client, KeeperOptions{}}, |
| 52 |
&keeper{KeyResourceID(project, location, keyRing, keyID2), h.client, KeeperOptions{}}, nil |
| 53 |
} |
| 54 |
|
| 55 |
func (h *harness) Close() { |
| 56 |
h.close() |
| 57 |
} |
| 58 |
|
| 59 |
func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) { |
| 60 |
t.Helper() |
| 61 |
|
| 62 |
conn, done := setup.NewGCPgRPCConn(ctx, t, endPoint, "secrets") |
| 63 |
client, err := cloudkms.NewKeyManagementClient(ctx, option.WithGRPCConn(conn)) |
| 64 |
if err != nil { |
| 65 |
return nil, err |
| 66 |
} |
| 67 |
return &harness{ |
| 68 |
client: client, |
| 69 |
close: func() { |
| 70 |
client.Close() |
| 71 |
done() |
| 72 |
}, |
| 73 |
}, nil |
| 74 |
} |
| 75 |
|
| 76 |
func TestConformance(t *testing.T) { |
| 77 |
drivertest.RunConformanceTests(t, newHarness, []drivertest.AsTest{verifyAs{}}) |
| 78 |
} |
| 79 |
|
| 80 |
type verifyAs struct{} |
| 81 |
|
| 82 |
func (v verifyAs) Name() string { |
| 83 |
return "verify As function" |
| 84 |
} |
| 85 |
|
| 86 |
func (v verifyAs) ErrorCheck(k *secrets.Keeper, err error) error { |
| 87 |
var s *status.Status |
| 88 |
if !k.ErrorAs(err, &s) { |
| 89 |
return errors.New("Keeper.ErrorAs failed") |
| 90 |
} |
| 91 |
return nil |
| 92 |
} |
| 93 |
|
| 94 |
// KMS-specific tests. |
| 95 |
|
| 96 |
func TestNoConnectionError(t *testing.T) { |
| 97 |
ctx := context.Background() |
| 98 |
client, done, err := Dial(ctx, oauth2.StaticTokenSource(&oauth2.Token{ |
| 99 |
AccessToken: "fake", |
| 100 |
})) |
| 101 |
if err != nil { |
| 102 |
t.Fatal(err) |
| 103 |
} |
| 104 |
defer done() |
| 105 |
|
| 106 |
keeper := OpenKeeper(client, "", nil) |
| 107 |
defer keeper.Close() |
| 108 |
|
| 109 |
if _, err := keeper.Encrypt(ctx, []byte("test")); err == nil { |
| 110 |
t.Error("got nil, want rpc error") |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func TestAdditionalAuthenticatedData(t *testing.T) { |
| 115 |
ctx := context.Background() |
| 116 |
conn, done := setup.NewGCPgRPCConn(ctx, t, endPoint, "secrets") |
| 117 |
defer done() |
| 118 |
client, err := cloudkms.NewKeyManagementClient(ctx, option.WithGRPCConn(conn)) |
| 119 |
if err != nil { |
| 120 |
t.Fatalf("failed to connect: %v", err) |
| 121 |
} |
| 122 |
|
| 123 |
opts := KeeperOptions{ |
| 124 |
AdditionalAuthenticatedData: []byte("sample AAD"), |
| 125 |
} |
| 126 |
k1a := OpenKeeper(client, KeyResourceID(project, location, keyRing, keyID1), &opts) |
| 127 |
defer k1a.Close() |
| 128 |
k1b := OpenKeeper(client, KeyResourceID(project, location, keyRing, keyID1), &opts) |
| 129 |
defer k1b.Close() |
| 130 |
opts.AdditionalAuthenticatedData = []byte("different AAD") |
| 131 |
k2 := OpenKeeper(client, KeyResourceID(project, location, keyRing, keyID1), &opts) |
| 132 |
defer k2.Close() |
| 133 |
|
| 134 |
// Encrypt/Decrypt with an AAD should work. |
| 135 |
secret := []byte("a secret") |
| 136 |
encryptedSecret, err := k1a.Encrypt(ctx, secret) |
| 137 |
if err != nil { |
| 138 |
t.Fatalf("failed to encrypt: %v", err) |
| 139 |
} |
| 140 |
if got, err := k1b.Decrypt(ctx, encryptedSecret); err != nil { |
| 141 |
t.Fatalf("failed to decrypt: %v", err) |
| 142 |
} else if string(got) != string(secret) { |
| 143 |
t.Errorf("unexpected decrypt result: %v", string(got)) |
| 144 |
} |
| 145 |
|
| 146 |
// Decrypting with a different AAD should fail. |
| 147 |
if _, err := k2.Decrypt(ctx, encryptedSecret); err == nil { |
| 148 |
t.Errorf("expected Decrypt with a different AAD to fail, but it did not") |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func TestOpenKeeper(t *testing.T) { |
| 153 |
cleanup := setup.FakeGCPDefaultCredentials(t) |
| 154 |
defer cleanup() |
| 155 |
|
| 156 |
tests := []struct { |
| 157 |
URL string |
| 158 |
WantErr bool |
| 159 |
}{ |
| 160 |
// OK. |
| 161 |
{"gcpkms://projects/MYPROJECT/locations/MYLOCATION/keyRings/MYKEYRING/cryptoKeys/MYKEY", false}, |
| 162 |
// Invalid additional_authenticated_data. |
| 163 |
{"gcpkms://projects/MYPROJECT/locations/MYLOCATION/keyRings/MYKEYRING/cryptoKeys/MYKEY?additional_authenticated_data=22", true}, |
| 164 |
// OK, with valid additional_authenticated_data. |
| 165 |
{"gcpkms://projects/MYPROJECT/locations/MYLOCATION/keyRings/MYKEYRING/cryptoKeys/MYKEY?additional_authenticated_data=SGVsbG8sIOS4lueVjA==", false}, |
| 166 |
// Invalid query parameter. |
| 167 |
{"gcpkms://projects/MYPROJECT/locations/MYLOCATION/keyRings/MYKEYRING/cryptoKeys/MYKEY?param=val", true}, |
| 168 |
} |
| 169 |
|
| 170 |
ctx := context.Background() |
| 171 |
for _, test := range tests { |
| 172 |
keeper, err := secrets.OpenKeeper(ctx, test.URL) |
| 173 |
if (err != nil) != test.WantErr { |
| 174 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 175 |
} |
| 176 |
if err == nil { |
| 177 |
if err = keeper.Close(); err != nil { |
| 178 |
t.Errorf("%s: got error during close: %v", test.URL, err) |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |