use of org.jasypt.util.text.TextEncryptor in project incubator-gobblin by apache.
the class PasswordManager method decryptPassword.
/**
* Decrypt an encrypted password. A master password file must have been provided in the constructor.
* @param encrypted An encrypted password.
* @return The decrypted password.
*/
public String decryptPassword(String encrypted) {
Preconditions.checkArgument(this.encryptors.size() > 0, "A master password needs to be provided for decrypting passwords.");
for (TextEncryptor encryptor : encryptors) {
try {
return encryptor.decrypt(encrypted);
} catch (Exception e) {
LOG.warn("Failed attempt to decrypt secret {}", encrypted, e);
}
}
LOG.error("All {} decrypt attempt(s) failed.", encryptors.size());
throw new RuntimeException("Failed to decrypt password ENC(" + encrypted + ")");
}
use of org.jasypt.util.text.TextEncryptor 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;
}
use of org.jasypt.util.text.TextEncryptor in project incubator-gobblin by apache.
the class CLIPasswordEncryptor method main.
public static void main(String[] args) throws ParseException {
CommandLine cl = parseArgs(args);
if (shouldPrintUsageAndExit(cl)) {
printUsage();
return;
}
String masterPassword = getMasterPassword(cl);
TextEncryptor encryptor = getEncryptor(cl, masterPassword);
if (cl.hasOption(ENCRYPTED_PWD_OPTION)) {
Matcher matcher = ENCRYPTED_PATTERN.matcher(cl.getOptionValue(ENCRYPTED_PWD_OPTION));
if (matcher.find()) {
String encrypted = matcher.group(1);
System.out.println(encryptor.decrypt(encrypted));
} else {
throw new RuntimeException("Input encrypted password does not match pattern \"ENC(...)\"");
}
} else if (cl.hasOption(PLAIN_PWD_OPTION)) {
System.out.println("ENC(" + encryptor.encrypt(cl.getOptionValue(PLAIN_PWD_OPTION)) + ")");
} else {
printUsage();
throw new RuntimeException(String.format("Must provide -%s or -%s option.", PLAIN_PWD_OPTION, ENCRYPTED_PWD_OPTION));
}
}
Aggregations