Skip to content

Commit

Permalink
Merge pull request #214 from Karm/tested-fixes-for-service-a-b-separa…
Browse files Browse the repository at this point in the history
…tion

Fixes service-a service-b separation, all servers tested
  • Loading branch information
Karm authored Sep 14, 2019
2 parents 376d514 + f81990b commit bae5602
Show file tree
Hide file tree
Showing 33 changed files with 381 additions and 441 deletions.
40 changes: 40 additions & 0 deletions gencerts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh

# Generates certificates for JWT_AUTH demo examples.

pushd src/main/resources/

openssl genrsa -out private.key 2048
cat << EOF | openssl req -new -key private.key \
-x509 -days 0 \
-out publickey.cer
--
JWT
JWT
JWT
JWT
JWT
JWT
EOF

keytool -import -noprompt -alias theKeyId -keystore public.jks -file publickey.cer -storepass atbash

cat private.key | openssl pkcs8 -topk8 -nocrypt -out private.pem
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
rm -rf private.key
rm -rf publickey.cer
mv public.jks ./files/liberty/public.jks.tpl
mv private.pem ./files/privateKey.pem.tpl
mv public.pem ./files/publicKey.pem.tpl

# As soon as we switch to VertX JWT https://github.com/eclipse/microprofile-starter/issues/206
# This conversion won't be needed:
# org.bouncycastle.asn1.pkcs.PrivateKeyInfo cannot be cast to org.bouncycastle.openssl.PEMKeyPair
openssl rsa -in ./files/privateKey.pem.tpl -text -out ./files/privateKey.pem.tpl.long
mv ./files/privateKey.pem.tpl.long ./files/privateKey.pem.tpl

# KumuluzEE needs pub key in config
KEY=`cat ./files/publicKey.pem.tpl | grep -ve '^-' | tr -d '\n'`
sed -i "s~[ ]*public-key:.*~ public-key: $KEY~g" ./files/kumuluzEE/service-b/config.yaml.tpl

popd
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,12 @@ public void adaptMavenModel(Model pomFile, JessieModel model, boolean mainProjec
}

if (model.hasMainAndSecondaryProject()) {
String artifactID = pomFile.getArtifactId();
if (mainProject) {
pomFile.setArtifactId(artifactID + "-" + JessieModel.MAIN_INDICATOR);
pomFile.setArtifactId(model.getMaven().getArtifactId() + "-" + JessieModel.MAIN_INDICATOR);
} else {
pomFile.setArtifactId(artifactID + "-" + JessieModel.SECONDARY_INDICATOR);
pomFile.setArtifactId(model.getMaven().getArtifactId() + "-" + JessieModel.SECONDARY_INDICATOR);
}
pomFile.getBuild().setFinalName(artifactID);
}

}

private Profile findProfile(String profileName) {
Expand Down Expand Up @@ -186,6 +183,8 @@ public Set<String> alternativesNames(JessieModel model) {
public void createFiles(JessieModel model) {

Set<String> alternatives = model.getParameter(JessieModel.Parameter.ALTERNATIVES);
Set<String> bAlternatives = new HashSet<>(alternatives);
bAlternatives.add(JessieModel.SECONDARY_INDICATOR);
Map<String, String> variables = model.getVariables();

String serverName = model.getOptions().get("mp.server").getSingleValue();
Expand All @@ -194,8 +193,8 @@ public void createFiles(JessieModel model) {
String artifactId = model.getMaven().getArtifactId();
variables.put("jar_file", defineJarFileName(supportedServer, artifactId));
variables.put("jar_parameters", defineJarParameters(supportedServer));
variables.put("test_url", defineTestURL(supportedServer, artifactId));
variables.put("secondary_url", defineSecondaryURL(supportedServer, artifactId));
variables.put("port_service_a", supportedServer.getPortServiceA());
variables.put("port_service_b", supportedServer.getPortServiceB());
variables.put("artifact_id", artifactId);

String rootJava = getJavaApplicationRootPackage(model);
Expand Down Expand Up @@ -228,56 +227,49 @@ public void createFiles(JessieModel model) {
processTemplateFile(faultDirectory, "ResilienceController.java", alternatives, variables);
}

if (microprofileSpecs.contains(MicroprofileSpec.JWT_AUTH)) {
String secureDirectory;

if (model.hasMainAndSecondaryProject()) {
secureDirectory = model.getDirectory(false) + "/" + rootJava + "/secure";
directoryCreator.createDirectory(secureDirectory);

processTemplateFile(secureDirectory, "ProtectedController.java", alternatives, variables);

}
}


if (microprofileSpecs.contains(MicroprofileSpec.REST_CLIENT)) {
String clientMainDirectory = model.getDirectory(true) + "/" + rootJava + "/client";
directoryCreator.createDirectory(clientMainDirectory);

String clientSecondaryDirectory = model.getDirectory(false) + "/" + rootJava + "/client";
directoryCreator.createDirectory(clientSecondaryDirectory);

processTemplateFile(clientSecondaryDirectory, "ServiceController.java", alternatives, variables);
processTemplateFile(clientSecondaryDirectory, "ServiceController.java", bAlternatives, variables);
processTemplateFile(clientMainDirectory, "Service.java", alternatives, variables);
processTemplateFile(clientMainDirectory, "ClientController.java", alternatives, variables);
}

if (microprofileSpecs.contains(MicroprofileSpec.JWT_AUTH)) {
String javaDirectory = model.getDirectory(true) + "/" + rootJava + "/secure";
if (model.hasMainAndSecondaryProject()) {
String bSecureDirectory = model.getDirectory(false) + "/" + rootJava + "/secure";
directoryCreator.createDirectory(bSecureDirectory);
processTemplateFile(bSecureDirectory, "ProtectedController.java", bAlternatives, variables);
}

String aSecureDirectory = model.getDirectory(true) + "/" + rootJava + "/secure";

processTemplateFile(javaDirectory, "TestSecureController.java", alternatives, variables);
processTemplateFile(javaDirectory, "MPJWTToken.java", alternatives, variables);
processTemplateFile(aSecureDirectory, "TestSecureController.java", alternatives, variables);
processTemplateFile(aSecureDirectory, "MPJWTToken.java", alternatives, variables);

String resourceDirectory = getResourceDirectory(model, true);

processTemplateFile(resourceDirectory, "privateKey.pem", alternatives, variables);

}

// TODO : Verify : This is for all specs?
// With KumuluzEE, it properties are integrated within config.yaml
if (supportedServer != SupportedServer.KUMULUZEE) {
// With kumuluzEE, it properties are integrated within config.yaml
String metaInfDirectory = getResourceDirectory(model, true) + "/META-INF";

directoryCreator.createDirectory(metaInfDirectory);
processTemplateFile(metaInfDirectory, "microprofile-config.properties", alternatives, variables);
}

// Demo index file to all endpoints
String webDirectory = model.getDirectory(true) + "/" + MavenCreator.SRC_MAIN_WEBAPP;
directoryCreator.createDirectory(webDirectory);
processTemplateFile(webDirectory, "index.html", alternatives, variables);
// Helidon should have it in src/main/resources/WEB
if (supportedServer != SupportedServer.HELIDON) {
// Demo index file to all endpoints
String webDirectory = model.getDirectory(true) + "/" + MavenCreator.SRC_MAIN_WEBAPP;
directoryCreator.createDirectory(webDirectory);
processTemplateFile(webDirectory, "index.html", alternatives, variables);
}

processTemplateFile(model.getDirectory(true), "readme.md", alternatives, variables);
if (model.hasMainAndSecondaryProject()) {
Expand All @@ -293,13 +285,4 @@ private String defineJarFileName(SupportedServer supportedServer, String artifac
private String defineJarParameters(SupportedServer supportedServer) {
return supportedServer.getJarParameters();
}

private String defineTestURL(SupportedServer supportedServer, String artifactId) {
return String.format(supportedServer.getTestURL(), artifactId);
}

private String defineSecondaryURL(SupportedServer supportedServer, String artifactId) {
return String.format(supportedServer.getSecondaryURL(), artifactId);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,56 +34,56 @@ public enum SupportedServer {
Collections.singletonList(MicroProfileVersion.MP12)
, "%s-swarm.jar" //jarFileName
, "-Dswarm.port.offset=100" //jarParameters
, "http://localhost:8080" //testURL
, "http://localhost:8180" //secondaryURL // This need to match with port value from defineJarParameters()
, "8080" //portServiceA
, "8180" //portServiceB
)
, THORNTAIL_V2("thorntail-v2", "Thorntail V2",
Arrays.asList(MicroProfileVersion.MP12, MicroProfileVersion.MP13, MicroProfileVersion.MP21,
MicroProfileVersion.MP22, MicroProfileVersion.MP30)
, "%s-thorntail.jar" //jarFileName
, "-Dswarm.port.offset=100" //jarParameters
, "http://localhost:8080" //testURL
, "http://localhost:8180" //secondaryURL // This need to match with port value from defineJarParameters()
, "8080" //portServiceA
, "8180" //portServiceB
)
, LIBERTY("liberty", "Open Liberty",
Arrays.asList(MicroProfileVersion.MP12, MicroProfileVersion.MP13, MicroProfileVersion.MP14,
MicroProfileVersion.MP20, MicroProfileVersion.MP21, MicroProfileVersion.MP22,
MicroProfileVersion.MP30)
, "%s.jar" //jarFileName
, "" //jarParameters // Hard coded in server.xml since no way of overriding a default.
, "http://localhost:8181/%s" //testURL
, "http://localhost:8281/%s" //secondaryURL // This need to match with port value from server.xml
, "8181" //portServiceA
, "8281" //portServiceB
)
, KUMULUZEE("kumuluzEE", "KumuluzEE",
Arrays.asList(MicroProfileVersion.MP12, MicroProfileVersion.MP13, MicroProfileVersion.MP14,
MicroProfileVersion.MP20, MicroProfileVersion.MP21, MicroProfileVersion.MP22)
, "%s.jar" //jarFileName
, "" //jarParameters // Hard coded in config.xml since we needed a specific version for secondary app.
, "http://localhost:8080" //testURL
, "http://localhost:8180" //secondaryURL // This need to match with port value from secondary/config.yaml
, "8080" //portServiceA
, "8180" //portServiceB // This need to match with port value from secondary/config.yaml
)
, PAYARA_MICRO("payara-micro", "Payara Micro",
Arrays.asList(MicroProfileVersion.MP12, MicroProfileVersion.MP13, MicroProfileVersion.MP14,
MicroProfileVersion.MP20, MicroProfileVersion.MP21, MicroProfileVersion.MP22)
, "%s-microbundle.jar" //jarFileName
, "--port 8180" //jarParameters
, "http://localhost:8080" //testURL
, "http://localhost:8180" //secondaryURL // This need to match with port value from defineJarParameters()
, "8080" //portServiceA
, "8180" //portServiceB // This need to match with port value from defineJarParameters()
)
, TOMEE("tomee", "Apache TomEE 8.0.0-M3",
Arrays.asList(MicroProfileVersion.MP12, MicroProfileVersion.MP13, MicroProfileVersion.MP14,
MicroProfileVersion.MP20,MicroProfileVersion.MP21)
MicroProfileVersion.MP20, MicroProfileVersion.MP21)
, "%s-exec.jar" //jarFileName
, "" //jarParameters // Done by TomeeServer.adaptMavenModel
, "http://localhost:8080" // testURL
, "http://localhost:8180" //secondaryURL // This need to match with Port value from TomeeServer.adjustPOM
, "8080" // portServiceA
, "8180" //portServiceB // This need to match with Port value from TomeeServer.adjustPOM
)
, HELIDON("helidon", "Helidon",
Arrays.asList(MicroProfileVersion.MP12, MicroProfileVersion.MP22)
, "%s.jar" //jarFileName
, "" //jarParameters // Done by secondary/helidon/microprofile-config.properties
, "http://localhost:8080" //testURL
, "http://localhost:8180" //secondaryURL // This need to match Port vcalue from secondary/microprofile-config.proeprties
, "8080" //portServiceA
, "8180" //portServiceB // This need to match Port vcalue from secondary/microprofile-config.proeprties
);
// @formatter:on

Expand All @@ -92,18 +92,18 @@ public enum SupportedServer {
private List<MicroProfileVersion> mpVersions;
private String jarFileName;
private String jarParameters;
private String testURL;
private String secondaryURL;
private String portServiceA;
private String portServiceB;

SupportedServer(String code, String displayName, List<MicroProfileVersion> mpVersions, String jarFileName
, String jarParameters, String testURL, String secondaryURL) {
, String jarParameters, String portServiceA, String portServiceB) {
this.code = code;
this.displayName = displayName;
this.mpVersions = mpVersions;
this.jarFileName = jarFileName;
this.jarParameters = jarParameters;
this.testURL = testURL;
this.secondaryURL = secondaryURL;
this.portServiceA = portServiceA;
this.portServiceB = portServiceB;
}

public String getCode() {
Expand All @@ -126,12 +126,12 @@ public String getJarParameters() {
return jarParameters;
}

public String getTestURL() {
return testURL;
public String getPortServiceA() {
return portServiceA;
}

public String getSecondaryURL() {
return secondaryURL;
public String getPortServiceB() {
return portServiceB;
}

public static SupportedServer valueFor(String data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.maven.model.Model;
import org.eclipse.microprofile.starter.addon.microprofile.servers.AbstractMicroprofileAddon;
import org.eclipse.microprofile.starter.addon.microprofile.servers.model.MicroprofileSpec;
import org.eclipse.microprofile.starter.addon.microprofile.servers.model.SupportedServer;
import org.eclipse.microprofile.starter.core.artifacts.CDICreator;
import org.eclipse.microprofile.starter.core.artifacts.MavenCreator;
Expand All @@ -29,6 +30,8 @@
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand All @@ -53,29 +56,54 @@ public void createFiles(JessieModel model) {
Set<String> alternatives = model.getParameter(JessieModel.Parameter.ALTERNATIVES);
Map<String, String> variables = model.getVariables();

// kumuluzEE is JAR based, so needs beans.xml within META-INF
// Helidon's html files reside in resources/WEB; we can delete webapp dir.
String webHtmlDir = model.getDirectory(true) + "/" + MavenCreator.SRC_MAIN_RESOURCES + "/" + "WEB";
directoryCreator.createDirectory(webHtmlDir);
processTemplateFile(webHtmlDir, "index.html", alternatives, variables);
directoryCreator.removeDirectory(model.getDirectory(true) + "/" + MavenCreator.SRC_MAIN_WEBAPP);
if (model.hasMainAndSecondaryProject()) {
directoryCreator.removeDirectory(model.getDirectory(false) + "/" + MavenCreator.SRC_MAIN_WEBAPP);
}

// Helidon is JAR based, so needs beans.xml within META-INF
cdiCreator.createCDIFilesForJar(model, true);
if (model.hasMainAndSecondaryProject()) {
cdiCreator.createCDIFilesForJar(model, false);
}

// Remove WEB-INF containing the beans.xml
String webDirectory = model.getDirectory(true) + "/" + MavenCreator.SRC_MAIN_WEBAPP + "/WEB-INF";
directoryCreator.removeDirectory(webDirectory);

String rootJava = MavenCreator.SRC_MAIN_JAVA + "/" + directoryCreator.createPathForGroupAndArtifact(model.getMaven());
String viewDirectory = model.getDirectory(true) + "/" + rootJava;

String resourcesDirectory = model.getDirectory(true) + "/" + MavenCreator.SRC_MAIN_RESOURCES;
String resourcesDirectory = model.getDirectory(true) + "/" + MavenCreator.SRC_MAIN_RESOURCES;
directoryCreator.createDirectory(resourcesDirectory);

processTemplateFile(resourcesDirectory, "application.yaml", alternatives, variables);
processTemplateFile(resourcesDirectory, "logging.properties", alternatives, variables);
processTemplateFile(resourcesDirectory, "publicKey.pem", alternatives, variables);
processTemplateFile(resourcesDirectory, "privateKey.pem", alternatives, variables);

String artifactId = variables.get("artifact");
List<MicroprofileSpec> microprofileSpecs = model.getParameter(JessieModel.Parameter.MICROPROFILESPECS);

String restAppFile = thymeleafEngine.processFile("RestApplication.java", alternatives, variables);
fileCreator.writeContents(viewDirectory, artifactId + "RestApplication.java", restAppFile);
if (model.hasMainAndSecondaryProject()) {
String viewDirectory = model.getDirectory(false) + "/" + rootJava;
directoryCreator.createDirectory(viewDirectory);
Set<String> tempAlternative = new HashSet<>(alternatives);
tempAlternative.add(JessieModel.SECONDARY_INDICATOR);
String javaFile = thymeleafEngine.processFile("RestApplication.java", tempAlternative, variables);
fileCreator.writeContents(viewDirectory, variables.get("application") + "RestApplication.java", javaFile);

String bResourcesDir = model.getDirectory(false) + "/" + MavenCreator.SRC_MAIN_RESOURCES;
String bResourcesMETAINFDir = bResourcesDir + "/META-INF";
directoryCreator.createDirectory(bResourcesMETAINFDir);

if (microprofileSpecs.contains(MicroprofileSpec.JWT_AUTH)) {
processTemplateFile(bResourcesDir, "publicKey.pem", alternatives, variables);
}

String configFile = thymeleafEngine.processFile("microprofile-config.properties", tempAlternative, variables);
fileCreator.writeContents(bResourcesMETAINFDir, "microprofile-config.properties", configFile);
}
}

@Override
Expand Down
Loading

0 comments on commit bae5602

Please sign in to comment.