-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathclient_side_encryption.prose.26.custom_aws_credential_providers.test.ts
More file actions
150 lines (134 loc) · 4.69 KB
/
client_side_encryption.prose.26.custom_aws_credential_providers.test.ts
File metadata and controls
150 lines (134 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { expect } from 'chai';
/* eslint-disable @typescript-eslint/no-restricted-imports */
import { ClientEncryption } from '../../../src/client-side-encryption/client_encryption';
import { AWSSDKCredentialProvider, Binary, MongoClient } from '../../mongodb';
import { getEncryptExtraOptions } from '../../tools/utils';
const metadata: MongoDBMetadataUI = {
requires: {
clientSideEncryption: true,
topology: '!load-balanced'
}
} as const;
const masterKey = {
region: 'us-east-1',
key: 'arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0'
};
describe('26. Custom AWS Credential Providers', metadata, () => {
let keyVaultClient;
let credentialProvider;
beforeEach(async function () {
keyVaultClient = this.configuration.newClient(process.env.MONGODB_UR);
credentialProvider = AWSSDKCredentialProvider.awsSDK;
});
afterEach(async () => {
await keyVaultClient?.close();
});
context(
'Case 1: ClientEncryption with credentialProviders and incorrect kmsProviders',
metadata,
function () {
it('throws an error', metadata, function () {
expect(() => {
new ClientEncryption(keyVaultClient, {
keyVaultNamespace: 'keyvault.datakeys',
kmsProviders: {
aws: {
accessKeyId: process.env.FLE_AWS_KEY,
secretAccessKey: process.env.FLE_AWS_SECRET
}
},
credentialProviders: { aws: credentialProvider.fromNodeProviderChain() }
});
}).to.throw(/Can only provide a custom AWS credential provider/);
});
}
);
context('Case 2: ClientEncryption with credentialProviders works', metadata, function () {
let clientEncryption;
let providerCount = 0;
beforeEach(function () {
const options = {
keyVaultNamespace: 'keyvault.datakeys',
kmsProviders: { aws: {} },
credentialProviders: {
aws: async () => {
providerCount++;
return {
accessKeyId: process.env.FLE_AWS_KEY,
secretAccessKey: process.env.FLE_AWS_SECRET
};
}
},
extraOptions: getEncryptExtraOptions()
};
clientEncryption = new ClientEncryption(keyVaultClient, options);
});
it('is successful', metadata, async function () {
const dk = await clientEncryption.createDataKey('aws', { masterKey });
expect(dk).to.be.instanceOf(Binary);
expect(providerCount).to.be.greaterThan(0);
});
});
context(
'Case 3: AutoEncryptionOpts with credentialProviders and incorrect kmsProviders',
metadata,
function () {
it('throws an error', metadata, function () {
expect(() => {
new MongoClient('mongodb://127.0.0.1:27017', {
autoEncryption: {
keyVaultNamespace: 'keyvault.datakeys',
kmsProviders: {
aws: {
accessKeyId: process.env.FLE_AWS_KEY,
secretAccessKey: process.env.FLE_AWS_SECRET
}
},
credentialProviders: { aws: credentialProvider.fromNodeProviderChain() }
}
});
}).to.throw(/Can only provide a custom AWS credential provider/);
});
}
);
context(
'ClientEncryption with credentialProviders and valid environment variables',
metadata,
function () {
let clientEncryption;
let providerCount = 0;
let previousAccessKey;
let previousSecretKey;
beforeEach(function () {
previousAccessKey = process.env.AWS_ACCESS_KEY_ID;
previousSecretKey = process.env.AWS_SECRET_ACCESS_KEY;
process.env.AWS_ACCESS_KEY_ID = process.env.FLE_AWS_KEY;
process.env.AWS_SECRET_ACCESS_KEY = process.env.FLE_AWS_SECRET;
const options = {
keyVaultNamespace: 'keyvault.datakeys',
kmsProviders: { aws: {} },
credentialProviders: {
aws: async () => {
providerCount++;
return {
accessKeyId: process.env.FLE_AWS_KEY,
secretAccessKey: process.env.FLE_AWS_SECRET
};
}
},
extraOptions: getEncryptExtraOptions()
};
clientEncryption = new ClientEncryption(keyVaultClient, options);
});
afterEach(function () {
process.env.AWS_ACCESS_KEY_ID = previousAccessKey;
process.env.AWS_SECRET_ACCESS_KEY = previousSecretKey;
});
it('is successful', metadata, async function () {
const dk = await clientEncryption.createDataKey('aws', { masterKey });
expect(dk).to.be.instanceOf(Binary);
expect(providerCount).to.be.greaterThan(0);
});
}
);
});