forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_side_encryption.prose.26.custom_aws_credential_providers.test.ts
More file actions
163 lines (147 loc) · 5.51 KB
/
client_side_encryption.prose.26.custom_aws_credential_providers.test.ts
File metadata and controls
163 lines (147 loc) · 5.51 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
151
152
153
154
155
156
157
158
159
160
161
162
163
import { expect } from 'chai';
import * as process from 'process';
import { Binary, MongoClient } from '../../../src';
import { ClientEncryption } from '../../../src/client-side-encryption/client_encryption';
import { AWSSDKCredentialProvider } from '../../../src/cmap/auth/aws_temporary_credentials';
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/);
});
}
);
// Ensure a valid AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are present in the environment.
// Create a MongoClient named setupClient.
// Create a ClientEncryption object with the following options:
// class ClientEncryptionOpts {
// keyVaultClient: <setupClient>,
// keyVaultNamespace: "keyvault.datakeys",
// kmsProviders: { "aws": {} },
// credentialProviders: { "aws": <object/function that returns valid credentials from the secrets manager> }
// }
// Use the client encryption to create a datakey using the "aws" KMS provider. This should successfully load
// and use the AWS credentials that were provided by the secrets manager for the remote provider. Assert the
// datakey was created and that the custom credential provider was called at least once.
context(
'Case 4: 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);
});
}
);
});