Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 87 additions & 45 deletions server/src/main/java/com/arcadedb/server/http/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import com.arcadedb.server.http.handler.PostQueryHandler;
import com.arcadedb.server.http.handler.PostRollbackHandler;
import com.arcadedb.server.http.handler.PostServerCommandHandler;
import com.arcadedb.server.http.ssl.SslUtils;
import com.arcadedb.server.http.ssl.TlsProtocol;
import com.arcadedb.server.http.ws.WebSocketConnectionHandler;
import com.arcadedb.server.http.ws.WebSocketEventBus;
import com.arcadedb.server.security.ServerSecurityException;
Expand All @@ -66,17 +68,23 @@
import java.security.cert.*;
import java.util.logging.*;

import static com.arcadedb.GlobalConfiguration.NETWORK_SSL_KEYSTORE;
import static com.arcadedb.GlobalConfiguration.NETWORK_SSL_KEYSTORE_PASSWORD;
import static com.arcadedb.GlobalConfiguration.NETWORK_SSL_TRUSTSTORE;
import static com.arcadedb.GlobalConfiguration.NETWORK_SSL_TRUSTSTORE_PASSWORD;
import static com.arcadedb.server.http.ssl.KeystoreType.JKS;
import static com.arcadedb.server.http.ssl.KeystoreType.PKCS12;
import static io.undertow.UndertowOptions.SHUTDOWN_TIMEOUT;

public class HttpServer implements ServerPlugin {
private final ArcadeDBServer server;
private final ArcadeDBServer server;
private final HttpSessionManager sessionManager;
private final JsonSerializer jsonSerializer = new JsonSerializer();
private final WebSocketEventBus webSocketEventBus;
private Undertow undertow;
private String listeningAddress;
private String host;
private int httpPortListening;
private final JsonSerializer jsonSerializer = new JsonSerializer();
private final WebSocketEventBus webSocketEventBus;
private Undertow undertow;
private String listeningAddress;
private String host;
private int httpPortListening;

public HttpServer(final ArcadeDBServer server) {
this.server = server;
Expand All @@ -88,12 +96,13 @@ public HttpServer(final ArcadeDBServer server) {
public void stopService() {
webSocketEventBus.stop();

if (undertow != null)
if (undertow != null) {
try {
undertow.stop();
} catch (final Exception e) {
// IGNORE IT
}
}

sessionManager.close();
}
Expand Down Expand Up @@ -147,8 +156,9 @@ public void startService() {
}

// REGISTER PLUGIN API
for (final ServerPlugin plugin : server.getPlugins())
for (final ServerPlugin plugin : server.getPlugins()) {
plugin.registerAPI(this, routes);
}

int httpsPortListening = httpsPortRange != null ? httpsPortRange[0] : 0;
for (httpPortListening = httpPortRange[0]; httpPortListening <= httpPortRange[1]; ++httpPortListening) {
Expand Down Expand Up @@ -178,8 +188,9 @@ public void startService() {
if (e.getCause() instanceof BindException) {
// RETRY
LogManager.instance().log(this, Level.WARNING, "- HTTP Port %s not available", httpPortListening);
if (httpsPortListening > 0)
if (httpsPortListening > 0) {
++httpsPortListening;
}

continue;
}
Expand All @@ -202,21 +213,21 @@ private int[] extractPortRange(final Object configuredPort) {
int portFrom;
int portTo;

if (configuredPort instanceof Number)
if (configuredPort instanceof Number) {
portFrom = portTo = ((Number) configuredPort).intValue();
else {
} else {
final String[] parts = configuredPort.toString().split("-");
if (parts.length > 2)
if (parts.length > 2) {
throw new IllegalArgumentException("Invalid format for http server port range");
else if (parts.length == 1)
} else if (parts.length == 1) {
portFrom = portTo = Integer.parseInt(parts[0]);
else {
} else {
portFrom = Integer.parseInt(parts[0]);
portTo = Integer.parseInt(parts[1]);
}
}

return new int[] { portFrom, portTo };
return new int[]{portFrom, portTo};
}

public HttpSessionManager getSessionManager() {
Expand Down Expand Up @@ -244,47 +255,78 @@ public WebSocketEventBus getWebSocketEventBus() {
}

private SSLContext createSSLContext() throws Exception {
final ContextConfiguration configuration = server.getConfiguration();
final String keyStorePath = configuration.getValueAsString(GlobalConfiguration.NETWORK_SSL_KEYSTORE);
if (keyStorePath == null || keyStorePath.isEmpty())
throw new ServerSecurityException("SSL key store path is empty");
ContextConfiguration configuration = server.getConfiguration();

String keystorePath = validateStoreProperty(configuration,
NETWORK_SSL_KEYSTORE,
"SSL key store path is empty"
);
String keystorePassword = validateStoreProperty(configuration,
NETWORK_SSL_KEYSTORE_PASSWORD,
"SSL key store password is empty"
);

final String trustStorePath = configuration.getValueAsString(GlobalConfiguration.NETWORK_SSL_TRUSTSTORE);
if (trustStorePath == null || trustStorePath.isEmpty())
throw new ServerSecurityException("SSL trust store path is empty");
String truststorePath = validateStoreProperty(configuration,
NETWORK_SSL_TRUSTSTORE,
"SSL trust store path is empty"
);
String truststorePassword = validateStoreProperty(configuration,
NETWORK_SSL_TRUSTSTORE_PASSWORD,
"SSL trust store password is empty"
);

final String trustStorePassword = configuration.getValueAsString(GlobalConfiguration.NETWORK_SSL_TRUSTSTORE_PASSWORD);
if (trustStorePassword == null || trustStorePassword.isEmpty())
throw new ServerSecurityException("SSL trust store password is empty");
KeyStore keyStore = configureSSLForKeystore(keystorePath,
keystorePassword);

final KeyStore keyStore = configureSSL(keyStorePath, trustStorePassword);
final KeyStore trustStore = configureSSL(trustStorePath, trustStorePassword);
KeyStore trustStore = configureSSLForTruststore(truststorePath,
truststorePassword);

final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, trustStorePassword.toCharArray());
final KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore,
keystorePassword.toCharArray());
KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();

final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
final TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

final SSLContext sslContext;
sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, null);
SSLContext sslContext = SSLContext.getInstance(TlsProtocol.getLatestTlsVersion().getTlsVersion());
sslContext.init(keyManagers,
trustManagers,
null
);

return sslContext;
}

private KeyStore configureSSL(final String path, final String password)
private KeyStore configureSSLForKeystore(String keystorePath,
String keystorePassword)
throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException {
final InputStream stream = SocketFactory.getAsStream(path);
if (stream == null)
throw new RuntimeException("Could not load keystore");

try (InputStream is = stream) {
KeyStore loadedKeystore = KeyStore.getInstance("JKS");
loadedKeystore.load(is, password.toCharArray());
return loadedKeystore;

return SslUtils.loadKeystoreFromStream(SocketFactory.getAsStream(keystorePath),
keystorePassword,
SslUtils.getDefaultKeystoreTypeForKeystore(() -> PKCS12)
);
}

private KeyStore configureSSLForTruststore(String truststorePath,
String truststorePassword)
throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException {

return SslUtils.loadKeystoreFromStream(SocketFactory.getAsStream(truststorePath),
truststorePassword,
SslUtils.getDefaultKeystoreTypeForTruststore(() -> JKS)
);
}

private String validateStoreProperty(ContextConfiguration contextConfiguration,
GlobalConfiguration configurationKey,
String errorMessage) {
String storePropertyValue = contextConfiguration.getValueAsString(configurationKey);
if ((storePropertyValue == null) || storePropertyValue.isEmpty()) {
throw new ServerSecurityException(errorMessage);
}
return storePropertyValue;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.arcadedb.server.http.ssl;

import static java.util.Arrays.stream;

public enum KeystoreType {

PKCS12("PKCS12"),
JKS("JKS");

private final String keystoreType;

KeystoreType(String keystoreType) {
this.keystoreType = keystoreType;
}

public String getKeystoreType() {
return keystoreType;
}

public static KeystoreType validateFromString(final String keystoreType) {
return stream(KeystoreType.values())
.filter(enumValue -> enumValue.getKeystoreType().equalsIgnoreCase(keystoreType))
.findFirst()
.orElseThrow(() -> new RuntimeException("Specified key store type is not valid"));
}

public static KeystoreType getFromStringWithDefault(final String keystoreType,
final KeystoreType defaultKeystoreType) {
return stream(KeystoreType.values())
.filter(enumValue -> enumValue.getKeystoreType().equalsIgnoreCase(keystoreType))
.findFirst()
.orElse(defaultKeystoreType);
}

}
77 changes: 77 additions & 0 deletions server/src/main/java/com/arcadedb/server/http/ssl/SslUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.arcadedb.server.http.ssl;

import com.arcadedb.server.security.ServerSecurityException;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.function.Supplier;

public class SslUtils {

private static final String JAVAX_NET_SSL_KEYSTORE_TYPE = "javax.net.ssl.keyStoreType";
private static final String JAVAX_NET_SSL_TRUSTSTORE_TYPE = "javax.net.ssl.trustStoreType";

private SslUtils() {
}

public static KeyStore loadKeystoreFromStream(InputStream keystoreInputStream,
String keystorePassword,
String keystoreType)
throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException {

try (InputStream validatedInputStream = doValidateKeystoreStream(keystoreInputStream)) {
KeyStore keystore = KeyStore.getInstance(keystoreType);
keystore.load(validatedInputStream,
keystorePassword.toCharArray());
return keystore;
}

}

public static String getDefaultKeystoreTypeForKeystore(Supplier<KeystoreType> defaultSupplier) {
return doGetDefaultKeystoreType(JAVAX_NET_SSL_KEYSTORE_TYPE,
defaultSupplier);
}

public static String getDefaultKeystoreTypeForTruststore(Supplier<KeystoreType> defaultSupplier) {
return doGetDefaultKeystoreType(JAVAX_NET_SSL_TRUSTSTORE_TYPE,
defaultSupplier);
}

private static String doGetDefaultKeystoreType(String propertyName,
Supplier<KeystoreType> defaultSupplier) {

String keystoreTypeValue = System.getProperty(propertyName);
// Keystore type is not defined take value from 'defaultSupplier'
if ((keystoreTypeValue == null) || keystoreTypeValue.isEmpty()) {
return doValidateDefaultKeystoreSupplier(defaultSupplier)
.get()
.getKeystoreType();
}
// Try to match with supported keystore types and return the first that matches
// If no match return the value from 'defaultSupplier'
return KeystoreType.getFromStringWithDefault(keystoreTypeValue,
doValidateDefaultKeystoreSupplier(defaultSupplier).get())
.getKeystoreType();

}

private static Supplier<KeystoreType> doValidateDefaultKeystoreSupplier(Supplier<KeystoreType> defaultSupplier) {
if ((defaultSupplier == null) || (defaultSupplier.get() == null)) {
throw new ServerSecurityException("Default key store supplier is not configured correctly");
}
return defaultSupplier;
}

private static InputStream doValidateKeystoreStream(InputStream keystoreInputStream) {
if (keystoreInputStream == null) {
throw new ServerSecurityException("Key store stream cannot be null");
}
return keystoreInputStream;
}

}
25 changes: 25 additions & 0 deletions server/src/main/java/com/arcadedb/server/http/ssl/TlsProtocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.arcadedb.server.http.ssl;

public enum TlsProtocol {

TLS("TLS"), // Supports some version of TLS
TLS_1("TLSv1"), // Supports RFC 2246: TLS version 1.0
TLS_1_1("TLSv1.1"), // Supports RFC 4346: TLS version 1.1
TLS_1_2("TLSv1.2"), // Supports RFC 5246: TLS version 1.2
TLS_1_3("TLSv1.3"); // Supports RFC 8446: TLS version 1.3

private final String tlsVersion;

TlsProtocol(String tlsVersion) {
this.tlsVersion = tlsVersion;
}

public String getTlsVersion() {
return tlsVersion;
}

public static TlsProtocol getLatestTlsVersion() {
return TLS_1_3;
}

}
12 changes: 6 additions & 6 deletions server/src/test/java/com/arcadedb/server/HTTPSSLIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public HTTPSSLIT() {
@Override
protected void onServerConfiguration(final ContextConfiguration configuration) {
configuration.setValue(GlobalConfiguration.NETWORK_USE_SSL, true);
configuration.setValue(GlobalConfiguration.NETWORK_SSL_KEYSTORE, "src/test/resources/master.jks");
configuration.setValue(GlobalConfiguration.NETWORK_SSL_KEYSTORE_PASSWORD, "keypassword");
configuration.setValue(GlobalConfiguration.NETWORK_SSL_TRUSTSTORE, "src/test/resources/master.jks");
configuration.setValue(GlobalConfiguration.NETWORK_SSL_TRUSTSTORE_PASSWORD, "storepassword");
configuration.setValue(GlobalConfiguration.NETWORK_SSL_KEYSTORE, "src/test/resources/keystore.pkcs12");
configuration.setValue(GlobalConfiguration.NETWORK_SSL_KEYSTORE_PASSWORD, "sos0nmzWniR0");
configuration.setValue(GlobalConfiguration.NETWORK_SSL_TRUSTSTORE, "src/test/resources/truststore.jks");
configuration.setValue(GlobalConfiguration.NETWORK_SSL_TRUSTSTORE_PASSWORD, "nphgDK7ugjGR");
}

@Override
Expand All @@ -56,8 +56,8 @@ public void testServerInfo() throws Exception {
testEachServer((serverIndex) -> {
final ContextConfiguration configuration = getServer(serverIndex).getConfiguration();

System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
System.setProperty("javax.net.ssl.trustStoreType", "jks");
System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");
System.setProperty("javax.net.ssl.trustStoreType", "JKS");
System.setProperty("javax.net.ssl.keyStore", configuration.getValueAsString(GlobalConfiguration.NETWORK_SSL_KEYSTORE));
System.setProperty("javax.net.ssl.trustStore", configuration.getValueAsString(GlobalConfiguration.NETWORK_SSL_TRUSTSTORE));
System.setProperty("javax.net.ssl.keyStorePassword", configuration.getValueAsString(GlobalConfiguration.NETWORK_SSL_KEYSTORE_PASSWORD));
Expand Down
Binary file added server/src/test/resources/keystore.pkcs12
Binary file not shown.
Binary file added server/src/test/resources/truststore.jks
Binary file not shown.