-
Notifications
You must be signed in to change notification settings - Fork 113
/
integration_test.go
229 lines (193 loc) · 6.29 KB
/
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//go:build integration
//
// Copyright 2023 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package azure contains utilities related to Microsoft Azure KMS.
package azure
import (
"bytes"
"context"
"fmt"
"os"
"strings"
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/google/go-cmp/cmp"
)
/*
The following environment variables must be set:
AZURE_KEY_REF - full azure key reference in the format azurekms://[Key Vault Name].vault.azure.net/[Key Name](/[Key Version]Optional)
KEY_NAME - Azure key name
VAULT_URL - Azure Vault URL
*/
func TestMain(m *testing.M) {
azureKeyRef := os.Getenv("AZURE_KEY_REF")
if azureKeyRef == "" {
panic("AZURE_KEY_REF must be set")
}
os.Exit(m.Run())
}
func TestGetAzClientOpts(t *testing.T) {
testCases := []struct {
env string
expectedConfig cloud.Configuration
}{{
env: "AZUREUSGOVERNMENT",
expectedConfig: cloud.AzureGovernment,
}, {
env: "AZUREUSGOVERNMENTCLOUD",
expectedConfig: cloud.AzureGovernment,
}, {
env: "AZURECHINACLOUD",
expectedConfig: cloud.AzureChina,
}, {
env: "AZURECLOUD",
expectedConfig: cloud.AzurePublic,
}, {
env: "AZUREPUBLICCLOUD",
expectedConfig: cloud.AzurePublic,
}, {
env: "",
expectedConfig: cloud.AzurePublic,
}}
for _, tc := range testCases {
t.Setenv("AZURE_ENVIRONMENT", tc.env)
opts := getAzClientOpts()
if !cmp.Equal(tc.expectedConfig, opts.Cloud) {
t.Errorf("opts.Cloud %v does not match expected config: %v", opts.Cloud, tc.expectedConfig)
}
}
}
func TestLoadSignerVerifier(t *testing.T) {
azureKeyRef := os.Getenv("AZURE_KEY_REF")
azureKeyName := os.Getenv("KEY_NAME")
if azureKeyName == "" {
t.Fatalf("KEY_NAME must be set")
}
azureVaultURL := os.Getenv("VAULT_URL")
if azureVaultURL == "" {
t.Fatalf("VAULT_URL must be set")
}
azureKeyVersion := os.Getenv("KEY_VERSION")
sv, err := LoadSignerVerifier(context.Background(), azureKeyRef)
if err != nil {
t.Errorf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err)
}
if sv == nil {
t.Errorf("LoadSignerVerifier failed to create a SignerVerifier instance")
}
if sv.client.vaultURL != fmt.Sprintf("https://%s/", azureVaultURL) {
t.Errorf("expected client.vaultURL to be %s, got %s", azureVaultURL, sv.client.vaultURL)
}
if sv.client.keyName != azureKeyName {
t.Errorf("expected client.keyName to be %s, got %s", azureKeyName, sv.client.keyName)
}
if sv.client.keyVersion != azureKeyVersion {
t.Errorf("expected client.keyVersion to be %s, got %s", azureKeyVersion, sv.client.keyVersion)
}
}
func TestCreateKey(t *testing.T) {
azureVaultURL := os.Getenv("VAULT_URL")
if azureVaultURL == "" {
t.Fatalf("VAULT_URL must be set")
}
newKeyRef := fmt.Sprintf("azurekms://%s.vault.azure.net/%s", azureVaultURL, "new-test-key")
sv, err := LoadSignerVerifier(context.Background(), newKeyRef)
if err != nil {
t.Fatalf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err)
}
publicKey, err := sv.client.createKey(context.Background())
if err != nil {
t.Errorf("getKey failed with error: %v", err)
}
if publicKey == nil {
t.Errorf("public key is nil")
}
if _, ok := publicKey.(*ecdsa.PublicKey); !ok {
t.Errorf("expected public key to be of type *ecdsa.PublicKey")
}
}
func TestGetKey(t *testing.T) {
azureKeyRef := os.Getenv("AZURE_KEY_REF")
sv, err := LoadSignerVerifier(context.Background(), azureKeyRef)
if err != nil {
t.Fatalf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err)
}
keyBundle, err := sv.client.getKey(context.Background())
if err != nil {
t.Errorf("getKey failed with error: %v", err)
}
if keyBundle.Key == nil {
t.Errorf("key bundle key is nil")
}
}
func TestPublicKey(t *testing.T) {
azureKeyRef := os.Getenv("AZURE_KEY_REF")
sv, err := LoadSignerVerifier(context.Background(), azureKeyRef)
if err != nil {
t.Fatalf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err)
}
pubKey, err := sv.PublicKey()
if err != nil {
t.Errorf("PublicKey failed with error: %v", err)
}
if pubKey == nil {
t.Errorf("PublicKey response is nil")
}
}
func TestGetKeyVaultHashFunc(t *testing.T) {
azureKeyRef := os.Getenv("AZURE_KEY_REF")
sv, err := LoadSignerVerifier(context.Background(), azureKeyRef)
if err != nil {
t.Fatalf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err)
}
_, _, err = sv.client.getKeyVaultHashFunc(context.Background())
if err != nil {
t.Errorf("failed to get crypto hash and signature algorithm associated with key: %v", err)
}
}
func TestSignMessage(t *testing.T) {
azureKeyRef := os.Getenv("AZURE_KEY_REF")
sv, err := LoadSignerVerifier(context.Background(), azureKeyRef)
if err != nil {
t.Fatalf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err)
}
messageToSign := strings.NewReader("myblob")
signed, err := sv.SignMessage(messageToSign)
if err != nil {
t.Errorf("SignMessage unexpectedly returned non-nil error: %v", err)
}
if signed == nil || len(signed) == 0 {
t.Errorf("SignMessage unexpected returned nil or empty signature")
}
}
func TestVerifySignature(t *testing.T) {
azureKeyRef := os.Getenv("AZURE_KEY_REF")
sv, err := LoadSignerVerifier(context.Background(), azureKeyRef)
if err != nil {
t.Fatalf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err)
}
messageToSign := "myblob"
signed, err := sv.SignMessage(strings.NewReader(messageToSign))
if err != nil {
t.Errorf("SignMessage unexpectedly returned non-nil error: %v", err)
}
if signed == nil || len(signed) == 0 {
t.Errorf("SignMessage unexpected returned nil or empty signature")
}
err = sv.VerifySignature(bytes.NewReader(signed), strings.NewReader(messageToSign))
if err != nil {
t.Errorf("VerifySignature unexpectedly returned non-nil error: %v", err)
}
}