Skip to content

Commit

Permalink
Use outputs all the way
Browse files Browse the repository at this point in the history
  • Loading branch information
rix0rrr committed Oct 24, 2024
1 parent af27bf6 commit eba63c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
17 changes: 8 additions & 9 deletions packages/aws-cdk/lib/api/util/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@ export async function getBootstrapStackInfo(sdk: ISDK, stackName: string): Promi
throw new Error(`Invalid BootstrapVersion value: ${versionOutput.OutputValue}`);
}

// try to get bucketname from the logical resource id
let bucketName: string | undefined;
const resourcesResponse = await cfn.describeStackResources({ StackName: stackName }).promise();
const bucketResource = resourcesResponse.StackResources?.find(resource =>
resource.ResourceType === 'AWS::S3::Bucket',
);
bucketName = bucketResource?.PhysicalResourceId;

let hasStagingBucket = !!bucketName;
// try to get bucketname from the logical resource id. If there is no
// bucketname, or the value doesn't look like an S3 bucket name, we assume
// the bucket doesn't exist.
//
// We would have preferred to look at the stack resources here, but
// unfortunately the deploy role doesn't have permissions call DescribeStackResources.
const bucketName = stack.Outputs?.find(output => output.OutputKey === 'BucketName')?.OutputValue;
const hasStagingBucket = !!(bucketName && bucketName.match(/^[a-z0-9-]+$/));

return {
hasStagingBucket,
Expand Down
31 changes: 12 additions & 19 deletions packages/aws-cdk/test/api/util/checks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ describe('determineAllowCrossAccountAssetPublishing', () => {
});
});

AWSMock.mock('CloudFormation', 'describeStackResources', (_params: any, callback: Function) => {
callback(null, { StackResources: [] });
});

const result = await determineAllowCrossAccountAssetPublishing(mockSDK);
expect(result).toBe(true);
});
Expand All @@ -37,15 +33,14 @@ describe('determineAllowCrossAccountAssetPublishing', () => {
AWSMock.mock('CloudFormation', 'describeStacks', (_params: any, callback: Function) => {
callback(null, {
Stacks: [{
Outputs: [{ OutputKey: 'BootstrapVersion', OutputValue: '21' }],
Outputs: [
{ OutputKey: 'BootstrapVersion', OutputValue: '21' },
{ OutputKey: 'BucketName', OutputValue: 'some-bucket' },
],
}],
});
});

AWSMock.mock('CloudFormation', 'describeStackResources', (_params: any, callback: Function) => {
callback(null, { StackResources: [{ ResourceType: 'AWS::S3::Bucket', PhysicalResourceId: 'some-bucket' }] });
});

const result = await determineAllowCrossAccountAssetPublishing(mockSDK);
expect(result).toBe(true);
});
Expand All @@ -63,15 +58,14 @@ describe('determineAllowCrossAccountAssetPublishing', () => {
AWSMock.mock('CloudFormation', 'describeStacks', (_params: any, callback: Function) => {
callback(null, {
Stacks: [{
Outputs: [{ OutputKey: 'BootstrapVersion', OutputValue: '20' }],
Outputs: [
{ OutputKey: 'BootstrapVersion', OutputValue: '20' },
{ OutputKey: 'BucketName', OutputValue: 'some-bucket' },
],
}],
});
});

AWSMock.mock('CloudFormation', 'describeStackResources', (_params: any, callback: Function) => {
callback(null, { StackResources: [{ ResourceType: 'AWS::S3::Bucket', PhysicalResourceId: 'some-bucket' }] });
});

const result = await determineAllowCrossAccountAssetPublishing(mockSDK);
expect(result).toBe(false);
});
Expand All @@ -94,15 +88,14 @@ describe('getBootstrapStackInfo', () => {
AWSMock.mock('CloudFormation', 'describeStacks', (_params: any, callback: Function) => {
callback(null, {
Stacks: [{
Outputs: [{ OutputKey: 'BootstrapVersion', OutputValue: '21' }],
Outputs: [
{ OutputKey: 'BootstrapVersion', OutputValue: '21' },
{ OutputKey: 'BucketName', OutputValue: 'some-bucket' },
],
}],
});
});

AWSMock.mock('CloudFormation', 'describeStackResources', (_params: any, callback: Function) => {
callback(null, { StackResources: [{ ResourceType: 'AWS::S3::Bucket', PhysicalResourceId: 'some-bucket' }] });
});

const result = await getBootstrapStackInfo(mockSDK, 'CDKToolkit');
expect(result).toEqual({
hasStagingBucket: true,
Expand Down

0 comments on commit eba63c8

Please sign in to comment.