Skip to content

Commit

Permalink
refactor to http-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal Al committed Sep 10, 2024
1 parent 9e46ad5 commit 1e9482d
Show file tree
Hide file tree
Showing 21 changed files with 1,024 additions and 473 deletions.
73 changes: 10 additions & 63 deletions client/src/main/java/io/split/client/SplitClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.split.client.impressions.ImpressionsManager;
import io.split.client.utils.FileTypeEnum;
import io.split.integrations.IntegrationsConfig;
import io.split.service.ProxyAuthScheme;
import io.split.service.SplitHttpClient;
import io.split.storages.enums.OperationMode;
import io.split.storages.enums.StorageMode;
Expand Down Expand Up @@ -93,9 +92,7 @@ public class SplitClientConfig {
private final HashSet<String> _flagSetsFilter;
private final int _invalidSets;
private final CustomHeaderDecorator _customHeaderDecorator;
private final ProxyAuthScheme _proxyAuthScheme;
private final String _proxyKerberosPrincipalName;
private final SplitHttpClient _proxyKerberosClient;
private final SplitHttpClient _alternativeHTTPModule;

public static Builder builder() {
return new Builder();
Expand Down Expand Up @@ -153,9 +150,7 @@ private SplitClientConfig(String endpoint,
HashSet<String> flagSetsFilter,
int invalidSets,
CustomHeaderDecorator customHeaderDecorator,
ProxyAuthScheme proxyAuthScheme,
String proxyKerberosPrincipalName,
SplitHttpClient proxyKerberosClient) {
SplitHttpClient alternativeHTTPModule) {
_endpoint = endpoint;
_eventsEndpoint = eventsEndpoint;
_featuresRefreshRate = pollForFeatureChangesEveryNSeconds;
Expand Down Expand Up @@ -208,9 +203,7 @@ private SplitClientConfig(String endpoint,
_flagSetsFilter = flagSetsFilter;
_invalidSets = invalidSets;
_customHeaderDecorator = customHeaderDecorator;
_proxyAuthScheme = proxyAuthScheme;
_proxyKerberosPrincipalName = proxyKerberosPrincipalName;
_proxyKerberosClient = proxyKerberosClient;
_alternativeHTTPModule = alternativeHTTPModule;

Properties props = new Properties();
try {
Expand Down Expand Up @@ -418,12 +411,8 @@ public int getInvalidSets() {
public CustomHeaderDecorator customHeaderDecorator() {
return _customHeaderDecorator;
}
public ProxyAuthScheme proxyAuthScheme() {
return _proxyAuthScheme;
}
public String proxyKerberosPrincipalName() { return _proxyKerberosPrincipalName; }

public SplitHttpClient proxyKerberosClient() { return _proxyKerberosClient; }
public SplitHttpClient alternativeHTTPModule() { return _alternativeHTTPModule; }
public static final class Builder {

private String _endpoint = SDK_ENDPOINT;
Expand Down Expand Up @@ -481,9 +470,7 @@ public static final class Builder {
private HashSet<String> _flagSetsFilter = new HashSet<>();
private int _invalidSetsCount = 0;
private CustomHeaderDecorator _customHeaderDecorator = null;
private ProxyAuthScheme _proxyAuthScheme = null;
private String _proxyKerberosPrincipalName = null;
private SplitHttpClient _proxyKerberosClient = null;
private SplitHttpClient _alternativeHTTPModule = null;

public Builder() {
}
Expand Down Expand Up @@ -979,35 +966,13 @@ public Builder customHeaderDecorator(CustomHeaderDecorator customHeaderDecorator
}

/**
* Authentication Scheme
*
* @param proxyAuthScheme
* @return this builder
*/
public Builder proxyAuthScheme(ProxyAuthScheme proxyAuthScheme) {
_proxyAuthScheme = proxyAuthScheme;
return this;
}

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

/**
* Kerberos Http Client
* Alternative Http Client
*
* @param proxyKerberosClient
* @param alternativeHTTPModule
* @return this builder
*/
public Builder proxyKerberosClient(SplitHttpClient proxyKerberosClient) {
_proxyKerberosClient = proxyKerberosClient;
public Builder alternativeHTTPModule(SplitHttpClient alternativeHTTPModule) {
_alternativeHTTPModule = alternativeHTTPModule;
return this;
}

Expand Down Expand Up @@ -1069,20 +1034,6 @@ private void verifyEndPoints() {
}
}

private void verifyAuthScheme() {
if (_proxyAuthScheme == ProxyAuthScheme.KERBEROS) {
if (proxy() == null) {
throw new IllegalStateException("Kerberos mode require Proxy parameters.");
}
if (_proxyKerberosPrincipalName == null) {
throw new IllegalStateException("Kerberos mode require Kerberos Principal Name.");
}
if (_proxyKerberosClient == null) {
throw new IllegalStateException("Kerberos mode require Kerberos Http Client.");
}
}
}

private void verifyAllModes() {
switch (_impressionsMode) {
case OPTIMIZED:
Expand Down Expand Up @@ -1148,8 +1099,6 @@ public SplitClientConfig build() {
throw new IllegalArgumentException("Number of threads for fetching segments MUST be greater than zero");
}

verifyAuthScheme();

return new SplitClientConfig(
_endpoint,
_eventsEndpoint,
Expand Down Expand Up @@ -1203,9 +1152,7 @@ public SplitClientConfig build() {
_flagSetsFilter,
_invalidSetsCount,
_customHeaderDecorator,
_proxyAuthScheme,
_proxyKerberosPrincipalName,
_proxyKerberosClient);
_alternativeHTTPModule);
}
}
}
6 changes: 3 additions & 3 deletions client/src/main/java/io/split/client/SplitFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import io.split.engine.segments.SegmentChangeFetcher;
import io.split.engine.segments.SegmentSynchronizationTaskImp;
import io.split.integrations.IntegrationsConfig;
import io.split.service.ProxyAuthScheme;
import io.split.service.SplitHttpClientImpl;
import io.split.service.SplitHttpClient;

Expand Down Expand Up @@ -191,12 +190,13 @@ public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyn

// HttpClient
_requestDecorator = new RequestDecorator(config.customHeaderDecorator());
if (config.proxyAuthScheme() != ProxyAuthScheme.KERBEROS) {
if (config.alternativeHTTPModule() == null) {
_splitHttpClient = buildSplitHttpClient(apiToken, config, _sdkMetadata, _requestDecorator);
} else {
_splitHttpClient = config.proxyKerberosClient();
_splitHttpClient = config.alternativeHTTPModule();
_splitHttpClient.setMetaData(_sdkMetadata);
_splitHttpClient.setRequestDecorator(_requestDecorator);
_splitHttpClient.setApiKey(apiToken);
}

// Roots
Expand Down
2 changes: 2 additions & 0 deletions client/src/main/java/io/split/service/SplitHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ public SplitHttpResponse post(URI uri,
public void setMetaData(SDKMetadata metadata);

public void setRequestDecorator(RequestDecorator requestDecorator);

public void setApiKey(String apiKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,9 @@ public void setMetaData(SDKMetadata metadata) {
public void setRequestDecorator(RequestDecorator requestDecorator) {
// only implemented for Kerberos client
}
@Override
public void setApiKey(String apiKey) {
// only implemented for Kerberos client
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.split.client.utils.LocalhostUtils;
import io.split.grammar.Treatments;
import io.split.service.SplitHttpClient;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down
33 changes: 0 additions & 33 deletions client/src/test/java/io/split/client/SplitClientConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.split.client.impressions.ImpressionsManager;
import io.split.client.dtos.RequestContext;
import io.split.integrations.IntegrationsConfig;
import io.split.service.ProxyAuthScheme;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -255,36 +254,4 @@ public Map<String, List<String>> getHeaderOverrides(RequestContext context) {
Assert.assertNull(config2.customHeaderDecorator());

}

@Test
public void checkExpectedAuthScheme() {
SplitClientConfig cfg = SplitClientConfig.builder()
.proxyAuthScheme(ProxyAuthScheme.KERBEROS)
.proxyKerberosPrincipalName("bilal@bilal")
.proxyHost("local")
.proxyPort(8080)
.build();
Assert.assertEquals(ProxyAuthScheme.KERBEROS, cfg.proxyAuthScheme());

cfg = SplitClientConfig.builder()
.build();
Assert.assertEquals(null, cfg.proxyAuthScheme());
}

@Test(expected = IllegalStateException.class)
public void testAuthSchemeWithoutProxy() {
SplitClientConfig.builder()
.proxyAuthScheme(ProxyAuthScheme.KERBEROS)
.proxyKerberosPrincipalName("bilal")
.build();
}

@Test(expected = IllegalStateException.class)
public void testAuthSchemeWithoutPrincipalName() {
SplitClientConfig.builder()
.proxyAuthScheme(ProxyAuthScheme.KERBEROS)
.proxyHost("local")
.proxyPort(8080)
.build();
}
}
116 changes: 1 addition & 115 deletions client/src/test/java/io/split/client/SplitFactoryImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@

import io.split.client.impressions.ImpressionsManager;
import io.split.client.utils.FileTypeEnum;
import io.split.client.utils.SDKMetadata;
import io.split.integrations.IntegrationsConfig;
import io.split.service.ProxyAuthScheme;
import io.split.service.SplitHttpClient;
import io.split.service.SplitHttpClientKerberosImpl;
import io.split.storages.enums.OperationMode;
import io.split.storages.pluggable.domain.UserStorageWrapper;
import io.split.telemetry.storage.TelemetryStorage;
import io.split.telemetry.synchronizer.TelemetrySynchronizer;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.BDDMockito;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Mockito.when;
import pluggable.CustomStorageWrapper;

import java.io.FileInputStream;
Expand All @@ -30,22 +21,9 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URISyntaxException;

import java.util.HashMap;
import java.util.Map;

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

import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SplitFactoryImpl.class)
public class SplitFactoryImplTest extends TestCase {
public static final String API_KEY ="29013ionasdasd09u";
public static final String ENDPOINT = "https://sdk.split-stage.io";
Expand Down Expand Up @@ -368,96 +346,4 @@ public void testLocalhosJsonInputStreamNullAndFileTypeNull() throws URISyntaxExc
Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig);
Assert.assertTrue(splitChangeFetcher instanceof LegacyLocalhostSplitChangeFetcher);
}

@Test
public void testBuildKerberosClientParams() throws URISyntaxException, IOException {
PowerMockito.mockStatic(SplitFactoryImpl.class);

ArgumentCaptor<Proxy> proxyCaptor = ArgumentCaptor.forClass(Proxy.class);
ArgumentCaptor<SplitClientConfig> configCaptor = ArgumentCaptor.forClass(SplitClientConfig.class);
ArgumentCaptor< HttpLoggingInterceptor> logCaptor = ArgumentCaptor.forClass( HttpLoggingInterceptor.class);
ArgumentCaptor<Authenticator> authCaptor = ArgumentCaptor.forClass(Authenticator.class);

SplitClientConfig splitClientConfig = SplitClientConfig.builder()
.setBlockUntilReadyTimeout(10000)
.proxyAuthScheme(ProxyAuthScheme.KERBEROS)
.proxyKerberosPrincipalName("bilal@localhost")
.proxyPort(6060)
.proxyHost(ENDPOINT)
.build();

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");
BDDMockito.given(SplitFactoryImpl.getProxyAuthenticator(splitClientConfig, kerberosOptions))
.willReturn(null);

RequestDecorator requestDecorator = new RequestDecorator(null);
SDKMetadata sdkmeta = new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP");
PowerMockito.when(SplitFactoryImpl.buildSplitHttpClient("qwer",
splitClientConfig,
sdkmeta,
requestDecorator)).thenCallRealMethod();

SplitHttpClient splitHttpClient = SplitFactoryImpl.buildSplitHttpClient("qwer",
splitClientConfig,
sdkmeta,
requestDecorator);

PowerMockito.verifyStatic();
SplitFactoryImpl.buildOkHttpClient(proxyCaptor.capture(), configCaptor.capture(),logCaptor.capture(), authCaptor.capture());

Assert.assertTrue(splitHttpClient instanceof SplitHttpClientKerberosImpl);
Assert.assertEquals("HTTP @ https://sdk.split-stage.io:6060", proxyCaptor.getValue().toString());
Assert.assertTrue(logCaptor.getValue() instanceof okhttp3.logging.HttpLoggingInterceptor);
}

@Test
public void testFactoryKerberosInstance() throws URISyntaxException, IOException {
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
PowerMockito.stub(PowerMockito.method(SplitFactoryImpl.class, "buildOkHttpClient")).toReturn(okHttpClient);
PowerMockito.stub(PowerMockito.method(SplitFactoryImpl.class, "getProxyAuthenticator")).toReturn(null);

SplitClientConfig splitClientConfig = SplitClientConfig.builder()
.setBlockUntilReadyTimeout(10000)
.proxyAuthScheme(ProxyAuthScheme.KERBEROS)
.proxyKerberosPrincipalName("bilal@localhost")
.proxyPort(6060)
.proxyHost(ENDPOINT)
.build();

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");

RequestDecorator requestDecorator = new RequestDecorator(null);
SDKMetadata sdkmeta = new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP");
SplitHttpClient splitHttpClient = SplitFactoryImpl.buildSplitHttpClient("qwer",
splitClientConfig,
sdkmeta,
requestDecorator);
Assert.assertTrue(splitHttpClient instanceof SplitHttpClientKerberosImpl);
}

@Test
public void testBuildOkHttpClient() {
SplitClientConfig splitClientConfig = SplitClientConfig.builder()
.setBlockUntilReadyTimeout(10000)
.proxyAuthScheme(ProxyAuthScheme.KERBEROS)
.proxyKerberosPrincipalName("bilal@localhost")
.proxyPort(6060)
.proxyHost(ENDPOINT)
.build();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("host", 8080));
OkHttpClient okHttpClient = SplitFactoryImpl.buildOkHttpClient(proxy,
splitClientConfig, loggingInterceptor, Authenticator.NONE);
assertEquals(Authenticator.NONE, okHttpClient.authenticator());
assertEquals(proxy, okHttpClient.proxy());
assertEquals(loggingInterceptor, okHttpClient.interceptors().get(0));
}
}
Loading

0 comments on commit 1e9482d

Please sign in to comment.