@@ -9,21 +9,54 @@ import {
9
9
getUserProfile ,
10
10
} from "../../api/functions/entraId.js" ;
11
11
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" ;
13
17
import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager" ;
14
18
import { issueAppleWalletMembershipCard } from "../../api/functions/mobileWallet.js" ;
15
19
import { generateMembershipEmailCommand } from "../../api/functions/ses.js" ;
16
20
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
+ } ;
17
53
18
54
export const emailMembershipPassHandler : SQSHandlerFunction <
19
55
AvailableSQSFunctions . EmailMembershipPass
20
56
> = async ( payload , _metadata , logger ) => {
21
57
const email = payload . email ;
22
58
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 ) ;
27
60
const entraIdToken = await getEntraIdToken (
28
61
clients ,
29
62
currentEnvironmentConfig . AadValidClientId ,
0 commit comments