@@ -37,6 +37,11 @@ import ( |
37 | 37 | // DR-published artifact. |
38 | 38 | const drIdentitySubject = `^https://github\.com/devantler-tech/platform/\.github/workflows/dr-rebuild\.yaml@refs/heads/main$` |
39 | 39 |
|
| 40 | +// drIdentityIssuer is the OIDC issuer half of that identity. cosign matches an |
| 41 | +// identity on the issuer/subject PAIR, so a correct subject under a different |
| 42 | +// issuer does not admit the DR signature and must not satisfy this gate. |
| 43 | +const drIdentityIssuer = `^https://token\.actions\.githubusercontent\.com$` |
| 44 | + |
40 | 45 | // cdContractGateJob is the cd.yaml job that runs this validator on the |
41 | 46 | // direct-push production path. Named once so the wiring check and the workflow |
42 | 47 | // cannot drift apart silently. |
@@ -238,13 +243,85 @@ func describePublicationStep(step map[string]any, index int) string { |
238 | 243 | return fmt.Sprintf("publication action step %d", index) |
239 | 244 | } |
240 | 245 |
|
| 246 | +// clusterVerifyConfig mirrors only the fragment of the KSail cluster config this |
| 247 | +// gate reads: the cosign allow-list at spec.workload.flux.verify, which is the |
| 248 | +// path KSail renders onto the root OCIRepository. Decoding the exact path is |
| 249 | +// what makes a block sitting anywhere else a failure rather than a pass, because |
| 250 | +// such a block yields zero entries here. |
| 251 | +// |
| 252 | +// Decoding reads the FIRST YAML document only, which is what a KSail cluster |
| 253 | +// config is. A verify block hidden in a later document would therefore yield no |
| 254 | +// entries and FAIL this gate rather than satisfy it — the safe direction, and the |
| 255 | +// same direction every other shape below fails in. |
| 256 | +type clusterVerifyConfig struct { |
| 257 | + Spec struct { |
| 258 | + Workload struct { |
| 259 | + Flux struct { |
| 260 | + Verify struct { |
| 261 | + MatchOIDCIdentity []struct { |
| 262 | + Issuer string `yaml:"issuer"` |
| 263 | + Subject string `yaml:"subject"` |
| 264 | + } `yaml:"matchOIDCIdentity"` |
| 265 | + } `yaml:"verify"` |
| 266 | + } `yaml:"flux"` |
| 267 | + } `yaml:"workload"` |
| 268 | + } `yaml:"spec"` |
| 269 | +} |
| 270 | + |
| 271 | +// validateVerifyAllowList checks that the cluster's cosign allow-list actually |
| 272 | +// admits the DR rebuild identity. |
| 273 | +// |
| 274 | +// 🔴 THE CONFIG IS PARSED, NOT SCANNED, for the same reason validateCDWiring |
| 275 | +// parses workflows rather than scanning them — this is the config half of that |
| 276 | +// same correction. Four ways a text scan says "allowed" about an allow-list that |
| 277 | +// is not in effect, each reproduced as an ablation in the tests: |
| 278 | +// |
| 279 | +// 1. The identity appears in a COMMENT. A comment has no effect on the cluster, |
| 280 | +// so the gate certifies an allow-list that does not exist. Measured on the |
| 281 | +// scanning version: removing the real matcher and re-adding the identical |
| 282 | +// regex as `# …` returned "contract passed", exit 0. |
| 283 | +// 2. The whole verify block sits at a path KSail DISCARDS — spec.cluster.verify |
| 284 | +// is exactly the #2627 outage. The bytes are in the file, so a scan finds |
| 285 | +// them; KSail never renders them onto the OCIRepository. |
| 286 | +// 3. A subject that merely CONTAINS this identity satisfies a substring match, |
| 287 | +// and that direction is the dangerous one: `^dr$|^attacker$` contains the DR |
| 288 | +// identity while also admitting a second signer. |
| 289 | +// 4. The right subject under the WRONG ISSUER. cosign matches on the |
| 290 | +// issuer/subject pair, so such an entry does not admit the DR signature — but |
| 291 | +// a scan looking only for the subject line accepts it. |
| 292 | +// |
| 293 | +// An allow-list entry that is inert, discarded, over-broad, or issued by someone |
| 294 | +// else does not make a DR-published artifact verifiable, which is the one failure |
| 295 | +// that is unrecoverable by hand during an incident. |
241 | 296 | func validateVerifyAllowList(config string) error { |
242 | | - if !containsLine(config, drIdentitySubject) { |
| 297 | + var parsed clusterVerifyConfig |
| 298 | + if err := yaml.Unmarshal([]byte(config), &parsed); err != nil { |
| 299 | + return fmt.Errorf( |
| 300 | + "cluster config does not parse, so its verification allow-list cannot be established: %w", err, |
| 301 | + ) |
| 302 | + } |
| 303 | + |
| 304 | + entries := parsed.Spec.Workload.Flux.Verify.MatchOIDCIdentity |
| 305 | + if len(entries) == 0 { |
243 | 306 | return errors.New( |
244 | | - "cosign matchOIDCIdentity does not allow the DR rebuild identity, so a DR-published artifact stays unverifiable", |
| 307 | + "no cosign matchOIDCIdentity entries at spec.workload.flux.verify, the path KSail renders onto the root " + |
| 308 | + "OCIRepository, so a DR-published artifact stays unverifiable; a verify block at any other path " + |
| 309 | + "(spec.cluster.verify in particular) is discarded and does not count", |
245 | 310 | ) |
246 | 311 | } |
247 | | - return nil |
| 312 | + |
| 313 | + for _, entry := range entries { |
| 314 | + if entry.Issuer == drIdentityIssuer && entry.Subject == drIdentitySubject { |
| 315 | + return nil |
| 316 | + } |
| 317 | + } |
| 318 | + |
| 319 | + return fmt.Errorf( |
| 320 | + "cosign matchOIDCIdentity at spec.workload.flux.verify has %d entries but none is exactly the DR rebuild "+ |
| 321 | + "identity, so a DR-published artifact stays unverifiable; add an entry with issuer %s and subject %s "+ |
| 322 | + "(both compared exactly — a wider regex containing this one is not accepted)", |
| 323 | + len(entries), drIdentityIssuer, drIdentitySubject, |
| 324 | + ) |
248 | 325 | } |
249 | 326 |
|
250 | 327 | // validateCDWiring checks that the direct-push production path cannot deploy |
|