Search in sources :

Example 1 with InsecureShiftCodec

use of org.apache.gobblin.test.crypto.InsecureShiftCodec in project incubator-gobblin by apache.

the class AvroStringFieldEncryptorConverterTest method testNestedConversion.

@Test
public void testNestedConversion() throws DataConversionException, IOException, SchemaConversionException {
    AvroStringFieldEncryptorConverter converter = new AvroStringFieldEncryptorConverter();
    WorkUnitState wuState = new WorkUnitState();
    wuState.getJobState().setProp("converter.fieldsToEncrypt", "nestedRecords.*.fieldToEncrypt");
    wuState.getJobState().setProp("converter.encrypt.algorithm", "insecure_shift");
    converter.init(wuState);
    GenericRecord inputRecord = getRecordFromFile(getClass().getClassLoader().getResource("record_with_arrays.avro").getPath());
    Schema inputSchema = inputRecord.getSchema();
    Schema outputSchema = converter.convertSchema(inputSchema, wuState);
    List<String> origValues = new ArrayList<>();
    for (Object o : (List) inputRecord.get("nestedRecords")) {
        GenericRecord r = (GenericRecord) o;
        origValues.add(r.get("fieldToEncrypt").toString());
    }
    Iterable<GenericRecord> recordIt = converter.convertRecord(outputSchema, inputRecord, wuState);
    GenericRecord record = recordIt.iterator().next();
    Assert.assertEquals(outputSchema, inputSchema);
    List<String> decryptedValues = new ArrayList<>();
    for (Object o : (List) record.get("nestedRecords")) {
        GenericRecord r = (GenericRecord) o;
        String encryptedValue = r.get("fieldToEncrypt").toString();
        InsecureShiftCodec codec = new InsecureShiftCodec(Maps.<String, Object>newHashMap());
        InputStream in = codec.decodeInputStream(new ByteArrayInputStream(encryptedValue.getBytes(StandardCharsets.UTF_8)));
        byte[] decryptedValue = new byte[in.available()];
        in.read(decryptedValue);
        decryptedValues.add(new String(decryptedValue, StandardCharsets.UTF_8));
    }
    Assert.assertEquals(decryptedValues, origValues);
}
Also used : InsecureShiftCodec(org.apache.gobblin.test.crypto.InsecureShiftCodec) WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) List(java.util.List) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Example 2 with InsecureShiftCodec

use of org.apache.gobblin.test.crypto.InsecureShiftCodec in project incubator-gobblin by apache.

the class AvroStringFieldEncryptorConverterTest method testConversion.

@Test
public void testConversion() throws DataConversionException, IOException, SchemaConversionException {
    AvroStringFieldEncryptorConverter converter = new AvroStringFieldEncryptorConverter();
    WorkUnitState wuState = new WorkUnitState();
    wuState.getJobState().setProp("converter.fieldsToEncrypt", "field1");
    wuState.getJobState().setProp("converter.encrypt.algorithm", "insecure_shift");
    converter.init(wuState);
    GenericRecord inputRecord = TestUtils.generateRandomAvroRecord();
    Schema inputSchema = inputRecord.getSchema();
    Schema outputSchema = converter.convertSchema(inputSchema, wuState);
    String fieldValue = (String) inputRecord.get("field1");
    Iterable<GenericRecord> recordIt = converter.convertRecord(outputSchema, inputRecord, wuState);
    GenericRecord record = recordIt.iterator().next();
    Assert.assertEquals(outputSchema, inputSchema);
    String encryptedValue = (String) record.get("field1");
    InsecureShiftCodec codec = new InsecureShiftCodec(Maps.<String, Object>newHashMap());
    InputStream in = codec.decodeInputStream(new ByteArrayInputStream(encryptedValue.getBytes(StandardCharsets.UTF_8)));
    byte[] decryptedValue = new byte[in.available()];
    in.read(decryptedValue);
    Assert.assertEquals(new String(decryptedValue, StandardCharsets.UTF_8), fieldValue);
}
Also used : InsecureShiftCodec(org.apache.gobblin.test.crypto.InsecureShiftCodec) ByteArrayInputStream(java.io.ByteArrayInputStream) WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Schema (org.apache.avro.Schema)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 WorkUnitState (org.apache.gobblin.configuration.WorkUnitState)2 InsecureShiftCodec (org.apache.gobblin.test.crypto.InsecureShiftCodec)2 Test (org.testng.annotations.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1