Skip to content

Commit 03d1466

Browse files
committed
fix(apply): redact secrets that live in typed documents
The drift preview and the dry-run diff the node returns mask secrets by a list of v1alpha1 paths. From contract v1.14 the generator puts the Kubernetes CA keys, the service-account key, the etcd encryption keys and the discovery secret in typed documents instead, so `talm apply --dry-run` against a node with different secrets printed them in the clear. Registry credentials, the client key of RegistryTLSConfig, the HCloud API token, SideroLink tokens and volume encryption passphrases leaked the same way on every contract that has those documents. Secret fields are now also listed per typed document kind, from what each document's Talos Redact method masks: a field name alone cannot tell KubeEtcdEncryptionConfig's `config` from KubeletConfig's. A test generates a config, lets Talos redact it, and requires every field Talos masked to be masked in both outputs. In the typed documents the CA and service-account keys are multi-line PEM, and the node's diff prints each line of a key on a line of its own behind a `+`/`-` marker and the indentation, so the whole value never matched. Lines of multi-line secrets are now masked one diff line at a time; the PEM armor lines stay. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 0548298 commit 03d1466

8 files changed

Lines changed: 473 additions & 19 deletions

‎docs/manual-test-plan.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,8 @@ Expected: the drift preview line for `machine.token` reads `machine.token: ***re
825825

826826
Regression anchor: rotating any field in the allowlist (`cluster.{secret,token,aescbcEncryptionSecret,secretboxEncryptionSecret}`, `cluster.{ca,aggregatorCA,serviceAccount,etcd.ca}.key`, `cluster.acceptedCAs[].key`, `machine.{token,ca.key}`, `machine.acceptedCAs[].key`) MUST redact. A regression that silently leaks a secret value into stderr is a security-class bug — verify the substring with `grep -F` against the captured output.
827827

828+
On a project pinned at `talosVersion: "v1.14"` the same material lives in typed documents. Regenerate `secrets.yaml` (keep a copy of the original) and run `talm apply --dry-run -f nodes/node0.yaml`. Expected: the `~ KubeAPIServerCAConfig`, `~ KubeAggregatorCAConfig`, `~ KubeServiceAccountConfig`, `~ KubeEtcdEncryptionConfig` and `~ DiscoveryIdentityConfig` entries show `issuingCA.key`, `issuer.privateKey`, `config.resources` and `clusterSecret` as `***redacted (len=N)***`, and no key from either `secrets.yaml` appears anywhere in stderr, the `Config diff:` block included. Restore the original `secrets.yaml` before applying anything.
829+
828830
### C5a. Drift preview redacts user secret values (encrypted value files)
829831

830832
With an encrypted value file in scope (B2a) injecting a secret into a NON-allowlisted field (e.g. a `machine.pods[]` env, or `machine.registries.config.<reg>.auth.password`):
@@ -1080,7 +1082,7 @@ The Phase 1 walker validates the syntactic shape of net-addr fields in three v1a
10801082

10811083
#### Secret-bearing field redaction
10821084

1083-
The drift preview redacts allowlisted paths by default. The opt-out is operator-explicit: `--show-secrets-in-drift`. Allowlist lives in `secretFieldPaths` (`pkg/commands/preflight_apply_safety_redact.go`).
1085+
The drift preview redacts allowlisted paths by default. The opt-out is operator-explicit: `--show-secrets-in-drift`. Allowlist lives in `secretFieldPaths` and, per typed document kind, `secretTypedFieldPaths` (`pkg/commands/preflight_apply_safety_redact.go`); `TestDriftPreview_MasksEverySecretTalosRedacts` and `TestModeDetails_CollectsEverySecretTalosRedacts` check both against what Talos itself redacts.
10841086

10851087
| Case | Trigger | Expected |
10861088
| --- | --- | --- |

‎docs/reference/apply.md‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/commands/apply.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ func init() {
12491249
applyCmd.Flags().BoolVar(&applyCmdFlags.skipResourceValidation, "skip-resource-validation", false, "skip the pre-apply check that declared host resources (links, disks) exist on the target node")
12501250
applyCmd.Flags().BoolVar(&applyCmdFlags.skipDriftPreview, "skip-drift-preview", false, "skip the pre-apply diff of on-node vs rendered MachineConfig")
12511251
applyCmd.Flags().BoolVar(&applyCmdFlags.skipPostApplyVerify, "skip-post-apply-verify", true, "skip the post-apply structural verification of on-node vs sent MachineConfig (default skip until the Talos-mutated field allowlist lands)")
1252-
applyCmd.Flags().BoolVar(&applyCmdFlags.showSecretsInDrift, "show-secrets-in-drift", false, "show secret-bearing field values verbatim in drift preview / post-apply verify output (default: redacted). Covers both the Talos bootstrap allowlist (cluster.token, cluster.ca.key, machine.token, Wireguard private keys, etc.) and values from encrypted value files (*.encrypted.yaml). Counterpart on template is --show-secrets, which governs the same values in template's stdout render.")
1252+
applyCmd.Flags().BoolVar(&applyCmdFlags.showSecretsInDrift, "show-secrets-in-drift", false, "show secret-bearing field values verbatim in drift preview / post-apply verify output (default: redacted). Covers the fields Talos treats as secret (cluster.token, cluster.ca.key, machine.token, Wireguard private keys, the CA, service-account and etcd encryption keys in their own documents from Talos v1.14, registry credentials, etc.) and values from encrypted value files (*.encrypted.yaml). Counterpart on template is --show-secrets, which governs the same values in template's stdout render.")
12531253
helpers.AddModeFlags(&applyCmdFlags.Mode, applyCmd)
12541254

12551255
// Shell completion for `talm apply` flags. `--file` returns the
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
package commands
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"strings"
8+
"testing"
9+
10+
"github.com/cockroachdb/errors"
11+
"github.com/siderolabs/talos/pkg/machinery/config/configloader"
12+
"github.com/siderolabs/talos/pkg/machinery/config/encoder"
13+
"github.com/siderolabs/talos/pkg/machinery/config/machine"
14+
"gopkg.in/yaml.v3"
15+
16+
"github.com/cozystack/talm/pkg/applycheck"
17+
"github.com/cozystack/talm/pkg/engine"
18+
)
19+
20+
const talosRedactionMarker = "TALM-TEST-REDACTED"
21+
22+
// typedSecretDocs carries one secret in every typed document kind whose
23+
// Redact method masks something, beyond the ones the generator emits.
24+
const typedSecretDocs = `---
25+
apiVersion: v1alpha1
26+
kind: RegistryAuthConfig
27+
name: registry.example.com
28+
username: operator
29+
password: registry-password-value
30+
---
31+
apiVersion: v1alpha1
32+
kind: RegistryTLSConfig
33+
name: tls.example.com
34+
clientIdentity:
35+
cert: registry-client-crt-value
36+
key: registry-client-key-value
37+
---
38+
apiVersion: v1alpha1
39+
kind: HCloudVIPConfig
40+
name: hcloud-vip
41+
link: eth0
42+
apiToken: hcloud-token-value
43+
---
44+
apiVersion: v1alpha1
45+
kind: UserVolumeConfig
46+
name: data
47+
provisioning:
48+
diskSelector:
49+
match: "!system_disk"
50+
encryption:
51+
provider: luks2
52+
keys:
53+
- slot: 0
54+
static:
55+
passphrase: volume-passphrase-value
56+
`
57+
58+
// v114ControlPlaneWithSecrets renders a v1.14 control-plane config the way
59+
// talm does, with its own freshly generated secrets, plus typedSecretDocs.
60+
func v114ControlPlaneWithSecrets(t *testing.T) []byte {
61+
t.Helper()
62+
63+
configBundle, err := engine.InitializeConfigBundle(engine.Options{TalosVersion: "v1.14", KubernetesVersion: "v1.34.3"})
64+
if err != nil {
65+
t.Fatalf("InitializeConfigBundle: %v", err)
66+
}
67+
68+
out, err := configBundle.Serialize(encoder.CommentsDisabled, machine.TypeControlPlane)
69+
if err != nil {
70+
t.Fatalf("Serialize: %v", err)
71+
}
72+
73+
return append(out, []byte(typedSecretDocs)...)
74+
}
75+
76+
// talosSecretLeaves returns every string leaf Talos itself masks in cfgBytes,
77+
// as kind -> dotted path -> value, by diffing the config against its own
78+
// RedactSecrets copy.
79+
func talosSecretLeaves(t *testing.T, cfgBytes []byte) map[string]map[string]string {
80+
t.Helper()
81+
82+
cfg, err := configloader.NewFromBytes(cfgBytes)
83+
if err != nil {
84+
t.Fatalf("loading config: %v", err)
85+
}
86+
87+
redactedBytes, err := cfg.RedactSecrets(talosRedactionMarker).EncodeBytes(encoder.WithComments(encoder.CommentsDisabled))
88+
if err != nil {
89+
t.Fatalf("encoding redacted config: %v", err)
90+
}
91+
92+
originalBytes, err := cfg.EncodeBytes(encoder.WithComments(encoder.CommentsDisabled))
93+
if err != nil {
94+
t.Fatalf("encoding config: %v", err)
95+
}
96+
97+
original, redacted := decodeAll(t, originalBytes), decodeAll(t, redactedBytes)
98+
if len(original) != len(redacted) {
99+
t.Fatalf("redaction changed the document count: %d -> %d", len(original), len(redacted))
100+
}
101+
102+
out := map[string]map[string]string{}
103+
104+
for i := range original {
105+
kind, _ := original[i]["kind"].(string)
106+
if kind == "" {
107+
kind = "MachineConfig"
108+
}
109+
110+
collectChangedLeaves(original[i], redacted[i], "", func(path, value string) {
111+
if out[kind] == nil {
112+
out[kind] = map[string]string{}
113+
}
114+
115+
out[kind][path] = value
116+
})
117+
}
118+
119+
return out
120+
}
121+
122+
func decodeAll(t *testing.T, data []byte) []map[string]any {
123+
t.Helper()
124+
125+
var docs []map[string]any
126+
127+
dec := yaml.NewDecoder(bytes.NewReader(data))
128+
129+
for {
130+
var doc map[string]any
131+
132+
err := dec.Decode(&doc)
133+
if errors.Is(err, io.EOF) {
134+
return docs
135+
}
136+
137+
if err != nil {
138+
t.Fatalf("decoding config: %v", err)
139+
}
140+
141+
if doc != nil {
142+
docs = append(docs, doc)
143+
}
144+
}
145+
}
146+
147+
func collectChangedLeaves(original, redacted any, path string, add func(path, value string)) {
148+
switch o := original.(type) {
149+
case map[string]any:
150+
r, _ := redacted.(map[string]any)
151+
for key, value := range o {
152+
child := key
153+
if path != "" {
154+
child = path + "." + key
155+
}
156+
157+
collectChangedLeaves(value, r[key], child, add)
158+
}
159+
case []any:
160+
r, _ := redacted.([]any)
161+
for i, value := range o {
162+
var rv any
163+
if i < len(r) {
164+
rv = r[i]
165+
}
166+
167+
collectChangedLeaves(value, rv, fmt.Sprintf("%s[%d]", path, i), add)
168+
}
169+
case string:
170+
if r, _ := redacted.(string); r != o {
171+
add(path, o)
172+
}
173+
}
174+
}
175+
176+
// Every field Talos itself treats as secret has to be masked in the drift
177+
// preview, whatever document carries it. From contract v1.14 the CA keys,
178+
// the service-account key, the etcd encryption keys and the discovery
179+
// secret live in typed documents, and a path list written for v1alpha1
180+
// printed them in the clear.
181+
func TestDriftPreview_MasksEverySecretTalosRedacts(t *testing.T) {
182+
current := v114ControlPlaneWithSecrets(t)
183+
desired := v114ControlPlaneWithSecrets(t)
184+
185+
changes, err := applycheck.Diff(current, desired)
186+
if err != nil {
187+
t.Fatalf("Diff: %v", err)
188+
}
189+
190+
buf := &bytes.Buffer{}
191+
printDriftPreview(buf, "drift:", changes, secretRedactor{})
192+
preview := buf.String()
193+
194+
for _, cfg := range [][]byte{current, desired} {
195+
for kind, leaves := range talosSecretLeaves(t, cfg) {
196+
for path, value := range leaves {
197+
if strings.Contains(preview, value) {
198+
t.Errorf("%s %s printed in the clear by the drift preview", kind, path)
199+
}
200+
}
201+
}
202+
}
203+
204+
// The fixture must actually differ in its generated secrets, or the
205+
// preview would have nothing to leak and the check above proves nothing.
206+
if !strings.Contains(preview, "KubeServiceAccountConfig") {
207+
t.Fatalf("expected the two configs to differ in their service-account keys:\n%s", preview)
208+
}
209+
}
210+
211+
// The dry-run diff the node returns is opaque text, so talm masks it by the
212+
// secret values the rendered config carries; those have to include every
213+
// secret Talos redacts, typed documents included.
214+
func TestModeDetails_CollectsEverySecretTalosRedacts(t *testing.T) {
215+
cfg := v114ControlPlaneWithSecrets(t)
216+
217+
collected, err := collectConfigSecretValues(cfg)
218+
if err != nil {
219+
t.Fatalf("collectConfigSecretValues: %v", err)
220+
}
221+
222+
for kind, leaves := range talosSecretLeaves(t, cfg) {
223+
for path, value := range leaves {
224+
if _, ok := collected[value]; !ok {
225+
t.Errorf("%s %s is not collected for dry-run diff redaction", kind, path)
226+
}
227+
}
228+
}
229+
}
230+
231+
// The etcd encryption config is a whole Kubernetes EncryptionConfiguration,
232+
// and only its `secret` leaves are secret. Collecting the rest (provider key
233+
// names, the `secrets` resource) would mask those words everywhere in the
234+
// dry-run diff, as Talos itself does not.
235+
func TestModeDetails_EtcdEncryptionCollectsOnlySecretLeaves(t *testing.T) {
236+
values, err := collectConfigSecretValues(v114ControlPlaneWithSecrets(t))
237+
if err != nil {
238+
t.Fatalf("collectConfigSecretValues: %v", err)
239+
}
240+
241+
for _, plain := range []string{"secrets", "key2"} {
242+
if _, ok := values[plain]; ok {
243+
t.Errorf("%q is collected as a secret value", plain)
244+
}
245+
}
246+
247+
printed := redactValuesInText("+ - hostPath: /var/lib/secrets\n+ name: key2-volume\n", values)
248+
if strings.Contains(printed, modeDetailsRedactionSentinel) {
249+
t.Errorf("plain diff lines were masked:\n%s", printed)
250+
}
251+
}
252+
253+
// The same field name is secret on one kind and harmless on another:
254+
// KubeEtcdEncryptionConfig.config holds the encryption keys,
255+
// KubeletConfig.config holds kubelet settings an operator needs to see.
256+
func TestSecretRedactor_TypedPathsAreKeyedByKind(t *testing.T) {
257+
if !(secretRedactor{}).forKind("KubeEtcdEncryptionConfig").redactPath("config.resources") {
258+
t.Error("KubeEtcdEncryptionConfig config.resources is not redacted")
259+
}
260+
261+
if (secretRedactor{}).forKind("KubeletConfig").redactPath("config.maxPods") {
262+
t.Error("KubeletConfig config.maxPods is redacted")
263+
}
264+
265+
if (secretRedactor{show: true}).forKind("KubeEtcdEncryptionConfig").redactPath("config.resources") {
266+
t.Error("--show-secrets-in-drift does not reveal typed secrets")
267+
}
268+
}

‎pkg/commands/contract_modedetails_redaction_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,32 @@ func TestContract_RedactValuesInText(t *testing.T) {
134134
}
135135
}
136136

137+
// A multi-line secret, such as a PEM key in a typed document, reaches the diff
138+
// as one prefixed line per line of the key and is masked line by line. A
139+
// base64 line can itself start with `+`, so only one diff marker is dropped;
140+
// the armor lines are not secret and stay.
141+
func TestContract_RedactValuesInText_MultiLineSecret(t *testing.T) {
142+
const key = "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIAp+Ue3pzd6MDqVdJK5WfuOx4abcdefghijklmnopqrstuv\n+k0sbpQwkE50w/5mixhKjerqWCXabcdefghijklmnopqrstuvwxyz01\n-----END EC PRIVATE KEY-----\n"
143+
144+
text := "@@ -1,6 +1,6 @@\n issuingCA:\n- key: |\n+ key: |\n+ -----BEGIN EC PRIVATE KEY-----\n+ MHcCAQEEIAp+Ue3pzd6MDqVdJK5WfuOx4abcdefghijklmnopqrstuv\n+ +k0sbpQwkE50w/5mixhKjerqWCXabcdefghijklmnopqrstuvwxyz01\n+ -----END EC PRIVATE KEY-----\n"
145+
146+
got := redactValuesInText(text, secretSetOf(key))
147+
148+
for _, leaked := range []string{"MHcCAQEEIAp", "k0sbpQwkE50w"} {
149+
if strings.Contains(got, leaked) {
150+
t.Errorf("a line of the key survived redaction:\n%s", got)
151+
}
152+
}
153+
154+
if strings.Count(got, "+ "+modeDetailsRedactionSentinel) != 2 {
155+
t.Errorf("each key line must become the sentinel behind its marker and indentation:\n%s", got)
156+
}
157+
158+
if !strings.Contains(got, "-----BEGIN EC PRIVATE KEY-----") {
159+
t.Errorf("armor lines are not secret and must stay:\n%s", got)
160+
}
161+
}
162+
137163
// TestContract_PrintApplyResultsRedacted pins the end-to-end print path: a
138164
// ModeDetails diff carrying a secret is masked when the value is in scope and
139165
// printed verbatim when the value set is empty (show-secrets).

‎pkg/commands/preflight_apply_safety.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func printDriftPreview(w io.Writer, header string, changes []applycheck.Change,
313313

314314
for j := range change.Fields {
315315
f := &change.Fields[j]
316-
_, _ = fmt.Fprintf(w, " %s\n", formatFieldChangeLine(f, redactor))
316+
_, _ = fmt.Fprintf(w, " %s\n", formatFieldChangeLine(f, redactor.forKind(change.ID.Kind)))
317317
}
318318
}
319319

0 commit comments

Comments
 (0)