use of org.bson.codecs.UuidCodec in project mongo-java-driver by mongodb.
the class ClientSideEncryptionCorpusTest method setUp.
@Before
public void setUp() throws IOException, URISyntaxException {
assumeTrue(serverVersionAtLeast(4, 2));
assumeTrue("Corpus tests disabled", hasEncryptionTestsEnabled());
MongoClientSettings clientSettings = getMongoClientSettingsBuilder().codecRegistry(fromRegistries(fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)), MongoClientSettings.getDefaultCodecRegistry())).build();
// Step 1: create unencrypted client
client = MongoClients.create(clientSettings);
MongoDatabase db = client.getDatabase("db");
// Step 2: Drop and recreate db.coll with schema
BsonDocument schemaDocument = bsonDocumentFromPath("corpus-schema.json");
db.getCollection("coll").drop();
db.runCommand(new BsonDocument("create", new BsonString("coll")).append("validator", new BsonDocument("$jsonSchema", schemaDocument)));
// Step 3: Drop and create keyvault.datakeys
MongoDatabase keyvaultDatabase = client.getDatabase("keyvault");
MongoCollection<BsonDocument> dataKeysCollection = keyvaultDatabase.getCollection("datakeys", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
dataKeysCollection.drop();
dataKeysCollection.insertOne(bsonDocumentFromPath("corpus-key-aws.json"));
dataKeysCollection.insertOne(bsonDocumentFromPath("corpus-key-azure.json"));
dataKeysCollection.insertOne(bsonDocumentFromPath("corpus-key-gcp.json"));
dataKeysCollection.insertOne(bsonDocumentFromPath("corpus-key-kmip.json"));
dataKeysCollection.insertOne(bsonDocumentFromPath("corpus-key-local.json"));
// Step 4: Configure our objects
Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {
{
put("aws", new HashMap<String, Object>() {
{
put("accessKeyId", System.getProperty("org.mongodb.test.awsAccessKeyId"));
put("secretAccessKey", System.getProperty("org.mongodb.test.awsSecretAccessKey"));
}
});
put("azure", new HashMap<String, Object>() {
{
put("tenantId", System.getProperty("org.mongodb.test.azureTenantId"));
put("clientId", System.getProperty("org.mongodb.test.azureClientId"));
put("clientSecret", System.getProperty("org.mongodb.test.azureClientSecret"));
}
});
put("gcp", new HashMap<String, Object>() {
{
put("email", System.getProperty("org.mongodb.test.gcpEmail"));
put("privateKey", System.getProperty("org.mongodb.test.gcpPrivateKey"));
}
});
put("kmip", new HashMap<String, Object>() {
{
put("endpoint", System.getProperty("org.mongodb.test.kmipEndpoint", "localhost:5698"));
}
});
put("local", new HashMap<String, Object>() {
{
put("key", "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM" + "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
}
});
}
};
HashMap<String, BsonDocument> schemaMap = new HashMap<String, BsonDocument>();
schemaMap.put("db.coll", schemaDocument);
AutoEncryptionSettings.Builder autoEncryptionSettingsBuilder = AutoEncryptionSettings.builder().keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders);
if (useLocalSchema) {
autoEncryptionSettingsBuilder.schemaMap(schemaMap);
}
clientSettings = getMongoClientSettingsBuilder().codecRegistry(fromRegistries(fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)), MongoClientSettings.getDefaultCodecRegistry())).autoEncryptionSettings(autoEncryptionSettingsBuilder.build()).build();
autoEncryptingClient = MongoClients.create(clientSettings);
ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder().keyVaultMongoClientSettings(getMongoClientSettings()).kmsProviders(kmsProviders).keyVaultNamespace("keyvault.datakeys").build();
clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
}
use of org.bson.codecs.UuidCodec in project mongo-java-driver by mongodb.
the class SimpleSessionContext method createNewServerSessionIdentifier.
private static BsonDocument createNewServerSessionIdentifier() {
UuidCodec uuidCodec = new UuidCodec(UuidRepresentation.STANDARD);
BsonDocument holder = new BsonDocument();
BsonDocumentWriter bsonDocumentWriter = new BsonDocumentWriter(holder);
bsonDocumentWriter.writeStartDocument();
bsonDocumentWriter.writeName("id");
uuidCodec.encode(bsonDocumentWriter, UUID.randomUUID(), EncoderContext.builder().build());
bsonDocumentWriter.writeEndDocument();
return holder;
}
use of org.bson.codecs.UuidCodec in project mongo-java-driver by mongodb.
the class ClientSideEncryptionCorpusTest method setUp.
@Before
public void setUp() throws IOException, URISyntaxException {
assumeTrue(serverVersionAtLeast(4, 2));
assumeTrue("Corpus tests disabled", hasEncryptionTestsEnabled());
MongoClientSettings clientSettings = getMongoClientBuilderFromConnectionString().codecRegistry(fromRegistries(fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)), MongoClientSettings.getDefaultCodecRegistry())).build();
// Step 1: create unencrypted client
client = MongoClients.create(clientSettings);
MongoDatabase db = client.getDatabase("db");
// Step 2: Drop and recreate db.coll with schema
BsonDocument schemaDocument = bsonDocumentFromPath("corpus-schema.json");
Mono.from(db.getCollection("coll").drop()).block(TIMEOUT_DURATION);
Mono.from(db.runCommand(new BsonDocument("create", new BsonString("coll")).append("validator", new BsonDocument("$jsonSchema", schemaDocument)))).block(TIMEOUT_DURATION);
// Step 3: Drop and create keyvault.datakeys
MongoDatabase keyVaultDatabase = client.getDatabase("keyvault");
MongoCollection<BsonDocument> dataKeysCollection = keyVaultDatabase.getCollection("datakeys", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
Mono.from(dataKeysCollection.drop()).block(TIMEOUT_DURATION);
Mono.from(dataKeysCollection.insertOne(bsonDocumentFromPath("corpus-key-aws.json"))).block(TIMEOUT_DURATION);
Mono.from(dataKeysCollection.insertOne(bsonDocumentFromPath("corpus-key-azure.json"))).block(TIMEOUT_DURATION);
Mono.from(dataKeysCollection.insertOne(bsonDocumentFromPath("corpus-key-gcp.json"))).block(TIMEOUT_DURATION);
Mono.from(dataKeysCollection.insertOne(bsonDocumentFromPath("corpus-key-kmip.json"))).block(TIMEOUT_DURATION);
Mono.from(dataKeysCollection.insertOne(bsonDocumentFromPath("corpus-key-local.json"))).block(TIMEOUT_DURATION);
// Step 4: Configure our objects
Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {
{
put("aws", new HashMap<String, Object>() {
{
put("accessKeyId", System.getProperty("org.mongodb.test.awsAccessKeyId"));
put("secretAccessKey", System.getProperty("org.mongodb.test.awsSecretAccessKey"));
}
});
put("azure", new HashMap<String, Object>() {
{
put("tenantId", System.getProperty("org.mongodb.test.azureTenantId"));
put("clientId", System.getProperty("org.mongodb.test.azureClientId"));
put("clientSecret", System.getProperty("org.mongodb.test.azureClientSecret"));
}
});
put("gcp", new HashMap<String, Object>() {
{
put("email", System.getProperty("org.mongodb.test.gcpEmail"));
put("privateKey", System.getProperty("org.mongodb.test.gcpPrivateKey"));
}
});
put("kmip", new HashMap<String, Object>() {
{
put("endpoint", System.getProperty("org.mongodb.test.kmipEndpoint", "localhost:5698"));
}
});
put("local", new HashMap<String, Object>() {
{
put("key", "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM" + "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
}
});
}
};
HashMap<String, BsonDocument> schemaMap = new HashMap<>();
schemaMap.put("db.coll", schemaDocument);
AutoEncryptionSettings.Builder autoEncryptionSettingsBuilder = AutoEncryptionSettings.builder().keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders);
if (useLocalSchema) {
autoEncryptionSettingsBuilder.schemaMap(schemaMap);
}
clientSettings = getMongoClientBuilderFromConnectionString().codecRegistry(fromRegistries(fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)), MongoClientSettings.getDefaultCodecRegistry())).autoEncryptionSettings(autoEncryptionSettingsBuilder.build()).build();
autoEncryptingClient = MongoClients.create(clientSettings);
ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder().keyVaultMongoClientSettings(getMongoClientSettings()).kmsProviders(kmsProviders).keyVaultNamespace("keyvault.datakeys").build();
clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
}
use of org.bson.codecs.UuidCodec in project immutables by immutables.
the class BsonModule method defaultRegistry.
private static CodecRegistry defaultRegistry() {
CodecRegistry standard = CodecRegistries.fromProviders(new BsonValueCodecProvider(), new Jsr310CodecProvider());
// avoid codecs for String / Long / Boolean etc. They're already handled by jackson
// choose the ones which need to be natively serialized in non-JSON format (BSON)
CodecRegistry others = CodecRegistries.fromCodecs(new ObjectIdCodec(), new DateCodec(), new UuidCodec(UuidRepresentation.JAVA_LEGACY), new Decimal128Codec(), new PatternCodec(), new BigDecimalCodec(), new ByteArrayCodec());
return CodecRegistries.fromRegistries(standard, others);
}
use of org.bson.codecs.UuidCodec in project mongo-java-driver by mongodb.
the class LazyBSONObject method readValue.
Object readValue(final BsonBinaryReader reader) {
switch(reader.getCurrentBsonType()) {
case DOCUMENT:
return readDocument(reader);
case ARRAY:
return readArray(reader);
case DOUBLE:
return reader.readDouble();
case STRING:
return reader.readString();
case BINARY:
byte binarySubType = reader.peekBinarySubType();
if (BsonBinarySubType.isUuid(binarySubType) && reader.peekBinarySize() == 16) {
return new UuidCodec().decode(reader, DecoderContext.builder().build());
}
BsonBinary binary = reader.readBinaryData();
if (binarySubType == BINARY.getValue() || binarySubType == OLD_BINARY.getValue()) {
return binary.getData();
} else {
return new Binary(binary.getType(), binary.getData());
}
case NULL:
reader.readNull();
return null;
case UNDEFINED:
reader.readUndefined();
return null;
case OBJECT_ID:
return reader.readObjectId();
case BOOLEAN:
return reader.readBoolean();
case DATE_TIME:
return new Date(reader.readDateTime());
case REGULAR_EXPRESSION:
BsonRegularExpression regularExpression = reader.readRegularExpression();
return Pattern.compile(regularExpression.getPattern(), BSON.regexFlags(regularExpression.getOptions()));
case DB_POINTER:
BsonDbPointer dbPointer = reader.readDBPointer();
return callback.createDBRef(dbPointer.getNamespace(), dbPointer.getId());
case JAVASCRIPT:
return new Code(reader.readJavaScript());
case SYMBOL:
return new Symbol(reader.readSymbol());
case JAVASCRIPT_WITH_SCOPE:
return new CodeWScope(reader.readJavaScriptWithScope(), (BSONObject) readJavaScriptWithScopeDocument(reader));
case INT32:
return reader.readInt32();
case TIMESTAMP:
BsonTimestamp timestamp = reader.readTimestamp();
return new BSONTimestamp(timestamp.getTime(), timestamp.getInc());
case INT64:
return reader.readInt64();
case DECIMAL128:
return reader.readDecimal128();
case MIN_KEY:
reader.readMinKey();
return new MinKey();
case MAX_KEY:
reader.readMaxKey();
return new MaxKey();
default:
throw new IllegalArgumentException("unhandled BSON type: " + reader.getCurrentBsonType());
}
}
Aggregations