Skip to content
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

Fix type marshalling for boolean #29

Merged
merged 3 commits into from
Mar 4, 2020
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
20 changes: 18 additions & 2 deletions provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface ResourceProperties {
S3Bucket: string;
S3Path: string;
Mappings: string; // json encoded Mappings;
WholeFile: boolean;
WholeFile: boolean | string;
SecretArn: string;
SourceHash: string;
FileType: string | undefined;
Expand Down Expand Up @@ -61,6 +61,22 @@ interface Response {

type Event = CreateEvent | UpdateEvent | DeleteEvent;

const normaliseBoolean = (value: boolean | string): boolean => {
if (typeof value === 'boolean') {
return value;
}
if (typeof value === 'string') {
if (value === 'true') {
return true;
}
if (value === 'false') {
return false;
}
throw new Error(`Unexpected string value when normalising boolean: ${value}`);
}
throw new Error(`Unexpected type ${typeof value}, ${value} when normalising boolean`);
};

const determineFileType = (s3Path: string, fileType: string | undefined, wholeFile: boolean): string => {
if (fileType) {
return fileType;
Expand Down Expand Up @@ -179,7 +195,7 @@ const handleCreate = async (event: CreateEvent): Promise<Response> => {
const s3BucketName = event.ResourceProperties.S3Bucket;
const s3Path = event.ResourceProperties.S3Path;
const mappings = JSON.parse(event.ResourceProperties.Mappings) as Mappings;
const wholeFile = event.ResourceProperties.WholeFile;
const wholeFile = normaliseBoolean(event.ResourceProperties.WholeFile);
const secretArn = event.ResourceProperties.SecretArn;
// const sourceHash = event.ResourceProperties.SourceHash;
const fileType = event.ResourceProperties.FileType;
Expand Down
41 changes: 41 additions & 0 deletions provider/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,47 @@ describe('onCreate', () => {
});
});

test('wholeFile as string value', async () => {
setMockSpawn({
stdoutData: JSON.stringify({
a: {
b: 'c',
},
}),
});

await onEvent({
RequestType: 'Create',
ResourceProperties: {
KMSKeyArn: undefined,
S3Bucket: 'mys3bucket',
S3Path: 'mys3path.yaml',
Mappings: JSON.stringify({
key: {
path: ['a'],
encoding: 'json',
},
}),
WholeFile: 'false', // because a boolean set in the CDK becomes a string once it reaches the provider
SecretArn: 'mysecretarn',
SourceHash: '123',
FileType: undefined,
},
});

expect(mockSecretsManagerPutSecretValue).toBeCalledWith({
SecretId: 'mysecretarn',
SecretString: expect.any(String),
});

expect(JSON.parse(mockSecretsManagerPutSecretValue.mock.calls[0][0].SecretString)).toEqual({
key: expect.any(String),
});
expect(JSON.parse(JSON.parse(mockSecretsManagerPutSecretValue.mock.calls[0][0].SecretString).key)).toEqual({
b: 'c',
});
});

test('whole file', async () => {
setMockSpawn({
stdoutData: JSON.stringify({
Expand Down