Skip to content

Commit a9556f4

Browse files
committed
change configs to live centrally in config.ts
1 parent 1e64bc2 commit a9556f4

File tree

4 files changed

+75
-44
lines changed

4 files changed

+75
-44
lines changed

cloudformation/main.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,10 @@ Mappings:
3939
ApiCertificateArn: arn:aws:acm:us-east-1:427040638965:certificate/63ccdf0b-d2b5-44f0-b589-eceffb935c23
4040
HostedZoneId: Z04502822NVIA85WM2SML
4141
ApiDomainName: "aws.qa.acmuiuc.org"
42-
ValidCorsOrigins: ["*"]
43-
AadValidClientId: "39c28870-94e4-47ee-b4fb-affe0bf96c9f"
4442
prod:
4543
ApiCertificateArn: arn:aws:acm:us-east-1:298118738376:certificate/6142a0e2-d62f-478e-bf15-5bdb616fe705
4644
HostedZoneId: Z05246633460N5MEB9DBF
4745
ApiDomainName: "aws.acmuiuc.org" # CHANGE ME
48-
ValidCorsOrigins: ["https://acm.illinois.edu", "https://www.acm.illinois.edu"]
49-
AadValidClientId: "5e08cf0f-53bb-4e09-9df2-e9bdc3467296"
5046
EnvironmentToCidr:
5147
dev:
5248
SecurityGroupIds:
@@ -97,8 +93,6 @@ Resources:
9793
Environment:
9894
Variables:
9995
RunEnvironment: !Ref RunEnvironment
100-
ValidCorsOrigins: !Join [",", !FindInMap [ApiGwConfig, !Ref RunEnvironment, ValidCorsOrigins]]
101-
AadValidClientId: !FindInMap [ApiGwConfig, !Ref RunEnvironment, AadValidClientId]
10296
VpcConfig:
10397
Ipv6AllowedForDualStack: !If [ShouldAttachVpc, True, !Ref AWS::NoValue]
10498
SecurityGroupIds: !If [ShouldAttachVpc, !FindInMap [EnvironmentToCidr, !Ref RunEnvironment, SecurityGroupIds], !Ref AWS::NoValue]

src/config.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,51 @@
1-
export default {
2-
DYNAMO_TABLE_NAME: "infra-events-api-records",
1+
import { AppRoles, RunEnvironment } from "./roles.js";
2+
3+
type GroupRoleMapping = Record<string, AppRoles[]>;
4+
type AzureRoleMapping = Record<string, AppRoles[]>;
5+
6+
type ConfigType = {
7+
GroupRoleMapping: GroupRoleMapping;
8+
AzureRoleMapping: AzureRoleMapping;
9+
ValidCorsOrigins: (string | RegExp)[];
10+
AadValidClientId: string;
311
};
12+
13+
type GenericConfigType = {
14+
DynamoTableName: string;
15+
ConfigSecretName: string;
16+
};
17+
18+
type EnvironmentConfigType = {
19+
[env in RunEnvironment]: ConfigType;
20+
};
21+
22+
const genericConfig: GenericConfigType = {
23+
DynamoTableName: "infra-events-api-records",
24+
ConfigSecretName: "infra-events-api-config",
25+
} as const;
26+
27+
const environmentConfig: EnvironmentConfigType = {
28+
dev: {
29+
GroupRoleMapping: {
30+
"48591dbc-cdcb-4544-9f63-e6b92b067e33": [AppRoles.MANAGER], // Infra Chairs
31+
"940e4f9e-6891-4e28-9e29-148798495cdb": [AppRoles.MANAGER], // ACM Infra Team
32+
"f8dfc4cf-456b-4da3-9053-f7fdeda5d5d6": [AppRoles.MANAGER], // Infra Leads
33+
"0": [AppRoles.MANAGER], // Dummy Group for development only
34+
},
35+
AzureRoleMapping: { AutonomousWriters: [AppRoles.MANAGER] },
36+
ValidCorsOrigins: ['http://localhost:3000', /\.acmuiuc\.\.pages\.dev$/, 'https://acmuiuc.pages.dev'],
37+
AadValidClientId: '39c28870-94e4-47ee-b4fb-affe0bf96c9f'
38+
},
39+
prod: {
40+
GroupRoleMapping: {
41+
"48591dbc-cdcb-4544-9f63-e6b92b067e33": [AppRoles.MANAGER], // Infra Chairs
42+
"ff49e948-4587-416b-8224-65147540d5fc": [AppRoles.MANAGER], // Officers
43+
"ad81254b-4eeb-4c96-8191-3acdce9194b1": [AppRoles.MANAGER], // Exec
44+
},
45+
AzureRoleMapping: { AutonomousWriters: [AppRoles.MANAGER] },
46+
ValidCorsOrigins: ['https:///acm.illinois.edu', 'https:///www.acm.illinois.edu'],
47+
AadValidClientId: '5e08cf0f-53bb-4e09-9df2-e9bdc3467296'
48+
},
49+
} as const;
50+
51+
export { genericConfig, environmentConfig };

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { InternalServerError } from "./errors/index.js";
1010
import eventsPlugin from "./routes/events.js";
1111
import cors from "@fastify/cors";
1212
import fastifyZodValidationPlugin from "./plugins/validate.js";
13+
import { environmentConfig } from "./config.js";
1314

1415
const now = () => Date.now();
1516

@@ -66,7 +67,7 @@ async function init() {
6667
{ prefix: "/api/v1" },
6768
);
6869
await app.register(cors, {
69-
origin: (process.env.ValidCorsOrigins || "*").split(","),
70+
origin: environmentConfig[app.runEnvironment].ValidCorsOrigins,
7071
});
7172

7273
return app;

src/plugins/auth.ts

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,14 @@ import {
66
SecretsManagerClient,
77
GetSecretValueCommand,
88
} from "@aws-sdk/client-secrets-manager";
9-
import { AppRoles, RunEnvironment } from "../roles.js";
9+
import { AppRoles } from "../roles.js";
1010
import {
1111
BaseError,
1212
InternalServerError,
1313
UnauthenticatedError,
1414
UnauthorizedError,
1515
} from "../errors/index.js";
16-
17-
const CONFIG_SECRET_NAME = "infra-events-api-config" as const;
18-
const AzureRoleMapping: Record<RunEnvironment, Record<string, AppRoles[]>> = {
19-
prod: {
20-
AutonomousWriters: [AppRoles.MANAGER],
21-
},
22-
dev: {
23-
AutonomousWriters: [AppRoles.MANAGER],
24-
},
25-
};
26-
27-
const GroupRoleMapping: Record<RunEnvironment, Record<string, AppRoles[]>> = {
28-
prod: {
29-
"48591dbc-cdcb-4544-9f63-e6b92b067e33": [AppRoles.MANAGER], // Infra Chairs
30-
"ff49e948-4587-416b-8224-65147540d5fc": [AppRoles.MANAGER], // Officers
31-
"ad81254b-4eeb-4c96-8191-3acdce9194b1": [AppRoles.MANAGER], // Exec
32-
},
33-
dev: {
34-
"48591dbc-cdcb-4544-9f63-e6b92b067e33": [AppRoles.MANAGER], // Infra Chairs
35-
"940e4f9e-6891-4e28-9e29-148798495cdb": [AppRoles.MANAGER], // ACM Infra Team
36-
"f8dfc4cf-456b-4da3-9053-f7fdeda5d5d6": [AppRoles.MANAGER], // Infra Leads
37-
"0": [AppRoles.MANAGER], // Dummy Group for development only
38-
},
39-
};
16+
import { environmentConfig, genericConfig } from "../config.js";
4017

4118
function intersection<T>(setA: Set<T>, setB: Set<T>): Set<T> {
4219
const _intersection = new Set<T>();
@@ -133,8 +110,11 @@ const authPlugin: FastifyPluginAsync = async (fastify, _options) => {
133110
}
134111
signingKey =
135112
process.env.JwtSigningKey ||
136-
(((await getSecretValue(CONFIG_SECRET_NAME)) || { jwt_key: "" })
137-
.jwt_key as string) ||
113+
((
114+
(await getSecretValue(genericConfig.ConfigSecretName)) || {
115+
jwt_key: "",
116+
}
117+
).jwt_key as string) ||
138118
"";
139119
if (signingKey === "") {
140120
throw new UnauthenticatedError({
@@ -181,24 +161,32 @@ const authPlugin: FastifyPluginAsync = async (fastify, _options) => {
181161
const expectedRoles = new Set(validRoles);
182162
if (verifiedTokenData.groups) {
183163
for (const group of verifiedTokenData.groups) {
184-
if (!GroupRoleMapping[fastify.runEnvironment][group]) {
164+
if (
165+
!environmentConfig[fastify.runEnvironment]["GroupRoleMapping"][
166+
group
167+
]
168+
) {
185169
continue;
186170
}
187-
for (const role of GroupRoleMapping[fastify.runEnvironment][
188-
group
189-
]) {
171+
for (const role of environmentConfig[fastify.runEnvironment][
172+
"GroupRoleMapping"
173+
][group]) {
190174
userRoles.add(role);
191175
}
192176
}
193177
} else {
194178
if (verifiedTokenData.roles) {
195179
for (const group of verifiedTokenData.roles) {
196-
if (!AzureRoleMapping[fastify.runEnvironment][group]) {
180+
if (
181+
!environmentConfig[fastify.runEnvironment]["AzureRoleMapping"][
182+
group
183+
]
184+
) {
197185
continue;
198186
}
199-
for (const role of AzureRoleMapping[fastify.runEnvironment][
200-
group
201-
]) {
187+
for (const role of environmentConfig[fastify.runEnvironment][
188+
"AzureRoleMapping"
189+
][group]) {
202190
userRoles.add(role);
203191
}
204192
}

0 commit comments

Comments
 (0)