Skip to content

Commit 146e35c

Browse files
committed
let SQS handler assume entra role
1 parent 173658b commit 146e35c

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

cloudformation/iam.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ Resources:
168168
- Fn::GetAtt: ApiLambdaIAMRole.Arn
169169
Action:
170170
- sts:AssumeRole
171+
- Effect: Allow
172+
Principal:
173+
AWS:
174+
- Fn::GetAtt: SqsLambdaIAMRole.Arn
175+
Action:
176+
- sts:AssumeRole
171177
Policies:
172178
- PolicyName: lambda-get-entra-secret
173179
PolicyDocument:

src/api/sqs/handlers.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,54 @@ import {
99
getUserProfile,
1010
} from "../../api/functions/entraId.js";
1111
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
12-
import { environmentConfig, genericConfig } from "../../common/config.js";
12+
import {
13+
environmentConfig,
14+
genericConfig,
15+
roleArns,
16+
} from "../../common/config.js";
1317
import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager";
1418
import { issueAppleWalletMembershipCard } from "../../api/functions/mobileWallet.js";
1519
import { generateMembershipEmailCommand } from "../../api/functions/ses.js";
1620
import { SESClient } from "@aws-sdk/client-ses";
21+
import pino from "pino";
22+
import { getRoleCredentials } from "api/functions/sts.js";
23+
24+
const getAuthorizedClients = async (
25+
logger: pino.Logger,
26+
commonConfig: { region: string },
27+
) => {
28+
if (roleArns.Entra) {
29+
logger.info(
30+
`Attempting to assume Entra role ${roleArns.Entra} to get the Entra token...`,
31+
);
32+
const credentials = await getRoleCredentials(roleArns.Entra);
33+
const clients = {
34+
smClient: new SecretsManagerClient({
35+
region: genericConfig.AwsRegion,
36+
credentials,
37+
}),
38+
dynamoClient: new DynamoDBClient({
39+
region: genericConfig.AwsRegion,
40+
credentials,
41+
}),
42+
};
43+
logger.info(`Assumed Entra role ${roleArns.Entra} to get the Entra token.`);
44+
return clients;
45+
} else {
46+
logger.debug("Did not assume Entra role as no env variable was present");
47+
return {
48+
smClient: new SecretsManagerClient(commonConfig),
49+
dynamoClient: new DynamoDBClient(commonConfig),
50+
};
51+
}
52+
};
1753

1854
export const emailMembershipPassHandler: SQSHandlerFunction<
1955
AvailableSQSFunctions.EmailMembershipPass
2056
> = async (payload, _metadata, logger) => {
2157
const email = payload.email;
2258
const commonConfig = { region: genericConfig.AwsRegion };
23-
const clients = {
24-
smClient: new SecretsManagerClient(commonConfig),
25-
dynamoClient: new DynamoDBClient(commonConfig),
26-
};
59+
const clients = await getAuthorizedClients(logger, commonConfig);
2760
const entraIdToken = await getEntraIdToken(
2861
clients,
2962
currentEnvironmentConfig.AadValidClientId,

0 commit comments

Comments
 (0)