|
import com.mongodb.AutoEncryptionSettings;
|
import com.mongodb.MongoClientSettings;
|
import com.mongodb.client.MongoClients;
|
import org.bson.Document;
|
|
import java.security.SecureRandom;
|
import java.util.Map;
|
|
public class ClientSideEncryptionSimpleTest {
|
public static void main(String[] args) {
|
// This would have to be the same master key as was used to create the encryption key
|
var localMasterKey = new byte[96];
|
new SecureRandom().nextBytes(localMasterKey);
|
|
var kmsProviders = Map.of("local",
|
Map.<String, Object>of("key", localMasterKey));
|
var keyVaultNamespace = "admin.datakeys";
|
|
var autoEncryptionSettings =
|
AutoEncryptionSettings.builder()
|
.keyVaultNamespace(keyVaultNamespace)
|
.kmsProviders(kmsProviders)
|
.build();
|
var clientSettings = MongoClientSettings.builder()
|
.autoEncryptionSettings(autoEncryptionSettings)
|
.build();
|
|
var client = MongoClients.create(clientSettings);
|
var collection = client.getDatabase("test").getCollection("coll");
|
collection.drop();
|
|
collection.insertOne(new Document("encryptedField", "123456789"));
|
|
System.out.println(collection.find().first().toJson());
|
}
|
}
|