| 1 |
// |
| 2 |
// Copyright 2021 The Sigstore Authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
|
| 16 |
package hashivault |
| 17 |
|
| 18 |
import ( |
| 19 |
"os" |
| 20 |
"path/filepath" |
| 21 |
"testing" |
| 22 |
) |
| 23 |
|
| 24 |
func TestNewHashivaultClient(t *testing.T) { |
| 25 |
tests := []struct { |
| 26 |
name string |
| 27 |
address string |
| 28 |
token string |
| 29 |
transitSecretEnginePath string |
| 30 |
keyResourceID string |
| 31 |
keyVersion uint64 |
| 32 |
addressEnv string |
| 33 |
tokenEnv string |
| 34 |
vaultTokenFile string |
| 35 |
tokenHelperScript string |
| 36 |
wantErr bool |
| 37 |
wantAddress string |
| 38 |
wantToken string |
| 39 |
wantTransitSecretEnginePath string |
| 40 |
}{ |
| 41 |
{ |
| 42 |
name: "directly provided parameters", |
| 43 |
address: "https://vault.example.com", |
| 44 |
token: "hvc.exampletoken", |
| 45 |
transitSecretEnginePath: "", |
| 46 |
keyResourceID: "hashivault://test", |
| 47 |
keyVersion: 1, |
| 48 |
wantErr: false, |
| 49 |
wantAddress: "https://vault.example.com", |
| 50 |
wantToken: "hvc.exampletoken", |
| 51 |
wantTransitSecretEnginePath: "transit", |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "environment variables", |
| 55 |
keyResourceID: "hashivault://test", |
| 56 |
keyVersion: 1, |
| 57 |
addressEnv: "https://vault.example.com", |
| 58 |
tokenEnv: "hvc.exampletoken", |
| 59 |
wantErr: false, |
| 60 |
wantAddress: "https://vault.example.com", |
| 61 |
wantToken: "hvc.exampletoken", |
| 62 |
wantTransitSecretEnginePath: "transit", |
| 63 |
}, |
| 64 |
{ |
| 65 |
name: "default token helper ~/.vault-token", |
| 66 |
address: "https://vault.example.com", |
| 67 |
keyResourceID: "hashivault://test", |
| 68 |
keyVersion: 1, |
| 69 |
vaultTokenFile: "hvc.exampletoken", |
| 70 |
wantErr: false, |
| 71 |
wantAddress: "https://vault.example.com", |
| 72 |
wantToken: "hvc.exampletoken", |
| 73 |
wantTransitSecretEnginePath: "transit", |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "custom token helper script", |
| 77 |
address: "https://vault.example.com", |
| 78 |
keyResourceID: "hashivault://test", |
| 79 |
keyVersion: 1, |
| 80 |
tokenHelperScript: `#!/bin/bash |
| 81 |
set -euo pipefail |
| 82 |
|
| 83 |
cmd="${1}" |
| 84 |
|
| 85 |
case "$cmd" in |
| 86 |
get) |
| 87 |
# Print only the token to stdout |
| 88 |
echo -n "hvc.exampletoken" |
| 89 |
;; |
| 90 |
store) |
| 91 |
# Read token from stdin (ignored) |
| 92 |
cat >/dev/null |
| 93 |
;; |
| 94 |
erase) |
| 95 |
# Nothing to delete |
| 96 |
;; |
| 97 |
*) |
| 98 |
echo "unknown command: $cmd" >&2 |
| 99 |
exit 1 |
| 100 |
;; |
| 101 |
esac |
| 102 |
`, |
| 103 |
wantErr: false, |
| 104 |
wantAddress: "https://vault.example.com", |
| 105 |
wantToken: "hvc.exampletoken", |
| 106 |
wantTransitSecretEnginePath: "transit", |
| 107 |
}, |
| 108 |
} |
| 109 |
for _, tt := range tests { |
| 110 |
t.Run(tt.name, func(t *testing.T) { |
| 111 |
// backup environment variables |
| 112 |
oldAddressEnv := os.Getenv("VAULT_ADDR") |
| 113 |
oldTokenEnv := os.Getenv("VAULT_TOKEN") |
| 114 |
oldTransitSecretEnginePathEnv := os.Getenv("VAULT_TRANSIT_SECRET_ENGINE_PATH") |
| 115 |
os.Unsetenv("VAULT_ADDR") |
| 116 |
os.Unsetenv("VAULT_TOKEN") |
| 117 |
os.Unsetenv("VAULT_TRANSIT_SECRET_ENGINE_PATH") |
| 118 |
defer func() { |
| 119 |
os.Setenv("VAULT_ADDR", oldAddressEnv) |
| 120 |
os.Setenv("VAULT_TOKEN", oldTokenEnv) |
| 121 |
os.Setenv("VAULT_TRANSIT_SECRET_ENGINE_PATH", oldTransitSecretEnginePathEnv) |
| 122 |
}() |
| 123 |
|
| 124 |
// backup files |
| 125 |
homeDir, err := os.UserHomeDir() |
| 126 |
if err != nil { |
| 127 |
t.Fatalf("failed to get user home directory: %v", err) |
| 128 |
} |
| 129 |
oldVaultTokenFile, err := os.ReadFile(filepath.Join(homeDir, ".vault-token")) |
| 130 |
if err == nil { |
| 131 |
defer os.WriteFile(filepath.Join(homeDir, ".vault-token"), oldVaultTokenFile, 0o600) |
| 132 |
} else { |
| 133 |
defer os.Remove(filepath.Join(homeDir, ".vault-token")) |
| 134 |
} |
| 135 |
os.Remove(filepath.Join(homeDir, ".vault-token")) |
| 136 |
oldVaultConfigFile, err := os.ReadFile(filepath.Join(homeDir, ".vault")) |
| 137 |
if err == nil { |
| 138 |
defer os.WriteFile(filepath.Join(homeDir, ".vault"), oldVaultConfigFile, 0o600) |
| 139 |
} else { |
| 140 |
defer os.Remove(filepath.Join(homeDir, ".vault")) |
| 141 |
} |
| 142 |
os.Remove(filepath.Join(homeDir, ".vault")) |
| 143 |
|
| 144 |
// prepare environment variables and files |
| 145 |
if tt.addressEnv != "" { |
| 146 |
t.Setenv("VAULT_ADDR", tt.addressEnv) |
| 147 |
} |
| 148 |
if tt.tokenEnv != "" { |
| 149 |
t.Setenv("VAULT_TOKEN", tt.tokenEnv) |
| 150 |
} |
| 151 |
if tt.vaultTokenFile != "" { |
| 152 |
// write to ~/.vault-token |
| 153 |
err = os.WriteFile(filepath.Join(homeDir, ".vault-token"), []byte(tt.vaultTokenFile), 0o600) |
| 154 |
if err != nil { |
| 155 |
t.Fatalf("failed to write to ~/.vault-token: %v", err) |
| 156 |
} |
| 157 |
} |
| 158 |
if tt.tokenHelperScript != "" { |
| 159 |
// write "token_helper = \"<tmp-file-for-token-helper-script>\" to ~/.vault |
| 160 |
// write tt.tokenHelperScript to <tmp-file-for-token-helper-script> |
| 161 |
tmpFile, err := os.CreateTemp("", "token-helper.sh") |
| 162 |
if err != nil { |
| 163 |
t.Fatalf("failed to create temp file for token helper script: %v", err) |
| 164 |
} |
| 165 |
defer os.Remove(tmpFile.Name()) |
| 166 |
_, err = tmpFile.WriteString(tt.tokenHelperScript) |
| 167 |
if err != nil { |
| 168 |
t.Fatalf("failed to write to temp file for token helper script: %v", err) |
| 169 |
} |
| 170 |
err = tmpFile.Chmod(0o700) |
| 171 |
if err != nil { |
| 172 |
t.Fatalf("failed to chmod temp file for token helper script: %v", err) |
| 173 |
} |
| 174 |
tmpFile.Close() |
| 175 |
|
| 176 |
// write to ~/.vault |
| 177 |
vaultConfigContent := []byte("token_helper = \"" + tmpFile.Name() + "\"\n") |
| 178 |
err = os.WriteFile(filepath.Join(homeDir, ".vault"), vaultConfigContent, 0o600) |
| 179 |
if err != nil { |
| 180 |
t.Fatalf("failed to write to ~/.vault: %v", err) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
// setup client |
| 185 |
client, err := newHashivaultClient(tt.address, tt.token, tt.transitSecretEnginePath, tt.keyResourceID, tt.keyVersion) |
| 186 |
|
| 187 |
// check results |
| 188 |
if (err != nil) != tt.wantErr { |
| 189 |
t.Errorf("newHashivaultClient() error = %v, wantErr %v", err, tt.wantErr) |
| 190 |
return |
| 191 |
} |
| 192 |
if client.client.Address() != tt.wantAddress { |
| 193 |
t.Errorf("newHashivaultClient() got address = %v, want %v", client.client.Address(), tt.wantAddress) |
| 194 |
} |
| 195 |
token := client.client.Token() |
| 196 |
if token != tt.wantToken { |
| 197 |
t.Errorf("newHashivaultClient() got token = %v, want %v", token, tt.wantToken) |
| 198 |
} |
| 199 |
if client.transitSecretEnginePath != tt.wantTransitSecretEnginePath { |
| 200 |
t.Errorf("newHashivaultClient() got transitSecretEnginePath = %v, want %v", client.transitSecretEnginePath, tt.wantTransitSecretEnginePath) |
| 201 |
} |
| 202 |
}) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func TestParseReference(t *testing.T) { |
| 207 |
tests := []struct { |
| 208 |
in string |
| 209 |
wantKey string |
| 210 |
wantErr bool |
| 211 |
}{ |
| 212 |
{ |
| 213 |
in: "hashivault://cosign", |
| 214 |
wantKey: "cosign", |
| 215 |
wantErr: false, |
| 216 |
}, |
| 217 |
{ |
| 218 |
in: "hashivault://cosign/nested", |
| 219 |
wantErr: true, |
| 220 |
}, |
| 221 |
{ |
| 222 |
in: "foo://bar", |
| 223 |
wantErr: true, |
| 224 |
}, |
| 225 |
{ |
| 226 |
in: "", |
| 227 |
wantErr: true, |
| 228 |
}, |
| 229 |
} |
| 230 |
for _, tt := range tests { |
| 231 |
t.Run(tt.in, func(t *testing.T) { |
| 232 |
gotKey, err := parseReference(tt.in) |
| 233 |
if (err != nil) != tt.wantErr { |
| 234 |
t.Errorf("parseReference() error = %v, wantErr %v", err, tt.wantErr) |
| 235 |
return |
| 236 |
} |
| 237 |
if gotKey != tt.wantKey { |
| 238 |
t.Errorf("parseReference() gotKey = %v, want %v", gotKey, tt.wantKey) |
| 239 |
} |
| 240 |
}) |
| 241 |
} |
| 242 |
} |