Search in sources :

Example 46 with BsonBinary

use of org.bson.BsonBinary in project drill by axbaretto.

the class TestBsonRecordReader method testBinaryTypes.

@Test
public void testBinaryTypes() throws IOException {
    // test with different binary types
    BsonDocument bsonDoc = new BsonDocument();
    // Binary
    // String
    byte[] bytes = "binaryValue".getBytes();
    bsonDoc.append("binaryKey", new BsonBinary(BsonBinarySubType.BINARY, bytes));
    // String
    byte[] bytesString = "binaryStringValue".getBytes();
    bsonDoc.append("binaryStringKey", new BsonBinary((byte) 2, bytesString));
    // Double
    byte[] bytesDouble = new byte[8];
    java.nio.ByteBuffer.wrap(bytesDouble).putDouble(23.0123);
    BsonBinary bsonDouble = new BsonBinary((byte) 1, bytesDouble);
    bsonDoc.append("binaryDouble", bsonDouble);
    // Boolean
    byte[] booleanBytes = new byte[8];
    java.nio.ByteBuffer.wrap(booleanBytes).put((byte) 1);
    BsonBinary bsonBoolean = new BsonBinary((byte) 8, booleanBytes);
    bsonDoc.append("bsonBoolean", bsonBoolean);
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    assertTrue(Arrays.equals(bytes, mapReader.reader("binaryKey").readByteArray()));
    assertEquals("binaryStringValue", mapReader.reader("binaryStringKey").readText().toString());
    assertEquals(23.0123, mapReader.reader("binaryDouble").readDouble().doubleValue(), 0);
    FieldReader reader = mapReader.reader("bsonBoolean");
    assertEquals(true, reader.readBoolean().booleanValue());
}
Also used : BsonDocument(org.bson.BsonDocument) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonBinary(org.bson.BsonBinary) BsonDocumentReader(org.bson.BsonDocumentReader) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader) Test(org.junit.Test)

Example 47 with BsonBinary

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

the class JsonReader method visitHexDataConstructor.

private BsonBinary visitHexDataConstructor() {
    verifyToken(JsonTokenType.LEFT_PAREN);
    JsonToken subTypeToken = popToken();
    if (subTypeToken.getType() != JsonTokenType.INT32) {
        throw new JsonParseException("JSON reader expected a binary subtype but found '%s'.", subTypeToken.getValue());
    }
    verifyToken(JsonTokenType.COMMA);
    String hex = readStringFromExtendedJson();
    verifyToken(JsonTokenType.RIGHT_PAREN);
    if ((hex.length() & 1) != 0) {
        hex = "0" + hex;
    }
    for (final BsonBinarySubType subType : BsonBinarySubType.values()) {
        if (subType.getValue() == subTypeToken.getValue(Integer.class)) {
            return new BsonBinary(subType, decodeHex(hex));
        }
    }
    return new BsonBinary(decodeHex(hex));
}
Also used : BsonBinarySubType(org.bson.BsonBinarySubType) BsonBinary(org.bson.BsonBinary)

Example 48 with BsonBinary

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

the class JsonReader method visitLegacyBinaryExtendedJson.

private BsonBinary visitLegacyBinaryExtendedJson(final String firstKey) {
    Mark mark = new Mark();
    try {
        verifyToken(JsonTokenType.COLON);
        byte[] data;
        byte type;
        if (firstKey.equals("$binary")) {
            data = Base64.getDecoder().decode(readStringFromExtendedJson());
            verifyToken(JsonTokenType.COMMA);
            verifyString("$type");
            verifyToken(JsonTokenType.COLON);
            type = readBinarySubtypeFromExtendedJson();
        } else {
            type = readBinarySubtypeFromExtendedJson();
            verifyToken(JsonTokenType.COMMA);
            verifyString("$binary");
            verifyToken(JsonTokenType.COLON);
            data = Base64.getDecoder().decode(readStringFromExtendedJson());
        }
        verifyToken(JsonTokenType.END_OBJECT);
        return new BsonBinary(type, data);
    } catch (JsonParseException e) {
        mark.reset();
        return null;
    } catch (NumberFormatException e) {
        mark.reset();
        return null;
    } finally {
        mark.discard();
    }
}
Also used : BsonBinary(org.bson.BsonBinary) BsonReaderMark(org.bson.BsonReaderMark)

Example 49 with BsonBinary

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

the class ClientSideEncryptionAutoEncryptionSettingsTour method main.

/**
 * Run this main method to see the output of this quick example.
 *
 * Requires the mongodb-crypt library in the class path and mongocryptd on the system path.
 *
 * @param args ignored args
 */
public static void main(final String[] args) {
    // This would have to be the same master key as was used to create the encryption key
    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);
                }
            });
        }
    };
    String keyVaultNamespace = "admin.datakeys";
    ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder().keyVaultMongoClientSettings(MongoClientSettings.builder().applyConnectionString(new ConnectionString("mongodb://localhost")).build()).keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders).build();
    ClientEncryption clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
    BsonBinary dataKeyId = clientEncryption.createDataKey("local", new DataKeyOptions());
    final String base64DataKeyId = Base64.getEncoder().encodeToString(dataKeyId.getData());
    final String dbName = "test";
    final String collName = "coll";
    AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders).schemaMap(new HashMap<String, BsonDocument>() {

        {
            put(dbName + "." + collName, // Need a schema that references the new data key
            BsonDocument.parse("{" + "  properties: {" + "    encryptedField: {" + "      encrypt: {" + "        keyId: [{" + "          \"$binary\": {" + "            \"base64\": \"" + base64DataKeyId + "\"," + "            \"subType\": \"04\"" + "          }" + "        }]," + "        bsonType: \"string\"," + "        algorithm: \"AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic\"" + "      }" + "    }" + "  }," + "  \"bsonType\": \"object\"" + "}"));
        }
    }).build();
    MongoClientSettings clientSettings = MongoClientSettings.builder().autoEncryptionSettings(autoEncryptionSettings).build();
    MongoClient mongoClient = MongoClients.create(clientSettings);
    MongoCollection<Document> collection = mongoClient.getDatabase("test").getCollection("coll");
    // Clear old data
    collection.drop();
    collection.insertOne(new Document("encryptedField", "123456789"));
    System.out.println(collection.find().first().toJson());
    // release resources
    mongoClient.close();
}
Also used : HashMap(java.util.HashMap) BsonBinary(org.bson.BsonBinary) ClientEncryption(com.mongodb.client.vault.ClientEncryption) SecureRandom(java.security.SecureRandom) ConnectionString(com.mongodb.ConnectionString) MongoClientSettings(com.mongodb.MongoClientSettings) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) DataKeyOptions(com.mongodb.client.model.vault.DataKeyOptions) MongoClient(com.mongodb.client.MongoClient) ClientEncryptionSettings(com.mongodb.ClientEncryptionSettings) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) ConnectionString(com.mongodb.ConnectionString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 50 with BsonBinary

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

the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldEncodeDocumentWithUuidRepresentation.

@Test
public void shouldEncodeDocumentWithUuidRepresentation() {
    documentCollection.insertOne(new Document("_id", uuid));
    BsonDocument document = bsonDocumentCollection.find().first();
    assertNotNull(document);
    BsonBinary uuidAsBinary = document.getBinary("_id");
    assertEquals(subType.getValue(), uuidAsBinary.getType());
    assertArrayEquals(encodedValue, uuidAsBinary.getData());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Aggregations

BsonBinary (org.bson.BsonBinary)78 Test (org.junit.Test)51 BsonDocument (org.bson.BsonDocument)32 BsonString (org.bson.BsonString)16 Document (org.bson.Document)16 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)13 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)11 BSONException (org.bson.BSONException)9 Map (java.util.Map)8 BasicDBObject (com.mongodb.BasicDBObject)6 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)6 ConnectionString (com.mongodb.ConnectionString)6 MongoClientSettings (com.mongodb.MongoClientSettings)6 SecureRandom (java.security.SecureRandom)6 HashMap (java.util.HashMap)6 ClientEncryption (com.mongodb.client.vault.ClientEncryption)5 UUID (java.util.UUID)5 Binary (org.bson.types.Binary)5 MongoNamespace (com.mongodb.MongoNamespace)4 IndexOptions (com.mongodb.client.model.IndexOptions)4