|
| 1 | +import { |
| 2 | + CloudFormationClient, |
| 3 | + DescribeChangeSetCommand, |
| 4 | + DescribeChangeSetCommandOutput, |
| 5 | + Change as CloudFormationChange |
| 6 | +} from "@aws-sdk/client-cloudformation" |
| 7 | + |
| 8 | +export type ChangeRequiringAttention = { |
| 9 | + logicalId: string; |
| 10 | + physicalId: string; |
| 11 | + resourceType: string; |
| 12 | + reason: string; |
| 13 | +} |
| 14 | + |
| 15 | +export type AllowedDestructiveChange = { |
| 16 | + LogicalResourceId: string; |
| 17 | + PhysicalResourceId: string; |
| 18 | + ResourceType: string; |
| 19 | + ExpiryDate: string | Date; |
| 20 | + StackName: string; |
| 21 | + AllowedReason: string; |
| 22 | +} |
| 23 | + |
| 24 | +const requiresReplacement = (replacement: unknown): boolean => { |
| 25 | + if (replacement === undefined || replacement === null) { |
| 26 | + return false |
| 27 | + } |
| 28 | + |
| 29 | + const normalized = String(replacement) |
| 30 | + return normalized === "True" || normalized === "Conditional" |
| 31 | +} |
| 32 | + |
| 33 | +const toDate = (value: Date | string | number | undefined | null): Date | undefined => { |
| 34 | + if (value === undefined || value === null) { |
| 35 | + return undefined |
| 36 | + } |
| 37 | + |
| 38 | + const date = value instanceof Date ? value : new Date(value) |
| 39 | + return Number.isNaN(date.getTime()) ? undefined : date |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Extracts the subset of CloudFormation changes that either require replacement or remove resources. |
| 44 | + * |
| 45 | + * @param changeSet - Raw change-set details returned from `DescribeChangeSet`. |
| 46 | + * @returns Array of changes that need operator attention. |
| 47 | + */ |
| 48 | +export function checkDestructiveChanges( |
| 49 | + changeSet: DescribeChangeSetCommandOutput | undefined | null |
| 50 | +): Array<ChangeRequiringAttention> { |
| 51 | + if (!changeSet || typeof changeSet !== "object") { |
| 52 | + throw new Error("A change set object must be provided") |
| 53 | + } |
| 54 | + |
| 55 | + const {Changes} = changeSet |
| 56 | + const changes = Array.isArray(Changes) ? (Changes as Array<CloudFormationChange>) : [] |
| 57 | + |
| 58 | + return changes |
| 59 | + .map((change: CloudFormationChange) => { |
| 60 | + const resourceChange = change?.ResourceChange |
| 61 | + if (!resourceChange) { |
| 62 | + return undefined |
| 63 | + } |
| 64 | + |
| 65 | + const replacementNeeded = requiresReplacement(resourceChange.Replacement) |
| 66 | + const action = resourceChange.Action |
| 67 | + const isRemoval = action === "Remove" |
| 68 | + |
| 69 | + if (!replacementNeeded && !isRemoval) { |
| 70 | + return undefined |
| 71 | + } |
| 72 | + |
| 73 | + return { |
| 74 | + logicalId: resourceChange.LogicalResourceId ?? "<unknown logical id>", |
| 75 | + physicalId: resourceChange.PhysicalResourceId ?? "<unknown physical id>", |
| 76 | + resourceType: resourceChange.ResourceType ?? "<unknown type>", |
| 77 | + reason: replacementNeeded |
| 78 | + ? `Replacement: ${String(resourceChange.Replacement)}` |
| 79 | + : `Action: ${action ?? "<unknown action>"}` |
| 80 | + } |
| 81 | + }) |
| 82 | + .filter((change): change is ChangeRequiringAttention => Boolean(change)) |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Describes a CloudFormation change set, applies waiver logic, and throws if destructive changes remain. |
| 87 | + * |
| 88 | + * @param changeSetName - Name or ARN of the change set. |
| 89 | + * @param stackName - Name or ARN of the stack that owns the change set. |
| 90 | + * @param region - AWS region where the stack resides. |
| 91 | + * @param allowedChanges - Optional waivers that temporarily allow specific destructive changes. |
| 92 | + */ |
| 93 | +export async function checkDestructiveChangeSet( |
| 94 | + changeSetName: string, |
| 95 | + stackName: string, |
| 96 | + region: string, |
| 97 | + allowedChanges: Array<AllowedDestructiveChange> = []): Promise<void> { |
| 98 | + if (!changeSetName || !stackName || !region) { |
| 99 | + throw new Error("Change set name, stack name, and region are required") |
| 100 | + } |
| 101 | + |
| 102 | + const client = new CloudFormationClient({region}) |
| 103 | + const command = new DescribeChangeSetCommand({ |
| 104 | + ChangeSetName: changeSetName, |
| 105 | + StackName: stackName |
| 106 | + }) |
| 107 | + |
| 108 | + const response: DescribeChangeSetCommandOutput = await client.send(command) |
| 109 | + const destructiveChanges = checkDestructiveChanges(response) |
| 110 | + const creationTime = toDate(response.CreationTime) |
| 111 | + const changeSetStackName = response.StackName |
| 112 | + |
| 113 | + const remainingChanges = destructiveChanges.filter(change => { |
| 114 | + const waiver = allowedChanges.find(allowed => |
| 115 | + allowed.LogicalResourceId === change.logicalId && |
| 116 | + allowed.PhysicalResourceId === change.physicalId && |
| 117 | + allowed.ResourceType === change.resourceType |
| 118 | + ) |
| 119 | + |
| 120 | + if (!waiver || !creationTime || !changeSetStackName || waiver.StackName !== changeSetStackName) { |
| 121 | + return true |
| 122 | + } |
| 123 | + |
| 124 | + const expiryDate = toDate(waiver.ExpiryDate) |
| 125 | + if (!expiryDate) { |
| 126 | + return true |
| 127 | + } |
| 128 | + |
| 129 | + if (expiryDate.getTime() > creationTime.getTime()) { |
| 130 | + |
| 131 | + console.log( |
| 132 | + // eslint-disable-next-line max-len |
| 133 | + `Allowing destructive change ${change.logicalId} (${change.resourceType}) until ${expiryDate.toISOString()} - ${waiver.AllowedReason}` |
| 134 | + ) |
| 135 | + return false |
| 136 | + } |
| 137 | + |
| 138 | + console.error( |
| 139 | + `Waiver for ${change.logicalId} (${change.resourceType}) expired on ${expiryDate.toISOString()}` |
| 140 | + ) |
| 141 | + return true |
| 142 | + }) |
| 143 | + |
| 144 | + if (remainingChanges.length === 0) { |
| 145 | + console.log(`Change set ${changeSetName} for stack ${stackName} has no destructive changes that are not waived.`) |
| 146 | + return |
| 147 | + } |
| 148 | + |
| 149 | + console.error("Resources that require attention:") |
| 150 | + remainingChanges.forEach(({logicalId, physicalId, resourceType, reason}) => { |
| 151 | + console.error(`- LogicalId: ${logicalId}, PhysicalId: ${physicalId}, Type: ${resourceType}, Reason: ${reason}`) |
| 152 | + }) |
| 153 | + throw new Error(`Change set ${changeSetName} contains destructive changes`) |
| 154 | +} |
0 commit comments