Skip to content

Commit

Permalink
Bumped finagle (#108)
Browse files Browse the repository at this point in the history
* Bumped finagle with custom SslClientConfiguration

* Converted finagle service example from java to scala

* Removed unused imports

* Reformatted option

* Corrected if statement

* fixed name

* Adjusted reference to finagle
  • Loading branch information
Hakky54 authored Nov 29, 2024
1 parent 6bd0a7a commit 49948fc
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 187 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ All client examples use the same base ssl configuration created within the [SSLC
* [Ktor with Okhttp engine](https://github.com/ktorio/ktor) -> [Client Configuration](https://github.com/Hakky54/mutual-tls-ssl/blob/master/client/src/main/java/nl/altindag/client/service/KtorOkHttpClientService.kt) | [Example request](https://github.com/Hakky54/mutual-tls-ssl/blob/master/client/src/main/java/nl/altindag/client/service/KtorHttpClientService.kt)

**Scala**
* [Twitter Finagle](https://github.com/twitter/finagle) -> [Client Configuration](https://github.com/Hakky54/mutual-tls-ssl/blob/35cba2f3a2dcd73b01fa323b99eec7777f7429bb/client/src/main/java/nl/altindag/client/ClientConfig.java#L233) | [Example request](https://github.com/Hakky54/mutual-tls-ssl/blob/master/client/src/main/java/nl/altindag/client/service/FinagleHttpClientService.java)
* [Twitter Finagle](https://github.com/twitter/finagle) -> [Client Configuration & Example request](https://github.com/Hakky54/mutual-tls-ssl/blob/master/client/src/main/java/nl/altindag/client/service/FinagleHttpClientService.java)
* [Twitter Finagle Featherbed](https://github.com/finagle/featherbed) -> [Client Configuration & Example request](https://github.com/Hakky54/mutual-tls-ssl/blob/d78e4e81b8b775d3ff09c11b0a7c1532a741199e/client/src/main/java/nl/altindag/client/service/FeatherbedRequestService.scala#L19)
* [Akka Http Client](https://github.com/akka/akka-http) -> [Client Configuration](https://github.com/Hakky54/mutual-tls-ssl/blob/35cba2f3a2dcd73b01fa323b99eec7777f7429bb/client/src/main/java/nl/altindag/client/ClientConfig.java#L253) | [Example request](https://github.com/Hakky54/mutual-tls-ssl/blob/master/client/src/main/java/nl/altindag/client/service/AkkaHttpClientService.java)
* [Dispatch Reboot](https://github.com/dispatch/reboot) -> [Client Configuration & Example request](https://github.com/Hakky54/mutual-tls-ssl/blob/master/client/src/main/java/nl/altindag/client/service/DispatchRebootService.scala)
Expand Down
17 changes: 0 additions & 17 deletions client/src/main/java/nl/altindag/client/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
import com.google.gson.GsonBuilder;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.HTTPSProperties;
import com.twitter.finagle.Http;
import com.twitter.finagle.Service;
import com.twitter.finagle.http.Request;
import com.twitter.finagle.http.Response;
import com.typesafe.config.ConfigFactory;
import feign.Feign;
import feign.googlehttpclient.GoogleHttpClient;
Expand Down Expand Up @@ -70,8 +66,6 @@
import retrofit2.converter.gson.GsonConverterFactory;

import javax.net.ssl.SSLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;

@Component
Expand Down Expand Up @@ -243,17 +237,6 @@ public Retrofit retrofit(@Qualifier("okHttpClient") OkHttpClient okHttpClient) {
.build();
}

@Bean
public Service<Request, Response> finagle(SSLFactory sslFactory) throws URISyntaxException {
var uri = new URI(Constants.getServerUrl());
var client = Http.client().withNoHttp2();
if (uri.getScheme().equals("https")) {
client = client.withTransport()
.tls(sslFactory.getSslContext());
}
return client.newService(uri.getHost() + ":" + uri.getPort());
}

@Bean
public ActorSystem actorSystem() {
return ActorSystem.create(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2018 Thunderberry.
*
* 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
*
* https://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 nl.altindag.client.service

import com.twitter.finagle.http.{Request, RequestBuilder, Response}
import com.twitter.finagle.ssl.client.SslClientConfiguration
import com.twitter.finagle.ssl.{KeyCredentials, TrustCredentials}
import com.twitter.finagle.{Http, Service}
import nl.altindag.client.ClientType.FINAGLE
import nl.altindag.client.Constants.HEADER_KEY_CLIENT_TYPE
import nl.altindag.client.model.ClientResponse
import nl.altindag.client.{ClientType, Constants}
import nl.altindag.ssl.SSLFactory
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Bean
import org.springframework.stereotype
import org.springframework.stereotype.Component

import java.net.URI
import java.util.concurrent.TimeUnit
import scala.jdk.javaapi.OptionConverters

@stereotype.Service
class FinagleHttpClientService(@Qualifier("finagleClient") service: Service[Request, Response]) extends RequestService {

private val TIMEOUT_AMOUNT_IN_SECONDS = 5

override def executeRequest(url: String): ClientResponse = {
val request = RequestBuilder()
.addHeader(HEADER_KEY_CLIENT_TYPE, getClientType.getValue)
.url(url)
.buildGet()

service.apply(request)
.map(response => new ClientResponse(response.contentString, response.statusCode))
.toJavaFuture
.get(TIMEOUT_AMOUNT_IN_SECONDS, TimeUnit.SECONDS)
}

override def getClientType: ClientType = FINAGLE
}

@Component
class FinagleHttpClientConfiguration {

@Bean(name = Array("finagleClient"))
def createFinagle(sslFactory: SSLFactory): Service[Request, Response] = {
val uri = new URI(Constants.getServerUrl)
var client = Http.client

if ("https".equals(uri.getScheme)) {
val sslClientConfiguration = SslClientConfiguration(
keyCredentials = OptionConverters.toScala(sslFactory.getKeyManagerFactory)
.map(kmf => KeyCredentials.KeyManagerFactory(kmf))
.getOrElse(KeyCredentials.Unspecified),
trustCredentials = OptionConverters.toScala(sslFactory.getTrustManagerFactory)
.map(tmf => TrustCredentials.TrustManagerFactory(tmf))
.getOrElse(TrustCredentials.Unspecified)
)

client = client.withTransport.tls(sslClientConfiguration)
}

client.newService(uri.getHost + ":" + uri.getPort)
}

}
36 changes: 3 additions & 33 deletions client/src/test/java/nl/altindag/client/ClientConfigShould.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
import akka.http.javadsl.Http;
import com.github.mizosoft.methanol.Methanol;
import com.google.api.client.http.HttpTransport;
import com.twitter.finagle.Service;
import com.twitter.finagle.http.Request;
import com.twitter.finagle.http.Response;
import feign.Feign;
import jakarta.ws.rs.client.Client;
import kong.unirest.Unirest;
Expand All @@ -40,15 +37,16 @@
import javax.net.ssl.SSLException;
import java.io.IOException;
import java.net.ConnectException;
import java.net.URISyntaxException;
import java.net.http.HttpClient;

import static nl.altindag.client.util.AssertJCustomConditions.GSON_CONVERTER_FACTORY;
import static nl.altindag.client.util.AssertJCustomConditions.SUBSTRING_OF_HTTP_OR_HTTPS_SERVER_URL;
import static nl.altindag.client.util.SSLFactoryTestHelper.createSSLFactory;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
class ClientConfigShould {
Expand Down Expand Up @@ -306,34 +304,6 @@ void createRetrofitWithProvidedOkHttpClient() {
assertThat(retrofit.converterFactories()).has(GSON_CONVERTER_FACTORY);
}

@Test
void createFinagleClientWithoutSecurity() throws URISyntaxException {
System.setProperty("url", TestConstants.HTTP_URL);
Service<Request, Response> service = victim.finagle(null);

assertThat(service.isAvailable()).isTrue();
assertThat(service.status()).hasToString("Open");

service.close();
System.clearProperty("url");
}

@Test
void createFinagleClientWithSecurity() throws URISyntaxException {
System.setProperty("url", TestConstants.HTTPS_URL);
SSLFactory sslFactory = createSSLFactory(false, true);

Service<Request, Response> service = victim.finagle(sslFactory);

verify(sslFactory, times(1)).getSslContext();

assertThat(service.isAvailable()).isTrue();
assertThat(service.status()).hasToString("Open");

service.close();
System.clearProperty("url");
}

@Test
void createAkkaHttpClient() {
SSLFactory sslFactory = createSSLFactory(false, true);
Expand Down

This file was deleted.

Loading

0 comments on commit 49948fc

Please sign in to comment.