Search in sources :

Example 56 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class ClientSideEncryptionSessionTest method setUp.

@Before
public void setUp() throws IOException, URISyntaxException {
    assumeTrue(serverVersionAtLeast(4, 2));
    assumeTrue(isClientSideEncryptionTest());
    assumeFalse(isStandalone());
    /* Step 1: get unencrypted client and recreate keys collection */
    client = getMongoClient();
    MongoDatabase keyvaultDatabase = client.getDatabase("keyvault");
    MongoCollection<BsonDocument> datakeys = keyvaultDatabase.getCollection("datakeys", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
    datakeys.drop();
    datakeys.insertOne(bsonDocumentFromPath("external-key.json"));
    /* Step 2: create encryption objects. */
    Map<String, Map<String, Object>> kmsProviders = new HashMap<>();
    Map<String, Object> localMasterkey = new HashMap<>();
    Map<String, BsonDocument> schemaMap = new HashMap<>();
    byte[] localMasterkeyBytes = Base64.getDecoder().decode("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM" + "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
    localMasterkey.put("key", localMasterkeyBytes);
    kmsProviders.put("local", localMasterkey);
    schemaMap.put(getDefaultDatabaseName() + "." + COLLECTION_NAME, bsonDocumentFromPath("external-schema.json"));
    AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder().keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders).schemaMap(schemaMap).build();
    MongoClientSettings clientSettings = getMongoClientSettingsBuilder().autoEncryptionSettings(autoEncryptionSettings).build();
    clientEncrypted = MongoClients.create(clientSettings);
    CollectionHelper<BsonDocument> collectionHelper = new CollectionHelper<>(new BsonDocumentCodec(), new MongoNamespace(getDefaultDatabaseName(), COLLECTION_NAME));
    collectionHelper.drop();
    collectionHelper.create();
}
Also used : HashMap(java.util.HashMap) BsonString(org.bson.BsonString) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace) BsonDocument(org.bson.BsonDocument) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) CollectionHelper(com.mongodb.client.test.CollectionHelper) HashMap(java.util.HashMap) Map(java.util.Map) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Before(org.junit.Before)

Aggregations

BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)56 BsonDocument (org.bson.BsonDocument)29 MongoNamespace (com.mongodb.MongoNamespace)9 BsonBinaryReader (org.bson.BsonBinaryReader)8 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)8 BsonBinaryWriter (org.bson.BsonBinaryWriter)5 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)5 JsonReader (org.bson.json.JsonReader)5 Test (org.junit.Test)5 CollectionHelper (com.mongodb.client.test.CollectionHelper)4 NoOpFieldNameValidator (com.mongodb.internal.validator.NoOpFieldNameValidator)4 StringWriter (java.io.StringWriter)4 BsonArray (org.bson.BsonArray)4 BsonString (org.bson.BsonString)4 JsonWriter (org.bson.json.JsonWriter)4 ByteBuffer (java.nio.ByteBuffer)3 BsonDocumentWriter (org.bson.BsonDocumentWriter)3 Before (org.junit.Before)3 MongoClientSettings (com.mongodb.MongoClientSettings)2 MongoCommandException (com.mongodb.MongoCommandException)2