use of org.jasypt.util.text.StrongTextEncryptor in project incubator-gobblin by apache.
the class PasswordManagerTest method testStrongEncryptionAndDecryption.
@Test
public void testStrongEncryptionAndDecryption() throws IOException {
String password = UUID.randomUUID().toString();
String masterPassword = UUID.randomUUID().toString();
File masterPwdFile = getMasterPwdFile(masterPassword);
State state = new State();
state.setProp(ConfigurationKeys.ENCRYPT_KEY_LOC, masterPwdFile.toString());
state.setProp(ConfigurationKeys.ENCRYPT_USE_STRONG_ENCRYPTOR, true);
try {
StrongTextEncryptor encryptor = new StrongTextEncryptor();
encryptor.setPassword(masterPassword);
String encrypted = encryptor.encrypt(password);
encrypted = "ENC(" + encrypted + ")";
String decrypted = PasswordManager.getInstance(state).readPassword(encrypted);
Assert.assertEquals(decrypted, password);
} catch (EncryptionOperationNotPossibleException e) {
// no strong encryption is supported
}
}
use of org.jasypt.util.text.StrongTextEncryptor in project incubator-gobblin by apache.
the class CLIPasswordEncryptor method getEncryptor.
private static TextEncryptor getEncryptor(CommandLine cl, String masterPassword) {
if (cl.hasOption(STRONG_ENCRYPTOR_OPTION)) {
StrongTextEncryptor encryptor = new StrongTextEncryptor();
encryptor.setPassword(masterPassword);
return encryptor;
} else {
BasicTextEncryptor encryptor = new BasicTextEncryptor();
encryptor.setPassword(masterPassword);
return encryptor;
}
}
use of org.jasypt.util.text.StrongTextEncryptor in project incubator-gobblin by apache.
the class PasswordManager method getEncryptors.
private List<TextEncryptor> getEncryptors(CachedInstanceKey cacheKey) {
List<TextEncryptor> encryptors = new ArrayList<>();
int numOfEncryptionKeys = cacheKey.numOfEncryptionKeys;
String suffix = "";
int i = 1;
if (cacheKey.masterPasswordFile == null || numOfEncryptionKeys < 1) {
return encryptors;
}
Exception exception = null;
do {
Path currentMasterPasswordFile = new Path(cacheKey.masterPasswordFile + suffix);
try (Closer closer = Closer.create()) {
if (!fs.exists(currentMasterPasswordFile) || fs.getFileStatus(currentMasterPasswordFile).isDirectory()) {
continue;
}
InputStream in = closer.register(fs.open(currentMasterPasswordFile));
String masterPassword = new LineReader(new InputStreamReader(in, Charsets.UTF_8)).readLine();
TextEncryptor encryptor = useStrongEncryptor ? new StrongTextEncryptor() : new BasicTextEncryptor();
// setPassword() needs to be called via reflection since the TextEncryptor interface doesn't have this method.
encryptor.getClass().getMethod("setPassword", String.class).invoke(encryptor, masterPassword);
encryptors.add(encryptor);
suffix = "." + String.valueOf(i);
} catch (FileNotFoundException fnf) {
// It is ok for password files not being present
LOG.warn("Master password file " + currentMasterPasswordFile + " not found.");
} catch (IOException ioe) {
exception = ioe;
LOG.warn("Master password could not be read from file " + currentMasterPasswordFile);
} catch (Exception e) {
LOG.warn("Encryptor could not be instantiated.");
}
} while (i++ < numOfEncryptionKeys);
// Throw exception if could not read any existing password file
if (encryptors.size() < 1 && exception != null) {
throw new RuntimeException("Master Password could not be read from any master password file.", exception);
}
return encryptors;
}
Aggregations