Skip to content

Commit 3286e39

Browse files
authored
enable read only entra client (#106)
* enable read only entra client * fix IAM
1 parent ca4496e commit 3286e39

File tree

5 files changed

+18
-11
lines changed

5 files changed

+18
-11
lines changed

cloudformation/iam.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ Resources:
176176
Effect: Allow
177177
Resource:
178178
- Fn::Sub: arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:infra-core-api-entra*
179+
- Fn::Sub: arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:infra-core-api-ro-entra*
179180

180181
# SQS Lambda IAM Role
181182
SqsLambdaIAMRole:

src/api/functions/entraId.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ export async function getEntraIdToken(
3737
clients: { smClient: SecretsManagerClient; dynamoClient: DynamoDBClient },
3838
clientId: string,
3939
scopes: string[] = ["https://graph.microsoft.com/.default"],
40+
secretName?: string,
4041
) {
42+
if (!secretName) {
43+
secretName = genericConfig.EntraSecretName;
44+
}
4145
const secretApiConfig =
42-
(await getSecretValue(clients.smClient, genericConfig.EntraSecretName)) ||
43-
{};
46+
(await getSecretValue(clients.smClient, secretName)) || {};
4447
if (
4548
!secretApiConfig.entra_id_private_key ||
4649
!secretApiConfig.entra_id_thumbprint
@@ -55,7 +58,7 @@ export async function getEntraIdToken(
5558
).toString("utf8");
5659
const cachedToken = await getItemFromCache(
5760
clients.dynamoClient,
58-
"entra_id_access_token",
61+
`entra_id_access_token_${secretName}`,
5962
);
6063
if (cachedToken) {
6164
return cachedToken["token"] as string;
@@ -85,7 +88,7 @@ export async function getEntraIdToken(
8588
if (result?.accessToken) {
8689
await insertItemIntoCache(
8790
clients.dynamoClient,
88-
"entra_id_access_token",
91+
`entra_id_access_token_${secretName}`,
8992
{ token: result?.accessToken },
9093
date,
9194
);

src/api/routes/iam.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FastifyPluginAsync } from "fastify";
2-
import { allAppRoles, AppRoles } from "../../common/roles.js";
2+
import { AppRoles } from "../../common/roles.js";
33
import { zodToJsonSchema } from "zod-to-json-schema";
44
import {
55
addToTenant,
@@ -16,7 +16,6 @@ import {
1616
EntraInvitationError,
1717
InternalServerError,
1818
NotFoundError,
19-
UnauthorizedError,
2019
} from "../../common/errors/index.js";
2120
import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb";
2221
import { genericConfig, roleArns } from "../../common/config.js";
@@ -465,7 +464,9 @@ const iamRoutes: FastifyPluginAsync = async (fastify, _options) => {
465464
}
466465
const entraIdToken = await getEntraIdToken(
467466
await getAuthorizedClients(),
468-
fastify.environmentConfig.AadValidClientId,
467+
fastify.environmentConfig.AadValidReadOnlyClientId,
468+
undefined,
469+
genericConfig.EntraReadOnlySecretName,
469470
);
470471
const response = await listGroupMembers(entraIdToken, groupId);
471472
reply.status(200).send(response);

src/api/routes/roomRequests.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import rateLimiter from "api/plugins/rateLimiter.js";
33
import {
44
formatStatus,
55
roomGetResponse,
6-
roomRequestBaseSchema,
76
RoomRequestFormValues,
87
roomRequestPostResponse,
98
roomRequestSchema,
@@ -18,7 +17,6 @@ import {
1817
DatabaseFetchError,
1918
DatabaseInsertError,
2019
InternalServerError,
21-
UnauthenticatedError,
2220
} from "common/errors/index.js";
2321
import {
2422
PutItemCommand,
@@ -27,7 +25,6 @@ import {
2725
} from "@aws-sdk/client-dynamodb";
2826
import { genericConfig, notificationRecipients } from "common/config.js";
2927
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
30-
import { z } from "zod";
3128
import { AvailableSQSFunctions, SQSPayload } from "common/types/sqsMessage.js";
3229
import { SendMessageCommand, SQSClient } from "@aws-sdk/client-sqs";
3330

src/common/config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type ConfigType = {
2020
SqsQueueUrl: string;
2121
PaidMemberGroupId: string;
2222
PaidMemberPriceId: string;
23+
AadValidReadOnlyClientId: string;
2324
};
2425

2526
export type GenericConfigType = {
@@ -42,14 +43,15 @@ export type GenericConfigType = {
4243
ProtectedEntraIDGroups: string[]; // these groups are too privileged to be modified via this portal and must be modified directly in Entra ID.
4344
RoomRequestsTableName: string;
4445
RoomRequestsStatusTableName: string;
46+
EntraReadOnlySecretName: string;
4547
};
4648

4749
type EnvironmentConfigType = {
4850
[env in RunEnvironment]: ConfigType;
4951
};
5052

5153
export const infraChairsGroupId = "c0702752-50da-49da-83d4-bcbe6f7a9b1b";
52-
export const officersGroupId = "ff49e948-4587-416b-8224-65147540d5fc";
54+
export const officersGroupId = "c4ddcc9f-a9c0-47e7-98c1-f1b345d53121";
5355
export const officersGroupTestingId = "0e6e9199-506f-4ede-9d1b-e73f6811c9e5";
5456
export const execCouncilGroupId = "ad81254b-4eeb-4c96-8191-3acdce9194b1";
5557
export const execCouncilTestingGroupId = "dbe18eb2-9675-46c4-b1ef-749a6db4fedd";
@@ -64,6 +66,7 @@ const genericConfig: GenericConfigType = {
6466
CacheDynamoTableName: "infra-core-api-cache",
6567
ConfigSecretName: "infra-core-api-config",
6668
EntraSecretName: "infra-core-api-entra",
69+
EntraReadOnlySecretName: "infra-core-api-ro-entra",
6770
UpcomingEventThresholdSeconds: 1800, // 30 mins
6871
AwsRegion: process.env.AWS_REGION || "us-east-1",
6972
EntraTenantId: "c8d9148f-9a59-4db3-827d-42ea0c2b6e2e",
@@ -99,6 +102,7 @@ const environmentConfig: EnvironmentConfigType = {
99102
"https://sqs.us-east-1.amazonaws.com/427040638965/infra-core-api-sqs",
100103
PaidMemberGroupId: "9222451f-b354-4e64-ba28-c0f367a277c2",
101104
PaidMemberPriceId: "price_1R4TcTDGHrJxx3mKI6XF9cNG",
105+
AadValidReadOnlyClientId: "2c6a0057-5acc-496c-a4e5-4adbf88387ba"
102106
},
103107
prod: {
104108
UserFacingUrl: "https://core.acm.illinois.edu",
@@ -119,6 +123,7 @@ const environmentConfig: EnvironmentConfigType = {
119123
"https://sqs.us-east-1.amazonaws.com/298118738376/infra-core-api-sqs",
120124
PaidMemberGroupId: "172fd9ee-69f0-4384-9786-41ff1a43cf8e",
121125
PaidMemberPriceId: "price_1MUGIRDiGOXU9RuSChPYK6wZ",
126+
AadValidReadOnlyClientId: "2c6a0057-5acc-496c-a4e5-4adbf88387ba"
122127
},
123128
};
124129

0 commit comments

Comments
 (0)