|
| 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