@@ -5,7 +5,7 @@ import { PublicKey } from 'aws-cdk-lib/aws-cloudfront';
55import { type DotStack } from '../constructs/Stack' ;
66
77import { addSecret } from './secret' ;
8- import { addParam } from './ssm' ;
8+ import { addParam , getParamValue } from './ssm' ;
99
1010const 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} ;
0 commit comments