Search in sources :

Example 1 with TextEncryptor

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 + ")");
}
Also used : TextEncryptor(org.jasypt.util.text.TextEncryptor) StrongTextEncryptor(org.jasypt.util.text.StrongTextEncryptor) BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with TextEncryptor

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;
}
Also used : Path(org.apache.hadoop.fs.Path) Closer(com.google.common.io.Closer) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) TextEncryptor(org.jasypt.util.text.TextEncryptor) StrongTextEncryptor(org.jasypt.util.text.StrongTextEncryptor) BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) StrongTextEncryptor(org.jasypt.util.text.StrongTextEncryptor) LineReader(com.google.common.io.LineReader) BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor)

Example 3 with TextEncryptor

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));
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) Matcher(java.util.regex.Matcher) TextEncryptor(org.jasypt.util.text.TextEncryptor) BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor) StrongTextEncryptor(org.jasypt.util.text.StrongTextEncryptor)

Aggregations

BasicTextEncryptor (org.jasypt.util.text.BasicTextEncryptor)3 StrongTextEncryptor (org.jasypt.util.text.StrongTextEncryptor)3 TextEncryptor (org.jasypt.util.text.TextEncryptor)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Closer (com.google.common.io.Closer)1 LineReader (com.google.common.io.LineReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 CommandLine (org.apache.commons.cli.CommandLine)1 Path (org.apache.hadoop.fs.Path)1