Skip to content

Commit

Permalink
Provider: more logging (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
plumdog authored Jan 25, 2021
1 parent 5a730dc commit 6aeb0a2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
62 changes: 51 additions & 11 deletions provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ import * as childProcess from 'child_process';
import { Writable } from 'stream';
import { TextDecoder } from 'util';

const log = (message: string, extra: Record<string, unknown> = {}): void => {
console.log(
JSON.stringify({
message,
...extra,
}),
);
};

const logError = (error: Error, message: string, extra: Record<string, unknown> = {}): void => {
const stack = error.stack;
const stackLines = stack ? stack.split(/\n/) : [];
console.error(
JSON.stringify({
error: {
name: error.name,
message: error.message,
stack: stackLines,
},
message,
...extra,
}),
);
};

type MappingEncoding = 'string' | 'json';

interface Mapping {
Expand Down Expand Up @@ -202,22 +227,36 @@ const handleCreate = async (event: CreateEvent): Promise<Response> => {

const s3 = new aws.S3();

const obj = await s3
.getObject({
Bucket: s3BucketName,
Key: s3Path,
})
.promise();

const data = await sopsDecode((obj.Body as Buffer).toString('utf-8'), determineFileType(s3Path, fileType, wholeFile), kmsKeyArn);
const getObjectParams = {
Bucket: s3BucketName,
Key: s3Path,
};
log('Getting object from S3', { params: getObjectParams });
const obj = await s3.getObject(getObjectParams).promise();

log('Reading file');
const fileBody = (obj.Body as Buffer).toString('utf-8');
log('Determining file type', { s3Path, fileType, wholeFile });
const fileTypeToUse = determineFileType(s3Path, fileType, wholeFile);
log('Decoding with sops', {
fileBody,
fileTypeToUse,
kmsKeyArn,
});
const data = await sopsDecode(fileBody, fileTypeToUse, kmsKeyArn);
log('Successfully decoded secret data with sops');

if (wholeFile) {
log('Writing decoded data to secretsmanager as whole file', { secretArn });
const wholeFileData = (data as SopsWholeFileData).data || '';
await setSecretString(wholeFileData, secretArn);
} else {
log('Mapping values from decoded data', { mappings });
const mappedValues = resolveMappings(data, mappings);
log('Writing decoded data to secretsmanager as JSON file', { secretArn });
await setSecretString(JSON.stringify(mappedValues), secretArn);
}
log('Wrote data to secretsmanager');

return Promise.resolve({
PhysicalResourceId: `secretdata_${secretArn}`,
Expand All @@ -242,6 +281,7 @@ const handleDelete = async (event: DeleteEvent): Promise<Response> => {
};

export const onEvent = (event: Event): Promise<Response> => {
log('Handling event', { event });
try {
const eventType = event.RequestType as string;
switch (eventType) {
Expand All @@ -252,9 +292,9 @@ export const onEvent = (event: Event): Promise<Response> => {
case 'Delete':
return handleDelete(event as DeleteEvent);
}
return Promise.reject(`Unknown event type ${eventType}`);
throw new Error(`Unknown event type ${eventType}`);
} catch (err) {
console.error(err);
return Promise.reject('Failed');
logError(err, 'Unhandled error, failing');
return Promise.reject(new Error('Failed'));
}
};
8 changes: 4 additions & 4 deletions provider/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ module.exports = {
collectCoverage: true,
coverageThreshold: {
global: {
statements: 81,
branches: 64,
functions: 88,
lines: 81,
statements: 86,
branches: 65,
functions: 90,
lines: 86,
},
},
};
15 changes: 15 additions & 0 deletions provider/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,18 @@ describe('onDelete', () => {
expect(mockSecretsManagerPutSecretValue).not.toHaveBeenCalled();
});
});

describe('unknown event type', () => {
test('simple', async () => {
await expect(
onEvent({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RequestType: 'BadEventType' as any,
PhysicalResourceId: 'abc123',
}),
).rejects.toThrow('Failed');

expect(mockS3GetObject).not.toHaveBeenCalled();
expect(mockSecretsManagerPutSecretValue).not.toHaveBeenCalled();
});
});

0 comments on commit 6aeb0a2

Please sign in to comment.