Search in sources :

Example 1 with StrongTextEncryptor

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
    }
}
Also used : StrongTextEncryptor(org.jasypt.util.text.StrongTextEncryptor) State(org.apache.gobblin.configuration.State) EncryptionOperationNotPossibleException(org.jasypt.exceptions.EncryptionOperationNotPossibleException) File(java.io.File) Test(org.testng.annotations.Test)

Example 2 with StrongTextEncryptor

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;
    }
}
Also used : StrongTextEncryptor(org.jasypt.util.text.StrongTextEncryptor) BasicTextEncryptor(org.jasypt.util.text.BasicTextEncryptor)

Example 3 with StrongTextEncryptor

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;
}
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)

Aggregations

StrongTextEncryptor (org.jasypt.util.text.StrongTextEncryptor)3 BasicTextEncryptor (org.jasypt.util.text.BasicTextEncryptor)2 Closer (com.google.common.io.Closer)1 LineReader (com.google.common.io.LineReader)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 State (org.apache.gobblin.configuration.State)1 Path (org.apache.hadoop.fs.Path)1 EncryptionOperationNotPossibleException (org.jasypt.exceptions.EncryptionOperationNotPossibleException)1 TextEncryptor (org.jasypt.util.text.TextEncryptor)1 Test (org.testng.annotations.Test)1