|
| 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 | +} |
0 commit comments