|
| 1 | +import { expect } from 'chai'; |
| 2 | + |
| 3 | +import { BSON, Collection, Db, MongoServerError } from '../../../src'; |
| 4 | +import { installNodeDNSWorkaroundHooks } from '../../tools/runner/hooks/configuration'; |
| 5 | + |
| 6 | +const metadata = { |
| 7 | + requires: { |
| 8 | + clientSideEncryption: true, |
| 9 | + mongodb: '>=6.0.0', |
| 10 | + topology: '!single' |
| 11 | + } |
| 12 | +} as const; |
| 13 | + |
| 14 | +const documentValidationFailureCode = 121; |
| 15 | +const typeMismatchCode = 14; |
| 16 | + |
| 17 | +describe('21. Automatic Data Encryption Keys', () => { |
| 18 | + installNodeDNSWorkaroundHooks(); |
| 19 | + |
| 20 | + let db: Db; |
| 21 | + let clientEncryption; |
| 22 | + let client; |
| 23 | + let MongoCryptCreateEncryptedCollectionError; |
| 24 | + |
| 25 | + const runProseTestsFor = provider => { |
| 26 | + const masterKey = { |
| 27 | + aws: { |
| 28 | + region: 'us-east-1', |
| 29 | + key: 'arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0' |
| 30 | + }, |
| 31 | + local: null |
| 32 | + }[provider]; |
| 33 | + beforeEach(async function () { |
| 34 | + client = this.configuration.newClient(); |
| 35 | + const { |
| 36 | + ClientEncryption, |
| 37 | + MongoCryptCreateEncryptedCollectionError: MongoCryptCreateEncryptedCollectionErrorCtor |
| 38 | + } = this.configuration.mongodbClientEncryption; |
| 39 | + MongoCryptCreateEncryptedCollectionError = MongoCryptCreateEncryptedCollectionErrorCtor; |
| 40 | + |
| 41 | + if (typeof process.env.CSFLE_KMS_PROVIDERS !== 'string') { |
| 42 | + if (this.currentTest) { |
| 43 | + this.currentTest.skipReason = 'This test requires env CSFLE_KMS_PROVIDERS to be set'; |
| 44 | + } |
| 45 | + return this.currentTest?.skip(); |
| 46 | + } |
| 47 | + |
| 48 | + const { aws, local } = BSON.EJSON.parse(process.env.CSFLE_KMS_PROVIDERS); |
| 49 | + |
| 50 | + clientEncryption = new ClientEncryption(client, { |
| 51 | + keyVaultClient: client, |
| 52 | + keyVaultNamespace: 'keyvault.datakeys', |
| 53 | + kmsProviders: { aws, local } |
| 54 | + }); |
| 55 | + |
| 56 | + db = client.db('automatic_data_encryption_keys'); |
| 57 | + await db.dropDatabase().catch(() => null); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(async function () { |
| 61 | + await db?.dropDatabase().catch(() => null); |
| 62 | + await client?.close(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('Case 1: Simple Creation and Validation', metadata, async () => { |
| 66 | + const createCollectionOptions = { |
| 67 | + encryptedFields: { fields: [{ path: 'ssn', bsonType: 'string', keyId: null }] } |
| 68 | + }; |
| 69 | + |
| 70 | + const { collection } = await clientEncryption.createEncryptedCollection(db, 'testing1', { |
| 71 | + provider, |
| 72 | + createCollectionOptions, |
| 73 | + masterKey |
| 74 | + }); |
| 75 | + |
| 76 | + expect(collection).to.be.instanceOf(Collection); |
| 77 | + expect(collection.namespace).to.equal('automatic_data_encryption_keys.testing1'); |
| 78 | + |
| 79 | + const result = await collection.insertOne({ ssn: '123-45-6789' }).catch(error => error); |
| 80 | + expect(result).to.be.instanceOf(MongoServerError); |
| 81 | + expect(result).to.have.property('code', documentValidationFailureCode); |
| 82 | + }); |
| 83 | + |
| 84 | + it('Case 2: Missing encryptedFields', metadata, async () => { |
| 85 | + const createCollectionOptions = {}; |
| 86 | + |
| 87 | + const result = await clientEncryption |
| 88 | + .createEncryptedCollection(db, 'testing1', { |
| 89 | + provider, |
| 90 | + createCollectionOptions, |
| 91 | + masterKey |
| 92 | + }) |
| 93 | + .catch(error => error); |
| 94 | + |
| 95 | + expect(result).to.be.instanceOf(TypeError); |
| 96 | + }); |
| 97 | + |
| 98 | + it('Case 3: Invalid keyId', metadata, async () => { |
| 99 | + const createCollectionOptions = { |
| 100 | + encryptedFields: { fields: [{ path: 'ssn', bsonType: 'string', keyId: false }] } |
| 101 | + }; |
| 102 | + |
| 103 | + const result = await clientEncryption |
| 104 | + .createEncryptedCollection(db, 'testing1', { |
| 105 | + provider, |
| 106 | + createCollectionOptions, |
| 107 | + masterKey |
| 108 | + }) |
| 109 | + .catch(error => error); |
| 110 | + |
| 111 | + expect(result).to.be.instanceOf(MongoCryptCreateEncryptedCollectionError); |
| 112 | + expect(result).nested.property('cause.code', typeMismatchCode); |
| 113 | + // BSON field 'create.encryptedFields.fields.keyId' is the wrong type 'bool', expected type 'binData' |
| 114 | + expect(result.cause.message) |
| 115 | + .to.match(/bool/i) |
| 116 | + .and.match(/binData/i) |
| 117 | + .and.match(/keyId/i); |
| 118 | + }); |
| 119 | + |
| 120 | + it('Case 4: Insert encrypted value', metadata, async () => { |
| 121 | + const createCollectionOptions = { |
| 122 | + encryptedFields: { fields: [{ path: 'ssn', bsonType: 'string', keyId: null }] } |
| 123 | + }; |
| 124 | + |
| 125 | + const { collection, encryptedFields } = await clientEncryption.createEncryptedCollection( |
| 126 | + db, |
| 127 | + 'testing1', |
| 128 | + { |
| 129 | + provider, |
| 130 | + createCollectionOptions, |
| 131 | + masterKey |
| 132 | + } |
| 133 | + ); |
| 134 | + |
| 135 | + expect(collection).to.be.instanceOf(Collection); |
| 136 | + expect(collection.namespace).to.equal('automatic_data_encryption_keys.testing1'); |
| 137 | + |
| 138 | + const ssn = clientEncryption.encrypt('123-45-6789', { |
| 139 | + algorithm: 'Unindexed', |
| 140 | + keyId: encryptedFields.fields[0].keyId |
| 141 | + }); |
| 142 | + |
| 143 | + const result = await collection.insertOne({ ssn }).catch(error => error); |
| 144 | + expect(result).to.be.instanceOf(MongoServerError); |
| 145 | + expect(result).to.have.property('code', documentValidationFailureCode); |
| 146 | + expect(result).to.have.nested.property( |
| 147 | + 'errInfo.details.schemaRulesNotSatisfied[0].propertiesNotSatisfied[0].propertyName', |
| 148 | + 'ssn' |
| 149 | + ); |
| 150 | + }); |
| 151 | + }; |
| 152 | + |
| 153 | + for (const provider of ['local', 'aws']) { |
| 154 | + context(`${provider}`, () => { |
| 155 | + runProseTestsFor(provider); |
| 156 | + }); |
| 157 | + } |
| 158 | +}); |
0 commit comments