Skip to content

Commit 7dd50d7

Browse files
remove CSFLE_KMS_PROVIDERS
1 parent cf300a3 commit 7dd50d7

12 files changed

+163
-182
lines changed

test/csfle-env.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
const { Binary } = require("bson");
4+
5+
const csfleKMSProviders = {
6+
"aws": {
7+
"accessKeyId": process.env.FLE_AWS_KEY,
8+
"secretAccessKey": process.env.FLE_AWS_SECRET
9+
},
10+
"azure": {
11+
"tenantId": process.env.FLE_AZURE_TENANTID,
12+
"clientId": process.env.FLE_AZURE_CLIENTID,
13+
"clientSecret": process.env.FLE_AZURE_CLIENTSECRET
14+
},
15+
"gcp": {
16+
"email": process.env.FLE_GCP_EMAIL,
17+
"privateKey": process.env.FLE_GCP_PRIVATEKEY
18+
},
19+
"local": {
20+
"key": Buffer.from("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk", 'base64')
21+
},
22+
"kmip": {
23+
"endpoint": "localhost:5698"
24+
}
25+
}
26+
27+
function getCSFLEKMSProviders() {
28+
return structuredClone(
29+
csfleKMSProviders
30+
)
31+
}
32+
33+
const keys = [
34+
'FLE_AWS_KEY',
35+
'FLE_AWS_SECRET',
36+
'FLE_AZURE_TENANTID',
37+
'FLE_AZURE_CLIENTID',
38+
'FLE_AZURE_CLIENTSECRET',
39+
'FLE_GCP_EMAIL',
40+
'FLE_GCP_PRIVATEKEY'
41+
];
42+
43+
module.exports = {
44+
getCSFLEKMSProviders,
45+
kmsCredentialsPresent: keys.every(key => typeof process.env[key] === 'string' && process.env[key].length > 0)
46+
}

test/integration/client-side-encryption/client_side_encryption.prose.06.corpus.test.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as path from 'path';
99
import { ClientEncryption } from '../../../src/client-side-encryption/client_encryption';
1010
import { type MongoClient, WriteConcern } from '../../mongodb';
1111
import { getEncryptExtraOptions } from '../../tools/utils';
12+
import { getCSFLEKMSProviders } from '../../csfle-env';
1213

1314
describe('Client Side Encryption Prose Corpus Test', function () {
1415
const metadata = {
@@ -25,17 +26,7 @@ describe('Client Side Encryption Prose Corpus Test', function () {
2526
});
2627
}
2728

28-
const CSFLE_KMS_PROVIDERS = process.env.CSFLE_KMS_PROVIDERS;
29-
const kmsProviders = CSFLE_KMS_PROVIDERS ? EJSON.parse(CSFLE_KMS_PROVIDERS) : {};
30-
kmsProviders.local = {
31-
key: Buffer.from(
32-
'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk',
33-
'base64'
34-
)
35-
};
36-
kmsProviders.kmip = {
37-
endpoint: 'localhost:5698'
38-
};
29+
const kmsProviders = getCSFLEKMSProviders();
3930

4031
// TODO: build this into EJSON
4132
// TODO: make a custom chai assertion for this

test/integration/client-side-encryption/client_side_encryption.prose.21.automatic_data_encryption_keys.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { expect } from 'chai';
44
import { ClientEncryption } from '../../../src/client-side-encryption/client_encryption';
55
/* eslint-disable @typescript-eslint/no-restricted-imports */
66
import { MongoCryptCreateEncryptedCollectionError } from '../../../src/client-side-encryption/errors';
7-
import { BSON, Collection, type Db, MongoServerError } from '../../mongodb';
7+
import { Collection, type Db, MongoServerError } from '../../mongodb';
8+
import { getCSFLEKMSProviders, kmsCredentialsPresent } from '../../csfle-env';
89

910
const metadata: MongoDBMetadataUI = {
1011
requires: {
@@ -33,14 +34,14 @@ describe('21. Automatic Data Encryption Keys', () => {
3334
beforeEach(async function () {
3435
client = this.configuration.newClient();
3536

36-
if (typeof process.env.CSFLE_KMS_PROVIDERS !== 'string') {
37+
if (!kmsCredentialsPresent) {
3738
if (this.currentTest) {
3839
this.currentTest.skipReason = 'This test requires env CSFLE_KMS_PROVIDERS to be set';
3940
}
4041
return this.currentTest?.skip();
4142
}
4243

43-
const { aws, local } = BSON.EJSON.parse(process.env.CSFLE_KMS_PROVIDERS);
44+
const { aws, local } = getCSFLEKMSProviders();
4445

4546
clientEncryption = new ClientEncryption(client, {
4647
keyVaultClient: client,

test/integration/client-side-encryption/client_side_encryption.prose.22.range_explicit_encryption.test.ts

Lines changed: 78 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import { Decimal128, type Document, Double, Long, type MongoClient } from '../..
88
import { ClientEncryption } from '../../../src/client-side-encryption/client_encryption';
99
/* eslint-disable @typescript-eslint/no-restricted-imports */
1010
import { MongoCryptError } from '../../../src/client-side-encryption/errors';
11+
import { getCSFLEKMSProviders } from '../../csfle-env';
1112

1213
const getKmsProviders = () => {
13-
const result = EJSON.parse(process.env.CSFLE_KMS_PROVIDERS || '{}') as unknown as {
14-
local: unknown;
15-
};
14+
const result = getCSFLEKMSProviders();
1615

1716
return { local: result.local };
1817
};
@@ -46,85 +45,85 @@ const prepareOptions = opts =>
4645

4746
const dataTypes: ReadonlyArray<{
4847
type:
49-
| 'DecimalNoPrecision'
50-
| 'DecimalPrecision'
51-
| 'DoubleNoPrecision'
52-
| 'DoublePrecision'
53-
| 'Long'
54-
| 'Int'
55-
| 'Date';
48+
| 'DecimalNoPrecision'
49+
| 'DecimalPrecision'
50+
| 'DoubleNoPrecision'
51+
| 'DoublePrecision'
52+
| 'Long'
53+
| 'Int'
54+
| 'Date';
5655
rangeOptions: Document;
5756
factory: (number) => unknown;
5857
}> = [
59-
{
60-
type: 'DecimalNoPrecision',
61-
rangeOptions: prepareOptions({
62-
sparsity: { $numberLong: '1' },
63-
trimFactor: { $numberInt: '1' }
64-
}),
65-
factory: value => new Decimal128(value.toString())
66-
},
67-
{
68-
type: 'DecimalPrecision',
69-
rangeOptions: prepareOptions({
70-
min: { $numberDecimal: '0' },
71-
max: { $numberDecimal: '200' },
72-
trimFactor: { $numberInt: '1' },
73-
sparsity: { $numberLong: '1' },
74-
precision: 2
75-
}),
76-
factory: value => new Decimal128(value.toString())
77-
},
78-
{
79-
type: 'DoubleNoPrecision',
80-
rangeOptions: prepareOptions({
81-
trimFactor: { $numberInt: '1' },
82-
sparsity: { $numberLong: '1' }
83-
}),
84-
factory: value => new Double(value)
85-
},
86-
{
87-
type: 'DoublePrecision',
88-
rangeOptions: prepareOptions({
89-
min: { $numberDouble: '0' },
90-
max: { $numberDouble: '200' },
91-
trimFactor: { $numberInt: '1' },
92-
sparsity: { $numberLong: '1' },
93-
precision: 2
94-
}),
95-
factory: value => new Double(value)
96-
},
97-
{
98-
type: 'Date',
99-
rangeOptions: prepareOptions({
100-
min: { $date: { $numberLong: '0' } },
101-
max: { $date: { $numberLong: '200' } },
102-
trimFactor: { $numberInt: '1' },
103-
sparsity: { $numberLong: '1' }
104-
}),
105-
factory: value => new Date(value)
106-
},
107-
{
108-
type: 'Int',
109-
rangeOptions: prepareOptions({
110-
min: { $numberInt: '0' },
111-
max: { $numberInt: '200' },
112-
trimFactor: { $numberInt: '1' },
113-
sparsity: { $numberLong: '1' }
114-
}),
115-
factory: value => value
116-
},
117-
{
118-
type: 'Long',
119-
rangeOptions: prepareOptions({
120-
min: { $numberLong: '0' },
121-
max: { $numberLong: '200' },
122-
trimFactor: { $numberInt: '1' },
123-
sparsity: { $numberLong: '1' }
124-
}),
125-
factory: value => Long.fromNumber(value)
126-
}
127-
];
58+
{
59+
type: 'DecimalNoPrecision',
60+
rangeOptions: prepareOptions({
61+
sparsity: { $numberLong: '1' },
62+
trimFactor: { $numberInt: '1' }
63+
}),
64+
factory: value => new Decimal128(value.toString())
65+
},
66+
{
67+
type: 'DecimalPrecision',
68+
rangeOptions: prepareOptions({
69+
min: { $numberDecimal: '0' },
70+
max: { $numberDecimal: '200' },
71+
trimFactor: { $numberInt: '1' },
72+
sparsity: { $numberLong: '1' },
73+
precision: 2
74+
}),
75+
factory: value => new Decimal128(value.toString())
76+
},
77+
{
78+
type: 'DoubleNoPrecision',
79+
rangeOptions: prepareOptions({
80+
trimFactor: { $numberInt: '1' },
81+
sparsity: { $numberLong: '1' }
82+
}),
83+
factory: value => new Double(value)
84+
},
85+
{
86+
type: 'DoublePrecision',
87+
rangeOptions: prepareOptions({
88+
min: { $numberDouble: '0' },
89+
max: { $numberDouble: '200' },
90+
trimFactor: { $numberInt: '1' },
91+
sparsity: { $numberLong: '1' },
92+
precision: 2
93+
}),
94+
factory: value => new Double(value)
95+
},
96+
{
97+
type: 'Date',
98+
rangeOptions: prepareOptions({
99+
min: { $date: { $numberLong: '0' } },
100+
max: { $date: { $numberLong: '200' } },
101+
trimFactor: { $numberInt: '1' },
102+
sparsity: { $numberLong: '1' }
103+
}),
104+
factory: value => new Date(value)
105+
},
106+
{
107+
type: 'Int',
108+
rangeOptions: prepareOptions({
109+
min: { $numberInt: '0' },
110+
max: { $numberInt: '200' },
111+
trimFactor: { $numberInt: '1' },
112+
sparsity: { $numberLong: '1' }
113+
}),
114+
factory: value => value
115+
},
116+
{
117+
type: 'Long',
118+
rangeOptions: prepareOptions({
119+
min: { $numberLong: '0' },
120+
max: { $numberLong: '200' },
121+
trimFactor: { $numberInt: '1' },
122+
sparsity: { $numberLong: '1' }
123+
}),
124+
factory: value => Long.fromNumber(value)
125+
}
126+
];
128127

129128
const basePath = '/test/spec/client-side-encryption/etc/data';
130129

test/integration/client-side-encryption/client_side_encryption.prose.23.range_encryption_defaults.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { expect } from 'chai';
22

33
/* eslint-disable @typescript-eslint/no-restricted-imports */
44
import { ClientEncryption } from '../../../src/client-side-encryption/client_encryption';
5-
import { type Binary, EJSON, Int32, Long } from '../../mongodb';
5+
import { type Binary, Int32, Long } from '../../mongodb';
6+
import { getCSFLEKMSProviders } from '../../csfle-env';
67

78
const metaData: MongoDBMetadataUI = {
89
requires: {
@@ -17,10 +18,8 @@ const metaData: MongoDBMetadataUI = {
1718
}
1819
};
1920

20-
const getKmsProviders = (): { local: { key: string } } => {
21-
const result = EJSON.parse(process.env.CSFLE_KMS_PROVIDERS || '{}') as unknown as {
22-
local: { key: string };
23-
};
21+
const getKmsProviders = (): { local: { key: Buffer } } => {
22+
const result = getCSFLEKMSProviders();
2423

2524
return { local: result.local };
2625
};

test/integration/client-side-encryption/client_side_encryption.prose.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ const { ClientEncryption } = require('../../../src/client-side-encryption/client
1919
const {
2020
ClientSideEncryptionFilter
2121
} = require('../../tools/runner/filters/client_encryption_filter');
22+
const { getCSFLEKMSProviders } = require('../../csfle-env');
2223

2324
const getKmsProviders = (localKey, kmipEndpoint, azureEndpoint, gcpEndpoint) => {
24-
const result = BSON.EJSON.parse(process.env.CSFLE_KMS_PROVIDERS || '{}');
25+
const result = getCSFLEKMSProviders();
2526
if (localKey) {
2627
result.local = { key: localKey };
2728
}

test/integration/client-side-encryption/driver.test.ts

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
sleep
3030
} from '../../tools/utils';
3131
import { filterForCommands } from '../shared';
32+
import { getCSFLEKMSProviders } from '../../csfle-env';
3233

3334
const metadata: MongoDBMetadataUI = {
3435
requires: {
@@ -37,12 +38,8 @@ const metadata: MongoDBMetadataUI = {
3738
}
3839
};
3940

40-
const getLocalKmsProvider = (): { local: { key: Binary } } => {
41-
const { local } = EJSON.parse(process.env.CSFLE_KMS_PROVIDERS || '{}') as {
42-
local: { key: Binary };
43-
[key: string]: unknown;
44-
};
45-
41+
const getLocalKmsProvider = (): { local: { key: Buffer } } => {
42+
const { local } = getCSFLEKMSProviders();
4643
return { local };
4744
};
4845

@@ -53,41 +50,6 @@ describe('Client Side Encryption Functional', function () {
5350
const keyVaultCollName = 'datakeys';
5451
const keyVaultNamespace = `${keyVaultDbName}.${keyVaultCollName}`;
5552

56-
it('CSFLE_KMS_PROVIDERS should be valid EJSON', function () {
57-
const CSFLE_KMS_PROVIDERS = process.env.CSFLE_KMS_PROVIDERS;
58-
if (typeof CSFLE_KMS_PROVIDERS === 'string') {
59-
/**
60-
* The shape of CSFLE_KMS_PROVIDERS is as follows:
61-
*
62-
* ```ts
63-
* interface CSFLE_kms_providers {
64-
* aws: {
65-
* accessKeyId: string;
66-
* secretAccessKey: string;
67-
* };
68-
* azure: {
69-
* tenantId: string;
70-
* clientId: string;
71-
* clientSecret: string;
72-
* };
73-
* gcp: {
74-
* email: string;
75-
* privateKey: string;
76-
* };
77-
* local: {
78-
* // EJSON handle converting this, its actually the canonical -> { $binary: { base64: string; subType: string } }
79-
* // **NOTE**: The dollar sign has to be escaped when using this as an ENV variable
80-
* key: Binary;
81-
* }
82-
* }
83-
* ```
84-
*/
85-
expect(() => EJSON.parse(CSFLE_KMS_PROVIDERS)).to.not.throw(SyntaxError);
86-
} else {
87-
this.skip();
88-
}
89-
});
90-
9153
describe('Collection', metadata, function () {
9254
describe('#bulkWrite()', metadata, function () {
9355
context('when encryption errors', function () {
@@ -640,10 +602,8 @@ describe('Range Explicit Encryption with JS native types', function () {
640602
}
641603
};
642604

643-
const getKmsProviders = (): { local: { key: string } } => {
644-
const result = EJSON.parse(process.env.CSFLE_KMS_PROVIDERS || '{}') as unknown as {
645-
local: { key: string };
646-
};
605+
const getKmsProviders = (): { local: { key: Buffer } } => {
606+
const result = getCSFLEKMSProviders();
647607

648608
return { local: result.local };
649609
};
@@ -1135,7 +1095,7 @@ describe('CSOT', function () {
11351095
};
11361096

11371097
beforeEach(async function () {
1138-
local_key = { local: EJSON.parse(process.env.CSFLE_KMS_PROVIDERS).local };
1098+
local_key = { local: getCSFLEKMSProviders().local };
11391099
client = this.configuration.newClient({ timeoutMS });
11401100
await client.connect();
11411101
await client.db('keyvault').createCollection('datakeys');

0 commit comments

Comments
 (0)