Skip to content

Commit ad03e1e

Browse files
committed
cache secrets for 90 seconds
1 parent 551ce57 commit ad03e1e

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

src/api/index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,22 @@ async function init(prettyPrint: boolean = false) {
232232
app.runEnvironment = process.env.RunEnvironment as RunEnvironment;
233233
app.environmentConfig =
234234
environmentConfig[app.runEnvironment as RunEnvironment];
235-
app.nodeCache = new NodeCache({ checkperiod: 30 });
235+
app.nodeCache = new NodeCache({ checkperiod: 15 });
236236
app.dynamoClient = dynamoClient;
237237
app.secretsManagerClient = secretsManagerClient;
238238
app.redisClient = redisClient;
239-
app.secretConfig = secret;
240-
app.refreshSecretConfig = async () => {
241-
app.secretConfig = (await getSecretValue(
242-
app.secretsManagerClient,
243-
genericConfig.ConfigSecretName,
244-
)) as SecretConfig;
239+
app.getCachedSecret = async (secretName: string) => {
240+
const cacheKey = `_SECRET:${secretName}`;
241+
const cachedValue = app.nodeCache.get(cacheKey);
242+
if (!cachedValue) {
243+
const realValue = (await getSecretValue(
244+
app.secretsManagerClient,
245+
secretName,
246+
)) as SecretConfig;
247+
app.nodeCache.set(cacheKey, JSON.stringify(realValue), 90);
248+
return realValue as SecretConfig;
249+
}
250+
return cachedValue as SecretConfig;
245251
};
246252
app.addHook("onRequest", (req, _, done) => {
247253
req.startTime = now();

src/api/plugins/auth.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
UnauthenticatedError,
1414
UnauthorizedError,
1515
} from "../../common/errors/index.js";
16-
import { SecretConfig } from "../../common/config.js";
16+
import { genericConfig, SecretConfig } from "../../common/config.js";
1717
import {
1818
AUTH_DECISION_CACHE_SECONDS,
1919
getGroupRoles,
@@ -193,10 +193,11 @@ const authPlugin: FastifyPluginAsync = async (fastify, _options) => {
193193
message: "Custom JWTs cannot be used in Prod environment.",
194194
});
195195
}
196+
const config = await fastify.getCachedSecret(
197+
genericConfig.ConfigSecretName,
198+
);
196199
signingKey =
197-
process.env.JwtSigningKey ||
198-
(fastify.secretConfig.jwt_key as string) ||
199-
"";
200+
process.env.JwtSigningKey || (config.jwt_key as string) || "";
200201
if (signingKey === "") {
201202
throw new UnauthenticatedError({
202203
message: "Invalid token.",

src/api/routes/events.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ const eventsPlugin: FastifyPluginAsyncZodOpenApi = async (
358358
try {
359359
if (request.body.featured && !request.body.repeats) {
360360
await updateDiscord(
361-
fastify.secretConfig,
361+
await fastify.getCachedSecret(genericConfig.ConfigSecretName),
362362
entry,
363363
request.username,
364364
false,
@@ -496,7 +496,7 @@ const eventsPlugin: FastifyPluginAsyncZodOpenApi = async (
496496
}),
497497
);
498498
await updateDiscord(
499-
fastify.secretConfig,
499+
await fastify.getCachedSecret(genericConfig.ConfigSecretName),
500500
{ id } as IUpdateDiscord,
501501
request.username,
502502
true,

src/api/routes/stripe.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
106106
if (!request.username) {
107107
throw new UnauthenticatedError({ message: "No username found" });
108108
}
109-
const secretApiConfig = fastify.secretConfig;
109+
const secretApiConfig = await fastify.getCachedSecret(
110+
genericConfig.ConfigSecretName,
111+
);
110112
const payload: StripeLinkCreateParams = {
111113
...request.body,
112114
createdBy: request.username,

src/api/types.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ declare module "fastify" {
3636
redisClient: Redis;
3737
secretsManagerClient: SecretsManagerClient;
3838
cloudfrontKvClient: CloudFrontKeyValueStoreClient;
39-
secretConfig: SecretConfig;
40-
refreshSecretConfig: CallableFunction;
39+
getCachedSecret: (secretName: string) => Promise<SecretConfig>;
4140
}
4241
interface FastifyRequest {
4342
startTime: number;

0 commit comments

Comments
 (0)