|
| 1 | +import { type Binary, EJSON, Int32, Long } from 'bson'; |
| 2 | +import { expect } from 'chai'; |
| 3 | + |
| 4 | +/* eslint-disable @typescript-eslint/no-restricted-imports */ |
| 5 | +import { ClientEncryption } from '../../../src/client-side-encryption/client_encryption'; |
| 6 | +import { installNodeDNSWorkaroundHooks } from '../../tools/runner/hooks/configuration'; |
| 7 | + |
| 8 | +const metaData: MongoDBMetadataUI = { |
| 9 | + requires: { |
| 10 | + clientSideEncryption: '>=6.1.0', |
| 11 | + |
| 12 | + // The Range Explicit Encryption tests require MongoDB server 7.0+ for QE v2. |
| 13 | + // The tests must not run against a standalone. |
| 14 | + // |
| 15 | + // `range` is not supported on 8.0+ servers. |
| 16 | + mongodb: '>=8.0.0', |
| 17 | + topology: '!single' |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +const getKmsProviders = (): { local: { key: string } } => { |
| 22 | + const result = EJSON.parse(process.env.CSFLE_KMS_PROVIDERS || '{}') as unknown as { |
| 23 | + local: { key: string }; |
| 24 | + }; |
| 25 | + |
| 26 | + return { local: result.local }; |
| 27 | +}; |
| 28 | + |
| 29 | +describe('Range Explicit Encryption Defaults', function () { |
| 30 | + installNodeDNSWorkaroundHooks(); |
| 31 | + |
| 32 | + let clientEncryption: ClientEncryption; |
| 33 | + let keyId; |
| 34 | + let keyVaultClient; |
| 35 | + let payload_defaults: Binary; |
| 36 | + |
| 37 | + beforeEach(async function () { |
| 38 | + // Create a MongoClient named `keyVaultClient`. |
| 39 | + keyVaultClient = this.configuration.newClient(); |
| 40 | + |
| 41 | + // Create a ClientEncryption object named `clientEncryption` with these options: |
| 42 | + // ```typescript |
| 43 | + // class ClientEncryptionOpts { |
| 44 | + // keyVaultClient: keyVaultClient, |
| 45 | + // keyVaultNamespace: "keyvault.datakeys", |
| 46 | + // kmsProviders: { "local": { "key": "<base64 decoding of LOCAL_MASTERKEY>" } }, |
| 47 | + // } |
| 48 | + // ``` |
| 49 | + clientEncryption = new ClientEncryption(keyVaultClient, { |
| 50 | + keyVaultNamespace: 'keyvault.datakeys', |
| 51 | + kmsProviders: getKmsProviders() |
| 52 | + }); |
| 53 | + |
| 54 | + // Create a key with `clientEncryption.createDataKey`. Store the returned key ID in a variable named `keyId`. |
| 55 | + keyId = await clientEncryption.createDataKey('local'); |
| 56 | + |
| 57 | + // Call `clientEncryption.encrypt` to encrypt the int32 value `123` with these options: |
| 58 | + // ```typescript |
| 59 | + // class EncryptOpts { |
| 60 | + // keyId : keyId, |
| 61 | + // algorithm: "Range", |
| 62 | + // contentionFactor: 0, |
| 63 | + // rangeOpts: RangeOpts { |
| 64 | + // min: 0, |
| 65 | + // max: 1000 |
| 66 | + // } |
| 67 | + // } |
| 68 | + // ``` |
| 69 | + // Store the result in a variable named `payload_defaults`. |
| 70 | + payload_defaults = await clientEncryption.encrypt(new Int32(123), { |
| 71 | + keyId, |
| 72 | + algorithm: 'Range', |
| 73 | + contentionFactor: 0, |
| 74 | + rangeOptions: { |
| 75 | + min: 0, |
| 76 | + max: 1000 |
| 77 | + } |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + afterEach(async function () { |
| 82 | + await keyVaultClient.close(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('Case 1: Uses libmongocrypt defaults', metaData, async function () { |
| 86 | + // Call `clientEncryption.encrypt` to encrypt the int32 value `123` with these options: |
| 87 | + // ```typescript |
| 88 | + // class EncryptOpts { |
| 89 | + // keyId : keyId, |
| 90 | + // algorithm: "Range", |
| 91 | + // contentionFactor: 0, |
| 92 | + // rangeOpts: RangeOpts { |
| 93 | + // min: 0, |
| 94 | + // max: 1000, |
| 95 | + // sparsity: 2, |
| 96 | + // trimFactor: 6 |
| 97 | + // } |
| 98 | + // } |
| 99 | + // ``` |
| 100 | + const encrypted = await clientEncryption.encrypt(new Int32(123), { |
| 101 | + keyId: keyId, |
| 102 | + algorithm: 'Range', |
| 103 | + contentionFactor: 0, |
| 104 | + rangeOptions: { |
| 105 | + min: 0, |
| 106 | + max: 1000, |
| 107 | + sparsity: new Long(2), |
| 108 | + trimFactor: new Int32(6) |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + // Assert the returned payload size equals the size of `payload_defaults`. |
| 113 | + expect(encrypted.length()).to.equal(payload_defaults.length()); |
| 114 | + }); |
| 115 | + |
| 116 | + it('Case 2: can find encrypted range and return the maximum', metaData, async function () { |
| 117 | + // Call `clientEncryption.encrypt` to encrypt the int32 value `123` with these options: |
| 118 | + // ```typescript |
| 119 | + // class EncryptOpts { |
| 120 | + // keyId : keyId, |
| 121 | + // algorithm: "Range", |
| 122 | + // contentionFactor: 0, |
| 123 | + // rangeOpts: RangeOpts { |
| 124 | + // min: 0, |
| 125 | + // max: 1000, |
| 126 | + // trimFactor: 0 |
| 127 | + // } |
| 128 | + // } |
| 129 | + // ``` |
| 130 | + const encrypted = await clientEncryption.encrypt(new Int32(123), { |
| 131 | + keyId: keyId, |
| 132 | + algorithm: 'Range', |
| 133 | + contentionFactor: 0, |
| 134 | + rangeOptions: { |
| 135 | + min: 0, |
| 136 | + max: 1000, |
| 137 | + trimFactor: new Int32(0) |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + // Assert the returned payload size is greater than the size of `payload_defaults`. |
| 142 | + expect(encrypted.length()).to.be.greaterThan(payload_defaults.length()); |
| 143 | + }); |
| 144 | +}); |
0 commit comments