Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const decryptObject = (object, metadata) =>
.then(dataKey =>
({
object: cloneDeepWith(object, (value, key) => {
if (metadata.fields.includes(key)) {
if (metadata.fields && metadata.fields.includes(key)) {
return decryptValue(key, value, dataKey, metadata.AES);
} else {
return undefined;
Expand Down Expand Up @@ -63,7 +63,7 @@ const encryptDataKeyPerRegion = metadata => ({ Plaintext, CiphertextBlob }) =>
}));

const decryptDataKey = metadata => new Connector(metadata.masterKeyAlias)
.decryptDataKey(metadata.dataKeys[process.env.AWS_REGION])
.decryptDataKey(metadata.dataKeys && metadata.dataKeys[process.env.AWS_REGION])
.catch((e1) => {
logError(e1, 0, process.env.AWS_REGION);
return Promise.all(otherRegions(metadata)
Expand Down
138 changes: 138 additions & 0 deletions test/unit/functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,142 @@ describe('functions.js', () => {
console.error('EXPECTED: ', e.message);
}
});

it('should not crash when metadata.fields is missing', async () => {
sinon.stub(Connector.prototype, 'generateDataKey')
.resolves(MOCK_GEN_DK_RESPONSE);
sinon.stub(Connector.prototype, 'decryptDataKey')
.resolves(MOCK_DECRYPT_DK_RESPONSE);

const obj = {
id: '0',
type: 'thing-created',
partitionKey: '1',
timestamp: 1572832690000,
tags: {
region: 'us-west-2',
},
raw: {
new: {
pk: '1',
sk: 'thing',
discriminator: 'thing',
name: 'name',
description: 'description',
status: 's1',
},
},
eem: {
dataKeys: {
'us-west-2': 'AQIDAHg5lIgUTrMBJZSEOmrJ/GqVqgcTMUj+cIw/EBA4XAX5TgG+mTJFoKz0VU0tljNQLcGwAAAAfjB8BgkqhkiG9w0BBwagbzBtAgEAMGgGCSqGSIb3DQEHATAeBglghkgBZQMEAS4wEQQMWhuPTg3e+GjnWx6rAgEQgDsRc7csVqQxagsvXLPx/hAePtadw2GziixRC/UT3FpYU58/NHC8PvFdJDVQ3LkK8XYGLeowpHlC9CaqgA==',
},
masterKeyAlias: 'alias/aws-kms-ee',
},
};

const result = await decryptObject(obj, obj.eem);
expect(result.object).to.deep.equal(obj);
});

it('should not crash when metadata.masterKeyAlias is missing', async () => {
sinon.stub(Connector.prototype, 'generateDataKey')
.resolves(MOCK_GEN_DK_RESPONSE);
sinon.stub(Connector.prototype, 'decryptDataKey')
.resolves(MOCK_DECRYPT_DK_RESPONSE);

const obj = {
id: '0',
type: 'thing-created',
partitionKey: '1',
timestamp: 1572832690000,
tags: {
region: 'us-west-2',
},
raw: {
new: {
pk: '1',
sk: 'thing',
discriminator: 'thing',
name: 'name',
description: 'description',
status: 's1',
},
},
eem: {
dataKeys: {
'us-west-2': 'AQIDAHg5lIgUTrMBJZSEOmrJ/GqVqgcTMUj+cIw/EBA4XAX5TgG+mTJFoKz0VU0tljNQLcGwAAAAfjB8BgkqhkiG9w0BBwagbzBtAgEAMGgGCSqGSIb3DQEHATAeBglghkgBZQMEAS4wEQQMWhuPTg3e+GjnWx6rAgEQgDsRc7csVqQxagsvXLPx/hAePtadw2GziixRC/UT3FpYU58/NHC8PvFdJDVQ3LkK8XYGLeowpHlC9CaqgA==',
},
fields: [],
},
};

const result = await decryptObject(obj, obj.eem);
expect(result.object).to.deep.equal(obj);
});

it('should not crash when metadata.dataKeys is missing', async () => {
sinon.stub(Connector.prototype, 'generateDataKey')
.resolves(MOCK_GEN_DK_RESPONSE);
sinon.stub(Connector.prototype, 'decryptDataKey')
.resolves(MOCK_DECRYPT_DK_RESPONSE);

const obj = {
id: '0',
type: 'thing-created',
partitionKey: '1',
timestamp: 1572832690000,
tags: {
region: 'us-west-2',
},
raw: {
new: {
pk: '1',
sk: 'thing',
discriminator: 'thing',
name: 'name',
description: 'description',
status: 's1',
},
},
eem: {
masterKeyAlias: 'alias/aws-kms-ee',
fields: [],
},
};

const result = await decryptObject(obj, obj.eem);
expect(result.object).to.deep.equal(obj);
});

it('should not crash when metadata is empty', async () => {
sinon.stub(Connector.prototype, 'generateDataKey')
.resolves(MOCK_GEN_DK_RESPONSE);
sinon.stub(Connector.prototype, 'decryptDataKey')
.resolves(MOCK_DECRYPT_DK_RESPONSE);

const obj = {
id: '0',
type: 'thing-created',
partitionKey: '1',
timestamp: 1572832690000,
tags: {
region: 'us-west-2',
},
raw: {
new: {
pk: '1',
sk: 'thing',
discriminator: 'thing',
name: 'name',
description: 'description',
status: 's1',
},
},
eem: {
},
};

const result = await decryptObject(obj, obj.eem);
expect(result.object).to.deep.equal(obj);
});
});