Skip to content

Commit

Permalink
Merge pull request #58 from lifeomic/logger-obfucate-option
Browse files Browse the repository at this point in the history
fix: copy member for obfuscation
  • Loading branch information
rmneidermyer authored Nov 16, 2023
2 parents 7d246a3 + 49c2870 commit 7f181b8
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 8 deletions.
95 changes: 92 additions & 3 deletions src/dynamo-streams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const TestSchema = z.object({
id: z.string(),
name: z.string().optional(),
otherValue: z.string().optional(),
otherMap: z
.object({
name: z.string(),
age: z.number(),
})
.optional(),
});

const testSerializer = {
Expand Down Expand Up @@ -430,7 +436,6 @@ describe('DynamoStreamHandler', () => {
describe('error scenarios', () => {
const lambda = new DynamoStreamHandler({
logger,
loggerObfuscateImageKeys: ['secret'],
parse: testSerializer.parse,
createRunContext: () => ({ logger, dataSources }),
}).lambda();
Expand Down Expand Up @@ -491,7 +496,7 @@ describe('DynamoStreamHandler', () => {
{
eventName: 'MODIFY',
dynamodb: {
NewImage: { id: { S: 'test-id' }, secret: { S: 'test-id' } },
NewImage: { id: { S: 'test-id' } },
},
},
],
Expand All @@ -507,7 +512,7 @@ describe('DynamoStreamHandler', () => {
record: {
eventName: 'MODIFY',
dynamodb: {
NewImage: { id: { S: 'test-id' }, secret: { S: 'obfuscated' } },
NewImage: { id: { S: 'test-id' } },
},
},
});
Expand Down Expand Up @@ -748,4 +753,88 @@ describe('DynamoStreamHandler', () => {
expect(end - start).toBeGreaterThanOrEqual(400);
});
});

describe('logging obfuscation', () => {
test('MODIFY with no OldImage and obfuscated secret', async () => {
const lambda = new DynamoStreamHandler({
logger,
loggerObfuscateImageKeys: ['otherValue'],
parse: testSerializer.parse,
createRunContext: () => ({ logger, dataSources }),
}).lambda();

await lambda(
{
Records: [
{
eventName: 'MODIFY',
dynamodb: {
NewImage: {
id: { S: 'test-id' },
otherValue: { S: 'secret data' },
},
},
},
],
},
{} as any,
{} as any,
);

expect(logger.error).toHaveBeenCalledWith(
'No OldImage was defined for a MODIFY event',
);
expect(logger.child).toHaveBeenCalledWith({
record: {
eventName: 'MODIFY',
dynamodb: {
NewImage: { id: { S: 'test-id' }, otherValue: { S: 'obfuscated' } },
},
},
});
});

test('event not modified during obfuscation', async () => {
const lambda = new DynamoStreamHandler({
logger,
loggerObfuscateImageKeys: ['otherMap', 'otherValue'],
parse: testSerializer.parse,
createRunContext: () => ({ logger, dataSources }),
})
.onModify((ctx, oldEntity, newEntity) => {
ctx.dataSources.doSomething(oldEntity, newEntity);
})
.lambda();

await lambda(
{
Records: [
{
eventName: 'MODIFY',
dynamodb: {
OldImage: {
id: { S: 'old-modify' },
otherValue: { S: 'secret data' },
},
NewImage: {
id: { S: 'new-modify' },
otherMap: {
M: { name: { S: 'secret data' }, age: { N: '35' } },
},
},
},
},
],
},
{} as any,
{} as any,
);

expect(dataSources.doSomething).toHaveBeenCalledTimes(1);
expect(dataSources.doSomething).toHaveBeenCalledWith(
{ id: 'old-modify', otherValue: 'secret data' },
{ id: 'new-modify', otherMap: { name: 'secret data', age: 35 } },
);
});
});
});
11 changes: 6 additions & 5 deletions src/dynamo-streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export type DynamoStreamHandlerConfig<Entity, Context> = {
logger: LoggerInterface;
/**
* A listing of keys within a dynamo record's images to obfuscate in logging
* output.
* output. This will not perform a deep obfuscation of 'M' AttributeValue
* types and instead will simply obfuscate the entire value.
*/
loggerObfuscateImageKeys?: string[];
/**
Expand Down Expand Up @@ -137,13 +138,13 @@ export class DynamoStreamHandler<Entity, Context> {

private obfuscate(blob: any, keys: string[]): any {
if (blob === undefined) return undefined;
const obfuscated = blob;
const copy: any = { ...blob };
keys.forEach((k) => {
if (obfuscated[k]) {
obfuscated[k] = { S: 'obfuscated' };
if (copy[k]) {
copy[k] = { S: 'obfuscated' };
}
});
return obfuscated;
return copy;
}

private obfuscateRecord(dynamoRecord: DynamoDBRecord): DynamoDBRecord {
Expand Down

0 comments on commit 7f181b8

Please sign in to comment.