feat: preserve distinct ARN and default-status certificate entries in origin ALB snapshots across plan apply and rollback

This commit is contained in:
Matthew Meszaros
2026-11-04 18:34:05 +01:00
parent ed9c340f79
commit 89bdec5279
2 changed files with 48 additions and 1 deletions
+8 -1
View File
@@ -74,6 +74,13 @@ function sorted(rows, key) {
return rows.sort((a, b) => a[key].localeCompare(b[key]));
}
function sortedCertificates(rows) {
requireThat(Array.isArray(rows) && rows.every((row) => typeof row.CertificateArn === "string" && typeof row.IsDefault === "boolean") &&
new Set(rows.map((row) => canonical([row.CertificateArn, row.IsDefault]))).size === rows.length,
"Incomplete or duplicate ALB certificate response");
return rows.sort((a, b) => a.CertificateArn.localeCompare(b.CertificateArn) || Number(a.IsDefault) - Number(b.IsDefault));
}
async function collect(aws) {
const lb = await aws("elbv2", "describe-load-balancers", ["--load-balancer-arns", LB]);
requireThat(lb.LoadBalancers?.length === 1 && lb.LoadBalancers[0].LoadBalancerArn === LB &&
@@ -85,7 +92,7 @@ async function collect(aws) {
const details = { arn: listener.ListenerArn,
attributes: sorted((await aws("elbv2", "describe-listener-attributes", ["--listener-arn", listener.ListenerArn])).Attributes, "Key"),
rules: sorted((await aws("elbv2", "describe-rules", ["--listener-arn", listener.ListenerArn])).Rules, "RuleArn") };
if (listener.Protocol === "HTTPS") details.certificates = sorted((await aws("elbv2", "describe-listener-certificates", ["--listener-arn", listener.ListenerArn])).Certificates, "CertificateArn");
if (listener.Protocol === "HTTPS") details.certificates = sortedCertificates((await aws("elbv2", "describe-listener-certificates", ["--listener-arn", listener.ListenerArn])).Certificates);
listenerDetails.push(details);
}
const targetGroups = sorted((await aws("elbv2", "describe-target-groups", ["--load-balancer-arn", LB])).TargetGroups, "TargetGroupArn");
+40
View File
@@ -198,6 +198,46 @@ test("scoped rollback uses the same exact reviewed plan and restores only the ba
assert.deepEqual(f.writeCalls().map((c) => c.args), [["--listener-arn", LISTENER, "--ssl-policy", DESIRED], ["--listener-arn", LISTENER, "--ssl-policy", BASELINE]]);
});
for (const defaultFirst of [true, false]) test(`plan, apply and rollback preserve default and SNI certificate entries with default first: ${defaultFirst}`, async (t) => {
const f = fixture(t);
const certificates = [{ CertificateArn: CERT, IsDefault: false }, { CertificateArn: CERT, IsDefault: true }];
const inventory = f.live.listenerDetails.find((d) => d.arn === LISTENER);
inventory.certificates = structuredClone(certificates);
if (defaultFirst) inventory.certificates.reverse();
const baseline = structuredClone(f.live);
const config = await f.plan();
assert.deepEqual(f.live, baseline);
assert.equal(f.writeCalls().length, 0);
const plan = JSON.parse(f.objects.get(`${PREFIX}/100/1/plan.json`).bytes);
checkPlan(plan, { source: SOURCE, run: "100" });
for (const snapshot of [plan.before, plan.after]) {
assert.deepEqual(snapshot.listenerDetails.find((d) => d.arn === LISTENER).certificates, certificates);
}
for (const [mode, currentRun, policy] of [["apply", "200", DESIRED], ["rollback", "300", BASELINE]]) {
inventory.certificates.reverse();
const expected = structuredClone(f.live);
expected.listeners[0].SslPolicy = policy;
await run(f.aws, { ...config, mode, currentRun, work: join(f.dir, mode) }, () => {});
assert.deepEqual(f.live, expected);
const result = JSON.parse(f.objects.get(`${PREFIX}/${currentRun}/1/result.json`).bytes);
assert.equal(result.success, true);
assert.deepEqual(result.readback.listenerDetails.find((d) => d.arn === LISTENER).certificates, certificates);
}
assert.deepEqual(f.live, baseline);
assert.deepEqual(f.writeCalls().map((c) => c.args), [["--listener-arn", LISTENER, "--ssl-policy", DESIRED], ["--listener-arn", LISTENER, "--ssl-policy", BASELINE]]);
});
test("certificate inventory requires distinct ARN and default-status pairs with boolean status", async (t) => {
for (const status of [true, false, undefined, "true"]) {
const f = fixture(t);
f.live.listenerDetails[0].certificates = [
{ CertificateArn: CERT, IsDefault: status }, { CertificateArn: CERT, IsDefault: status },
];
await assert.rejects(collect(f.aws), /Incomplete or duplicate ALB certificate response/);
assert.equal(f.writeCalls().length, 0);
}
});
for (const [label, drift] of [
["policy", (f) => { f.live.listeners[0].SslPolicy = DESIRED; }],
["certificate", (f) => { f.live.listeners[0].Certificates = []; }],