Skip to content

Commit

Permalink
Bugfix for issue #24561
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Neuner committed Nov 22, 2024
1 parent 01f2dcd commit fe543ea
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
8 changes: 4 additions & 4 deletions packages/aws-cdk-lib/aws-servicecatalog/lib/product-stack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as path from 'path';
import { Construct } from 'constructs';
Expand All @@ -7,6 +6,7 @@ import { ProductStackHistory } from './product-stack-history';
import { IBucket } from '../../aws-s3';
import { ServerSideEncryption } from '../../aws-s3-deployment';
import * as cdk from '../../core';
import { fingerprint } from '../../core/lib/fs/fingerprint';

/**
* Product stack props.
Expand Down Expand Up @@ -144,7 +144,9 @@ export class ProductStack extends cdk.Stack {
*/
public _synthesizeTemplate(session: cdk.ISynthesisSession): void {
const cfn = JSON.stringify(this._toCloudFormation(), undefined, 2);
const templateHash = crypto.createHash('sha256').update(cfn).digest('hex');
const templateFilePath = path.join(session.assembly.outdir, this.templateFile);
fs.writeFileSync(templateFilePath, cfn);
const templateHash = fingerprint(templateFilePath);

this._templateUrl = this._parentStack.synthesizer.addFileAsset({
packaging: cdk.FileAssetPackaging.FILE,
Expand All @@ -155,8 +157,6 @@ export class ProductStack extends cdk.Stack {
if (this._parentProductStackHistory) {
this._parentProductStackHistory._writeTemplateToSnapshot(cfn);
}

fs.writeFileSync(path.join(session.assembly.outdir, this.templateFile), cfn);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { Construct } from 'constructs';
import { Match, Template } from '../../assertions';
Expand Down Expand Up @@ -500,4 +501,47 @@ class ProductWithAnAsset extends servicecatalog.ProductStack {
handler: 'index.handler',
});
}
}
}

test('Adding new version to product stack history', () => {
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'cdk-product-stack-snapshots'));
const createProduct = (currentVersion: string, previousVersion?: string) => {
const app = new cdk.App();
const mainStack = new cdk.Stack(app, 'MainStack');
const productStack = new servicecatalog.ProductStack(mainStack, 'ProductStack');
new s3.Bucket(productStack, `Bucket${currentVersion}`);
const productStackHistory = new servicecatalog.ProductStackHistory(mainStack, 'ProductStackHistory', {
currentVersionLocked: true,
currentVersionName: currentVersion,
productStack,
directory: tmpdir,
});
const productVersions = new Array<servicecatalog.CloudFormationProductVersion>();
if (previousVersion) {
productVersions.push(productStackHistory.versionFromSnapshot(previousVersion));
}
productVersions.push(productStackHistory.currentVersion());
const product = new servicecatalog.CloudFormationProduct(mainStack, 'Product', {
productName: 'Product',
owner: 'Owner',
productVersions,
});
app.synth();
const template = Template.fromStack(mainStack);
const productsInTemplate = template.findResources('AWS::ServiceCatalog::CloudFormationProduct');
expect(Object.values(productsInTemplate).length).toEqual(1);
return Object.values(productsInTemplate)[0]
.Properties
.ProvisioningArtifactParameters[0]
.Info
.LoadTemplateFromURL['Fn::Sub']
.replace(/^.*\//, '');
};
try {
const assetHash1 = createProduct('V1');
const assetHash2 = createProduct('V2', 'V1');
expect(assetHash1).toEqual(assetHash2);
} finally {
fs.rmSync(tmpdir, { recursive: true });
}
});

0 comments on commit fe543ea

Please sign in to comment.