|
| 1 | +import { ISDK } from '../aws-auth'; |
| 2 | +import { ChangeHotswapImpact, ChangeHotswapResult, HotswapOperation, HotswappableChangeCandidate/*, establishResourcePhysicalName*/ } from './common'; |
| 3 | +import { EvaluateCloudFormationTemplate } from './evaluate-cloudformation-template'; |
| 4 | + |
| 5 | +/** |
| 6 | + * This means that the value is required to exist by CloudFormation's API (or our S3 Bucket Deployment Lambda) |
| 7 | + * but the actual value specified is irrelevant |
| 8 | + */ |
| 9 | +export const REQUIRED_BY_CFN = 'required-to-be-present-by-cfn'; |
| 10 | + |
| 11 | +export async function isHotswappableS3BucketDeploymentChange( |
| 12 | + logicalId: string, change: HotswappableChangeCandidate, evaluateCfnTemplate: EvaluateCloudFormationTemplate, |
| 13 | +): Promise<ChangeHotswapResult> { |
| 14 | + // In old-style synthesis, the policy used by the lambda to copy assets Ref's the assets directly, |
| 15 | + // meaning that the changes made to the Policy are artifacts that can be safely ignored |
| 16 | + if (change.newValue.Type === 'AWS::IAM::Policy') { |
| 17 | + return changeIsForS3DeployCustomResourcePolicy(logicalId, change, evaluateCfnTemplate); |
| 18 | + } |
| 19 | + |
| 20 | + if (change.newValue.Type !== 'Custom::CDKBucketDeployment') { |
| 21 | + return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; |
| 22 | + } |
| 23 | + |
| 24 | + // note that this gives the ARN of the lambda, not the name. This is fine though, the invoke() sdk call will take either |
| 25 | + const functionName = await evaluateCfnTemplate.evaluateCfnExpression(change.newValue.Properties?.ServiceToken); |
| 26 | + if (!functionName) { |
| 27 | + return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; |
| 28 | + } |
| 29 | + |
| 30 | + const customResourceProperties = await evaluateCfnTemplate.evaluateCfnExpression({ |
| 31 | + ...change.newValue.Properties, |
| 32 | + ServiceToken: undefined, |
| 33 | + }); |
| 34 | + |
| 35 | + return new S3BucketDeploymentHotswapOperation(functionName, customResourceProperties); |
| 36 | +} |
| 37 | + |
| 38 | +class S3BucketDeploymentHotswapOperation implements HotswapOperation { |
| 39 | + public readonly service = 'custom-s3-deployment'; |
| 40 | + |
| 41 | + constructor(private readonly functionName: string, private readonly customResourceProperties: any) { |
| 42 | + } |
| 43 | + |
| 44 | + public async apply(sdk: ISDK): Promise<any> { |
| 45 | + return sdk.lambda().invoke({ |
| 46 | + FunctionName: this.functionName, |
| 47 | + // Lambda refuses to take a direct JSON object and requires it to be stringify()'d |
| 48 | + Payload: JSON.stringify({ |
| 49 | + RequestType: 'Update', |
| 50 | + ResponseURL: REQUIRED_BY_CFN, |
| 51 | + PhysicalResourceId: REQUIRED_BY_CFN, |
| 52 | + StackId: REQUIRED_BY_CFN, |
| 53 | + RequestId: REQUIRED_BY_CFN, |
| 54 | + LogicalResourceId: REQUIRED_BY_CFN, |
| 55 | + ResourceProperties: stringifyObject(this.customResourceProperties), // JSON.stringify() doesn't turn the actual objects to strings, but the lambda expects strings |
| 56 | + }), |
| 57 | + }).promise(); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +async function changeIsForS3DeployCustomResourcePolicy( |
| 62 | + iamPolicyLogicalId: string, change: HotswappableChangeCandidate, evaluateCfnTemplate: EvaluateCloudFormationTemplate, |
| 63 | +): Promise<ChangeHotswapResult> { |
| 64 | + const roles = change.newValue.Properties?.Roles; |
| 65 | + if (!roles) { |
| 66 | + return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; |
| 67 | + } |
| 68 | + |
| 69 | + for (const role of roles) { |
| 70 | + const roleLogicalId = await evaluateCfnTemplate.findLogicalIdForPhysicalName(await evaluateCfnTemplate.evaluateCfnExpression(role)); |
| 71 | + if (!roleLogicalId) { |
| 72 | + return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; |
| 73 | + } |
| 74 | + |
| 75 | + const roleRefs = evaluateCfnTemplate.findReferencesTo(roleLogicalId); |
| 76 | + for (const roleRef of roleRefs) { |
| 77 | + if (roleRef.Type === 'AWS::Lambda::Function') { |
| 78 | + const lambdaRefs = evaluateCfnTemplate.findReferencesTo(roleRef.LogicalId); |
| 79 | + for (const lambdaRef of lambdaRefs) { |
| 80 | + // If S3Deployment -> Lambda -> Role and IAM::Policy -> Role, then this IAM::Policy change is an |
| 81 | + // artifact of old-style synthesis |
| 82 | + if (lambdaRef.Type !== 'Custom::CDKBucketDeployment') { |
| 83 | + return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; |
| 84 | + } |
| 85 | + } |
| 86 | + } else if (roleRef.Type === 'AWS::IAM::Policy') { |
| 87 | + if (roleRef.LogicalId !== iamPolicyLogicalId) { |
| 88 | + return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; |
| 89 | + } |
| 90 | + } else { |
| 91 | + return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return new EmptyHotswapOperation(); |
| 97 | +} |
| 98 | + |
| 99 | +function stringifyObject(obj: any): any { |
| 100 | + if (obj == null) { |
| 101 | + return obj; |
| 102 | + } |
| 103 | + if (Array.isArray(obj)) { |
| 104 | + return obj.map(stringifyObject); |
| 105 | + } |
| 106 | + if (typeof obj !== 'object') { |
| 107 | + return obj.toString(); |
| 108 | + } |
| 109 | + |
| 110 | + const ret: { [k: string]: any } = {}; |
| 111 | + for (const [k, v] of Object.entries(obj)) { |
| 112 | + ret[k] = stringifyObject(v); |
| 113 | + } |
| 114 | + return ret; |
| 115 | +} |
| 116 | + |
| 117 | +class EmptyHotswapOperation implements HotswapOperation { |
| 118 | + readonly service = 'empty'; |
| 119 | + public async apply(sdk: ISDK): Promise<any> { |
| 120 | + return Promise.resolve(sdk); |
| 121 | + } |
| 122 | +} |
0 commit comments