| 1 |
//go:build integration |
| 2 |
|
| 3 |
// |
| 4 |
// Copyright 2023 The Sigstore Authors. |
| 5 |
// |
| 6 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
// you may not use this file except in compliance with the License. |
| 8 |
// You may obtain a copy of the License at |
| 9 |
// |
| 10 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
// |
| 12 |
// Unless required by applicable law or agreed to in writing, software |
| 13 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
// See the License for the specific language governing permissions and |
| 16 |
// limitations under the License. |
| 17 |
|
| 18 |
// Package azure contains utilities related to Microsoft Azure KMS. |
| 19 |
package azure |
| 20 |
|
| 21 |
import ( |
| 22 |
"bytes" |
| 23 |
"context" |
| 24 |
"fmt" |
| 25 |
"os" |
| 26 |
"strings" |
| 27 |
"testing" |
| 28 |
|
| 29 |
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud" |
| 30 |
"github.com/google/go-cmp/cmp" |
| 31 |
) |
| 32 |
|
| 33 |
/* |
| 34 |
The following environment variables must be set: |
| 35 |
AZURE_KEY_REF - full azure key reference in the format azurekms://[Key Vault Name].vault.azure.net/[Key Name](/[Key Version]Optional) |
| 36 |
KEY_NAME - Azure key name |
| 37 |
VAULT_URL - Azure Vault URL |
| 38 |
*/ |
| 39 |
|
| 40 |
func TestMain(m *testing.M) { |
| 41 |
azureKeyRef := os.Getenv("AZURE_KEY_REF") |
| 42 |
if azureKeyRef == "" { |
| 43 |
panic("AZURE_KEY_REF must be set") |
| 44 |
} |
| 45 |
os.Exit(m.Run()) |
| 46 |
} |
| 47 |
|
| 48 |
func TestGetAzClientOpts(t *testing.T) { |
| 49 |
testCases := []struct { |
| 50 |
env string |
| 51 |
expectedConfig cloud.Configuration |
| 52 |
}{{ |
| 53 |
env: "AZUREUSGOVERNMENT", |
| 54 |
expectedConfig: cloud.AzureGovernment, |
| 55 |
}, { |
| 56 |
env: "AZUREUSGOVERNMENTCLOUD", |
| 57 |
expectedConfig: cloud.AzureGovernment, |
| 58 |
}, { |
| 59 |
env: "AZURECHINACLOUD", |
| 60 |
expectedConfig: cloud.AzureChina, |
| 61 |
}, { |
| 62 |
env: "AZURECLOUD", |
| 63 |
expectedConfig: cloud.AzurePublic, |
| 64 |
}, { |
| 65 |
env: "AZUREPUBLICCLOUD", |
| 66 |
expectedConfig: cloud.AzurePublic, |
| 67 |
}, { |
| 68 |
env: "", |
| 69 |
expectedConfig: cloud.AzurePublic, |
| 70 |
}} |
| 71 |
|
| 72 |
for _, tc := range testCases { |
| 73 |
t.Setenv("AZURE_ENVIRONMENT", tc.env) |
| 74 |
|
| 75 |
opts := getAzClientOpts() |
| 76 |
if !cmp.Equal(tc.expectedConfig, opts.Cloud) { |
| 77 |
t.Errorf("opts.Cloud %v does not match expected config: %v", opts.Cloud, tc.expectedConfig) |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func TestLoadSignerVerifier(t *testing.T) { |
| 83 |
azureKeyRef := os.Getenv("AZURE_KEY_REF") |
| 84 |
|
| 85 |
azureKeyName := os.Getenv("KEY_NAME") |
| 86 |
if azureKeyName == "" { |
| 87 |
t.Fatalf("KEY_NAME must be set") |
| 88 |
} |
| 89 |
azureVaultURL := os.Getenv("VAULT_URL") |
| 90 |
if azureVaultURL == "" { |
| 91 |
t.Fatalf("VAULT_URL must be set") |
| 92 |
} |
| 93 |
azureKeyVersion := os.Getenv("KEY_VERSION") |
| 94 |
|
| 95 |
sv, err := LoadSignerVerifier(context.Background(), azureKeyRef) |
| 96 |
if err != nil { |
| 97 |
t.Errorf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err) |
| 98 |
} |
| 99 |
|
| 100 |
if sv == nil { |
| 101 |
t.Errorf("LoadSignerVerifier failed to create a SignerVerifier instance") |
| 102 |
} |
| 103 |
|
| 104 |
if sv.client.vaultURL != fmt.Sprintf("https://%s/", azureVaultURL) { |
| 105 |
t.Errorf("expected client.vaultURL to be %s, got %s", azureVaultURL, sv.client.vaultURL) |
| 106 |
} |
| 107 |
if sv.client.keyName != azureKeyName { |
| 108 |
t.Errorf("expected client.keyName to be %s, got %s", azureKeyName, sv.client.keyName) |
| 109 |
} |
| 110 |
if sv.client.keyVersion != azureKeyVersion { |
| 111 |
t.Errorf("expected client.keyVersion to be %s, got %s", azureKeyVersion, sv.client.keyVersion) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func TestCreateKey(t *testing.T) { |
| 116 |
azureVaultURL := os.Getenv("VAULT_URL") |
| 117 |
if azureVaultURL == "" { |
| 118 |
t.Fatalf("VAULT_URL must be set") |
| 119 |
} |
| 120 |
|
| 121 |
newKeyRef := fmt.Sprintf("azurekms://%s.vault.azure.net/%s", azureVaultURL, "new-test-key") |
| 122 |
|
| 123 |
sv, err := LoadSignerVerifier(context.Background(), newKeyRef) |
| 124 |
if err != nil { |
| 125 |
t.Fatalf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err) |
| 126 |
} |
| 127 |
|
| 128 |
publicKey, err := sv.client.createKey(context.Background()) |
| 129 |
if err != nil { |
| 130 |
t.Errorf("getKey failed with error: %v", err) |
| 131 |
} |
| 132 |
if publicKey == nil { |
| 133 |
t.Errorf("public key is nil") |
| 134 |
} |
| 135 |
|
| 136 |
if _, ok := publicKey.(*ecdsa.PublicKey); !ok { |
| 137 |
t.Errorf("expected public key to be of type *ecdsa.PublicKey") |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func TestGetKey(t *testing.T) { |
| 142 |
azureKeyRef := os.Getenv("AZURE_KEY_REF") |
| 143 |
|
| 144 |
sv, err := LoadSignerVerifier(context.Background(), azureKeyRef) |
| 145 |
if err != nil { |
| 146 |
t.Fatalf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err) |
| 147 |
} |
| 148 |
|
| 149 |
keyBundle, err := sv.client.getKey(context.Background()) |
| 150 |
if err != nil { |
| 151 |
t.Errorf("getKey failed with error: %v", err) |
| 152 |
} |
| 153 |
|
| 154 |
if keyBundle.Key == nil { |
| 155 |
t.Errorf("key bundle key is nil") |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func TestPublicKey(t *testing.T) { |
| 160 |
azureKeyRef := os.Getenv("AZURE_KEY_REF") |
| 161 |
|
| 162 |
sv, err := LoadSignerVerifier(context.Background(), azureKeyRef) |
| 163 |
if err != nil { |
| 164 |
t.Fatalf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err) |
| 165 |
} |
| 166 |
|
| 167 |
pubKey, err := sv.PublicKey() |
| 168 |
if err != nil { |
| 169 |
t.Errorf("PublicKey failed with error: %v", err) |
| 170 |
} |
| 171 |
if pubKey == nil { |
| 172 |
t.Errorf("PublicKey response is nil") |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func TestGetKeyVaultHashFunc(t *testing.T) { |
| 177 |
azureKeyRef := os.Getenv("AZURE_KEY_REF") |
| 178 |
|
| 179 |
sv, err := LoadSignerVerifier(context.Background(), azureKeyRef) |
| 180 |
if err != nil { |
| 181 |
t.Fatalf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err) |
| 182 |
} |
| 183 |
|
| 184 |
_, _, _, err = sv.client.getKeyVaultHashFunc(context.Background()) |
| 185 |
if err != nil { |
| 186 |
t.Errorf("failed to get crypto hash and signature algorithm associated with key: %v", err) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func TestSignMessage(t *testing.T) { |
| 191 |
azureKeyRef := os.Getenv("AZURE_KEY_REF") |
| 192 |
|
| 193 |
sv, err := LoadSignerVerifier(context.Background(), azureKeyRef) |
| 194 |
if err != nil { |
| 195 |
t.Fatalf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err) |
| 196 |
} |
| 197 |
|
| 198 |
messageToSign := strings.NewReader("myblob") |
| 199 |
signed, err := sv.SignMessage(messageToSign) |
| 200 |
if err != nil { |
| 201 |
t.Errorf("SignMessage unexpectedly returned non-nil error: %v", err) |
| 202 |
} |
| 203 |
if signed == nil || len(signed) == 0 { |
| 204 |
t.Errorf("SignMessage unexpected returned nil or empty signature") |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
func TestVerifySignature(t *testing.T) { |
| 209 |
azureKeyRef := os.Getenv("AZURE_KEY_REF") |
| 210 |
|
| 211 |
sv, err := LoadSignerVerifier(context.Background(), azureKeyRef) |
| 212 |
if err != nil { |
| 213 |
t.Fatalf("LoadSignerVerifier unexpectedly returned non-nil error: %v", err) |
| 214 |
} |
| 215 |
|
| 216 |
messageToSign := "myblob" |
| 217 |
signed, err := sv.SignMessage(strings.NewReader(messageToSign)) |
| 218 |
if err != nil { |
| 219 |
t.Errorf("SignMessage unexpectedly returned non-nil error: %v", err) |
| 220 |
} |
| 221 |
if signed == nil || len(signed) == 0 { |
| 222 |
t.Errorf("SignMessage unexpected returned nil or empty signature") |
| 223 |
} |
| 224 |
|
| 225 |
err = sv.VerifySignature(bytes.NewReader(signed), strings.NewReader(messageToSign)) |
| 226 |
if err != nil { |
| 227 |
t.Errorf("VerifySignature unexpectedly returned non-nil error: %v", err) |
| 228 |
} |
| 229 |
} |