Skip to content

Don't write to Dynamo audit log in local environment #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"setup": "git config blame.ignoreRevsFile .git-blame-ignore-revs",
"build": "concurrently --names 'api,ui' 'yarn workspace infra-core-api run build' 'yarn workspace infra-core-ui run build'",
"postbuild": "yarn lockfile-manage",
"dev": "concurrently --names 'api,ui' 'yarn workspace infra-core-api run dev' 'yarn workspace infra-core-ui run dev'",
"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'",
"lockfile-manage": "synp --with-workspace --source-file yarn.lock",
"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",
"prettier": "yarn workspaces run prettier && prettier --check tests/**/*.ts",
Expand Down
6 changes: 5 additions & 1 deletion src/api/functions/auditLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
entry,
}: AuditLogParams) {
if (process.env.DISABLE_AUDIT_LOG && process.env.RunEnvironment === "dev") {
console.log(`Audit log entry: ${JSON.stringify(entry)}`);

Check warning on line 35 in src/api/functions/auditLog.ts

View workflow job for this annotation

GitHub Actions / Run Unit Tests

Unexpected console statement
return;
}
const safeDynamoClient =
Expand All @@ -55,7 +55,11 @@
entry,
}: {
entry: AuditLogEntry;
}): TransactWriteItem {
}): TransactWriteItem | null {
if (process.env.DISABLE_AUDIT_LOG && process.env.RunEnvironment === "dev") {
console.log(`Audit log entry: ${JSON.stringify(entry)}`);

Check warning on line 60 in src/api/functions/auditLog.ts

View workflow job for this annotation

GitHub Actions / Run Unit Tests

Unexpected console statement
return null;
}
const item = buildMarshalledAuditLogItem(entry);
return {
Put: {
Expand Down
16 changes: 16 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,27 @@ async function init(prettyPrint: boolean = false) {
if (!process.env.RunEnvironment) {
process.env.RunEnvironment = "dev";
}

if (!runEnvironments.includes(process.env.RunEnvironment as RunEnvironment)) {
throw new InternalServerError({
message: `Invalid run environment ${app.runEnvironment}.`,
});
}
if (process.env.DISABLE_AUDIT_LOG) {
if (process.env.RunEnvironment !== "dev") {
throw new InternalServerError({
message: `Audit log can only be disabled if the run environment is "dev"!`,
});
}
if (process.env.LAMBDA_TASK_ROOT || process.env.AWS_LAMBDA_FUNCTION_NAME) {
throw new InternalServerError({
message: `Audit log cannot be disabled when running in AWS Lambda environment!`,
});
}
app.log.warn(
"Audit logging to Dynamo is disabled! Audit log statements will be logged to the console.",
);
}
app.runEnvironment = process.env.RunEnvironment as RunEnvironment;
app.environmentConfig =
environmentConfig[app.runEnvironment as RunEnvironment];
Expand Down
4 changes: 2 additions & 2 deletions src/api/routes/apiKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const apiKeyRoute: FastifyPluginAsync = async (fastify, _options) => {
};
const command = new TransactWriteItemsCommand({
TransactItems: [
logStatement,
...(logStatement ? [logStatement] : []),
{
Put: {
TableName: genericConfig.ApiKeyTable,
Expand Down Expand Up @@ -123,7 +123,7 @@ const apiKeyRoute: FastifyPluginAsync = async (fastify, _options) => {
});
const command = new TransactWriteItemsCommand({
TransactItems: [
logStatement,
...(logStatement ? [logStatement] : []),
{
Delete: {
TableName: genericConfig.ApiKeyTable,
Expand Down
11 changes: 7 additions & 4 deletions src/api/routes/roomRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
...request.body,
}),
};
const logPut = buildAuditLogTransactPut({
const logStatement = buildAuditLogTransactPut({
entry: {
module: Modules.ROOM_RESERVATIONS,
actor: request.username!,
Expand All @@ -115,7 +115,10 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
try {
await fastify.dynamoClient.send(
new TransactWriteItemsCommand({
TransactItems: [{ Put: itemPut }, logPut],
TransactItems: [
{ Put: itemPut },
...(logStatement ? [logStatement] : []),
],
}),
);
} catch (e) {
Expand Down Expand Up @@ -292,7 +295,7 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
"userId#requestId": `${request.username}#${requestId}`,
semesterId: request.body.semester,
};
const logPut = buildAuditLogTransactPut({
const logStatement = buildAuditLogTransactPut({
entry: {
module: Modules.ROOM_RESERVATIONS,
actor: request.username!,
Expand Down Expand Up @@ -324,7 +327,7 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
}),
},
},
logPut,
...(logStatement ? [logStatement] : []),
],
});
await fastify.dynamoClient.send(transactionCommand);
Expand Down
6 changes: 3 additions & 3 deletions src/api/routes/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
});
const dynamoCommand = new TransactWriteItemsCommand({
TransactItems: [
logStatement,
...(logStatement ? [logStatement] : []),
{
Put: {
TableName: genericConfig.StripeLinksDynamoTableName,
Expand Down Expand Up @@ -245,7 +245,7 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
});
const dynamoCommand = new TransactWriteItemsCommand({
TransactItems: [
logStatement,
...(logStatement ? [logStatement] : []),
{
Update: {
TableName: genericConfig.StripeLinksDynamoTableName,
Expand Down Expand Up @@ -437,7 +437,7 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
});
const dynamoCommand = new TransactWriteItemsCommand({
TransactItems: [
logStatement,
...(logStatement ? [logStatement] : []),
{
Update: {
TableName: genericConfig.StripeLinksDynamoTableName,
Expand Down
Loading