| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package config |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"crypto/x509" |
| 19 |
"encoding/pem" |
| 20 |
"errors" |
| 21 |
"os" |
| 22 |
"path/filepath" |
| 23 |
"reflect" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"github.com/notaryproject/notation-core-go/testhelper" |
| 27 |
"github.com/notaryproject/notation-go/dir" |
| 28 |
) |
| 29 |
|
| 30 |
var sampleSigningKeysInfo = SigningKeys{ |
| 31 |
Default: Ptr("wabbit-networks"), |
| 32 |
Keys: []KeySuite{ |
| 33 |
{ |
| 34 |
Name: "wabbit-networks", |
| 35 |
X509KeyPair: &X509KeyPair{ |
| 36 |
KeyPath: "/home/demo/.config/notation/localkeys/wabbit-networks.key", |
| 37 |
CertificatePath: "/home/demo/.config/notation/localkeys/wabbit-networks.crt", |
| 38 |
}, |
| 39 |
}, |
| 40 |
{ |
| 41 |
Name: "import.acme-rockets", |
| 42 |
X509KeyPair: &X509KeyPair{ |
| 43 |
KeyPath: "/home/demo/.config/notation/localkeys/import.acme-rockets.key", |
| 44 |
CertificatePath: "/home/demo/.config/notation/localkeys/import.acme-rockets.crt", |
| 45 |
}, |
| 46 |
}, |
| 47 |
{ |
| 48 |
Name: "external-key", |
| 49 |
ExternalKey: &ExternalKey{ |
| 50 |
|
| 51 |
ID: "id1", |
| 52 |
PluginName: "pluginX", |
| 53 |
PluginConfig: map[string]string{ |
| 54 |
"key": "value", |
| 55 |
}, |
| 56 |
}, |
| 57 |
}, |
| 58 |
}, |
| 59 |
} |
| 60 |
|
| 61 |
func TestLoadSigningKeysInfo(t *testing.T) { |
| 62 |
t.Run("Valid", func(t *testing.T) { |
| 63 |
dir.UserConfigDir = "./testdata/valid" |
| 64 |
got, err := LoadSigningKeys() |
| 65 |
if err != nil { |
| 66 |
t.Errorf("LoadSigningKeysInfo() error = \"%v\"", err) |
| 67 |
return |
| 68 |
} |
| 69 |
|
| 70 |
if !reflect.DeepEqual(sampleSigningKeysInfo.Default, got.Default) { |
| 71 |
t.Fatal("signingKeysInfo test failed.") |
| 72 |
} |
| 73 |
|
| 74 |
if !reflect.DeepEqual(sampleSigningKeysInfo.Keys, got.Keys) { |
| 75 |
t.Fatal("signingKeysInfo test failed.") |
| 76 |
} |
| 77 |
}) |
| 78 |
|
| 79 |
t.Run("DuplicateKeys", func(t *testing.T) { |
| 80 |
expectedErr := "malformed signingkeys.json: multiple keys with name 'wabbit-networks' found" |
| 81 |
dir.UserConfigDir = "./testdata/malformed-duplicate" |
| 82 |
_, err := LoadSigningKeys() |
| 83 |
if err == nil || err.Error() != expectedErr { |
| 84 |
t.Errorf("LoadSigningKeysInfo() error expected = \"%v\" but found = \"%v\"", expectedErr, err) |
| 85 |
} |
| 86 |
}) |
| 87 |
|
| 88 |
t.Run("InvalidDefault", func(t *testing.T) { |
| 89 |
expectedErr := "malformed signingkeys.json: default key 'missing-default' not found" |
| 90 |
dir.UserConfigDir = "./testdata/malformed-invalid-default" |
| 91 |
_, err := LoadSigningKeys() |
| 92 |
if err == nil || err.Error() != expectedErr { |
| 93 |
t.Errorf("LoadSigningKeysInfo() error expected = \"%v\" but found = \"%v\"", expectedErr, err) |
| 94 |
} |
| 95 |
}) |
| 96 |
} |
| 97 |
|
| 98 |
func TestSaveSigningKeys(t *testing.T) { |
| 99 |
t.Run("Valid", func(t *testing.T) { |
| 100 |
root := t.TempDir() |
| 101 |
dir.UserConfigDir = root |
| 102 |
sampleSigningKeysInfo.Save() |
| 103 |
info, err := LoadSigningKeys() |
| 104 |
if err != nil { |
| 105 |
t.Fatal("Load signingkeys.json from temp dir failed.") |
| 106 |
} |
| 107 |
|
| 108 |
if !reflect.DeepEqual(sampleSigningKeysInfo.Default, info.Default) { |
| 109 |
t.Fatal("Save signingkeys.json failed.") |
| 110 |
} |
| 111 |
|
| 112 |
if !reflect.DeepEqual(sampleSigningKeysInfo.Keys, info.Keys) { |
| 113 |
t.Fatal("Save signingkeys.json failed.") |
| 114 |
} |
| 115 |
}) |
| 116 |
|
| 117 |
t.Run("ValidWithoutDefault", func(t *testing.T) { |
| 118 |
root := t.TempDir() |
| 119 |
dir.UserConfigDir = root |
| 120 |
sampleSigningKeysInfoNoDefault := deepCopySigningKeys(sampleSigningKeysInfo) |
| 121 |
sampleSigningKeysInfoNoDefault.Default = nil |
| 122 |
sampleSigningKeysInfoNoDefault.Save() |
| 123 |
info, err := LoadSigningKeys() |
| 124 |
if err != nil { |
| 125 |
t.Fatal("Load signingkeys.json from temp dir failed.") |
| 126 |
} |
| 127 |
|
| 128 |
if !reflect.DeepEqual(sampleSigningKeysInfoNoDefault.Default, info.Default) { |
| 129 |
t.Fatal("Save signingkeys.json failed.") |
| 130 |
} |
| 131 |
|
| 132 |
if !reflect.DeepEqual(sampleSigningKeysInfoNoDefault.Keys, info.Keys) { |
| 133 |
t.Fatal("Save signingkeys.json failed.") |
| 134 |
} |
| 135 |
}) |
| 136 |
|
| 137 |
t.Run("DuplicateKeys", func(t *testing.T) { |
| 138 |
expectedErr := "malformed signingkeys.json: multiple keys with name 'import.acme-rockets' found" |
| 139 |
dir.UserConfigDir = t.TempDir() |
| 140 |
duplicateKeySignKeysInfo := deepCopySigningKeys(sampleSigningKeysInfo) |
| 141 |
duplicateKeySignKeysInfo.Keys = append(duplicateKeySignKeysInfo.Keys, KeySuite{ |
| 142 |
Name: "import.acme-rockets", |
| 143 |
X509KeyPair: &X509KeyPair{ |
| 144 |
KeyPath: "/keypath", |
| 145 |
CertificatePath: "/CertificatePath", |
| 146 |
}, |
| 147 |
}) |
| 148 |
err := duplicateKeySignKeysInfo.Save() |
| 149 |
if err == nil || err.Error() != expectedErr { |
| 150 |
t.Errorf("Save signingkeys.json failed, error expected = \"%v\" but found = \"%v\"", expectedErr, err) |
| 151 |
} |
| 152 |
}) |
| 153 |
|
| 154 |
t.Run("EmptyKeyName", func(t *testing.T) { |
| 155 |
expectedErr := "malformed signingkeys.json: key name cannot be empty" |
| 156 |
dir.UserConfigDir = t.TempDir() |
| 157 |
emptyKeyNameSignKeysInfo := deepCopySigningKeys(sampleSigningKeysInfo) |
| 158 |
emptyKeyNameSignKeysInfo.Keys[0].Name = "" |
| 159 |
|
| 160 |
err := emptyKeyNameSignKeysInfo.Save() |
| 161 |
if err == nil || err.Error() != expectedErr { |
| 162 |
t.Errorf("Save signingkeys.json failed, error expected = \"%v\" but found = \"%v\"", expectedErr, err) |
| 163 |
} |
| 164 |
}) |
| 165 |
|
| 166 |
t.Run("InvalidDefault", func(t *testing.T) { |
| 167 |
expectedErr := "malformed signingkeys.json: default key 'missing-default' not found" |
| 168 |
dir.UserConfigDir = t.TempDir() |
| 169 |
invalidDefaultSignKeysInfo := deepCopySigningKeys(sampleSigningKeysInfo) |
| 170 |
invalidDefaultSignKeysInfo.Default = Ptr("missing-default") |
| 171 |
err := invalidDefaultSignKeysInfo.Save() |
| 172 |
if err == nil || err.Error() != expectedErr { |
| 173 |
t.Errorf("Save signingkeys.json failed, error expected = \"%v\" but found = \"%v\"", expectedErr, err) |
| 174 |
} |
| 175 |
|
| 176 |
expectedErr = "malformed signingkeys.json: default key name cannot be empty" |
| 177 |
invalidDefaultSignKeysInfo.Default = Ptr("") |
| 178 |
err = invalidDefaultSignKeysInfo.Save() |
| 179 |
if err == nil || err.Error() != expectedErr { |
| 180 |
t.Errorf("Save signingkeys.json failed, error expected = \"%v\" but found = \"%v\"", expectedErr, err) |
| 181 |
} |
| 182 |
}) |
| 183 |
} |
| 184 |
|
| 185 |
func TestAdd(t *testing.T) { |
| 186 |
certPath, keyPath := createTempCertKey(t) |
| 187 |
t.Run("WithDefault", func(t *testing.T) { |
| 188 |
testSigningKeys := deepCopySigningKeys(sampleSigningKeysInfo) |
| 189 |
expectedTestKeyName := "name1" |
| 190 |
|
| 191 |
if err := testSigningKeys.Add(expectedTestKeyName, keyPath, certPath, true); err != nil { |
| 192 |
t.Errorf("Add() failed with err= %v", err) |
| 193 |
} |
| 194 |
|
| 195 |
expectedSigningKeys := append(deepCopySigningKeys(sampleSigningKeysInfo).Keys, KeySuite{ |
| 196 |
Name: expectedTestKeyName, |
| 197 |
X509KeyPair: &X509KeyPair{ |
| 198 |
KeyPath: keyPath, |
| 199 |
CertificatePath: certPath, |
| 200 |
}, |
| 201 |
}) |
| 202 |
|
| 203 |
if expectedTestKeyName != *testSigningKeys.Default { |
| 204 |
t.Error("Add() failed, incorrect default key") |
| 205 |
} |
| 206 |
if !reflect.DeepEqual(testSigningKeys.Keys, expectedSigningKeys) { |
| 207 |
t.Error("Add() failed, KeySuite mismatch") |
| 208 |
} |
| 209 |
}) |
| 210 |
|
| 211 |
t.Run("WithoutDefault", func(t *testing.T) { |
| 212 |
dir.UserConfigDir = t.TempDir() |
| 213 |
|
| 214 |
testSigningKeys := deepCopySigningKeys(sampleSigningKeysInfo) |
| 215 |
expectedTestKeyName := "name2" |
| 216 |
certPath, keyPath := createTempCertKey(t) |
| 217 |
if err := testSigningKeys.Add(expectedTestKeyName, keyPath, certPath, false); err != nil { |
| 218 |
t.Errorf("Add() failed with err= %v", err) |
| 219 |
} |
| 220 |
|
| 221 |
expectedSigningKeys := append(deepCopySigningKeys(sampleSigningKeysInfo).Keys, KeySuite{ |
| 222 |
Name: expectedTestKeyName, |
| 223 |
X509KeyPair: &X509KeyPair{ |
| 224 |
KeyPath: keyPath, |
| 225 |
CertificatePath: certPath, |
| 226 |
}, |
| 227 |
}) |
| 228 |
|
| 229 |
if *sampleSigningKeysInfo.Default != *testSigningKeys.Default { |
| 230 |
t.Error("Add() failed, default key changed") |
| 231 |
} |
| 232 |
if !reflect.DeepEqual(testSigningKeys.Keys, expectedSigningKeys) { |
| 233 |
t.Error("Add() failed, KeySuite mismatch") |
| 234 |
} |
| 235 |
}) |
| 236 |
|
| 237 |
t.Run("InvalidCertKeyLocation", func(t *testing.T) { |
| 238 |
err := sampleSigningKeysInfo.Add("name1", "invalid", "invalid", true) |
| 239 |
if err == nil { |
| 240 |
t.Error("expected Add() to fail for invalid cert and key location") |
| 241 |
} |
| 242 |
}) |
| 243 |
|
| 244 |
t.Run("InvalidName", func(t *testing.T) { |
| 245 |
err := sampleSigningKeysInfo.Add("", "invalid", "invalid", true) |
| 246 |
if err == nil { |
| 247 |
t.Error("expected Add() to fail for empty key name") |
| 248 |
} |
| 249 |
}) |
| 250 |
|
| 251 |
t.Run("InvalidName", func(t *testing.T) { |
| 252 |
err := sampleSigningKeysInfo.Add("", "invalid", "invalid", true) |
| 253 |
if err == nil { |
| 254 |
t.Error("expected Add() to fail for empty key name") |
| 255 |
} |
| 256 |
}) |
| 257 |
|
| 258 |
t.Run("DuplicateKey", func(t *testing.T) { |
| 259 |
err := sampleSigningKeysInfo.Add(sampleSigningKeysInfo.Keys[0].Name, "invalid", "invalid", true) |
| 260 |
if err == nil { |
| 261 |
t.Error("expected Add() to fail for duplicate name") |
| 262 |
} |
| 263 |
}) |
| 264 |
} |
| 265 |
|
| 266 |
func TestPluginAdd(t *testing.T) { |
| 267 |
config := map[string]string{"key1": "value1"} |
| 268 |
name := "name1" |
| 269 |
id := "pluginId1" |
| 270 |
pluginName := "pluginName1" |
| 271 |
|
| 272 |
t.Run("InvalidCertKeyLocation", func(t *testing.T) { |
| 273 |
err := sampleSigningKeysInfo.Add("name1", "invalid", "invalid", true) |
| 274 |
if err == nil { |
| 275 |
t.Error("expected AddPlugin() to fail for invalid cert and key location") |
| 276 |
} |
| 277 |
}) |
| 278 |
|
| 279 |
t.Run("InvalidName", func(t *testing.T) { |
| 280 |
err := sampleSigningKeysInfo.AddPlugin(context.Background(), "", id, pluginName, config, true) |
| 281 |
if err == nil { |
| 282 |
t.Error("expected AddPlugin() to fail for empty key name") |
| 283 |
} |
| 284 |
}) |
| 285 |
|
| 286 |
t.Run("InvalidId", func(t *testing.T) { |
| 287 |
err := sampleSigningKeysInfo.AddPlugin(context.Background(), name, "", pluginName, config, true) |
| 288 |
if err == nil { |
| 289 |
t.Error("expected AddPlugin() to fail for empty key name") |
| 290 |
} |
| 291 |
}) |
| 292 |
|
| 293 |
t.Run("InvalidPluginName", func(t *testing.T) { |
| 294 |
err := sampleSigningKeysInfo.AddPlugin(context.Background(), name, id, "", config, true) |
| 295 |
if err == nil { |
| 296 |
t.Error("AddPlugin AddPlugin() to fail for empty plugin name") |
| 297 |
} |
| 298 |
}) |
| 299 |
} |
| 300 |
|
| 301 |
func TestGet(t *testing.T) { |
| 302 |
t.Run("Valid", func(t *testing.T) { |
| 303 |
key, err := sampleSigningKeysInfo.Get("external-key") |
| 304 |
if err != nil { |
| 305 |
t.Errorf("Get() failed with error= %v", err) |
| 306 |
} |
| 307 |
|
| 308 |
if !reflect.DeepEqual(key, sampleSigningKeysInfo.Keys[2]) { |
| 309 |
t.Errorf("Get() returned %v but expected %v", key, sampleSigningKeysInfo.Keys[2]) |
| 310 |
} |
| 311 |
}) |
| 312 |
|
| 313 |
t.Run("NonExistent", func(t *testing.T) { |
| 314 |
_, err := sampleSigningKeysInfo.Get("nonExistent") |
| 315 |
if err == nil { |
| 316 |
t.Error("expected Get() to fail for nonExistent key name") |
| 317 |
} |
| 318 |
if !errors.Is(err, KeyNotFoundError{KeyName: "nonExistent"}) { |
| 319 |
t.Error("expected Get() to return ErrorKeyNotFound") |
| 320 |
} |
| 321 |
}) |
| 322 |
|
| 323 |
t.Run("EmptyName", func(t *testing.T) { |
| 324 |
_, err := sampleSigningKeysInfo.Get("") |
| 325 |
if err == nil { |
| 326 |
t.Error("expected Get() to fail for empty key name") |
| 327 |
} |
| 328 |
if !errors.Is(err, ErrKeyNameEmpty) { |
| 329 |
t.Error("expected Get() to return ErrorKeyNameEmpty") |
| 330 |
} |
| 331 |
}) |
| 332 |
} |
| 333 |
|
| 334 |
func TestGetDefault(t *testing.T) { |
| 335 |
t.Run("Valid", func(t *testing.T) { |
| 336 |
key, err := sampleSigningKeysInfo.GetDefault() |
| 337 |
if err != nil { |
| 338 |
t.Errorf("GetDefault() failed with error= %v", err) |
| 339 |
} |
| 340 |
|
| 341 |
if !reflect.DeepEqual(key.Name, *sampleSigningKeysInfo.Default) { |
| 342 |
t.Errorf("GetDefault() returned %s but expected %s", key.Name, *sampleSigningKeysInfo.Default) |
| 343 |
} |
| 344 |
}) |
| 345 |
|
| 346 |
t.Run("NoDefault", func(t *testing.T) { |
| 347 |
testSigningKeysInfo := deepCopySigningKeys(sampleSigningKeysInfo) |
| 348 |
testSigningKeysInfo.Default = nil |
| 349 |
if _, err := testSigningKeysInfo.GetDefault(); err == nil { |
| 350 |
t.Error("GetDefault Get() to fail there is no defualt key") |
| 351 |
} |
| 352 |
}) |
| 353 |
} |
| 354 |
|
| 355 |
func TestUpdateDefault(t *testing.T) { |
| 356 |
t.Run("Valid", func(t *testing.T) { |
| 357 |
testSigningKeysInfo := deepCopySigningKeys(sampleSigningKeysInfo) |
| 358 |
newDefault := sampleSigningKeysInfo.Keys[1].Name |
| 359 |
err := testSigningKeysInfo.UpdateDefault(newDefault) |
| 360 |
if err != nil { |
| 361 |
t.Errorf("UpdateDefault() failed with error= %v", err) |
| 362 |
} |
| 363 |
|
| 364 |
if !reflect.DeepEqual(newDefault, *testSigningKeysInfo.Default) { |
| 365 |
t.Errorf("UpdateDefault() didn't update default key") |
| 366 |
} |
| 367 |
}) |
| 368 |
|
| 369 |
t.Run("NonExistent", func(t *testing.T) { |
| 370 |
err := sampleSigningKeysInfo.UpdateDefault("nonExistent") |
| 371 |
if err == nil { |
| 372 |
t.Error("expected Get() to fail for nonExistent key name") |
| 373 |
} |
| 374 |
if !errors.Is(err, KeyNotFoundError{KeyName: "nonExistent"}) { |
| 375 |
t.Error("expected Get() to return ErrorKeyNotFound") |
| 376 |
} |
| 377 |
}) |
| 378 |
|
| 379 |
t.Run("EmptyName", func(t *testing.T) { |
| 380 |
err := sampleSigningKeysInfo.UpdateDefault("") |
| 381 |
if err == nil { |
| 382 |
t.Error("expected Get() to fail for empty key name") |
| 383 |
} |
| 384 |
if !errors.Is(err, ErrKeyNameEmpty) { |
| 385 |
t.Error("expected Get() to return ErrorKeyNameEmpty") |
| 386 |
} |
| 387 |
}) |
| 388 |
} |
| 389 |
|
| 390 |
func TestRemove(t *testing.T) { |
| 391 |
testKeyName := "wabbit-networks" |
| 392 |
testSigningKeysInfo := deepCopySigningKeys(sampleSigningKeysInfo) |
| 393 |
t.Run("Valid", func(t *testing.T) { |
| 394 |
keys, err := testSigningKeysInfo.Remove(testKeyName) |
| 395 |
if err != nil { |
| 396 |
t.Errorf("testSigningKeysInfo() failed with error= %v", err) |
| 397 |
} |
| 398 |
|
| 399 |
if _, err := testSigningKeysInfo.Get(testKeyName); err == nil { |
| 400 |
t.Error("Delete() filed to delete key") |
| 401 |
} |
| 402 |
if keys[0] != testKeyName { |
| 403 |
t.Error("Delete() deleted key name mismatch") |
| 404 |
} |
| 405 |
}) |
| 406 |
|
| 407 |
t.Run("NonExistent", func(t *testing.T) { |
| 408 |
_, err := testSigningKeysInfo.Remove("nonExistent") |
| 409 |
if err == nil { |
| 410 |
t.Error("expected Get() to fail for nonExistent key name") |
| 411 |
} |
| 412 |
if !errors.Is(err, KeyNotFoundError{KeyName: "nonExistent"}) { |
| 413 |
t.Error("expected Get() to return ErrorKeyNotFound") |
| 414 |
} |
| 415 |
}) |
| 416 |
|
| 417 |
t.Run("EmptyName", func(t *testing.T) { |
| 418 |
_, err := testSigningKeysInfo.Remove("") |
| 419 |
if err == nil { |
| 420 |
t.Error("expected Get() to fail for empty key name") |
| 421 |
} |
| 422 |
if !errors.Is(err, ErrKeyNameEmpty) { |
| 423 |
t.Error("expected Get() to return ErrorKeyNameEmpty") |
| 424 |
} |
| 425 |
}) |
| 426 |
} |
| 427 |
|
| 428 |
func deepCopySigningKeys(keys SigningKeys) SigningKeys { |
| 429 |
cpyKeys := make([]KeySuite, len(sampleSigningKeysInfo.Keys)) |
| 430 |
copy(cpyKeys, keys.Keys) |
| 431 |
cpyDefault := *keys.Default |
| 432 |
cpySignKeys := keys |
| 433 |
cpySignKeys.Default = &cpyDefault |
| 434 |
cpySignKeys.Keys = cpyKeys |
| 435 |
return cpySignKeys |
| 436 |
} |
| 437 |
|
| 438 |
func Ptr[T any](v T) *T { |
| 439 |
return &v |
| 440 |
} |
| 441 |
|
| 442 |
func createTempCertKey(t *testing.T) (string, string) { |
| 443 |
certTuple := testhelper.GetRSARootCertificate() |
| 444 |
certPath := filepath.Join(t.TempDir(), "cert.tmp") |
| 445 |
certData := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certTuple.Cert.Raw}) |
| 446 |
if err := os.WriteFile(certPath, certData, 0600); err != nil { |
| 447 |
panic(err) |
| 448 |
} |
| 449 |
keyPath := filepath.Join(t.TempDir(), "key.tmp") |
| 450 |
keyBytes, _ := x509.MarshalPKCS8PrivateKey(certTuple.PrivateKey) |
| 451 |
keyData := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyBytes}) |
| 452 |
if err := os.WriteFile(keyPath, keyData, 0600); err != nil { |
| 453 |
panic(err) |
| 454 |
} |
| 455 |
return certPath, keyPath |
| 456 |
} |