Skip to content

Commit 6cdd6a9

Browse files
committed
Don't write to Dynamo audit log in local environment
1 parent a2d5765 commit 6cdd6a9

File tree

6 files changed

+34
-11
lines changed

6 files changed

+34
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"setup": "git config blame.ignoreRevsFile .git-blame-ignore-revs",
1414
"build": "concurrently --names 'api,ui' 'yarn workspace infra-core-api run build' 'yarn workspace infra-core-ui run build'",
1515
"postbuild": "yarn lockfile-manage",
16-
"dev": "concurrently --names 'api,ui' 'yarn workspace infra-core-api run dev' 'yarn workspace infra-core-ui run dev'",
16+
"dev": "cross-env DISABLE_AUDIT_LOG=true concurrently --names 'api,ui' 'yarn workspace infra-core-api run dev' 'yarn workspace infra-core-ui run dev'",
1717
"lockfile-manage": "synp --with-workspace --source-file yarn.lock",
1818
"postlockfile-manage": "cp package-lock.json dist/lambda/ && cp package-lock.json dist/sqsConsumer/ && cp src/api/package.lambda.json dist/lambda/package.json && cp src/api/package.lambda.json dist/sqsConsumer/package.json && rm package-lock.json",
1919
"prettier": "yarn workspaces run prettier && prettier --check tests/**/*.ts",

src/api/functions/auditLog.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ export function buildAuditLogTransactPut({
5555
entry,
5656
}: {
5757
entry: AuditLogEntry;
58-
}): TransactWriteItem {
58+
}): TransactWriteItem | null {
59+
if (process.env.DISABLE_AUDIT_LOG && process.env.RunEnvironment === "dev") {
60+
console.log(`Audit log entry: ${JSON.stringify(entry)}`);
61+
return null;
62+
}
5963
const item = buildMarshalledAuditLogItem(entry);
6064
return {
6165
Put: {

src/api/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,27 @@ async function init(prettyPrint: boolean = false) {
224224
if (!process.env.RunEnvironment) {
225225
process.env.RunEnvironment = "dev";
226226
}
227+
227228
if (!runEnvironments.includes(process.env.RunEnvironment as RunEnvironment)) {
228229
throw new InternalServerError({
229230
message: `Invalid run environment ${app.runEnvironment}.`,
230231
});
231232
}
233+
if (process.env.DISABLE_AUDIT_LOG) {
234+
if (process.env.RunEnvironment !== "dev") {
235+
throw new InternalServerError({
236+
message: `Audit log can only be disabled if the run environment is "dev"!`,
237+
});
238+
}
239+
if (process.env.LAMBDA_TASK_ROOT || process.env.AWS_LAMBDA_FUNCTION_NAME) {
240+
throw new InternalServerError({
241+
message: `Audit log cannot be disabled when running in AWS Lambda environment!`,
242+
});
243+
}
244+
app.log.warn(
245+
"Audit logging to Dynamo is disabled! Audit log statements will be logged to the console.",
246+
);
247+
}
232248
app.runEnvironment = process.env.RunEnvironment as RunEnvironment;
233249
app.environmentConfig =
234250
environmentConfig[app.runEnvironment as RunEnvironment];

src/api/routes/apiKey.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const apiKeyRoute: FastifyPluginAsync = async (fastify, _options) => {
6565
};
6666
const command = new TransactWriteItemsCommand({
6767
TransactItems: [
68-
logStatement,
68+
...(logStatement ? [logStatement] : []),
6969
{
7070
Put: {
7171
TableName: genericConfig.ApiKeyTable,
@@ -123,7 +123,7 @@ const apiKeyRoute: FastifyPluginAsync = async (fastify, _options) => {
123123
});
124124
const command = new TransactWriteItemsCommand({
125125
TransactItems: [
126-
logStatement,
126+
...(logStatement ? [logStatement] : []),
127127
{
128128
Delete: {
129129
TableName: genericConfig.ApiKeyTable,

src/api/routes/roomRequests.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
103103
...request.body,
104104
}),
105105
};
106-
const logPut = buildAuditLogTransactPut({
106+
const logStatement = buildAuditLogTransactPut({
107107
entry: {
108108
module: Modules.ROOM_RESERVATIONS,
109109
actor: request.username!,
@@ -115,7 +115,10 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
115115
try {
116116
await fastify.dynamoClient.send(
117117
new TransactWriteItemsCommand({
118-
TransactItems: [{ Put: itemPut }, logPut],
118+
TransactItems: [
119+
{ Put: itemPut },
120+
...(logStatement ? [logStatement] : []),
121+
],
119122
}),
120123
);
121124
} catch (e) {
@@ -292,7 +295,7 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
292295
"userId#requestId": `${request.username}#${requestId}`,
293296
semesterId: request.body.semester,
294297
};
295-
const logPut = buildAuditLogTransactPut({
298+
const logStatement = buildAuditLogTransactPut({
296299
entry: {
297300
module: Modules.ROOM_RESERVATIONS,
298301
actor: request.username!,
@@ -324,7 +327,7 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
324327
}),
325328
},
326329
},
327-
logPut,
330+
...(logStatement ? [logStatement] : []),
328331
],
329332
});
330333
await fastify.dynamoClient.send(transactionCommand);

src/api/routes/stripe.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
140140
});
141141
const dynamoCommand = new TransactWriteItemsCommand({
142142
TransactItems: [
143-
logStatement,
143+
...(logStatement ? [logStatement] : []),
144144
{
145145
Put: {
146146
TableName: genericConfig.StripeLinksDynamoTableName,
@@ -245,7 +245,7 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
245245
});
246246
const dynamoCommand = new TransactWriteItemsCommand({
247247
TransactItems: [
248-
logStatement,
248+
...(logStatement ? [logStatement] : []),
249249
{
250250
Update: {
251251
TableName: genericConfig.StripeLinksDynamoTableName,
@@ -437,7 +437,7 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
437437
});
438438
const dynamoCommand = new TransactWriteItemsCommand({
439439
TransactItems: [
440-
logStatement,
440+
...(logStatement ? [logStatement] : []),
441441
{
442442
Update: {
443443
TableName: genericConfig.StripeLinksDynamoTableName,

0 commit comments

Comments
 (0)