|
| 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: `talm apply --dry-run` prints two diffs — talm's own structured |
| 16 | +// drift preview (redacted by path + user value) AND the server-returned |
| 17 | +// ModeDetails diff. The second one is opaque text, so it is redacted by VALUE. |
| 18 | +// These tests pin that ModeDetails never leaks a Talos bootstrap secret or a |
| 19 | +// user encrypted-value secret unless --show-secrets-in-drift is set. |
| 20 | + |
| 21 | +package commands |
| 22 | + |
| 23 | +import ( |
| 24 | + "bytes" |
| 25 | + "strings" |
| 26 | + "testing" |
| 27 | + |
| 28 | + machineapi "github.com/siderolabs/talos/pkg/machinery/api/machine" |
| 29 | +) |
| 30 | + |
| 31 | +// TestContract_CollectConfigSecretValues pins which rendered-config leaves feed |
| 32 | +// the ModeDetails redaction set: Talos bootstrap material (fixed paths) and |
| 33 | +// Wireguard key material (by leaf name), but NOT ordinary public fields. |
| 34 | +func TestContract_CollectConfigSecretValues(t *testing.T) { |
| 35 | + rendered := []byte(`machine: |
| 36 | + type: controlplane |
| 37 | + token: MACHINE-TOKEN-AAAA |
| 38 | + ca: |
| 39 | + crt: MACHINE-CA-CRT |
| 40 | + key: MACHINE-CA-KEY-BBBB |
| 41 | + network: |
| 42 | + hostname: node0 |
| 43 | + interfaces: |
| 44 | + - interface: eth0 |
| 45 | + wireguard: |
| 46 | + privateKey: WG-PRIVATE-CCCC |
| 47 | + peers: |
| 48 | + - publicKey: WG-PUBLIC-PUB |
| 49 | + presharedKey: WG-PSK-DDDD |
| 50 | + allowedIPs: |
| 51 | + - 10.0.0.0/8 |
| 52 | +cluster: |
| 53 | + secret: CLUSTER-SECRET-EEEE |
| 54 | + token: CLUSTER-TOKEN-FFFF |
| 55 | + ca: |
| 56 | + crt: CLUSTER-CA-CRT |
| 57 | + key: CLUSTER-CA-KEY-GGGG |
| 58 | + acceptedCAs: |
| 59 | + - crt: ACCEPTED-CA-CRT |
| 60 | + key: ACCEPTED-CA-KEY-HHHH |
| 61 | +`) |
| 62 | + |
| 63 | + got, err := collectConfigSecretValues(rendered) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("collectConfigSecretValues: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + mustCollect := []string{ |
| 69 | + "MACHINE-TOKEN-AAAA", |
| 70 | + "MACHINE-CA-KEY-BBBB", |
| 71 | + "WG-PRIVATE-CCCC", |
| 72 | + "WG-PSK-DDDD", |
| 73 | + "CLUSTER-SECRET-EEEE", |
| 74 | + "CLUSTER-TOKEN-FFFF", |
| 75 | + "CLUSTER-CA-KEY-GGGG", |
| 76 | + "ACCEPTED-CA-KEY-HHHH", |
| 77 | + } |
| 78 | + for _, want := range mustCollect { |
| 79 | + if _, ok := got[want]; !ok { |
| 80 | + t.Errorf("secret value %q must be collected for ModeDetails redaction; got %v", want, keysOf(got)) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Public / low-entropy fields must NOT enter the value set, so they stay |
| 85 | + // readable in the dry-run diff and never clobber unrelated text. |
| 86 | + mustNotCollect := []string{"node0", "eth0", "WG-PUBLIC-PUB", "10.0.0.0/8"} |
| 87 | + for _, unwanted := range mustNotCollect { |
| 88 | + if _, ok := got[unwanted]; ok { |
| 89 | + t.Errorf("non-secret field %q must NOT be collected (would over-redact the dry-run diff)", unwanted) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// TestContract_CollectConfigSecretValues_AcceptedCAsCrt pins the documented |
| 95 | +// bounded over-collection: acceptedCAs is an allowlisted slice path, so its |
| 96 | +// public crt is collected alongside the key. This is intentional — the slice is |
| 97 | +// redacted whole — and pinned so a future change to the granularity is a |
| 98 | +// conscious decision. |
| 99 | +func TestContract_CollectConfigSecretValues_AcceptedCAsCrt(t *testing.T) { |
| 100 | + rendered := []byte(`cluster: |
| 101 | + acceptedCAs: |
| 102 | + - crt: ACCEPTED-CA-CRT-XXXX |
| 103 | + key: ACCEPTED-CA-KEY-YYYY |
| 104 | +`) |
| 105 | + |
| 106 | + got, err := collectConfigSecretValues(rendered) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("collectConfigSecretValues: %v", err) |
| 109 | + } |
| 110 | + |
| 111 | + if _, ok := got["ACCEPTED-CA-CRT-XXXX"]; !ok { |
| 112 | + t.Error("acceptedCAs crt is collected whole with the slice (documented bounded over-collection)") |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// TestContract_RedactValuesInText pins the by-value text masking used on |
| 117 | +// ModeDetails: every occurrence of every secret value becomes the sentinel, an |
| 118 | +// empty value set is a no-op (the --show-secrets-in-drift path), and a value |
| 119 | +// that is a substring of another does not survive as a fragment. |
| 120 | +func TestContract_RedactValuesInText(t *testing.T) { |
| 121 | + text := "value: high-entropy-secret\nother: high-entropy-secret-LONGER\n" |
| 122 | + values := secretSetOf("high-entropy-secret", "high-entropy-secret-LONGER") |
| 123 | + |
| 124 | + got := redactValuesInText(text, values) |
| 125 | + if strings.Contains(got, "high-entropy-secret") { |
| 126 | + t.Errorf("no secret fragment may survive redaction:\n%s", got) |
| 127 | + } |
| 128 | + if !strings.Contains(got, modeDetailsRedactionSentinel) { |
| 129 | + t.Errorf("redacted text must carry the sentinel:\n%s", got) |
| 130 | + } |
| 131 | + |
| 132 | + if redactValuesInText(text, nil) != text { |
| 133 | + t.Error("empty value set must leave the text verbatim (show-secrets path)") |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// TestContract_PrintApplyResultsRedacted pins the end-to-end print path: a |
| 138 | +// ModeDetails diff carrying a secret is masked when the value is in scope and |
| 139 | +// printed verbatim when the value set is empty (show-secrets). |
| 140 | +func TestContract_PrintApplyResultsRedacted(t *testing.T) { |
| 141 | + const secret = "high-entropy-value-do-not-collide-abcdef0123456789" |
| 142 | + resp := &machineapi.ApplyConfigurationResponse{ |
| 143 | + Messages: []*machineapi.ApplyConfiguration{ |
| 144 | + {ModeDetails: "Config diff:\n+ value: " + secret + "\n"}, |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + var redacted bytes.Buffer |
| 149 | + printApplyResultsRedacted(resp, secretSetOf(secret), &redacted) |
| 150 | + if strings.Contains(redacted.String(), secret) { |
| 151 | + t.Errorf("ModeDetails must redact an in-scope secret:\n%s", redacted.String()) |
| 152 | + } |
| 153 | + |
| 154 | + var shown bytes.Buffer |
| 155 | + printApplyResultsRedacted(resp, nil, &shown) |
| 156 | + if !strings.Contains(shown.String(), secret) { |
| 157 | + t.Errorf("empty value set (show-secrets) must print ModeDetails verbatim:\n%s", shown.String()) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func keysOf(m map[string]struct{}) []string { |
| 162 | + out := make([]string, 0, len(m)) |
| 163 | + for k := range m { |
| 164 | + out = append(out, k) |
| 165 | + } |
| 166 | + |
| 167 | + return out |
| 168 | +} |
0 commit comments