Search in sources :

Example 51 with BsonString

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

the class InternalStreamConnectionInitializer method createHelloCommand.

private BsonDocument createHelloCommand(final Authenticator authenticator, final InternalConnection connection) {
    BsonDocument helloCommandDocument = new BsonDocument(getHandshakeCommandName(), new BsonInt32(1)).append("helloOk", BsonBoolean.TRUE);
    if (clientMetadataDocument != null) {
        helloCommandDocument.append("client", clientMetadataDocument);
    }
    if (clusterConnectionMode == ClusterConnectionMode.LOAD_BALANCED) {
        helloCommandDocument.append("loadBalanced", BsonBoolean.TRUE);
    }
    if (!requestedCompressors.isEmpty()) {
        BsonArray compressors = new BsonArray(this.requestedCompressors.size());
        for (MongoCompressor cur : this.requestedCompressors) {
            compressors.add(new BsonString(cur.getName()));
        }
        helloCommandDocument.append("compression", compressors);
    }
    if (checkSaslSupportedMechs) {
        MongoCredential credential = authenticator.getMongoCredential();
        helloCommandDocument.append("saslSupportedMechs", new BsonString(credential.getSource() + "." + credential.getUserName()));
    }
    if (authenticator instanceof SpeculativeAuthenticator) {
        BsonDocument speculativeAuthenticateDocument = ((SpeculativeAuthenticator) authenticator).createSpeculativeAuthenticateCommand(connection);
        if (speculativeAuthenticateDocument != null) {
            helloCommandDocument.append("speculativeAuthenticate", speculativeAuthenticateDocument);
        }
    }
    return helloCommandDocument;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) MongoCredential(com.mongodb.MongoCredential) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) MongoCompressor(com.mongodb.MongoCompressor)

Example 52 with BsonString

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

the class IdHoldingBsonWriter method writeString.

@Override
public void writeString(final String name, final String value) {
    setCurrentFieldName(name);
    addBsonValue(() -> new BsonString(value), () -> getIdBsonWriter().writeString(name, value));
    super.writeString(name, value);
}
Also used : BsonString(org.bson.BsonString)

Example 53 with BsonString

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

the class ClientEncryptionDataKeyAndDoubleEncryptionTest method testProvider.

@Test
public void testProvider() {
    String keyAltName = format("%s_altname", providerName);
    BsonBinary dataKeyId = clientEncryption.createDataKey(providerName, new DataKeyOptions().keyAltNames(singletonList(keyAltName)).masterKey(getMasterKey()));
    assertEquals(4, dataKeyId.getType());
    ArrayList<Document> dataKeys = client.getDatabase("keyvault").getCollection("datakeys").find(eq("_id", dataKeyId)).into(new ArrayList<>());
    assertEquals(1, dataKeys.size());
    Document dataKey = dataKeys.get(0);
    assertEquals(providerName, dataKey.get("masterKey", new Document()).get("provider", ""));
    String insertWriteConcern = commandListener.getCommandStartedEvent("insert").getCommand().getDocument("writeConcern", new BsonDocument()).getString("w", new BsonString("")).getValue();
    assertEquals("majority", insertWriteConcern);
    String stringToEncrypt = format("hello %s", providerName);
    BsonBinary encrypted = clientEncryption.encrypt(new BsonString(stringToEncrypt), new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyId(dataKeyId));
    assertEquals(6, encrypted.getType());
    Document insertDocument = new Document("_id", providerName);
    insertDocument.put("value", encrypted);
    clientEncrypted.getDatabase("db").getCollection("coll").insertOne(insertDocument);
    Document decryptedDocument = clientEncrypted.getDatabase("db").getCollection("coll").find(eq("_id", providerName)).first();
    assertNotNull(decryptedDocument);
    assertEquals(stringToEncrypt, decryptedDocument.get("value", ""));
    BsonBinary encryptedKeyAltName = clientEncryption.encrypt(new BsonString(stringToEncrypt), new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyAltName(keyAltName));
    assertEquals(encrypted, encryptedKeyAltName);
    assertThrows(MongoClientException.class, () -> clientEncrypted.getDatabase("db").getCollection("coll").insertOne(new Document("encrypted_placeholder", encrypted)));
}
Also used : BsonDocument(org.bson.BsonDocument) EncryptOptions(com.mongodb.client.model.vault.EncryptOptions) BsonBinary(org.bson.BsonBinary) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) DataKeyOptions(com.mongodb.client.model.vault.DataKeyOptions) Test(org.junit.Test)

Example 54 with BsonString

use of org.bson.BsonString 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 55 with BsonString

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

the class WriteConcernProseTest method setFailPoint.

private void setFailPoint() {
    failPointDocument = new BsonDocument("configureFailPoint", new BsonString("failCommand")).append("mode", new BsonDocument("times", new BsonInt32(1))).append("data", new BsonDocument("failCommands", new BsonArray(asList(new BsonString("insert")))).append("writeConcernError", new BsonDocument("code", new BsonInt32(100)).append("codeName", new BsonString("UnsatisfiableWriteConcern")).append("errmsg", new BsonString("Not enough data-bearing nodes")).append("errInfo", new BsonDocument("writeConcern", new BsonDocument("w", new BsonInt32(2)).append("wtimeout", new BsonInt32(0)).append("provenance", new BsonString("clientSupplied"))))));
    collectionHelper.runAdminCommand(failPointDocument);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray)

Aggregations

BsonString (org.bson.BsonString)178 BsonDocument (org.bson.BsonDocument)153 BsonInt32 (org.bson.BsonInt32)55 BsonArray (org.bson.BsonArray)48 Test (org.junit.Test)36 Document (org.bson.Document)32 BsonValue (org.bson.BsonValue)31 BsonInt64 (org.bson.BsonInt64)28 ArrayList (java.util.ArrayList)23 Test (org.junit.jupiter.api.Test)18 Map (java.util.Map)14 BsonDouble (org.bson.BsonDouble)14 MongoClientSettings (com.mongodb.MongoClientSettings)13 BsonBinary (org.bson.BsonBinary)13 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)12 HashMap (java.util.HashMap)12 MongoNamespace (com.mongodb.MongoNamespace)11 List (java.util.List)10 Before (org.junit.Before)10 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)9