Skip to content

Commit ade61c3

Browse files
committed
test(NODE-6804): implement more invalid KMS tests and set strict unhandled rejection flag
1 parent 398e361 commit ade61c3

8 files changed

+683
-575
lines changed

.mocharc.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ module.exports = {
1717
reporter: 'test/tools/reporter/mongodb_reporter.js',
1818
sort: true,
1919
color: true,
20-
'node-option': Number(major) >= 23 ? ['no-experimental-strip-types'] : undefined
20+
'node-option': [
21+
...(Number(major) >= 23 ? ['no-experimental-strip-types'] : []),
22+
'unhandled-rejections=strict'
23+
]
2124
};

test/csfle-kms-providers.js

+27
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,34 @@ const keys = [
3939
const isInEnvironment = key => typeof process.env[key] === 'string' && process.env[key].length > 0;
4040
const missingKeys = keys.filter(key => !isInEnvironment(key)).join(',');
4141

42+
/**
43+
* @deprecated Please use getCSFLEKMSProviders
44+
*
45+
* This helper was written for the JS tests in test/integration/client-side-encryption/client_side_encryption.prose.test.js
46+
* As we migrate tests to bespoke files per prose numbered test we moved this helper here so we did not create a copy.
47+
*/
48+
const getKmsProviders = (localKey, kmipEndpoint, azureEndpoint, gcpEndpoint) => {
49+
const result = getCSFLEKMSProviders();
50+
if (localKey) {
51+
result.local = { key: localKey };
52+
}
53+
result.kmip = {
54+
endpoint: kmipEndpoint || 'localhost:5698'
55+
};
56+
57+
if (result.azure && azureEndpoint) {
58+
result.azure.identityPlatformEndpoint = azureEndpoint;
59+
}
60+
61+
if (result.gcp && gcpEndpoint) {
62+
result.gcp.endpoint = gcpEndpoint;
63+
}
64+
65+
return result;
66+
};
67+
4268
module.exports = {
69+
getKmsProviders,
4370
getCSFLEKMSProviders,
4471
kmsCredentialsPresent: missingKeys === '',
4572
missingKeys
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { expect } from 'chai';
2+
3+
import { getCSFLEKMSProviders } from '../../csfle-kms-providers';
4+
import { ClientEncryption, type MongoClient } from '../../mongodb';
5+
6+
const metadata: MongoDBMetadataUI = {
7+
requires: {
8+
clientSideEncryption: true,
9+
mongodb: '>=4.2.0',
10+
topology: '!load-balanced'
11+
}
12+
};
13+
14+
describe('10. KMS TLS Tests', metadata, () => {
15+
let keyVaultClient: MongoClient;
16+
let clientEncryption: ClientEncryption;
17+
18+
const keyVaultNamespace = 'keyvault.datakeys';
19+
20+
beforeEach(async function () {
21+
keyVaultClient = this.configuration.newClient();
22+
clientEncryption = new ClientEncryption(keyVaultClient, {
23+
keyVaultNamespace,
24+
kmsProviders: getCSFLEKMSProviders()
25+
});
26+
});
27+
28+
afterEach(async function () {
29+
await keyVaultClient.close();
30+
});
31+
32+
describe('Invalid KMS Certificate', function () {
33+
const masterKey = {
34+
region: 'us-east-1',
35+
key: 'arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0',
36+
endpoint: '127.0.0.1:9000'
37+
};
38+
39+
it('fails with a relevant error message', metadata, async function () {
40+
const error = await clientEncryption.createDataKey('aws', { masterKey }).then(
41+
() => null,
42+
error => error
43+
);
44+
45+
/**
46+
* Expect this to fail with an exception with a message referencing an expired certificate.
47+
* This message will be language dependent.
48+
* - In Python, this message is "certificate verify failed: certificate has expired".
49+
* - In Go, this message is "certificate has expired or is not yet valid".
50+
*
51+
* If the language of implementation has a single, generic error message for all certificate validation errors, drivers may inspect other fields of the error to verify its meaning.
52+
*/
53+
54+
expect(error).to.have.property('cause').that.has.property('code', 'CERT_HAS_EXPIRED');
55+
});
56+
});
57+
58+
describe('Invalid Hostname in KMS Certificate', function () {
59+
const masterKey = {
60+
region: 'us-east-1',
61+
key: 'arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0',
62+
endpoint: '127.0.0.1:9001'
63+
};
64+
65+
it('fails with a relevant error message', metadata, async function () {
66+
const error = await clientEncryption.createDataKey('aws', { masterKey }).then(
67+
() => null,
68+
error => error
69+
);
70+
71+
/**
72+
* Expect this to fail with an exception with a message referencing an incorrect or unexpected host.
73+
* This message will be language dependent.
74+
* - In Python, this message is "certificate verify failed: IP address mismatch, certificate is not valid for '127.0.0.1'".
75+
* - In Go, this message is "cannot validate certificate for 127.0.0.1 because it doesn't contain any IP SANs".
76+
*
77+
* If the language of implementation has a single, generic error message for all certificate validation errors, drivers may inspect other fields of the error to verify its meaning.
78+
*/
79+
80+
expect(error)
81+
.to.have.property('cause')
82+
.that.has.property('code', 'SELF_SIGNED_CERT_IN_CHAIN');
83+
});
84+
});
85+
});

0 commit comments

Comments
 (0)