Skip to content

Commit

Permalink
Merge pull request #99 from guardian/pf/lambdas-can-edit-their-secrets
Browse files Browse the repository at this point in the history
Pollers can update their own secrets
  • Loading branch information
bryophyta authored Jan 14, 2025
2 parents 0acedf6 + d8ff01a commit d47d778
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
20 changes: 20 additions & 0 deletions cdk/lib/__snapshots__/newswires.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2530,6 +2530,16 @@ exports[`The Newswires stack matches the snapshot 1`] = `
"Ref": "apPollerSecret4DA8E7BD",
},
},
{
"Action": [
"secretsmanager:PutSecretValue",
"secretsmanager:UpdateSecret",
],
"Effect": "Allow",
"Resource": {
"Ref": "apPollerSecret4DA8E7BD",
},
},
{
"Action": [
"sqs:ReceiveMessage",
Expand Down Expand Up @@ -3582,6 +3592,16 @@ dpkg -i /newswires/newswires.deb",
"Ref": "reutersSecretA9E37489",
},
},
{
"Action": [
"secretsmanager:PutSecretValue",
"secretsmanager:UpdateSecret",
],
"Effect": "Allow",
"Resource": {
"Ref": "reutersSecretA9E37489",
},
},
{
"Action": [
"sqs:ReceiveMessage",
Expand Down
1 change: 1 addition & 0 deletions cdk/lib/constructs/pollerLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class PollerLambda {
});

secret.grantRead(lambda);
secret.grantWrite(lambda);

// wire up lambda to process its own queue
lambda.addEventSource(new SqsEventSource(lambdaQueue, { batchSize: 1 }));
Expand Down
23 changes: 19 additions & 4 deletions poller-lambdas/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
import {
GetSecretValueCommand,
PutSecretValueCommand,
} from '@aws-sdk/client-secrets-manager';
import type { SendMessageCommandInput } from '@aws-sdk/client-sqs';
import { SendMessageCommand } from '@aws-sdk/client-sqs';
import type { PollerId } from '../../shared/pollers';
Expand All @@ -14,12 +17,13 @@ const pollerWrapper =
(pollerFunction: PollFunction) =>
async ({ Records }: HandlerInputSqsPayload) => {
const startTimeEpochMillis = Date.now();
const secretName = getEnvironmentVariableOrCrash(
POLLER_LAMBDA_ENV_VAR_KEYS.SECRET_NAME,
);
const secret = await secretsManager
.send(
new GetSecretValueCommand({
SecretId: getEnvironmentVariableOrCrash(
POLLER_LAMBDA_ENV_VAR_KEYS.SECRET_NAME,
),
SecretId: secretName,
}),
)
.then((_) => _.SecretString);
Expand Down Expand Up @@ -90,6 +94,17 @@ const pollerWrapper =
MessageBody: output.valueForNextPoll,
});
}

if (output.newSecretValue) {
// set new value in secrets manager
console.log(`Updating secret value for ${secretName}`);
await secretsManager.send(
new PutSecretValueCommand({
SecretId: secretName,
SecretString: output.newSecretValue,
}),
);
}
})
.catch((error) => {
console.error('FAILED', error);
Expand Down
1 change: 1 addition & 0 deletions poller-lambdas/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type PollerInput = string;
export interface CorePollerOutput {
payloadForIngestionLambda: IngestorPayload[] | IngestorPayload;
valueForNextPoll: PollerInput;
newSecretValue?: SecretValue;
}
export type LongPollOutput = CorePollerOutput;

Expand Down

0 comments on commit d47d778

Please sign in to comment.