Skip to content

Commit

Permalink
Changed Kerberos client to use okhttp lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal Al committed Aug 21, 2024
1 parent e388362 commit acee253
Show file tree
Hide file tree
Showing 11 changed files with 528 additions and 189 deletions.
13 changes: 12 additions & 1 deletion client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.split.client</groupId>
<artifactId>java-client-parent</artifactId>
<version>4.13.0-rc1</version>
<version>4.13.0-rc2</version>
</parent>
<artifactId>java-client</artifactId>
<packaging>jar</packaging>
Expand Down Expand Up @@ -177,6 +177,17 @@
<artifactId>snakeyaml</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>4.12.0</version>
</dependency>


<!-- Test deps -->
<dependency>
Expand Down
21 changes: 19 additions & 2 deletions client/src/main/java/io/split/client/SplitClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public class SplitClientConfig {
private final int _invalidSets;
private final CustomHeaderDecorator _customHeaderDecorator;
private final HttpAuthScheme _authScheme;
private final String _kerberosPrincipalName;


public static Builder builder() {
Expand Down Expand Up @@ -151,7 +152,8 @@ private SplitClientConfig(String endpoint,
HashSet<String> flagSetsFilter,
int invalidSets,
CustomHeaderDecorator customHeaderDecorator,
HttpAuthScheme authScheme) {
HttpAuthScheme authScheme,
String kerberosPrincipalName) {
_endpoint = endpoint;
_eventsEndpoint = eventsEndpoint;
_featuresRefreshRate = pollForFeatureChangesEveryNSeconds;
Expand Down Expand Up @@ -205,6 +207,7 @@ private SplitClientConfig(String endpoint,
_invalidSets = invalidSets;
_customHeaderDecorator = customHeaderDecorator;
_authScheme = authScheme;
_kerberosPrincipalName = kerberosPrincipalName;

Properties props = new Properties();
try {
Expand Down Expand Up @@ -415,6 +418,7 @@ public CustomHeaderDecorator customHeaderDecorator() {
public HttpAuthScheme authScheme() {
return _authScheme;
}
public String kerberosPrincipalName() { return _kerberosPrincipalName; }

public static final class Builder {

Expand Down Expand Up @@ -474,6 +478,7 @@ public static final class Builder {
private int _invalidSetsCount = 0;
private CustomHeaderDecorator _customHeaderDecorator = null;
private HttpAuthScheme _authScheme = null;
private String _kerberosPrincipalName = null;

public Builder() {
}
Expand Down Expand Up @@ -979,6 +984,17 @@ public Builder authScheme(HttpAuthScheme authScheme) {
return this;
}

/**
* Kerberos Principal Account Name
*
* @param kerberosPrincipalName
* @return this builder
*/
public Builder kerberosPrincipalName(String kerberosPrincipalName) {
_kerberosPrincipalName = kerberosPrincipalName;
return this;
}

/**
* Thread Factory
*
Expand Down Expand Up @@ -1140,7 +1156,8 @@ public SplitClientConfig build() {
_flagSetsFilter,
_invalidSetsCount,
_customHeaderDecorator,
_authScheme);
_authScheme,
_kerberosPrincipalName);
}
}
}
44 changes: 37 additions & 7 deletions client/src/main/java/io/split/client/SplitFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import io.split.service.SplitHttpClient;
import io.split.service.SplitHttpClientImpl;
import io.split.service.SplitHttpClientKerberosImpl;
import io.split.service.HTTPKerberosAuthInterceptor;
import io.split.storages.SegmentCache;
import io.split.storages.SegmentCacheConsumer;
import io.split.storages.SegmentCacheProducer;
Expand Down Expand Up @@ -104,25 +105,35 @@
import org.apache.hc.core5.ssl.SSLContexts;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pluggable.CustomStorageWrapper;

import okhttp3.Authenticator;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;

import static io.split.client.utils.SplitExecutorFactory.buildExecutorService;

public class SplitFactoryImpl implements SplitFactory {
private static final Logger _log = LoggerFactory.getLogger(SplitFactory.class);
private static final org.slf4j.Logger _log = LoggerFactory.getLogger(SplitFactory.class);
private static final String LEGACY_LOG_MESSAGE = "The sdk initialize in localhost mode using Legacy file. The splitFile or "
+
"inputStream doesn't add it to the config.";
Expand Down Expand Up @@ -165,7 +176,7 @@ public class SplitFactoryImpl implements SplitFactory {
private final UniqueKeysTracker _uniqueKeysTracker;

// Constructor for standalone mode
public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyntaxException {
public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyntaxException, IOException {
_userStorageWrapper = null;
_operationMode = config.operationMode();
_startTime = System.currentTimeMillis();
Expand Down Expand Up @@ -495,7 +506,7 @@ public boolean isDestroyed() {

private static SplitHttpClient buildSplitHttpClient(String apiToken, SplitClientConfig config,
SDKMetadata sdkMetadata, RequestDecorator requestDecorator)
throws URISyntaxException {
throws URISyntaxException, IOException {
SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create()
.setSslContext(SSLContexts.createSystemDefault())
.setTlsVersions(TLS.V_1_1, TLS.V_1_2)
Expand Down Expand Up @@ -528,7 +539,26 @@ private static SplitHttpClient buildSplitHttpClient(String apiToken, SplitClient
}

if (config.authScheme() == HttpAuthScheme.KERBEROS) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(config.proxy().getHostName(), config.proxy().getPort()));
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);

Map<String, String> kerberosOptions = new HashMap<String, String>();
kerberosOptions.put("com.sun.security.auth.module.Krb5LoginModule", "required");
kerberosOptions.put("refreshKrb5Config", "false");
kerberosOptions.put("doNotPrompt", "false");
kerberosOptions.put("useTicketCache", "true");

Authenticator proxyAuthenticator = new HTTPKerberosAuthInterceptor(config.kerberosPrincipalName(), kerberosOptions);
OkHttpClient client = new Builder()
.proxy(proxy)
// .readTimeoutMillis(config.readTimeout())
.addInterceptor(logging)
.proxyAuthenticator(proxyAuthenticator)
.build();

return SplitHttpClientKerberosImpl.create(
client,
requestDecorator,
apiToken,
sdkMetadata);
Expand Down
Loading

0 comments on commit acee253

Please sign in to comment.