Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

SWARM-1103 - Support embedded HTTPS certificate #364

Merged
merged 1 commit into from Feb 21, 2017
Merged
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
@@ -1,28 +1,36 @@
package org.wildfly.swarm.undertow.runtime;

import static org.wildfly.swarm.spi.api.Defaultable.bool;
import static org.wildfly.swarm.spi.api.Defaultable.string;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Singleton;

import org.jboss.modules.Module;
import org.jboss.modules.ModuleIdentifier;
import org.wildfly.swarm.bootstrap.util.TempFileManager;
import org.wildfly.swarm.spi.api.Defaultable;
import org.wildfly.swarm.spi.api.annotations.Configurable;
import org.wildfly.swarm.undertow.UndertowFraction;
import org.wildfly.swarm.undertow.descriptors.CertInfo;

import static org.wildfly.swarm.spi.api.Defaultable.bool;
import static org.wildfly.swarm.spi.api.Defaultable.string;

/**
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
@ApplicationScoped
public class CertInfoProducer {

public static final String JBOSS_DATA_DIR = "jboss.server.data.dir";

@Inject
UndertowFraction undertow;

Expand All @@ -32,28 +40,47 @@ public class CertInfoProducer {
@Configurable("swarm.https.certificate.generate.host")
Defaultable<String> selfCertificateHost = string("localhost");

@Configurable("swarm.https.keystore.embedded")
Defaultable<Boolean> embeddedKeystore = bool(false);

@Produces
@Singleton
public CertInfo produceCertInfo() {
if (generateSelfCertificate.get()) {
// Remove when SWARM-634 is fixed
if (System.getProperty("jboss.server.data.dir") == null) {
File tmpDir = null;
checkDataDir();
return new CertInfo(selfCertificateHost.get(), JBOSS_DATA_DIR);
} else {
String keystorePath = undertow.keystorePath();
if (embeddedKeystore.get()) {
checkDataDir();
Path dataDir = Paths.get(System.getProperty(JBOSS_DATA_DIR));
Path certDestination = dataDir.resolve(keystorePath);
try {
tmpDir = TempFileManager.INSTANCE.newTempDirectory("wildfly-swarm-data", ".d");
System.setProperty("jboss.server.data.dir", tmpDir.getAbsolutePath());
} catch (IOException e) {
// Ignore
Module appModule = Module.getCallerModuleLoader().loadModule(ModuleIdentifier.create("swarm.application"));
URL jks = appModule.getClassLoader().getResource(keystorePath);
Files.copy(jks.openStream(), certDestination);
keystorePath = certDestination.toString();
} catch (Exception ie) {
throw new RuntimeException("Error copying embedded certificate", ie);
}
}
return new CertInfo(selfCertificateHost.get(), "jboss.server.data.dir");

} else {
String keystorePath = undertow.keystorePath();
String keystorePassword = undertow.keystorePassword();
String keyPassword = undertow.keyPassword();
String keystoreAlias = undertow.alias();
return new CertInfo(keystorePath, keystorePassword, keyPassword, keystoreAlias);
}
}

protected void checkDataDir() {
// Remove when SWARM-634 is fixed
if (System.getProperty(JBOSS_DATA_DIR) == null) {
File tmpDir = null;
try {
tmpDir = TempFileManager.INSTANCE.newTempDirectory("wildfly-swarm-data", ".d");
System.setProperty(JBOSS_DATA_DIR, tmpDir.getAbsolutePath());
} catch (IOException e) {
// Ignore
}
}
}
}