Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAYARA-2615 Add support for Payara Micro command line option --sslCert #2545

Merged
merged 1 commit into from Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -210,6 +210,10 @@ public boolean getSslAutoBind() {
public int getSslPort() {
return wrappee.getSslPort();
}

public String getSslCert() {
return wrappee.getSslCert();
}

public File getUberJar() {
return wrappee.getUberJar();
Expand Down Expand Up @@ -362,6 +366,11 @@ public PayaraMicro setSslPort(int sslPort) {
wrappee.setSslPort(sslPort);
return this;
}

public PayaraMicro setSslCert(String alias) {
wrappee.setSslCert(alias);
return this;
}

public PayaraMicro setUserLogFile(String fileName) {
wrappee.setUserLogFile(fileName);
Expand Down
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2016 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2016-2018 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -42,7 +42,6 @@
import fish.payara.micro.BootstrapException;
import fish.payara.micro.PayaraMicroRuntime;
import java.io.File;
import java.net.URL;

/**
*
Expand Down Expand Up @@ -227,6 +226,12 @@ public interface PayaraMicroBoot {
* @return The HTTPS port
*/
int getSslPort();

/**
* The name of the SSL certificate to use in the keystore
* @return
*/
String getSslCert();

/**
* The UberJar to create
Expand Down Expand Up @@ -466,6 +471,13 @@ public interface PayaraMicroBoot {
* @return
*/
PayaraMicroBoot setSslPort(int sslPort);

/**
* Sets the name of the certificate to use in the keystore
* @param alias the name of the certificate in the keystore
* @return
*/
PayaraMicroBoot setSslCert(String alias);

/**
* Set user defined file for the Log entries
Expand Down
Expand Up @@ -40,8 +40,8 @@
package fish.payara.micro.cmd.options;

/**
*
* @author steve
* ENUM used for command line switches for Payara Micro
* @author Steve Millidge (Payara Services Limited)
*/
public enum RUNTIME_OPTION {
nocluster(false),
Expand Down Expand Up @@ -100,6 +100,7 @@ public enum RUNTIME_OPTION {
unpackdir(true, new DirectoryValidator(true, true, true)),
clustermode(true,new PrefixStringListValidator("tcpip","domain","multicast")),
interfaces(true),
sslcert(true),
help(false);

private RUNTIME_OPTION(boolean hasValue) {
Expand Down
Expand Up @@ -91,8 +91,6 @@
import fish.payara.nucleus.hazelcast.HazelcastCore;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.logging.Formatter;
Expand Down Expand Up @@ -178,6 +176,7 @@ public class PayaraMicroImpl implements PayaraMicroBoot {
private String clustermode;
private String interfaces;
private String secretsDir;
private String sslCert;
private boolean showServletMappings;

/**
Expand Down Expand Up @@ -432,23 +431,23 @@ public PayaraMicroImpl setHttpPort(int httpPort) {
}

/**
* The configured port for HTTPS requests
* The UberJar to create
*
* @return The HTTPS port
* @return
*/
@Override
public int getSslPort() {
return sslPort;
public File getUberJar() {
return uberJar;
}

/**
* The UberJar to create
* The configured port for HTTPS requests
*
* @return
* @return The HTTPS port
*/
@Override
public File getUberJar() {
return uberJar;
public int getSslPort() {
return sslPort;
}

/**
Expand All @@ -468,6 +467,22 @@ public PayaraMicroImpl setSslPort(int sslPort) {
return this;
}

/**
* Set the certificate alias in the keystore to use for the server cert
* @param alias name of the certificate in the keystore
* @return
*/
@Override
public PayaraMicroImpl setSslCert(String alias) {
sslCert = alias;
return this;
}

@Override
public String getSslCert() {
return sslCert;
}

/**
* Gets the logical name for this PayaraMicro Server within the server
* cluster
Expand Down Expand Up @@ -1112,6 +1127,10 @@ private void scanArgs(String[] args) {
sslPort = Integer.parseInt(value);
break;
}
case sslcert: {
sslCert = value;
break;
}
case version: {
printVersion();
System.exit(1);
Expand Down Expand Up @@ -1422,7 +1441,7 @@ private void configureRequestTracingService() {
}

/**
* Process the user system properties in precendence
* Process the user system properties in precedence
* 1st loads the properties from the uber jar location
* then loads each command line system properties file which will override
* uber jar properties
Expand Down Expand Up @@ -1827,6 +1846,10 @@ private void configurePorts() throws GlassFishException {
throw new GlassFishException("Could not bind SSL port");
}
}

if (sslCert != null) {
preBootCommands.add(new BootCommand("set", "configs.config.server-config.network-config.protocols.protocol.https-listener.ssl.cert-nickname=" + sslCert));
}
}

private void configurePhoneHome() {
Expand Down Expand Up @@ -2101,6 +2124,7 @@ private void setArgumentsFromSystemProperties() {
enableHealthCheck = getBooleanProperty("payaramicro.enableHealthCheck");
httpPort = getIntegerProperty("payaramicro.port", Integer.MIN_VALUE);
sslPort = getIntegerProperty("payaramicro.sslPort", Integer.MIN_VALUE);
sslCert = getProperty("payaramicro.sslCert");
hzMulticastGroup = getProperty("payaramicro.mcAddress");
hzPort = getIntegerProperty("payaramicro.mcPort", Integer.MIN_VALUE);
hostAware = getBooleanProperty("payaramicro.hostAware","true");
Expand Down Expand Up @@ -2258,6 +2282,10 @@ private void packageUberJar() {
if (secretsDir != null) {
props.setProperty("payaramicro.secretsDir", secretsDir);
}

if (sslCert != null) {
props.setProperty("payaramicro.sslCert", sslCert);
}

props.setProperty("payaramicro.autoBindHttp", Boolean.toString(autoBindHttp));
props.setProperty("payaramicro.autoBindSsl", Boolean.toString(autoBindSsl));
Expand Down
Expand Up @@ -216,6 +216,10 @@ public void buildUberJar() {
is = new FileInputStream(postDeployCommands);
}else if (entry.toString().contains("MICRO-INF/domain/domain.xml") && (domainXML != null)) {
is = new FileInputStream(domainXML);
}else if (entry.toString().contains("MICRO-INF/domain/keystore.jks") && (System.getProperty("javax.net.ssl.keyStore") != null)) {
is = new FileInputStream(System.getProperty("javax.net.ssl.keyStore"));
}else if (entry.toString().contains("MICRO-INF/domain/cacerts.jks") && (System.getProperty("javax.net.ssl.trustStore") != null)) {
is = new FileInputStream(System.getProperty("javax.net.ssl.trustStore"));
}

byte[] buffer = new byte[4096];
Expand Down
Expand Up @@ -71,6 +71,20 @@ protected KeyManager[] getKeyManagers(String algorithm, String keyAlias) throws
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "Keystore type= {0}", keystoreType);
}

// validate that the alias is in one of the keystores otherwise emit warning
boolean aliasFound = false;
for (KeyStore keyStore : sslUtils.getKeyStores()) {
if (keyStore.isKeyEntry(keyAlias)) {
aliasFound = true;
break;
}
}

if (!aliasFound) {
logger.log(Level.WARNING, "Unable to find key pair alias {0} in any of the configured key stores, therefore the server may not be able to present a valid SSL Certificate", keyAlias);
}

KeyManager[] kMgrs = sslUtils.getKeyManagers(algorithm);
if (keyAlias != null && keyAlias.length() > 0 && kMgrs != null) {
for (int i = 0; i < kMgrs.length; i++) {
Expand Down
Expand Up @@ -248,6 +248,7 @@ protected synchronized static void loadStores(
keyStorePasswords.add(Arrays.copyOf(keyStorePass, keyStorePass.length));
tokenNames.add(tokenName);
} catch (Exception ex) {
_logger.severe("Failed to load key stores " + ex.getMessage());
throw new IllegalStateException(ex);
}
}
Expand Down