Search in sources :

Example 1 with UuidCodec

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);
}
Also used : UuidCodec(org.bson.codecs.UuidCodec) HashMap(java.util.HashMap) Fixture.getMongoClientSettings(com.mongodb.client.Fixture.getMongoClientSettings) MongoClientSettings(com.mongodb.MongoClientSettings) BsonString(org.bson.BsonString) ClientEncryptionSettings(com.mongodb.ClientEncryptionSettings) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 2 with UuidCodec

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;
}
Also used : UuidCodec(org.bson.codecs.UuidCodec) BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter)

Example 3 with UuidCodec

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);
}
Also used : UuidCodec(org.bson.codecs.UuidCodec) HashMap(java.util.HashMap) Fixture.getMongoClientSettings(com.mongodb.reactivestreams.client.Fixture.getMongoClientSettings) MongoClientSettings(com.mongodb.MongoClientSettings) BsonString(org.bson.BsonString) Fixture.getMongoClientBuilderFromConnectionString(com.mongodb.reactivestreams.client.Fixture.getMongoClientBuilderFromConnectionString) ClientEncryptionSettings(com.mongodb.ClientEncryptionSettings) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 4 with UuidCodec

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);
}
Also used : ByteArrayCodec(org.bson.codecs.ByteArrayCodec) UuidCodec(org.bson.codecs.UuidCodec) Jsr310CodecProvider(org.bson.codecs.jsr310.Jsr310CodecProvider) ObjectIdCodec(org.bson.codecs.ObjectIdCodec) DateCodec(org.bson.codecs.DateCodec) Decimal128Codec(org.bson.codecs.Decimal128Codec) BigDecimalCodec(org.bson.codecs.BigDecimalCodec) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) PatternCodec(org.bson.codecs.PatternCodec) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider)

Example 5 with UuidCodec

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());
    }
}
Also used : UuidCodec(org.bson.codecs.UuidCodec) Symbol(org.bson.types.Symbol) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Binary(org.bson.types.Binary)

Aggregations

UuidCodec (org.bson.codecs.UuidCodec)7 BsonDocument (org.bson.BsonDocument)4 Before (org.junit.Before)3 AutoEncryptionSettings (com.mongodb.AutoEncryptionSettings)2 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)2 MongoClientSettings (com.mongodb.MongoClientSettings)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BsonString (org.bson.BsonString)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 GuavaModule (com.fasterxml.jackson.datatype.guava.GuavaModule)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 Fixture.getMongoClientSettings (com.mongodb.client.Fixture.getMongoClientSettings)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 Fixture.getMongoClientBuilderFromConnectionString (com.mongodb.reactivestreams.client.Fixture.getMongoClientBuilderFromConnectionString)1 Fixture.getMongoClientSettings (com.mongodb.reactivestreams.client.Fixture.getMongoClientSettings)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1