Skip to content

Commit 491c66e

Browse files
committed
fix(cdk): dont try to create a new publickey if it exits. cdk bug
1 parent f316518 commit 491c66e

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

packages/cdk/src/methods/signing.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { PublicKey } from 'aws-cdk-lib/aws-cloudfront';
55
import { type DotStack } from '../constructs/Stack';
66

77
import { addSecret } from './secret';
8-
import { addParam } from './ssm';
8+
import { addParam, getParamValue } from './ssm';
99

1010
const generateRsaKeyPair = () => {
1111
const { privateKey, publicKey } = generateKeyPairSync('rsa', {
@@ -22,7 +22,19 @@ const generateRsaKeyPair = () => {
2222
return { privateKey, publicKey };
2323
};
2424

25-
export const addSigningKey = (scope: DotStack) => {
25+
export const addSigningKey = async (scope: DotStack) => {
26+
const baseName = 'signing-pubkey';
27+
const paramName = `${scope.ssmPrefix}/id/${baseName}`;
28+
const existingKeyId = await getParamValue(paramName);
29+
30+
if (existingKeyId) {
31+
return PublicKey.fromPublicKeyId(
32+
scope,
33+
`PublicKey-fromPublicKeyId-${+new Date()}`,
34+
existingKeyId
35+
);
36+
}
37+
2638
// FIXME: We have to not run this for additional deploys to prod
2739
// because for some reason it fails if the public key exists already
2840
// https://github.com/aws/aws-cdk/issues/15301
@@ -35,7 +47,6 @@ export const addSigningKey = (scope: DotStack) => {
3547
value: JSON.stringify(keyPair)
3648
});
3749

38-
const baseName = 'signing-pubkey';
3950
const publicKeyName = scope.resourceName(baseName);
4051
const cfKey = new PublicKey(scope, publicKeyName, {
4152
encodedKey: keyPair.publicKey,
@@ -46,8 +57,10 @@ export const addSigningKey = (scope: DotStack) => {
4657

4758
addParam({
4859
id: `${publicKeyName}-id`,
49-
name: `${scope.ssmPrefix}/id/${baseName}`,
60+
name: paramName,
5061
scope,
5162
value: cfKey.publicKeyId
5263
});
64+
65+
return cfKey;
5366
};

packages/cdk/src/methods/ssm.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ import { nanoid } from 'nanoid';
77

88
import { DotStack } from '../constructs/Stack';
99

10+
export const getParamValue = async (name: string) => {
11+
try {
12+
const client = new SSMClient({ region: DotStack.awsRegion });
13+
const command = new GetParameterCommand({ Name: name });
14+
const result = await client.send(command);
15+
16+
return result.Parameter?.Value;
17+
} catch (error: any) {
18+
return void 0;
19+
}
20+
};
21+
1022
export const paramExists = async (name: string): Promise<boolean> => {
1123
try {
1224
const client = new SSMClient({ region: DotStack.awsRegion });

0 commit comments

Comments
 (0)