Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mwangggg committed Nov 27, 2023
1 parent 2c47482 commit f830ff5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 41 deletions.
96 changes: 55 additions & 41 deletions src/main/java/io/cryostat/agent/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
Expand Down Expand Up @@ -108,7 +110,7 @@ class WebServer {
this.compressionFilter = new CompressionFilter();
}

void start() throws IOException, NoSuchAlgorithmException {
void start() {
if (this.https != null) {
stop();
}
Expand All @@ -119,63 +121,75 @@ void start() throws IOException, NoSuchAlgorithmException {
SSLContext sslContext = SSLContext.getInstance("TLS");

// initialize keystore
FileInputStream passwordFis = new FileInputStream("keystore.pass");
char[] password = new String(passwordFis.readAllBytes()).toCharArray();
passwordFis.close();
String content =
new String(
Files.readAllBytes((Paths.get("keystore.pass"))),
StandardCharsets.UTF_8);
char[] password = content.toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
FileInputStream keystoreFis = new FileInputStream("cryostat-keystore.p12");
FileInputStream keystoreFis = new FileInputStream("../certs/cryostat-keystore.p12");
ks.load(keystoreFis, password);
keystoreFis.close();

// set up certificate factory
FileInputStream certFis = new FileInputStream("server.cer");
FileInputStream certFis = new FileInputStream("../certs/server.cer");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(certFis);
ks.setCertificateEntry("serverCert", cert);
certFis.close();

// set up key manager factory
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyManagerFactory kmf =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, password);

// set up trust manager factory
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
TrustManagerFactory tmf =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);

// set up HTTPS context
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
this.https.setHttpsConfigurator(new HttpsConfigurator(sslContext) {
public void configure(HttpsParameters params) {
try {
SSLContext context = getSSLContext();
SSLEngine engine = context.createSSLEngine();
params.setNeedClientAuth(false);
params.setCipherSuites(engine.getEnabledCipherSuites());
params.setProtocols((engine.getEnabledProtocols()));
params.setSSLParameters(context.getDefaultSSLParameters());
} catch (Exception e) {
log.error("Failed to configure the HTTPS parameters", e);
}
}
});
} catch (KeyStoreException
| CertificateException
| UnrecoverableKeyException
| KeyManagementException e) {
this.https.setHttpsConfigurator(
new HttpsConfigurator(sslContext) {
public void configure(HttpsParameters params) {
try {
SSLContext context = getSSLContext();
SSLEngine engine = context.createSSLEngine();
params.setNeedClientAuth(false);
params.setCipherSuites(engine.getEnabledCipherSuites());
params.setProtocols((engine.getEnabledProtocols()));
params.setSSLParameters(context.getDefaultSSLParameters());
} catch (Exception e) {
log.error(
"Failed to configure the HTTPS context and parameters", e);
}
}
});

Set<RemoteContext> mergedContexts = new HashSet<>(remoteContexts.get());
mergedContexts.add(new PingContext(registration));
mergedContexts.stream()
.filter(RemoteContext::available)
.forEach(
rc -> {
HttpContext ctx =
this.https.createContext(rc.path(), wrap(rc::handle));
ctx.setAuthenticator(agentAuthenticator);
ctx.getFilters().add(requestLoggingFilter);
ctx.getFilters().add(compressionFilter);
});
this.https.setExecutor(executor);
this.https.start();

} catch (KeyStoreException
| CertificateException
| UnrecoverableKeyException
| KeyManagementException
| IOException
| NoSuchAlgorithmException e) {
log.error("Failed to set up HTTPS server", e);
}

Set<RemoteContext> mergedContexts = new HashSet<>(remoteContexts.get());
mergedContexts.add(new PingContext(registration));
mergedContexts.stream()
.filter(RemoteContext::available)
.forEach(
rc -> {
HttpContext ctx = this.https.createContext(rc.path(), wrap(rc::handle));
ctx.setAuthenticator(agentAuthenticator);
ctx.getFilters().add(requestLoggingFilter);
ctx.getFilters().add(compressionFilter);
});
this.https.setExecutor(executor);
this.https.start();
}

Path discoverCertPath() {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit f830ff5

Please sign in to comment.