-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
asadmin to import plain PKCS#8 RSA keypairs into the java keystore (#…
…2599) * Ability to import plain PKCS#8 RSA keypairs into the java keystore using the JAVA api directly (not via keytool). Don't allow empty or short key and/or keystore passwords. Methods to enfore glassfish convention of keys using the same passwords as the keystore. * Support for non-standard master-passwords * javadoc
- Loading branch information
1 parent
877421e
commit b3bd6b0
Showing
9 changed files
with
621 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...s/admin/server-mgmt/src/main/java/fish/payara/admin/servermgmt/cli/AddKeypairCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (c) 2018 Payara Foundation. All rights reserved. | ||
* | ||
* The contents of this file are subject to the terms of the Common Development | ||
* and Distribution License("CDDL") (collectively, the "License"). You | ||
* may not use this file except in compliance with the License. You can | ||
* obtain a copy of the License at | ||
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html | ||
* or packager/legal/LICENSE.txt. See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
* | ||
* When distributing the software, include this License Header Notice in each | ||
* file and include the License file at packager/legal/LICENSE.txt. | ||
*/ | ||
package fish.payara.admin.servermgmt.cli; | ||
|
||
import com.sun.enterprise.admin.servermgmt.DomainException; | ||
import com.sun.enterprise.admin.servermgmt.KeystoreManager; | ||
import com.sun.enterprise.admin.servermgmt.cli.LocalDomainCommand; | ||
import com.sun.enterprise.admin.servermgmt.domain.DomainConstants; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.security.KeyStoreException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.security.cert.Certificate; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.text.MessageFormat; | ||
import java.util.Collection; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import org.glassfish.hk2.api.PerLookup; | ||
import org.jvnet.hk2.annotations.Service; | ||
import org.glassfish.api.Param; | ||
import org.glassfish.api.admin.CommandException; | ||
import org.glassfish.api.admin.CommandValidationException; | ||
|
||
/** | ||
* Adds a new PKCS#8 encoded plain (unencrypted) RSA keypair to the domain's keystore | ||
* | ||
* @author ratcash | ||
*/ | ||
@Service(name = "add-pkcs8") // the name of the service is the asadmin command name | ||
@PerLookup // this means one instance is created every time the command is run | ||
public class AddKeypairCommand extends LocalDomainCommand { | ||
//private static final String DEFAULT_SSL_LISTENER = "http-listener-2"; | ||
|
||
@Param(name = "domain_name", optional = true) | ||
String userArgDomainName; | ||
|
||
@Param(optional = false) | ||
String destAlias; | ||
|
||
@Param(name = "priv-key-path", optional = false) | ||
String pkcs8PrivateKeyPath; | ||
|
||
@Param(name = "cert-chain-path", optional = false) | ||
String certPath; | ||
|
||
//@Inject | ||
KeystoreManager keyManager = new KeystoreManager(); | ||
|
||
private File getKeyStoreFile() throws DomainException, IOException { | ||
|
||
if (getServerDirs() == null) { | ||
return null; | ||
} | ||
|
||
File mp = new File(getServerDirs().getConfigDir(), DomainConstants.KEYSTORE_FILE); | ||
Logger.getLogger(AddKeypairCommand.class.getName()).log(Level.SEVERE, mp.getAbsolutePath()); | ||
if (!mp.canRead()) { | ||
return null; | ||
} | ||
return mp; | ||
} | ||
|
||
@Override | ||
protected void validate() | ||
throws CommandException, CommandValidationException { | ||
setDomainName(userArgDomainName); | ||
super.validate(); | ||
} | ||
|
||
@Override | ||
protected int executeCommand() throws CommandException, CommandValidationException { | ||
|
||
File privKeyFile = new File(pkcs8PrivateKeyPath); | ||
try { | ||
File destKeyStore = getKeyStoreFile(); | ||
try (InputStream privIn = new FileInputStream(privKeyFile)) { | ||
PrivateKey privKey = keyManager.readPlainPKCS8PrivateKey(privIn, "RSA"); | ||
Collection<? extends Certificate> certChain = keyManager.readPemCertificateChain(new File(certPath)); | ||
String mp = getMasterPassword(); | ||
keyManager.addKeyPair(destKeyStore, "JKS", | ||
mp.toCharArray(), privKey, certChain.toArray(new Certificate[1]), destAlias); | ||
logger.fine(() -> MessageFormat.format("Private key with alias [{0}] added to keystore {1}.", | ||
new Object[] {destAlias, destKeyStore.getAbsolutePath()})); | ||
} catch (IOException | NoSuchAlgorithmException | KeyStoreException ex) { | ||
Logger.getLogger(AddKeypairCommand.class.getName()).log(Level.SEVERE, null, ex); | ||
throw new CommandException(ex.getLocalizedMessage()); | ||
} | ||
} catch (DomainException | IOException | InvalidKeySpecException ex) { | ||
throw new CommandException(ex.getLocalizedMessage()); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
Oops, something went wrong.