|
| 1 | +import { expect } from 'chai'; |
| 2 | + |
| 3 | +/* eslint-disable @typescript-eslint/no-restricted-imports */ |
| 4 | +import { ClientEncryption } from '../../../src/client-side-encryption/client_encryption'; |
| 5 | +import { AWSTemporaryCredentialProvider, Binary } from '../../mongodb'; |
| 6 | + |
| 7 | +const metadata: MongoDBMetadataUI = { |
| 8 | + requires: { |
| 9 | + clientSideEncryption: true |
| 10 | + } |
| 11 | +} as const; |
| 12 | + |
| 13 | +const masterKey = { |
| 14 | + region: 'us-east-1', |
| 15 | + key: 'arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0', |
| 16 | + endpoint: '127.0.0.1:9002' |
| 17 | +}; |
| 18 | + |
| 19 | +const isMongoDBAWSAuthEnvironment = (process.env.MONGODB_URI ?? '').includes('MONGODB-AWS'); |
| 20 | + |
| 21 | +describe('25. Custom AWS Credential Providers', metadata, () => { |
| 22 | + let keyVaultClient; |
| 23 | + let credentialProvider; |
| 24 | + |
| 25 | + beforeEach(async function () { |
| 26 | + this.currentTest.skipReason = !isMongoDBAWSAuthEnvironment |
| 27 | + ? 'Test must run in an AWS auth testing environment' |
| 28 | + : !AWSTemporaryCredentialProvider.isAWSSDKInstalled |
| 29 | + ? 'This test must run in an environment where the AWS SDK is installed.' |
| 30 | + : undefined; |
| 31 | + this.currentTest?.skipReason && this.skip(); |
| 32 | + |
| 33 | + keyVaultClient = this.configuration.newClient(process.env.MONGODB_UR); |
| 34 | + // @ts-expect-error We intentionally access a protected variable. |
| 35 | + credentialProvider = AWSTemporaryCredentialProvider.awsSDK; |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(async () => { |
| 39 | + await keyVaultClient?.close(); |
| 40 | + }); |
| 41 | + |
| 42 | + context( |
| 43 | + 'Case 1: Explicit encryption with credentials and custom credential provider', |
| 44 | + function () { |
| 45 | + it('throws an error', function () { |
| 46 | + expect(() => { |
| 47 | + new ClientEncryption(keyVaultClient, { |
| 48 | + keyVaultNamespace: 'keyvault.datakeys', |
| 49 | + kmsProviders: { |
| 50 | + aws: { |
| 51 | + accessKeyId: process.env.FLE_AWS_KEY, |
| 52 | + secretAccessKey: process.env.FLE_AWS_SECRET |
| 53 | + } |
| 54 | + }, |
| 55 | + credentialProviders: { aws: credentialProvider.fromNodeProviderChain() } |
| 56 | + }); |
| 57 | + }).to.throw(); |
| 58 | + }); |
| 59 | + } |
| 60 | + ); |
| 61 | + |
| 62 | + context('Case 2: Explicit encryption with custom credential provider', function () { |
| 63 | + let clientEncryption; |
| 64 | + |
| 65 | + beforeEach(function () { |
| 66 | + clientEncryption = new ClientEncryption(keyVaultClient, { |
| 67 | + keyVaultNamespace: 'keyvault.datakeys', |
| 68 | + kmsProviders: { aws: {} }, |
| 69 | + credentialProviders: { aws: credentialProvider.fromNodeProviderChain() } |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('is successful', async function () { |
| 74 | + const dk = await clientEncryption.createDataKey('aws', masterKey); |
| 75 | + expect(dk).to.be.instanceOf(Binary); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + context('Case 3: Automatic encryption with different custom providers', function () { |
| 80 | + let client; |
| 81 | + |
| 82 | + beforeEach(function () { |
| 83 | + client = this.configuration.newClient(process.env.MONGODB_URI, { |
| 84 | + authMechanismProperties: { |
| 85 | + AWS_CREDENTIAL_PROVIDER: credentialProvider.fromNodeProviderChain() |
| 86 | + }, |
| 87 | + autoEncryption: { |
| 88 | + keyVaultNamespace: 'keyvault.datakeys', |
| 89 | + kmsProviders: { aws: {} }, |
| 90 | + credentialProviders: { |
| 91 | + aws: async () => { |
| 92 | + return { |
| 93 | + accessKeyId: process.env.FLE_AWS_KEY, |
| 94 | + secretAccessKey: process.env.FLE_AWS_SECRET |
| 95 | + }; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + afterEach(async function () { |
| 103 | + await client?.close(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('is successful', async function () { |
| 107 | + const result = await client.db('test').collection('test').insertOne({ n: 1 }); |
| 108 | + expect(result.ok).to.equal(1); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments