Skip to content

Commit

Permalink
Moved Jetty test to Jetty module
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakky54 committed Mar 27, 2024
1 parent 6b497b9 commit b3b9e0e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 46 deletions.
16 changes: 14 additions & 2 deletions sslcontext-kickstart-for-jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</developers>

<properties>
<version.jetty-util>9.4.54.v20240208</version.jetty-util>
<version.jetty>9.4.54.v20240208</version.jetty>
<version.jetty-reactive-httpclient>1.1.16</version.jetty-reactive-httpclient>
</properties>

Expand All @@ -48,7 +48,7 @@
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${version.jetty-util}</version>
<version>${version.jetty}</version>
<scope>provided</scope>
</dependency>

Expand Down Expand Up @@ -78,6 +78,18 @@
<groupId>io.github.hakky54</groupId>
<artifactId>ssl-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${version.jetty}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${version.jetty}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.altindag.ssl;
package nl.altindag.ssl.jetty;

import nl.altindag.ssl.SSLFactory;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
import nl.altindag.ssl.server.service.Server;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.jupiter.api.Test;

import javax.net.ssl.SSLParameters;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* @author Hakan Altindag
Expand Down Expand Up @@ -63,4 +67,44 @@ void executeHttpsRequestWithMutualAuthentication() throws Exception {
server.stop();
}

@Test
void swapCiphersWhileUsingJetty() throws Exception {
SSLFactory sslFactoryForServer = SSLFactory.builder()
.withIdentityMaterial("keystore/client-server/server-one/identity.jks", "secret".toCharArray())
.withTrustMaterial("keystore/client-server/server-one/truststore.jks", "secret".toCharArray())
.withNeedClientAuthentication()
.withCiphers("TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256")
.withSwappableSslParameters()
.build();

JettyServer jettyServer = new JettyServer(sslFactoryForServer);

SSLFactory sslFactoryForClient = SSLFactory.builder()
.withIdentityMaterial("keystore/client-server/client-one/identity.jks", "secret".toCharArray())
.withTrustMaterial("keystore/client-server/client-one/truststore.jks", "secret".toCharArray())
.build();

SslContextFactory.Client sslContextFactory = JettySslUtils.forClient(sslFactoryForClient);
HttpClient httpClient = new HttpClient(sslContextFactory);
httpClient.start();

Request request = httpClient.newRequest("https://localhost:8432/api/hello")
.method(HttpMethod.GET);

assertThatThrownBy(request::send).hasMessageContaining("Received fatal alert: handshake_failure");

SSLParameters sslParameters = sslFactoryForServer.getSslParameters();
sslParameters.setCipherSuites(sslFactoryForClient.getCiphers().toArray(new String[0]));

ContentResponse contentResponse = httpClient.newRequest("https://localhost:8432/api/hello")
.method(HttpMethod.GET)
.send();

int statusCode = contentResponse.getStatus();
assertThat(statusCode).isEqualTo(200);

httpClient.stop();
jettyServer.stop();
}

}
13 changes: 0 additions & 13 deletions sslcontext-kickstart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
</developers>

<properties>
<version.jetty>9.4.54.v20240208</version.jetty>
<version.maven-site-plugin>3.12.1</version.maven-site-plugin>
<version.maven-project-info-reports-plugin>3.5.0</version.maven-project-info-reports-plugin>
<version.maven-surefire-report-plugin>3.2.5</version.maven-surefire-report-plugin>
Expand Down Expand Up @@ -73,18 +72,6 @@
<groupId>io.github.hakky54</groupId>
<artifactId>ssl-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${version.jetty}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${version.jetty}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,35 +451,6 @@ void swapCiphersWhileUsingNetty() throws Exception {
Security.removeProvider("Fenix");
}

@Test
void swapCiphersWhileUsingJetty() throws Exception {
SSLFactory sslFactoryForServer = SSLFactory.builder()
.withIdentityMaterial("keystore/client-server/server-one/identity.jks", "secret".toCharArray())
.withTrustMaterial("keystore/client-server/server-one/truststore.jks", "secret".toCharArray())
.withNeedClientAuthentication()
.withCiphers("TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256")
.withSwappableSslParameters()
.build();

JettyServer jettyServer = new JettyServer(sslFactoryForServer);

SSLFactory sslFactoryForClient = SSLFactory.builder()
.withIdentityMaterial("keystore/client-server/client-one/identity.jks", "secret".toCharArray())
.withTrustMaterial("keystore/client-server/client-one/truststore.jks", "secret".toCharArray())
.build();

assertThatThrownBy(() -> executeRequest("https://localhost:8432/api/hello", sslFactoryForClient.getSslSocketFactory()))
.hasMessageContaining("Received fatal alert: handshake_failure");

SSLParameters sslParameters = sslFactoryForServer.getSslParameters();
sslParameters.setCipherSuites(sslFactoryForClient.getCiphers().toArray(new String[0]));

Response response = executeRequest("https://localhost:8432/api/hello", sslFactoryForClient.getSslSocketFactory());
assertThat(response.getStatusCode()).isEqualTo(200);

jettyServer.stop();
}

private Response executeRequest(String url, SSLSocketFactory sslSocketFactory) throws IOException {
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
connection.setSSLSocketFactory(sslSocketFactory);
Expand All @@ -494,7 +465,6 @@ private Response executeRequest(String url, SSLSocketFactory sslSocketFactory) t
return new Response(statusCode, body);
}


private static final class Response {
private final int statusCode;
private final String body;
Expand Down

0 comments on commit b3b9e0e

Please sign in to comment.