-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WFLY-18476] helloworld-mutual-ssl Quickstart Common Enhancements CY2…
…023Q3
- Loading branch information
Prarthona Paul
committed
Dec 20, 2023
1 parent
543ee74
commit 83d5ac2
Showing
9 changed files
with
346 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: WildFly helloworld-mutual-ssl Quickstart CI | ||
|
||
on: | ||
pull_request: | ||
types: [opened, synchronize, reopened, ready_for_review] | ||
paths: | ||
- 'helloworld-mutual-ssl/**' | ||
- '.github/workflows/quickstart_ci.yml' | ||
|
||
jobs: | ||
call-quickstart_ci: | ||
uses: ./.github/workflows/quickstart_ci.yml | ||
with: | ||
QUICKSTART_PATH: helloworld-mutual-ssl | ||
SERVER_PROVISIONING_SERVER_HOST: https://localhost:8443/ | ||
TEST_PROVISIONED_SERVER: true | ||
TEST_OPENSHIFT: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Configure the client's keystore. This will be used to generate the client's certificate. | ||
# The path to the keystore file doesn’t actually have to exist yet. | ||
/subsystem=elytron/key-store=clientKS:add(path=client.keystore.P12, relative-to=jboss.server.config.dir, credential-reference={clear-text=secret}, type=PKCS12) | ||
|
||
# Generate a new key pair for the client. We'll use an RSA key of size 2048 and we'll use CN=quickstartUser | ||
/subsystem=elytron/key-store=clientKS:generate-key-pair(alias=quickstartUser, algorithm=RSA, key-size=2048, validity=365, credential-reference={clear-text=secret}, distinguished-name="cn=quickstartUser") | ||
|
||
# Export the client's certificate to a file called clientCert.crt | ||
/subsystem=elytron/key-store=clientKS:export-certificate(alias=quickstartUser, path=clientCert.crt, relative-to=jboss.server.config.dir, pem=true) | ||
|
||
# Create the server's truststore | ||
/subsystem=elytron/key-store=serverTS:add(path=server.truststore, relative-to=jboss.server.config.dir, credential-reference={clear-text=secret}, type=PKCS12) | ||
|
||
# Import the client certificate into the server's truststore | ||
/subsystem=elytron/key-store=serverTS:import-certificate(alias=quickstartUser, path=clientCert.crt, relative-to=jboss.server.config.dir, credential-reference={clear-text=secret}, validate=false) | ||
|
||
# Persist the changes we've made to the client's keystore and the server's truststore | ||
/subsystem=elytron/key-store=serverTS:store() | ||
/subsystem=elytron/key-store=clientKS:store() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Remove the keypairs and certificates from the keystore and truststore | ||
/subsystem=elytron/key-store=serverTS:remove-alias(alias=quickstartUser) | ||
/subsystem=elytron/key-store=clientKS:remove-alias(alias=quickstartUser) | ||
|
||
# Remove the keystore and truststore | ||
/subsystem=elytron/key-store=serverTS:remove | ||
/subsystem=elytron/key-store=clientKS:remove |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...tual-ssl/src/test/java/org/jboss/as/quickstarts/helloworld_mutual_ssl/BasicRuntimeIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2023 JBoss by Red Hat. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jboss.as.quickstarts.helloworld_mutual_ssl; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.security.KeyStore; | ||
import java.security.KeyStoreException; | ||
|
||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.config.Registry; | ||
import org.apache.http.config.RegistryBuilder; | ||
import org.apache.http.conn.socket.ConnectionSocketFactory; | ||
import org.apache.http.conn.socket.PlainConnectionSocketFactory; | ||
import org.apache.http.conn.ssl.NoopHostnameVerifier; | ||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.apache.http.impl.conn.DefaultSchemePortResolver; | ||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | ||
import org.apache.http.ssl.SSLContextBuilder; | ||
import org.apache.http.ssl.SSLContexts; | ||
import javax.net.ssl.SSLContext; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.HttpResponse; | ||
|
||
import static org.jboss.as.quickstarts.helloworld_mutual_ssl.KeyStoreUtils.loadKeyPairFromKeyStore; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* The very basic runtime integration testing. | ||
* @author Prarthona Paul | ||
* @author emartins | ||
*/ | ||
public class BasicRuntimeIT { | ||
|
||
private static final String DEFAULT_SERVER_HOST = "https://localhost:8443/helloworld-mutual-ssl"; | ||
|
||
@Test | ||
public void testHTTPEndpointIsAvailable() throws IOException, InterruptedException, URISyntaxException, KeyStoreException { | ||
String serverHost = System.getenv("SERVER_HOST"); | ||
if (serverHost == null) { | ||
serverHost = System.getProperty("server.host"); | ||
} | ||
if (serverHost == null) { | ||
serverHost = DEFAULT_SERVER_HOST; | ||
} | ||
String serverDir = System.getenv("SERVER_HOME"); | ||
if (serverDir == null) { | ||
if (System.getProperty("jboss.server.config.dir").contains("target/server")) { | ||
serverDir = System.getProperty("user.dir") + "/" + System.getProperty("jboss.server.config.dir"); | ||
} else { | ||
serverDir = System.getProperty("jboss.server.config.dir"); | ||
} | ||
} | ||
HttpGet request = new HttpGet(new URI(serverHost+"/")); | ||
KeyStore trustStore = loadKeyPairFromKeyStore(serverDir, "application.keystore", "password", "server", "PKCS12"); | ||
final HttpClient client = getHttpClientWithSSL(new File(serverDir + "/client.keystore.P12"), "secret", "PKCS12", new File(serverDir + "/client.truststore"), "password", "PKCS12"); | ||
HttpResponse response = client.execute(request); | ||
assertEquals(200, response.getStatusLine().getStatusCode()); | ||
} | ||
|
||
public HttpClient getHttpClientWithSSL(File keyStoreFile, String keyStorePassword, String keyStoreProvider, | ||
File trustStoreFile, String trustStorePassword, String trustStoreProvider) { | ||
|
||
try { | ||
KeyStore trustStore = KeyStore.getInstance(trustStoreProvider); | ||
try (FileInputStream fis = new FileInputStream(trustStoreFile)) { | ||
trustStore.load(fis, trustStorePassword.toCharArray()); | ||
} | ||
SSLContextBuilder sslContextBuilder = SSLContexts.custom() | ||
.setProtocol("TLS") | ||
.loadTrustMaterial(trustStore, null); | ||
if (keyStoreFile != null) { | ||
KeyStore keyStore = KeyStore.getInstance(keyStoreProvider); | ||
try (FileInputStream fis = new FileInputStream(keyStoreFile)) { | ||
keyStore.load(fis, keyStorePassword.toCharArray()); | ||
} | ||
sslContextBuilder.loadKeyMaterial(keyStore, keyStorePassword.toCharArray(), null); | ||
} | ||
SSLContext sslContext = sslContextBuilder.build(); | ||
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); | ||
|
||
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() | ||
.register("http", PlainConnectionSocketFactory.getSocketFactory()) | ||
.register("https", socketFactory) | ||
.build(); | ||
|
||
return HttpClientBuilder.create() | ||
.setSSLSocketFactory(socketFactory) | ||
// .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) | ||
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) | ||
.setConnectionManager(new PoolingHttpClientConnectionManager(registry)) | ||
.setSchemePortResolver(new DefaultSchemePortResolver()) | ||
.build(); | ||
|
||
} catch (Exception e) { | ||
throw new RuntimeException("Creating HttpClient with customized SSL failed.", e); | ||
} | ||
} | ||
} |
Oops, something went wrong.