Skip to content

Commit 7cc74b7

Browse files
committed
wip: store audit logs into a different table
1 parent 6fbf703 commit 7cc74b7

File tree

6 files changed

+114
-16
lines changed

6 files changed

+114
-16
lines changed

jest.config.base.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,27 @@ const createDynaliteTables = (options = {}) => {
180180
amount: 5
181181
}),
182182
data: options.data || []
183+
},
184+
{
185+
TableName: process.env.DB_TABLE_AUDIT_LOGS,
186+
KeySchema: [
187+
{ AttributeName: "PK", KeyType: "HASH" },
188+
{ AttributeName: "SK", KeyType: "RANGE" }
189+
],
190+
AttributeDefinitions: [
191+
{ AttributeName: "PK", AttributeType: "S" },
192+
{ AttributeName: "SK", AttributeType: "S" },
193+
...createGlobalSecondaryIndexesAttributeDefinitions(2)
194+
],
195+
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
196+
GlobalSecondaryIndexes: createGlobalSecondaryIndexes({
197+
amount: 2
198+
}),
199+
data: options.data || [],
200+
ttl: {
201+
attributeName: "expiresAt",
202+
enabled: true
203+
}
183204
}
184205
],
185206
basePort: 8000

packages/api-audit-logs/__tests__/createAuditLog.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe("create audit log", () => {
8484
const partitionKey = `#CME#wby-aco-${result!.id}`;
8585

8686
const scanned = await client.scan({
87-
TableName: process.env.DB_TABLE
87+
TableName: process.env.DB_TABLE_AUDIT_LOGS
8888
});
8989

9090
for (const item of scanned.Items || []) {
@@ -149,7 +149,7 @@ describe("create audit log", () => {
149149
const { id: partitionKey } = parseIdentifier(`${result!.id}`);
150150

151151
const scanned = await client.scan({
152-
TableName: process.env.DB_TABLE
152+
TableName: process.env.DB_TABLE_AUDIT_LOGS
153153
});
154154

155155
for (const item of scanned.Items || []) {

packages/cwp-template-aws/template/common/types/env/index.d.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ declare namespace NodeJS {
22
export interface ProcessEnv {
33
NODE_ENV?: "test" | "prod" | "dev" | string;
44
DB_TABLE?: string;
5-
DB_TABLE_TENANCY?: string;
6-
DB_TABLE_PRERENDERING_SERVICE?: string;
75
DB_TABLE_ELASTICSEARCH?: string;
8-
DB_TABLE_ADMIN_USERS?: string;
9-
DB_TABLE_FILE_MANGER?: string;
10-
DB_TABLE_HEADLESS_CMS?: string;
11-
DB_PAGE_BUILDER?: string;
12-
DB_TABLE_PAGE_BUILDER?: string;
6+
DB_TABLE_LOG?: string;
7+
DB_TABLE_AUDIT_LOGS?: string;
138
ELASTICSEARCH_SHARED_INDEXES?: "true" | "false" | string;
149
WEBINY_VERSION?: string;
1510
WEBINY_ENABLE_VERSION_HEADER?: "true" | "false" | string;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as aws from "@pulumi/aws";
2+
import type { PulumiApp, PulumiAppModule } from "@webiny/pulumi";
3+
import { createAppModule } from "@webiny/pulumi";
4+
5+
export type AuditLogsDynamo = PulumiAppModule<typeof AuditLogsDynamo>;
6+
7+
export const AuditLogsDynamo = createAppModule({
8+
name: "AuditLogsDynamoDb",
9+
config(app: PulumiApp, params: { protect: boolean }) {
10+
return app.addResource(aws.dynamodb.Table, {
11+
name: "webiny-audit-logs",
12+
config: {
13+
attributes: [
14+
{ name: "PK", type: "S" },
15+
{ name: "SK", type: "S" },
16+
{ name: "GSI1_PK", type: "S" },
17+
{ name: "GSI1_SK", type: "S" },
18+
{ name: "GSI2_PK", type: "S" },
19+
{ name: "GSI2_SK", type: "S" },
20+
{ name: "GSI3_PK", type: "S" },
21+
{ name: "GSI3_SK", type: "S" },
22+
{ name: "GSI4_PK", type: "S" },
23+
{ name: "GSI4_SK", type: "S" },
24+
{ name: "GSI5_PK", type: "S" },
25+
{ name: "GSI5_SK", type: "S" }
26+
],
27+
billingMode: "PAY_PER_REQUEST",
28+
hashKey: "PK",
29+
rangeKey: "SK",
30+
globalSecondaryIndexes: [
31+
{
32+
name: "GSI1",
33+
hashKey: "GSI1_PK",
34+
rangeKey: "GSI1_SK",
35+
projectionType: "ALL"
36+
},
37+
{
38+
name: "GSI2",
39+
hashKey: "GSI2_PK",
40+
rangeKey: "GSI2_SK",
41+
projectionType: "ALL"
42+
},
43+
{
44+
name: "GSI3",
45+
hashKey: "GSI3_PK",
46+
rangeKey: "GSI3_SK",
47+
projectionType: "ALL"
48+
},
49+
{
50+
name: "GSI4",
51+
hashKey: "GSI4_PK",
52+
rangeKey: "GSI4_SK",
53+
projectionType: "ALL"
54+
},
55+
{
56+
name: "GSI5",
57+
hashKey: "GSI5_PK",
58+
rangeKey: "GSI5_SK",
59+
projectionType: "ALL"
60+
}
61+
],
62+
ttl: {
63+
attributeName: "expiresAt",
64+
enabled: true
65+
}
66+
},
67+
opts: {
68+
protect: params.protect
69+
}
70+
});
71+
}
72+
});

packages/pulumi-aws/src/apps/api/createApiPulumiApp.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { attachSyncSystem } from "../syncSystem/api/index.js";
3131
import { getAwsAccountId } from "~/apps/awsUtils";
3232
import type { WithServiceManifest } from "~/utils/withServiceManifest.js";
3333
import { ApiScheduler } from "~/apps/api/ApiScheduler.js";
34+
import { AuditLogsDynamo } from "~/apps/api/AuditLogsDynamo.js";
3435

3536
export type ApiPulumiApp = ReturnType<typeof createApiPulumiApp>;
3637

@@ -47,6 +48,12 @@ export interface ApiOpenSearchConfig {
4748
}
4849

4950
export interface CreateApiPulumiAppParams {
51+
/**
52+
* Secures against deleting database by accident.
53+
* By default enabled in production environments.
54+
*/
55+
protect?: PulumiAppParam<boolean>;
56+
5057
/**
5158
* Enables ElasticSearch infrastructure.
5259
* Note that it requires also changes in application code.
@@ -151,6 +158,8 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
151158
const vpcEnabled = app.getParam(projectAppParams?.vpc) ?? isProduction;
152159
app.addModule(VpcConfig, { enabled: vpcEnabled });
153160

161+
const protect = app.getParam(projectAppParams.protect) ?? isProduction;
162+
154163
// const pageBuilder = app.addModule(ApiPageBuilder, {
155164
// env: {
156165
// COGNITO_REGION: getEnvVariableAwsRegion(),
@@ -169,6 +178,10 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
169178
// }
170179
// });
171180

181+
const auditLogsDynamo = app.addModule(AuditLogsDynamo, {
182+
protect
183+
});
184+
172185
const apwScheduler = app.addModule(ApiApwScheduler, {
173186
primaryDynamodbTableArn: core.primaryDynamodbTableArn,
174187

@@ -177,6 +190,7 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
177190
COGNITO_USER_POOL_ID: core.cognitoUserPoolId,
178191
DB_TABLE: core.primaryDynamodbTableName,
179192
DB_TABLE_LOG: core.logDynamodbTableName,
193+
DB_TABLE_AUDIT_LOGS: auditLogsDynamo.output.name,
180194
S3_BUCKET: core.fileManagerBucketId
181195
}
182196
});
@@ -187,6 +201,7 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
187201
COGNITO_USER_POOL_ID: core.cognitoUserPoolId,
188202
DB_TABLE: core.primaryDynamodbTableName,
189203
DB_TABLE_LOG: core.logDynamodbTableName,
204+
DB_TABLE_AUDIT_LOGS: auditLogsDynamo.output.name,
190205
DB_TABLE_ELASTICSEARCH: core.elasticsearchDynamodbTableName,
191206
ELASTIC_SEARCH_ENDPOINT: core.elasticsearchDomainEndpoint,
192207

@@ -286,6 +301,7 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
286301
apwSchedulerEventRule: apwScheduler.eventRule.output.name,
287302
apwSchedulerEventTargetId: apwScheduler.eventTarget.output.targetId,
288303
dynamoDbTable: core.primaryDynamodbTableName,
304+
auditLogsDynamoDbTable: auditLogsDynamo.output.name,
289305
migrationLambdaArn: migration.function.output.arn,
290306
graphqlLambdaName: graphql.functions.graphql.output.name,
291307
graphqlLambdaRole: graphql.role.output.arn,

typings/env/index.d.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ declare namespace NodeJS {
22
export interface ProcessEnv {
33
NODE_ENV?: "test" | "prod" | "dev" | string;
44
DB_TABLE?: string;
5-
DB_TABLE_TENANCY?: string;
6-
DB_TABLE_PRERENDERING_SERVICE?: string;
75
DB_TABLE_ELASTICSEARCH?: string;
8-
DB_TABLE_ADMIN_USERS?: string;
9-
DB_TABLE_FILE_MANGER?: string;
10-
DB_TABLE_HEADLESS_CMS?: string;
11-
DB_PAGE_BUILDER?: string;
12-
DB_TABLE_PAGE_BUILDER?: string;
136
DB_TABLE_LOG?: string;
7+
DB_TABLE_AUDIT_LOGS?: string;
148
ELASTICSEARCH_SHARED_INDEXES?: "true" | "false" | string;
159
WEBINY_VERSION?: string;
1610
WEBINY_IS_PRE_529?: "true" | "false";

0 commit comments

Comments
 (0)