Search in sources :

Example 1 with PasswordCipher

use of org.apache.openejb.cipher.PasswordCipher in project tomee by apache.

the class Cipher method availableCiphers.

private static void availableCiphers() {
    try {
        final ResourceFinder finder = new ResourceFinder("META-INF/");
        final Map<String, Class<? extends PasswordCipher>> impls = finder.mapAllImplementations(PasswordCipher.class);
        System.out.println("Available ciphers are: " + Join.join(", ", impls.keySet()));
    } catch (final Exception ignore) {
    // no-op
    }
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) PasswordCipher(org.apache.openejb.cipher.PasswordCipher) PasswordCipherException(org.apache.openejb.cipher.PasswordCipherException) ParseException(org.apache.commons.cli.ParseException) SystemExitException(org.apache.openejb.cli.SystemExitException)

Example 2 with PasswordCipher

use of org.apache.openejb.cipher.PasswordCipher in project tomee by apache.

the class BasicDataSource method createDataSource.

@Override
protected DataSource createDataSource() throws SQLException {
    if (dsRef != null) {
        return dsRef;
    }
    synchronized (this) {
        if (dsRef != null) {
            return dsRef;
        }
        // check password codec if available
        if (null != passwordCipher && !"PlainText".equals(passwordCipher)) {
            final PasswordCipher cipher = PasswordCipherFactory.getPasswordCipher(passwordCipher);
            // always use the initial encrypted value
            final String plainPwd = cipher.decrypt(initialPassword.toCharArray());
            // override previous password value
            super.setPassword(plainPwd);
        }
        // get the plugin
        final DataSourcePlugin helper = BasicDataSourceUtil.getDataSourcePlugin(getUrl());
        // configure this
        if (helper != null) {
            final String currentUrl = getUrl();
            final String newUrl = helper.updatedUrl(currentUrl);
            if (!currentUrl.equals(newUrl)) {
                super.setUrl(newUrl);
            }
        }
        // create the data source
        if (helper == null || !helper.enableUserDirHack()) {
            try {
                super.createDataSource();
            } catch (final Throwable e) {
                throw toSQLException(e);
            }
        } else {
            // wrap super call with code that sets user.dir to openejb.base and then resets it
            final Properties systemProperties = JavaSecurityManagers.getSystemProperties();
            final String userDir = systemProperties.getProperty("user.dir");
            try {
                final File base = SystemInstance.get().getBase().getDirectory();
                systemProperties.setProperty("user.dir", base.getAbsolutePath());
                try {
                    super.createDataSource();
                } catch (final Throwable e) {
                    throw toSQLException(e);
                }
            } finally {
                systemProperties.setProperty("user.dir", userDir);
            }
        }
        return dsRef = DataSource.class.cast(Reflections.get(this, "dataSource"));
    }
}
Also used : DataSourcePlugin(org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin) PasswordCipher(org.apache.openejb.cipher.PasswordCipher) Properties(java.util.Properties) File(java.io.File) XADataSource(javax.sql.XADataSource) CommonDataSource(javax.sql.CommonDataSource) DataSource(javax.sql.DataSource)

Example 3 with PasswordCipher

use of org.apache.openejb.cipher.PasswordCipher in project tomee by apache.

the class PasswordCodecTest method testStaticDesCodec.

public void testStaticDesCodec() {
    final PasswordCipher cipher = new StaticDESPasswordCipher();
    final char[] tmp = cipher.encrypt(PLAIN_PWD);
    assertEquals(PLAIN_PWD, cipher.decrypt(tmp));
}
Also used : StaticDESPasswordCipher(org.apache.openejb.cipher.StaticDESPasswordCipher) PlainTextPasswordCipher(org.apache.openejb.cipher.PlainTextPasswordCipher) PasswordCipher(org.apache.openejb.cipher.PasswordCipher) StaticDESPasswordCipher(org.apache.openejb.cipher.StaticDESPasswordCipher)

Example 4 with PasswordCipher

use of org.apache.openejb.cipher.PasswordCipher in project tomee by apache.

the class Cipher method main.

public static void main(final String[] args) throws SystemExitException {
    final CommandLineParser parser = new PosixParser();
    // create the Options
    final Options options = new Options();
    options.addOption(option("h", "help", "cmd.cipher.opt.help"));
    options.addOption(option("c", "cipher", "c", "cmd.cipher.opt.impl"));
    options.addOption(option("d", "decrypt", "cmd.cipher.opt.decrypt"));
    options.addOption(option("e", "encrypt", "cmd.cipher.opt.encrypt"));
    final CommandLine line;
    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (final ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }
    if (line.hasOption("help")) {
        help(options);
        return;
    }
    String cipherName = "Static3DES";
    if (line.hasOption("cipher")) {
        cipherName = line.getOptionValue("cipher");
    }
    if (line.getArgList().size() != 1) {
        System.out.println("Must specify either a plain text to encrypt or a ciphered value to decrypt.");
        help(options);
        return;
    }
    final PasswordCipher cipher;
    try {
        cipher = PasswordCipherFactory.getPasswordCipher(cipherName);
    } catch (final PasswordCipherException e) {
        System.out.println("Could not load password cipher implementation class. Check your classpath.");
        availableCiphers();
        throw new SystemExitException(-1);
    }
    if (line.hasOption("decrypt")) {
        final String pwdArg = (String) line.getArgList().get(0);
        final char[] encryptdPassword = pwdArg.toCharArray();
        System.out.println(cipher.decrypt(encryptdPassword));
    } else {
        // if option neither encrypt/decrypt is specified, we assume
        // it is encrypt.
        final String plainPassword = (String) line.getArgList().get(0);
        System.out.println(new String(cipher.encrypt(plainPassword)));
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PasswordCipher(org.apache.openejb.cipher.PasswordCipher) PasswordCipherException(org.apache.openejb.cipher.PasswordCipherException) PosixParser(org.apache.commons.cli.PosixParser) SystemExitException(org.apache.openejb.cli.SystemExitException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 5 with PasswordCipher

use of org.apache.openejb.cipher.PasswordCipher in project tomee by apache.

the class PasswordCodecTest method testPlainCodec.

public void testPlainCodec() {
    final PasswordCipher cipher = new PlainTextPasswordCipher();
    assertEquals(PLAIN_PWD, new String(cipher.encrypt(PLAIN_PWD)));
    assertEquals(PLAIN_PWD, cipher.decrypt(PLAIN_PWD.toCharArray()));
}
Also used : PlainTextPasswordCipher(org.apache.openejb.cipher.PlainTextPasswordCipher) PlainTextPasswordCipher(org.apache.openejb.cipher.PlainTextPasswordCipher) PasswordCipher(org.apache.openejb.cipher.PasswordCipher) StaticDESPasswordCipher(org.apache.openejb.cipher.StaticDESPasswordCipher)

Aggregations

PasswordCipher (org.apache.openejb.cipher.PasswordCipher)8 PasswordCipherException (org.apache.openejb.cipher.PasswordCipherException)3 PlainTextPasswordCipher (org.apache.openejb.cipher.PlainTextPasswordCipher)3 StaticDESPasswordCipher (org.apache.openejb.cipher.StaticDESPasswordCipher)3 File (java.io.File)2 Properties (java.util.Properties)2 ParseException (org.apache.commons.cli.ParseException)2 SystemExitException (org.apache.openejb.cli.SystemExitException)2 DataSourcePlugin (org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 CommonDataSource (javax.sql.CommonDataSource)1 DataSource (javax.sql.DataSource)1 XADataSource (javax.sql.XADataSource)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 Options (org.apache.commons.cli.Options)1 PosixParser (org.apache.commons.cli.PosixParser)1 SafePasswordCipher (org.apache.openejb.cipher.SafePasswordCipher)1 ResourceFinder (org.apache.xbean.finder.ResourceFinder)1