Skip to content

Commit

Permalink
feat: adds fingerprint and public key format as resource properties (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
udondan authored Mar 23, 2024
1 parent c99e66b commit 046e41d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lambda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ async function createKeyPair(
log.debug('Import successful', JSON.stringify(result, null, 2));
resource.addResponseValue('KeyPairName', result.KeyName!);
resource.addResponseValue('KeyPairID', result.KeyPairId!);
resource.addResponseValue('KeyFingerprint', result.KeyFingerprint!);
return result;
} catch (error) {
log.error('Import failed', error);
Expand All @@ -203,6 +204,7 @@ async function createKeyPair(
const result = await ec2Client.send(new CreateKeyPairCommand(params));
resource.addResponseValue('KeyPairName', result.KeyName!);
resource.addResponseValue('KeyPairID', result.KeyPairId!);
resource.addResponseValue('KeyPairFingerprint', result.KeyFingerprint!);
return result;
}
}
Expand All @@ -227,11 +229,9 @@ async function updateKeyPair(
}

const keyPair = result.KeyPairs[0];
const keyPairId = keyPair.KeyPairId!;
const keyPairName = keyPair.KeyName!;

resource.addResponseValue('KeyPairName', keyPairName);
resource.addResponseValue('KeyPairID', keyPairId);
resource.addResponseValue('KeyPairName', keyPair.KeyName!);
resource.addResponseValue('KeyPairID', keyPair.KeyPairId!);
resource.addResponseValue('KeyPairFingerprint', keyPair.KeyFingerprint!);
return keyPair;
}

Expand Down Expand Up @@ -330,7 +330,6 @@ async function deleteKeyPair(
};
log.debug('ec2.deleteKeyPair:', JSON.stringify(params, null, 2));
await ec2Client.send(new DeleteKeyPairCommand(params));
resource.addResponseValue('KeyPairName', resource.properties.Name.value);
}

async function createPrivateKeySecret(
Expand Down
12 changes: 12 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ export class KeyPair extends Resource implements ITaggable, IKeyPair {
*/
public readonly keyPairID: string = '';

/**
* Fingerprint of the Key Pair
*/
public readonly keyPairFingerprint: string = '';

/**
* Format of the public key
*/
public readonly publicKeyFormat: PublicKeyFormat;

/**
* Type of the Key Pair
*/
Expand Down Expand Up @@ -264,6 +274,7 @@ export class KeyPair extends Resource implements ITaggable, IKeyPair {
this.tags.setTag(createdByTag, ID);

this.keyType = props.keyType ?? KeyType.RSA;
this.publicKeyFormat = props.publicKeyFormat ?? PublicKeyFormat.SSH;

const kmsPrivate = props.kmsPrivateKey ?? props.kms;
const kmsPublic = props.kmsPublicKey ?? props.kms;
Expand Down Expand Up @@ -318,6 +329,7 @@ export class KeyPair extends Resource implements ITaggable, IKeyPair {
this.publicKeyValue = key.getAttString('PublicKeyValue');
this.keyPairName = key.getAttString('KeyPairName');
this.keyPairID = key.getAttString('KeyPairID');
this.keyPairFingerprint = key.getAttString('KeyPairFingerprint');
}

private ensureLambda(legacyLambdaName: boolean): aws_lambda.Function {
Expand Down
10 changes: 10 additions & 0 deletions test/lib/test-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ export class TestStack extends Stack {
value: keyPair.publicKeyValue,
});

new CfnOutput(this, 'Test-Public-Key-Fingerprint', {
exportName: 'TestPublicKeyFingerprint',
value: keyPair.keyPairFingerprint,
});

new CfnOutput(this, 'Test-Public-Key-Format', {
exportName: 'TestPublicKeyFormat',
value: keyPair.publicKeyFormat,
});

// import public key

const keyPairImport = new KeyPair(this, 'Test-Key-Pair-Import', {
Expand Down

0 comments on commit 046e41d

Please sign in to comment.