|
| 1 | +// Copyright Cozystack Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Contract: edge cases for the incremental encrypt/decrypt machinery |
| 16 | +// in age.go. EncryptSecretsFile uses mergeAndEncryptYAMLValues to |
| 17 | +// preserve unchanged ciphertext; the round-trip pin sits in |
| 18 | +// contract_test.go. This file exercises the type-mismatch / new-key |
| 19 | +// / new-list-element / type-changed branches of the recursive merger |
| 20 | +// — the surface area that decides what does and does NOT get |
| 21 | +// re-encrypted. |
| 22 | + |
| 23 | +package age |
| 24 | + |
| 25 | +import ( |
| 26 | + "os" |
| 27 | + "path/filepath" |
| 28 | + "strings" |
| 29 | + "testing" |
| 30 | + |
| 31 | + "gopkg.in/yaml.v3" |
| 32 | +) |
| 33 | + |
| 34 | +// === decryptYAMLValuesString === |
| 35 | + |
| 36 | +// Contract: a string already wrapped in the ENC[AGE,data:...] envelope |
| 37 | +// is decrypted to the plaintext it originally encoded. |
| 38 | +func TestContract_DecryptYAMLValuesString_RoundTrip(t *testing.T) { |
| 39 | + dir := t.TempDir() |
| 40 | + id, _, err := GenerateKey(dir) |
| 41 | + if err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + encrypted, err := encryptString("hello", id.Recipient()) |
| 45 | + if err != nil { |
| 46 | + t.Fatal(err) |
| 47 | + } |
| 48 | + envelope := ageEncryptionPrefix + encrypted + ageEncryptionSuffix |
| 49 | + |
| 50 | + got, err := decryptYAMLValuesString(envelope, id) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("decryptYAMLValuesString: %v", err) |
| 53 | + } |
| 54 | + if got != "hello" { |
| 55 | + t.Errorf("expected 'hello', got %q", got) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Contract: a string that is NOT wrapped in the envelope is returned |
| 60 | +// verbatim. This lets the merge logic feed both raw plaintext and |
| 61 | +// already-encrypted values through the same helper without a type |
| 62 | +// switch at the call site. |
| 63 | +func TestContract_DecryptYAMLValuesString_PassthroughForUnencrypted(t *testing.T) { |
| 64 | + dir := t.TempDir() |
| 65 | + id, _, err := GenerateKey(dir) |
| 66 | + if err != nil { |
| 67 | + t.Fatal(err) |
| 68 | + } |
| 69 | + got, err := decryptYAMLValuesString("just-a-plain-string", id) |
| 70 | + if err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + if got != "just-a-plain-string" { |
| 74 | + t.Errorf("expected passthrough, got %q", got) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// Contract: a string with the envelope prefix but corrupted base64 |
| 79 | +// payload errors precisely. Pin so a regression that swallows the |
| 80 | +// inner error and silently returns the corrupted string surfaces. |
| 81 | +func TestContract_DecryptYAMLValuesString_CorruptedEnvelope(t *testing.T) { |
| 82 | + dir := t.TempDir() |
| 83 | + id, _, err := GenerateKey(dir) |
| 84 | + if err != nil { |
| 85 | + t.Fatal(err) |
| 86 | + } |
| 87 | + corrupted := ageEncryptionPrefix + "not-valid-base64!!!" + ageEncryptionSuffix |
| 88 | + _, err = decryptYAMLValuesString(corrupted, id) |
| 89 | + if err == nil { |
| 90 | + t.Fatal("expected error for corrupted envelope") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// === mergeAndEncryptYAMLValues — through EncryptSecretsFile === |
| 95 | + |
| 96 | +// Contract: a NEW key added to plain secrets.yaml gets encrypted on |
| 97 | +// the next encrypt round; existing keys keep their byte-stable |
| 98 | +// ciphertext. |
| 99 | +func TestContract_IncrementalEncrypt_NewKeyEncryptedOldKeyStable(t *testing.T) { |
| 100 | + dir := t.TempDir() |
| 101 | + if err := writeYAML(dir, "secrets.yaml", "a: alpha\n"); err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + if err := EncryptSecretsFile(dir); err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + first := readYAML(t, dir, "secrets.encrypted.yaml") |
| 108 | + |
| 109 | + // Add a new key b. |
| 110 | + if err := writeYAML(dir, "secrets.yaml", "a: alpha\nb: beta\n"); err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + if err := EncryptSecretsFile(dir); err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + second := readYAML(t, dir, "secrets.encrypted.yaml") |
| 117 | + |
| 118 | + // a's ciphertext must stay byte-stable. |
| 119 | + if first["a"] != second["a"] { |
| 120 | + t.Errorf("a's ciphertext rotated unnecessarily\n first: %v\n second: %v", first["a"], second["a"]) |
| 121 | + } |
| 122 | + // b must be present and encrypted. |
| 123 | + bStr, ok := second["b"].(string) |
| 124 | + if !ok { |
| 125 | + t.Fatalf("b missing or not a string: %v", second["b"]) |
| 126 | + } |
| 127 | + if !strings.HasPrefix(bStr, ageEncryptionPrefix) { |
| 128 | + t.Errorf("b not encrypted: %q", bStr) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// Contract: a deeply-nested change re-encrypts only the innermost |
| 133 | +// changed leaf. Sibling values up and down the tree stay byte-stable. |
| 134 | +func TestContract_IncrementalEncrypt_NestedChangeLocalised(t *testing.T) { |
| 135 | + dir := t.TempDir() |
| 136 | + initial := `top: |
| 137 | + unchanged: keep-me |
| 138 | + sub: |
| 139 | + inner1: original |
| 140 | + inner2: keep-me-too |
| 141 | +` |
| 142 | + if err := writeYAML(dir, "secrets.yaml", initial); err != nil { |
| 143 | + t.Fatal(err) |
| 144 | + } |
| 145 | + if err := EncryptSecretsFile(dir); err != nil { |
| 146 | + t.Fatal(err) |
| 147 | + } |
| 148 | + first := readYAML(t, dir, "secrets.encrypted.yaml") |
| 149 | + |
| 150 | + // Change top.sub.inner1 only. |
| 151 | + changed := `top: |
| 152 | + unchanged: keep-me |
| 153 | + sub: |
| 154 | + inner1: NEW |
| 155 | + inner2: keep-me-too |
| 156 | +` |
| 157 | + if err := writeYAML(dir, "secrets.yaml", changed); err != nil { |
| 158 | + t.Fatal(err) |
| 159 | + } |
| 160 | + if err := EncryptSecretsFile(dir); err != nil { |
| 161 | + t.Fatal(err) |
| 162 | + } |
| 163 | + second := readYAML(t, dir, "secrets.encrypted.yaml") |
| 164 | + |
| 165 | + // Walk both trees, compare leaf-by-leaf. |
| 166 | + firstTop := first["top"].(map[string]any) |
| 167 | + secondTop := second["top"].(map[string]any) |
| 168 | + if firstTop["unchanged"] != secondTop["unchanged"] { |
| 169 | + t.Errorf("top.unchanged ciphertext rotated unnecessarily") |
| 170 | + } |
| 171 | + firstSub := firstTop["sub"].(map[string]any) |
| 172 | + secondSub := secondTop["sub"].(map[string]any) |
| 173 | + if firstSub["inner2"] != secondSub["inner2"] { |
| 174 | + t.Errorf("top.sub.inner2 ciphertext rotated unnecessarily") |
| 175 | + } |
| 176 | + if firstSub["inner1"] == secondSub["inner1"] { |
| 177 | + t.Error("top.sub.inner1 ciphertext did NOT rotate after plaintext change") |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +// Contract: when the encrypted-file structure differs from the |
| 182 | +// plain-file structure (type changed at a key — scalar -> map, or |
| 183 | +// vice versa), the affected branch is fully re-encrypted from |
| 184 | +// scratch. The chart's incremental rule degrades gracefully. |
| 185 | +func TestContract_IncrementalEncrypt_TypeChangeFallsBackToFullEncrypt(t *testing.T) { |
| 186 | + dir := t.TempDir() |
| 187 | + // Encrypted file holds scalar at k. |
| 188 | + if err := writeYAML(dir, "secrets.yaml", "k: scalar-value\n"); err != nil { |
| 189 | + t.Fatal(err) |
| 190 | + } |
| 191 | + if err := EncryptSecretsFile(dir); err != nil { |
| 192 | + t.Fatal(err) |
| 193 | + } |
| 194 | + |
| 195 | + // Plaintext now has k as a map. |
| 196 | + if err := writeYAML(dir, "secrets.yaml", "k:\n nested: value\n"); err != nil { |
| 197 | + t.Fatal(err) |
| 198 | + } |
| 199 | + if err := EncryptSecretsFile(dir); err != nil { |
| 200 | + t.Fatalf("re-encrypt with type change: %v", err) |
| 201 | + } |
| 202 | + |
| 203 | + out := readYAML(t, dir, "secrets.encrypted.yaml") |
| 204 | + kMap, ok := out["k"].(map[string]any) |
| 205 | + if !ok { |
| 206 | + t.Fatalf("expected k to be map after type change, got %T (%v)", out["k"], out["k"]) |
| 207 | + } |
| 208 | + nested, ok := kMap["nested"].(string) |
| 209 | + if !ok || !strings.HasPrefix(nested, ageEncryptionPrefix) { |
| 210 | + t.Errorf("expected k.nested to be an encrypted string, got %v", kMap["nested"]) |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +// Contract: when a list value's length changes, the entire list is |
| 215 | +// re-encrypted. Per-element merge requires a stable index mapping, |
| 216 | +// which a length change invalidates. |
| 217 | +func TestContract_IncrementalEncrypt_ListLengthChangeFullReencrypt(t *testing.T) { |
| 218 | + dir := t.TempDir() |
| 219 | + if err := writeYAML(dir, "secrets.yaml", "items:\n - one\n - two\n"); err != nil { |
| 220 | + t.Fatal(err) |
| 221 | + } |
| 222 | + if err := EncryptSecretsFile(dir); err != nil { |
| 223 | + t.Fatal(err) |
| 224 | + } |
| 225 | + first := readYAML(t, dir, "secrets.encrypted.yaml") |
| 226 | + |
| 227 | + if err := writeYAML(dir, "secrets.yaml", "items:\n - one\n - two\n - three\n"); err != nil { |
| 228 | + t.Fatal(err) |
| 229 | + } |
| 230 | + if err := EncryptSecretsFile(dir); err != nil { |
| 231 | + t.Fatal(err) |
| 232 | + } |
| 233 | + second := readYAML(t, dir, "secrets.encrypted.yaml") |
| 234 | + |
| 235 | + firstItems := first["items"].([]any) |
| 236 | + secondItems := second["items"].([]any) |
| 237 | + if len(secondItems) != 3 { |
| 238 | + t.Fatalf("expected 3 items after append, got %d", len(secondItems)) |
| 239 | + } |
| 240 | + // Existing items[0]/items[1] are EXPECTED to rotate ciphertext — |
| 241 | + // the list-length-changed branch re-encrypts the full slice. |
| 242 | + // Pin both that the new length holds AND that all entries are |
| 243 | + // freshly encrypted (envelope-wrapped). |
| 244 | + for i, e := range secondItems { |
| 245 | + s, ok := e.(string) |
| 246 | + if !ok || !strings.HasPrefix(s, ageEncryptionPrefix) { |
| 247 | + t.Errorf("items[%d] not encrypted: %v", i, e) |
| 248 | + } |
| 249 | + } |
| 250 | + _ = firstItems // referenced here to document the intentional non-comparison |
| 251 | +} |
| 252 | + |
| 253 | +// Contract: when an existing list element changes value but the |
| 254 | +// length stays the same, only that element re-encrypts; siblings |
| 255 | +// stay byte-stable. |
| 256 | +func TestContract_IncrementalEncrypt_ListSameLengthLocalised(t *testing.T) { |
| 257 | + dir := t.TempDir() |
| 258 | + if err := writeYAML(dir, "secrets.yaml", "items:\n - alpha\n - bravo\n - charlie\n"); err != nil { |
| 259 | + t.Fatal(err) |
| 260 | + } |
| 261 | + if err := EncryptSecretsFile(dir); err != nil { |
| 262 | + t.Fatal(err) |
| 263 | + } |
| 264 | + first := readYAML(t, dir, "secrets.encrypted.yaml") |
| 265 | + |
| 266 | + if err := writeYAML(dir, "secrets.yaml", "items:\n - alpha\n - DELTA\n - charlie\n"); err != nil { |
| 267 | + t.Fatal(err) |
| 268 | + } |
| 269 | + if err := EncryptSecretsFile(dir); err != nil { |
| 270 | + t.Fatal(err) |
| 271 | + } |
| 272 | + second := readYAML(t, dir, "secrets.encrypted.yaml") |
| 273 | + |
| 274 | + firstItems := first["items"].([]any) |
| 275 | + secondItems := second["items"].([]any) |
| 276 | + if firstItems[0] != secondItems[0] { |
| 277 | + t.Errorf("items[0] rotated unnecessarily") |
| 278 | + } |
| 279 | + if firstItems[2] != secondItems[2] { |
| 280 | + t.Errorf("items[2] rotated unnecessarily") |
| 281 | + } |
| 282 | + if firstItems[1] == secondItems[1] { |
| 283 | + t.Errorf("items[1] did NOT rotate after value change") |
| 284 | + } |
| 285 | +} |
| 286 | + |
| 287 | +// === helpers === |
| 288 | + |
| 289 | +func writeYAML(dir, name, body string) error { |
| 290 | + return os.WriteFile(filepath.Join(dir, name), []byte(body), 0o600) |
| 291 | +} |
| 292 | + |
| 293 | +func readYAML(t *testing.T, dir, name string) map[string]any { |
| 294 | + t.Helper() |
| 295 | + data, err := os.ReadFile(filepath.Join(dir, name)) |
| 296 | + if err != nil { |
| 297 | + t.Fatalf("read %s: %v", name, err) |
| 298 | + } |
| 299 | + var out map[string]any |
| 300 | + if err := yaml.Unmarshal(data, &out); err != nil { |
| 301 | + t.Fatalf("unmarshal %s: %v", name, err) |
| 302 | + } |
| 303 | + return out |
| 304 | +} |
0 commit comments