Ensure that the field name is present in the exception text if an error is thrown from the encryptor
Some of our encryptors throw IllegalArgumentException; explicitly
catch those as well as IOExceptions
@@ -48,8 +48,8 @@ public abstract class StringFieldEncryptorConverter<SCHEMA, DATA> extends Conver
@Override
public Converter<SCHEMA, SCHEMA, DATA, DATA> init(WorkUnitState workUnit) {
super.init(workUnit);
- Map<String, Object> config =
- EncryptionConfigParser.getConfigForBranch(EncryptionConfigParser.EntityType.CONVERTER, getClass().getSimpleName(), workUnit);
+ Map<String, Object> config = EncryptionConfigParser
+ .getConfigForBranch(EncryptionConfigParser.EntityType.CONVERTER, getClass().getSimpleName(), workUnit);
encryptor = EncryptionFactory.buildStreamCryptoProvider(config);
String fieldsToEncryptConfig = workUnit.getProp(FIELDS_TO_ENCRYPT_CONFIG_NAME, null);
@@ -71,7 +71,6 @@ public abstract class StringFieldEncryptorConverter<SCHEMA, DATA> extends Conver
@Override
public Iterable<DATA> convertRecord(SCHEMA outputSchema, DATA inputRecord, WorkUnitState workUnit)
throws DataConversionException {
- try {
RecordAccessor accessor = getRecordAccessor(inputRecord);
for (String field : fieldsToEncrypt) {
@@ -82,10 +81,14 @@ public abstract class StringFieldEncryptorConverter<SCHEMA, DATA> extends Conver
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ try {
OutputStream cipherStream = encryptor.encodeOutputStream(outputStream);
cipherStream.write(bytes);
cipherStream.flush();
cipherStream.close();
+ } catch (IOException | IllegalArgumentException | IllegalStateException e) {
+ throw new DataConversionException("Error while encrypting field " + field + ": " + e.getMessage(), e);
+ }
byte[] cipherBytes = outputStream.toByteArray();
accessor.set(entry.getKey(), new String(cipherBytes, StandardCharsets.UTF_8));
@@ -93,9 +96,6 @@ public abstract class StringFieldEncryptorConverter<SCHEMA, DATA> extends Conver
}
return Collections.singleton(inputRecord);
- } catch (IOException e) {
- throw new DataConversionException("Error encrypting field", e);
- }
}
protected List<String> getFieldsToEncrypt() {
