Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 7 additions & 26 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ overrides:
esbuild@>=0.27.3 <0.28.1: '>=0.28.1'
minimatch@>=10.0.0 <10.2.3: '>=10.2.3'
yaml@>=2.0.0 <2.8.3: '>=2.8.3'
js-yaml@>=3.0.0 <3.15.2: '>=3.15.2'
js-yaml@>=4.0.0 <4.3.2: '>=4.3.2'
resolutionMode: time-based
trustPolicy: no-downgrade
trustPolicyExclude:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('getPrivateKey', () => {
it('gets private key from ssm', async () => {
(parameterStore.getAllParameters as jest.Mock).mockReturnValue([
{
Name: `privatekey_20201105_${testKeyId1}.pem`,
Name: `/test_component/main/apim/private_key/privatekey_20201105_${testKeyId1}.pem`,
Value: testPrivateKey1,
},
]);
Expand All @@ -93,15 +93,15 @@ describe('getPrivateKey', () => {

(parameterStore.getAllParameters as jest.Mock).mockReturnValue([
{
Name: `privatekey_${todaysDate}_${testKeyId1}.pem`,
Name: `/test_component/main/apim/private_key/privatekey_${todaysDate}_${testKeyId1}.pem`,
Value: testPrivateKey1,
},
{
Name: `privatekey_20201203_${testKeyId2}.pem`,
Name: `/test_component/main/apim/private_key/privatekey_20201203_${testKeyId2}.pem`,
Value: testPrivateKey2,
},
{
Name: `privatekey_20211103_${testKeyId3}.pem`,
Name: `/test_component/main/apim/private_key/privatekey_20211103_${testKeyId3}.pem`,
Value: testPrivateKey3,
},
]);
Expand All @@ -124,15 +124,15 @@ describe('getPrivateKey', () => {

(parameterStore.getAllParameters as jest.Mock).mockReturnValue([
{
Name: `privatekey_${yesterdaysDate}_${testKeyId1}.pem`,
Name: `/test_component/main/apim/private_key/privatekey_${yesterdaysDate}_${testKeyId1}.pem`,
Value: testPrivateKey1,
},
{
Name: `privatekey_20201103_${testKeyId2}.pem`,
Name: `/test_component/main/apim/private_key/privatekey_20201103_${testKeyId2}.pem`,
Value: testPrivateKey2,
},
{
Name: `privatekey_20211103_${testKeyId3}.pem`,
Name: `/test_component/main/apim/private_key/privatekey_20211103_${testKeyId3}.pem`,
Value: testPrivateKey3,
},
]);
Expand All @@ -150,15 +150,15 @@ describe('getPrivateKey', () => {
it('selects youngest key when more than one key exists and the youngest key wasnt generated today or yesterday', async () => {
(parameterStore.getAllParameters as jest.Mock).mockReturnValue([
{
Name: `privatekey_20221103_${testKeyId1}.pem`,
Name: `/test_component/main/apim/private_key/privatekey_20221103_${testKeyId1}.pem`,
Value: testPrivateKey1,
},
{
Name: `privatekey_20201103_${testKeyId2}.pem`,
Name: `/test_component/main/apim/private_key/privatekey_20201103_${testKeyId2}.pem`,
Value: testPrivateKey2,
},
{
Name: `privatekey_20211103_${testKeyId3}.pem`,
Name: `/test_component/main/apim/private_key/privatekey_20211103_${testKeyId3}.pem`,
Value: testPrivateKey3,
},
]);
Expand Down
14 changes: 8 additions & 6 deletions src/utils/src/key-generation-utils/get-private-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import {

const PRIVATE_KEY_REGEX = /privatekey_(\d{8})_(.+)\.pem/;

const getPrivateKeyDate = (name: string) =>
PRIVATE_KEY_REGEX.exec(name)?.[1] ?? '';

const validateParamName = (name: string) => {
// eslint-disable-next-line sonarjs/prefer-regexp-exec
const nameComponents = name?.match(PRIVATE_KEY_REGEX);
const nameComponents = PRIVATE_KEY_REGEX.exec(name);
logger.info({ description: 'validating parameter name', parameter: name });
// return true if regex matches and <date> component parses as a yyyyMMdd format
return (
Expand All @@ -37,19 +39,19 @@ const getValidPrivateKey = async (ssmPath: string) => {
// generated private key may not be valid if APIM's cache has not
// been refreshed
const [youngestKey, secondYoungestKey] = keyList.toSorted((a, b) => {
const aCreatedDate = Number(a.Name.split('_')[1]);
const bCreatedDate = Number(b.Name.split('_')[1]);
const aCreatedDate = Number(getPrivateKeyDate(a.Name));
const bCreatedDate = Number(getPrivateKeyDate(b.Name));
return bCreatedDate - aCreatedDate;
});

if (!secondYoungestKey) {
logger.info({
description: `Selecting youngest private key: ${youngestKey.Name}`,
description: `Only one private key found: ${youngestKey.Name}`,
});
return youngestKey;
}

const youngestKeyCreatedDate = youngestKey.Name.split('_')[1];
const youngestKeyCreatedDate = getPrivateKeyDate(youngestKey.Name);

const todaysDateUnformatted = new Date();
const todaysDate = format(todaysDateUnformatted, 'yyyyMMdd');
Expand Down
Loading