-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathkeysource_integration_test.go
148 lines (120 loc) · 4.22 KB
/
keysource_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//go:build integration && disabled
// +build integration,disabled
// Copyright (C) 2022 The Flux authors
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package gcpkms
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"
. "github.com/onsi/gomega"
"go.mozilla.org/sops/v3/gcpkms"
"google.golang.org/api/option"
kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
kms "cloud.google.com/go/kms/apiv1"
)
var (
project = os.Getenv("TEST_PROJECT")
testKeyring = os.Getenv("TEST_KEYRING")
testKey = os.Getenv("TEST_CRYPTO_KEY")
testCredsJSON = os.Getenv("TEST_CRED_JSON")
resourceID = fmt.Sprintf("projects/%s/locations/global/keyRings/%s/cryptoKeys/%s",
project, testKeyring, testKey)
)
func TestMasterKey_Decrypt_SOPS_Compat(t *testing.T) {
g := NewWithT(t)
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", testCredsJSON)
g.Expect(createKMSKeyIfNotExists(resourceID)).To(Succeed())
dataKey := []byte("blue golden light")
encryptedKey := gcpkms.NewMasterKeyFromResourceID(resourceID)
g.Expect(encryptedKey.Encrypt(dataKey)).To(Succeed())
decryptionKey := MasterKeyFromResourceID(resourceID)
creds, err := ioutil.ReadFile(testCredsJSON)
g.Expect(err).ToNot(HaveOccurred())
decryptionKey.EncryptedKey = encryptedKey.EncryptedKey
decryptionKey.credentialJSON = creds
dec, err := decryptionKey.Decrypt()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(dec).To(Equal(dataKey))
}
func TestMasterKey_Encrypt_SOPS_Compat(t *testing.T) {
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", testCredsJSON)
g := NewWithT(t)
g.Expect(createKMSKeyIfNotExists(resourceID)).To(Succeed())
dataKey := []byte("silver golden lights")
encryptionKey := MasterKeyFromResourceID(resourceID)
creds, err := ioutil.ReadFile(testCredsJSON)
g.Expect(err).ToNot(HaveOccurred())
encryptionKey.credentialJSON = creds
err = encryptionKey.Encrypt(dataKey)
g.Expect(err).ToNot(HaveOccurred())
decryptionKey := gcpkms.NewMasterKeyFromResourceID(resourceID)
decryptionKey.EncryptedKey = encryptionKey.EncryptedKey
dec, err := decryptionKey.Decrypt()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(dec).To(Equal(dataKey))
}
func TestMasterKey_EncryptDecrypt_RoundTrip(t *testing.T) {
g := NewWithT(t)
g.Expect(createKMSKeyIfNotExists(resourceID)).To(Succeed())
key := MasterKeyFromResourceID(resourceID)
creds, err := ioutil.ReadFile(testCredsJSON)
g.Expect(err).ToNot(HaveOccurred())
key.credentialJSON = creds
datakey := []byte("a thousand splendid sons")
g.Expect(key.Encrypt(datakey)).To(Succeed())
g.Expect(key.EncryptedKey).ToNot(BeEmpty())
dec, err := key.Decrypt()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(dec).To(Equal(datakey))
}
func createKMSKeyIfNotExists(resourceID string) error {
ctx := context.Background()
// check if crypto key exists if not create it
c, err := kms.NewKeyManagementClient(ctx, option.WithCredentialsFile(testCredsJSON))
if err != nil {
return fmt.Errorf("err creating client: %q", err)
}
getCryptoKeyReq := &kmspb.GetCryptoKeyRequest{
Name: resourceID,
}
_, err = c.GetCryptoKey(ctx, getCryptoKeyReq)
if err == nil {
return nil
}
e, ok := status.FromError(err)
if !ok || (ok && e.Code() != codes.NotFound) {
return fmt.Errorf("err getting crypto key: %q", err)
}
projectID := fmt.Sprintf("projects/%s/locations/global", project)
createKeyRingReq := &kmspb.CreateKeyRingRequest{
Parent: projectID,
KeyRingId: testKeyring,
}
_, err = c.CreateKeyRing(ctx, createKeyRingReq)
e, ok = status.FromError(err)
if err != nil && !(ok && e.Code() == codes.AlreadyExists) {
return fmt.Errorf("err creating key ring: %q", err)
}
keyRingName := fmt.Sprintf("%s/keyRings/%s", projectID, testKeyring)
keyReq := &kmspb.CreateCryptoKeyRequest{
Parent: keyRingName,
CryptoKeyId: testKey,
CryptoKey: &kmspb.CryptoKey{
Purpose: kmspb.CryptoKey_ENCRYPT_DECRYPT,
},
}
_, err = c.CreateCryptoKey(ctx, keyReq)
e, ok = status.FromError(err)
if err != nil && !(ok && e.Code() == codes.AlreadyExists) {
return fmt.Errorf("err creating crypto key: %q", err)
}
return nil
}