jstests/fle2/libs/encrypted_client_util.js
/**
     * Create an encrypted collection. If key ids are not specified, it creates them automatically
     * in the key vault.
     *
     * @param {string} name Name of collection
     * @param {Object} options Create Collection options
     */
Create an encrypted collection. If key ids are not specified, it creates them automatically
in the key vault.

@param {string} name Name of collection
@param {Object} options Create Collection options
Create an encrypted collection . If key ids are not specified it creates them automatically in the key vault . @param { string } name Name of collection @param { Object } options Create Collection options
createEncryptionCollection(name, options) {
        assert(options != undefined);
        assert(options.hasOwnProperty("encryptedFields"));
        assert(options.encryptedFields.hasOwnProperty("fields"));
        for (let field of options.encryptedFields.fields) {
            if (!field.hasOwnProperty("keyId")) {
                let testkeyId = this._keyVault.createKey("local", "ignored");
                field["keyId"] = testkeyId;
            }
        }
        assert.neq(options,
                   undefined,
                   `createEncryptedCollection expected an options object, it is undefined`);
        assert(
            options.hasOwnProperty("encryptedFields") && typeof options.encryptedFields == "object",
            `options must contain an encryptedFields document'`);
        const res = assert.commandWorked(this._edb.createCollection(name, options));
        const cis = this._edb.getCollectionInfos({"name": name});
        assert.eq(cis.length, 1, `Expected to find one collection named '${name}'`);
        const ci = cis[0];
        assert(ci.hasOwnProperty("options"), `Expected collection '${name}' to have 'options'`);
        const storedOptions = ci.options;
        assert(options.hasOwnProperty("encryptedFields"),
               `Expected collection '${name}' to have 'encryptedFields'`);
        const ef = storedOptions.encryptedFields;
        if (this.useImplicitSharding) {
            let resShard = this._db.adminCommand({enableSharding: this._db.getName()});
            if (resShard.code !== ErrorCodes.AlreadyInitialized) {
                assert.commandWorked(
                    resShard, "enabling sharding on the '" + this._db.getName() + "' db failed");
            }
            let shardCollCmd = {
                shardCollection: this._db.getName() + "." + name,
                key: {last: "hashed"},
                collation: {locale: "simple"}
            };
            resShard = this._db.adminCommand(shardCollCmd);
            jsTestLog("Sharding: " + tojson(shardCollCmd));
        }
        assert.commandWorked(this._edb.getCollection(name).createIndex({__safeContent__: 1}));
        assert.commandWorked(this._edb.createCollection(ef.escCollection));
        assert.commandWorked(this._edb.createCollection(ef.eccCollection));
        assert.commandWorked(this._edb.createCollection(ef.ecocCollection));
        return res;
    }
createEncryptionCollection ( name , options ) { assert ( options != undefined ) ; assert ( options . hasOwnProperty ( "encryptedFields" ) ) ; assert ( options . encryptedFields . hasOwnProperty ( "fields" ) ) ; for ( let field of options . encryptedFields . fields ) { if ( ! field . hasOwnProperty ( "keyId" ) ) { let testkeyId = this . _keyVault . createKey ( "local" , "ignored" ) ; field [ "keyId" ] = testkeyId ; } } assert . neq ( options , undefined , ` ` ) ; assert ( options . hasOwnProperty ( "encryptedFields" ) && typeof options . encryptedFields == "object" , ` ` ) ; const res = assert . commandWorked ( this . _edb . createCollection ( name , options ) ) ; const cis = this . _edb . getCollectionInfos ( { "name" : name } ) ; assert . eq ( cis . length , 1 , ` ${ name } ` ) ; const ci = cis [ 0 ] ; assert ( ci . hasOwnProperty ( "options" ) , ` ${ name } ` ) ; const storedOptions = ci . options ; assert ( options . hasOwnProperty ( "encryptedFields" ) , ` ${ name } ` ) ; const ef = storedOptions . encryptedFields ; if ( this . useImplicitSharding ) { let resShard = this . _db . adminCommand ( { enableSharding : this . _db . getName ( ) } ) ; if ( resShard . code !== ErrorCodes . AlreadyInitialized ) { assert . commandWorked ( resShard , "enabling sharding on the '" + this . _db . getName ( ) + "' db failed" ) ; } let shardCollCmd = { shardCollection : this . _db . getName ( ) + "." + name , key : { last : "hashed" } , collation : { locale : "simple" } } ; resShard = this . _db . adminCommand ( shardCollCmd ) ; jsTestLog ( "Sharding: " + tojson ( shardCollCmd ) ) ; } assert . commandWorked ( this . _edb . getCollection ( name ) . createIndex ( { __safeContent__ : 1 } ) ) ; assert . commandWorked ( this . _edb . createCollection ( ef . escCollection ) ) ; assert . commandWorked ( this . _edb . createCollection ( ef . eccCollection ) ) ; assert . commandWorked ( this . _edb . createCollection ( ef . ecocCollection ) ) ; return res ; }
Create an encrypted collection.
Create an encrypted collection .
// All our tests use "last" as the key to query on so shard on "last" instead of "_id" // enableSharding may only be called once for a database.