Search in sources :

Example 11 with AutoEncryptionSettings

use of com.mongodb.AutoEncryptionSettings 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)

Example 12 with AutoEncryptionSettings

use of com.mongodb.AutoEncryptionSettings in project mongo-java-driver by mongodb.

the class ClientSideEncryptionViewAreProhibitedTest method setUp.

@Before
public void setUp() {
    assumeTrue(serverVersionAtLeast(4, 2));
    assumeTrue("Encryption test with external keyVault is disabled", isClientSideEncryptionTest());
    MongoClient client = getMongoClient();
    MongoDatabase db = client.getDatabase("db");
    db.getCollection("view").drop();
    db.createView("view", "coll", Collections.<BsonDocument>emptyList());
    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>();
    Map<String, Object> localMasterkey = new HashMap<String, Object>();
    byte[] localMasterkeyBytes = Base64.getDecoder().decode("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM" + "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
    localMasterkey.put("key", localMasterkeyBytes);
    kmsProviders.put("local", localMasterkey);
    AutoEncryptionSettings.Builder autoEncryptionSettingsBuilder = AutoEncryptionSettings.builder().keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders);
    AutoEncryptionSettings autoEncryptionSettings = autoEncryptionSettingsBuilder.build();
    MongoClientSettings.Builder clientSettingsBuilder = getMongoClientSettingsBuilder();
    MongoClientSettings clientSettings = clientSettingsBuilder.autoEncryptionSettings(autoEncryptionSettings).build();
    clientEncrypted = MongoClients.create(clientSettings);
}
Also used : HashMap(java.util.HashMap) BsonString(org.bson.BsonString) MongoClientSettings(com.mongodb.MongoClientSettings) Fixture.getMongoClient(com.mongodb.client.Fixture.getMongoClient) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 13 with AutoEncryptionSettings

use of com.mongodb.AutoEncryptionSettings in project mongo-java-driver by mongodb.

the class ClientSideEncryptionBypassAutoEncryptionTest method setUp.

@Before
public void setUp() throws Throwable {
    assumeTrue(serverVersionAtLeast(4, 2));
    final byte[] localMasterKey = new byte[96];
    new SecureRandom().nextBytes(localMasterKey);
    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {

        {
            put("local", new HashMap<String, Object>() {

                {
                    put("key", localMasterKey);
                }
            });
        }
    };
    MongoNamespace keyVaultNamespace = new MongoNamespace(Fixture.getDefaultDatabaseName(), "testKeyVault");
    Fixture.dropDatabase(Fixture.getDefaultDatabaseName());
    ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder().keyVaultMongoClientSettings(Fixture.getMongoClientSettings()).keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).build();
    clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
    AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).bypassAutoEncryption(true).build();
    MongoClientSettings clientSettings = Fixture.getMongoClientSettingsBuilder().autoEncryptionSettings(autoEncryptionSettings).build();
    clientEncrypted = MongoClients.create(clientSettings);
}
Also used : ClientEncryptionSettings(com.mongodb.ClientEncryptionSettings) HashMap(java.util.HashMap) SecureRandom(java.security.SecureRandom) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) BsonString(org.bson.BsonString) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 14 with AutoEncryptionSettings

use of com.mongodb.AutoEncryptionSettings in project mongo-java-driver by mongodb.

the class ClientSideEncryptionViewAreProhibitedTest method setUp.

@Before
public void setUp() {
    assumeTrue(serverVersionAtLeast(4, 2));
    assumeTrue("Encryption test with external keyVault is disabled", isClientSideEncryptionTest());
    MongoClient client = getMongoClient();
    MongoDatabase db = client.getDatabase("db");
    Mono.from(db.getCollection("view").drop()).block(TIMEOUT_DURATION);
    Mono.from(db.createView("view", "coll", Collections.<BsonDocument>emptyList())).block(TIMEOUT_DURATION);
    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>();
    Map<String, Object> localMasterkey = new HashMap<String, Object>();
    byte[] localMasterkeyBytes = Base64.getDecoder().decode("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM" + "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
    localMasterkey.put("key", localMasterkeyBytes);
    kmsProviders.put("local", localMasterkey);
    AutoEncryptionSettings.Builder autoEncryptionSettingsBuilder = AutoEncryptionSettings.builder().keyVaultNamespace("admin.datakeys").kmsProviders(kmsProviders);
    AutoEncryptionSettings autoEncryptionSettings = autoEncryptionSettingsBuilder.build();
    MongoClientSettings.Builder clientSettingsBuilder = getMongoClientBuilderFromConnectionString();
    MongoClientSettings clientSettings = clientSettingsBuilder.autoEncryptionSettings(autoEncryptionSettings).build();
    clientEncrypted = MongoClients.create(clientSettings);
}
Also used : HashMap(java.util.HashMap) BsonString(org.bson.BsonString) Fixture.getMongoClientBuilderFromConnectionString(com.mongodb.reactivestreams.client.Fixture.getMongoClientBuilderFromConnectionString) MongoClientSettings(com.mongodb.MongoClientSettings) Fixture.getMongoClient(com.mongodb.reactivestreams.client.Fixture.getMongoClient) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 15 with AutoEncryptionSettings

use of com.mongodb.AutoEncryptionSettings in project spring-data-mongodb by spring-projects.

the class MongoEncryptionSettingsFactoryBeanTests method createsAutoEncryptionSettings.

// DATAMONGO-2306
@Test
public void createsAutoEncryptionSettings() {
    RootBeanDefinition definition = new RootBeanDefinition(MongoEncryptionSettingsFactoryBean.class);
    definition.getPropertyValues().addPropertyValue("bypassAutoEncryption", true);
    definition.getPropertyValues().addPropertyValue("keyVaultNamespace", "ns");
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    factory.registerBeanDefinition("factory", definition);
    MongoEncryptionSettingsFactoryBean bean = factory.getBean("&factory", MongoEncryptionSettingsFactoryBean.class);
    assertThat(ReflectionTestUtils.getField(bean, "bypassAutoEncryption")).isEqualTo(true);
    AutoEncryptionSettings target = factory.getBean(AutoEncryptionSettings.class);
    assertThat(target.getKeyVaultNamespace()).isEqualTo("ns");
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) Test(org.junit.jupiter.api.Test)

Aggregations

AutoEncryptionSettings (com.mongodb.AutoEncryptionSettings)15 MongoClientSettings (com.mongodb.MongoClientSettings)14 HashMap (java.util.HashMap)13 Map (java.util.Map)13 BsonString (org.bson.BsonString)9 Before (org.junit.Before)9 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)8 BsonDocument (org.bson.BsonDocument)7 SecureRandom (java.security.SecureRandom)6 Document (org.bson.Document)5 MongoNamespace (com.mongodb.MongoNamespace)3 Fixture.getMongoClientBuilderFromConnectionString (com.mongodb.reactivestreams.client.Fixture.getMongoClientBuilderFromConnectionString)3 ConnectionString (com.mongodb.ConnectionString)2 Fixture.getMongoClient (com.mongodb.client.Fixture.getMongoClient)2 Fixture.getMongoClientSettings (com.mongodb.client.Fixture.getMongoClientSettings)2 MongoClient (com.mongodb.client.MongoClient)2 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)2 InsertOneResult (com.mongodb.client.result.InsertOneResult)2 Fixture.getMongoClient (com.mongodb.reactivestreams.client.Fixture.getMongoClient)2 MongoClient (com.mongodb.reactivestreams.client.MongoClient)2