Search in sources :

Example 1 with PEFileLayout

use of com.sun.enterprise.admin.servermgmt.pe.PEFileLayout in project Payara by payara.

the class KeystoreManager method changeS1ASAliasPassword.

/**
 * Changes the key password for the default cert whose alias is s1as. The assumption here is that the keystore password
 * is not the same as the key password. This is due to the fact that the keystore password should first be changed
 * followed next by the key password. The end result is that the keystore and s1as key both have the same passwords.
 * This function will tolerate deletion of the s1as alias, but it will not tolerate changing the s1as key from something
 * other than the database password.
 *
 * @param config
 * @param storePassword the keystore password
 * @param oldKeyPassword the old password for the s1as alias
 * @param newKeyPassword the new password for the s1as alias
 * @throws RepositoryException
 */
protected void changeS1ASAliasPassword(RepositoryConfig config, String storePassword, String oldKeyPassword, String newKeyPassword) throws RepositoryException {
    if (!storePassword.equals(oldKeyPassword) && !oldKeyPassword.equals(newKeyPassword)) {
        final PEFileLayout layout = getFileLayout(config);
        final File keystore = layout.getKeyStore();
        // First see if the alias exists. The user could have deleted it. Any failure in the
        // command indicates that the alias does not exist, so we return without error.
        String keyStoreType = System.getProperty("javax.net.ssl.keyStoreType");
        if (keyStoreType == null) {
            keyStoreType = KeyStore.getDefaultType();
        }
        // add code to change all the aliases that exist rather then change s1as only
        List<String> aliases = new ArrayList<>();
        FileInputStream is = null;
        try {
            KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            is = new FileInputStream(keystore);
            keyStore.load(is, storePassword.toCharArray());
            Enumeration<String> all = keyStore.aliases();
            while (all.hasMoreElements()) {
                aliases.add(all.nextElement());
            }
        } catch (Exception e) {
            aliases.add(CERTIFICATE_ALIAS);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ex) {
                    getLogger().log(Level.SEVERE, UNHANDLED_EXCEPTION, ex);
                }
            }
        }
        String[] keytoolCmd = { "-list", "-keystore", keystore.getAbsolutePath(), "-alias", CERTIFICATE_ALIAS };
        KeytoolExecutor p = new KeytoolExecutor(keytoolCmd, 30, new String[] { storePassword });
        try {
            p.execute("s1asKeyPasswordNotChanged", keystore);
        } catch (RepositoryException ex) {
            return;
        }
        // change truststore password from the default
        for (String alias : aliases) {
            keytoolCmd = new String[] { "-keypasswd", "-keystore", keystore.getAbsolutePath(), "-alias", alias };
            p = new KeytoolExecutor(keytoolCmd, 30, new String[] { storePassword, oldKeyPassword, newKeyPassword, newKeyPassword });
            p.execute("s1asKeyPasswordNotChanged", keystore);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyStoreException(java.security.KeyStoreException) CertificateExpiredException(java.security.cert.CertificateExpiredException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) ExecException(com.sun.enterprise.util.ExecException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) PEFileLayout(com.sun.enterprise.admin.servermgmt.pe.PEFileLayout) SmartFile(com.sun.enterprise.universal.io.SmartFile) File(java.io.File)

Example 2 with PEFileLayout

use of com.sun.enterprise.admin.servermgmt.pe.PEFileLayout in project Payara by payara.

the class RepositoryManager method getClearPasswordForAlias.

/**
 * retrieve clear password from password alias keystore
 *
 * @param config the {@link RepositoryConfig} which has the alias keystore
 * @param password the master password
 * @param alias for which the clear text password would returns
 * @return the cleartext password
 * @throws RepositoryException
 */
public String getClearPasswordForAlias(RepositoryConfig config, String password, String alias) throws RepositoryException {
    final PEFileLayout layout = getFileLayout(config);
    final File passwordAliases = layout.getPasswordAliasKeystore();
    try {
        PasswordAdapter p = new PasswordAdapter(passwordAliases.getAbsolutePath(), password.toCharArray());
        String clearPwd = p.getPasswordForAlias(alias);
        return clearPwd;
    } catch (Exception ex) {
        return null;
    }
}
Also used : PEFileLayout(com.sun.enterprise.admin.servermgmt.pe.PEFileLayout) PasswordAdapter(com.sun.enterprise.security.store.PasswordAdapter) File(java.io.File) ZipFile(com.sun.enterprise.util.zip.ZipFile) IOException(java.io.IOException)

Example 3 with PEFileLayout

use of com.sun.enterprise.admin.servermgmt.pe.PEFileLayout in project Payara by payara.

the class RepositoryManager method setPermissions.

/**
 * Sets the permissions for the domain directory, its config directory, startserv/stopserv scripts etc.
 * @param repositoryConfig the {@link RepositoryConfig} to set permissions for
 * @throws RepositoryException if unable to set permissions
 */
protected void setPermissions(RepositoryConfig repositoryConfig) throws RepositoryException {
    final PEFileLayout layout = getFileLayout(repositoryConfig);
    final File domainDir = layout.getRepositoryDir();
    try {
        chmod("-R 755", domainDir);
    } catch (Exception e) {
        throw new RepositoryException(STRING_MANAGER.getString("setPermissionError"), e);
    }
}
Also used : PEFileLayout(com.sun.enterprise.admin.servermgmt.pe.PEFileLayout) File(java.io.File) ZipFile(com.sun.enterprise.util.zip.ZipFile) IOException(java.io.IOException)

Example 4 with PEFileLayout

use of com.sun.enterprise.admin.servermgmt.pe.PEFileLayout in project Payara by payara.

the class RepositoryManager method changePasswordAliasKeystorePassword.

/**
 * Change the password protecting the password alias keystore
 *
 * @param config the config to find the keystore location from
 * @param oldPassword old password
 * @param newPassword new password
 * @throws RepositoryException
 */
protected void changePasswordAliasKeystorePassword(RepositoryConfig config, String oldPassword, String newPassword) throws RepositoryException {
    final PEFileLayout layout = getFileLayout(config);
    final File passwordAliases = layout.getPasswordAliasKeystore();
    // Change the password of the keystore alias file
    if (passwordAliases.exists()) {
        try {
            PasswordAdapter p = new PasswordAdapter(passwordAliases.getAbsolutePath(), oldPassword.toCharArray());
            p.changePassword(newPassword.toCharArray());
        } catch (Exception ex) {
            throw new RepositoryException(STRING_MANAGER.getString("passwordAliasPasswordNotChanged", passwordAliases), ex);
        }
    }
}
Also used : PEFileLayout(com.sun.enterprise.admin.servermgmt.pe.PEFileLayout) PasswordAdapter(com.sun.enterprise.security.store.PasswordAdapter) File(java.io.File) ZipFile(com.sun.enterprise.util.zip.ZipFile) IOException(java.io.IOException)

Example 5 with PEFileLayout

use of com.sun.enterprise.admin.servermgmt.pe.PEFileLayout in project Payara by payara.

the class RepositoryManager method createTimerWal.

/**
 * Create the timer database wal file.
 * @param config the {@link RepositoryConfig} to get the file locations from
 * @throws RepositoryException if an error occured creating the file
 */
protected void createTimerWal(RepositoryConfig config) throws RepositoryException {
    final PEFileLayout layout = getFileLayout(config);
    final File src = layout.getTimerWalTemplate();
    final File dest = layout.getTimerWal();
    try {
        FileUtils.copy(src, dest);
    } catch (IOException ioe) {
        throw new RepositoryException(STRING_MANAGER.getString("timerWalNotCreated"), ioe);
    }
}
Also used : PEFileLayout(com.sun.enterprise.admin.servermgmt.pe.PEFileLayout) IOException(java.io.IOException) File(java.io.File) ZipFile(com.sun.enterprise.util.zip.ZipFile)

Aggregations

PEFileLayout (com.sun.enterprise.admin.servermgmt.pe.PEFileLayout)14 File (java.io.File)14 IOException (java.io.IOException)11 ZipFile (com.sun.enterprise.util.zip.ZipFile)8 PasswordAdapter (com.sun.enterprise.security.store.PasswordAdapter)5 SmartFile (com.sun.enterprise.universal.io.SmartFile)2 ExecException (com.sun.enterprise.util.ExecException)2 FileNotFoundException (java.io.FileNotFoundException)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 UnrecoverableKeyException (java.security.UnrecoverableKeyException)2 CertificateException (java.security.cert.CertificateException)2 CertificateExpiredException (java.security.cert.CertificateExpiredException)2 CertificateNotYetValidException (java.security.cert.CertificateNotYetValidException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 ArrayList (java.util.ArrayList)2 RepositoryConfig (com.sun.enterprise.admin.servermgmt.RepositoryConfig)1 TokenValue (com.sun.enterprise.admin.util.TokenValue)1 TokenValueSet (com.sun.enterprise.admin.util.TokenValueSet)1 ProcessExecutor (com.sun.enterprise.util.ProcessExecutor)1