Skip to content

Commit

Permalink
TLS replace in HelidonConnector fix
Browse files Browse the repository at this point in the history
Signed-off-by: David Kral <david.k.kral@oracle.com>
  • Loading branch information
Verdent committed Jan 11, 2024
1 parent 227191a commit dd6c779
Show file tree
Hide file tree
Showing 11 changed files with 381 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,7 @@
import java.util.logging.Logger;

import io.helidon.common.Version;
import io.helidon.config.Config;
import io.helidon.webclient.WebClient;
import io.helidon.webclient.WebClientRequestBuilder;
import io.helidon.webclient.WebClientResponse;
Expand Down Expand Up @@ -91,12 +92,21 @@ public int read() throws IOException {

HelidonStructures.createProxy(config).ifPresent(webClientBuilder::proxy);

HelidonStructures.helidonConfig(config).ifPresent(webClientBuilder::config);
Optional<Config> helidonConfig = HelidonStructures.helidonConfig(config);
helidonConfig.ifPresent(webClientBuilder::config);

webClientBuilder.connectTimeout(ClientProperties.getValue(config.getProperties(),
ClientProperties.CONNECT_TIMEOUT, 10000), TimeUnit.MILLISECONDS);

HelidonStructures.createSSL(client.getSslContext()).ifPresent(webClientBuilder::tls);
//Whether WebClient TLS has been already set via config
boolean helidonConfigTlsSet = helidonConfig.map(hc -> hc.get("tls").exists()).orElse(false);
boolean isJerseyClient = client instanceof JerseyClient;
//Whether Jersey client has non-default SslContext set. If so, we should honor these settings
boolean jerseyHasDefaultSsl = isJerseyClient && ((JerseyClient) client).isDefaultSslContext();

if (!helidonConfigTlsSet || !isJerseyClient || !jerseyHasDefaultSsl) {
HelidonStructures.createSSL(client.getSslContext()).ifPresent(webClientBuilder::tls);
}

webClient = webClientBuilder.build();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019, 2023 Oracle and/or its affiliates.
Copyright (c) 2019, 2024 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,6 +58,7 @@
<module>jep290</module>
<module>mp-bean-validation</module>
<module>restclient</module>
<module>restclient-connector</module>
<module>oidc</module>
<module>gh-5792</module>
<module>se-gh-6845</module>
Expand Down
83 changes: 83 additions & 0 deletions tests/integration/restclient-connector/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates.
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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.tests.integration</groupId>
<artifactId>helidon-tests-integration</artifactId>
<version>3.2.3-SNAPSHOT</version>
</parent>

<artifactId>helidon-tests-integration-restclient-connector</artifactId>
<name>Helidon Integration Test RestClient Webclient Connector</name>

<dependencies>
<dependency>
<groupId>io.helidon.microprofile.bundles</groupId>
<artifactId>helidon-microprofile</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.rest-client</groupId>
<artifactId>helidon-microprofile-rest-client</artifactId>
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.jersey</groupId>
<artifactId>helidon-jersey-connector</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-libs</id>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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 io.helidon.tests.integration.resclient.connector;

import java.util.Collections;

import jakarta.json.Json;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

/**
* A typical greet resource that only handles a single GET for a default message.
*/
@Path("/greet")
public class GreetResource {

private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());

@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonObject getDefaultMessage() {
return createResponse("World");
}

private JsonObject createResponse(String who) {
String msg = String.format("%s %s!", "Hello", who);

return JSON.createObjectBuilder()
.add("message", msg)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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 io.helidon.tests.integration.resclient.connector;

import jakarta.json.JsonObject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;

/**
* RestClient interface for a simple greet resource that includes a few FT annotations.
*/
@Path("/greet")
@RegisterProvider(GreetResourceFilter.class)
public interface GreetResourceClient {

@GET
@Produces(MediaType.APPLICATION_JSON)
JsonObject getDefaultMessage();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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 io.helidon.tests.integration.resclient.connector;

import java.io.IOException;
import java.net.URI;

import io.helidon.common.context.Contexts;

import jakarta.ws.rs.client.ClientRequestContext;
import jakarta.ws.rs.client.ClientRequestFilter;

/**
* A client request filter that replaces port 8080 by the ephemeral port allocated for the
* webserver in each run. This is necessary since {@link GreetResourceClient} uses an annotation
* to specify the base URI, and its value cannot be changed dynamically.
*/
public class GreetResourceFilter implements ClientRequestFilter {

@Override
public void filter(ClientRequestContext requestContext) throws IOException {
URI uri = requestContext.getUri();
String fixedUri = uri.toString().replace("8080", extractDynamicPort());
requestContext.setUri(URI.create(fixedUri));
}

private String extractDynamicPort() {
URI uri = Contexts.globalContext().get(getClass(), URI.class).orElseThrow();
String uriString = uri.toString();
int k = uriString.lastIndexOf(":");
int j = uriString.indexOf("/", k);
j = j < 0 ? uriString.length() : j; //Prevent failing if / is missing after the port
return uriString.substring(k + 1, j);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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 io.helidon.tests.integration.resclient.connector;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates.
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.
-->

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
version="2.0"
bean-discovery-mode="annotated">
</beans>
Loading

0 comments on commit dd6c779

Please sign in to comment.