Skip to content

Commit

Permalink
added enum for AuthScheme
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal Al committed Aug 13, 2024
1 parent c1933cb commit 077accd
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 50 deletions.
18 changes: 6 additions & 12 deletions client/src/main/java/io/split/client/SplitClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.split.integrations.IntegrationsConfig;
import io.split.storages.enums.OperationMode;
import io.split.storages.enums.StorageMode;
import io.split.service.HttpAuthScheme;
import org.apache.hc.core5.http.HttpHost;
import pluggable.CustomStorageWrapper;

Expand Down Expand Up @@ -92,7 +93,7 @@ public class SplitClientConfig {
private final HashSet<String> _flagSetsFilter;
private final int _invalidSets;
private final CustomHeaderDecorator _customHeaderDecorator;
private final String _authScheme;
private final HttpAuthScheme _authScheme;


public static Builder builder() {
Expand Down Expand Up @@ -151,7 +152,7 @@ private SplitClientConfig(String endpoint,
HashSet<String> flagSetsFilter,
int invalidSets,
CustomHeaderDecorator customHeaderDecorator,
String authScheme) {
HttpAuthScheme authScheme) {
_endpoint = endpoint;
_eventsEndpoint = eventsEndpoint;
_featuresRefreshRate = pollForFeatureChangesEveryNSeconds;
Expand Down Expand Up @@ -412,7 +413,7 @@ public int getInvalidSets() {
public CustomHeaderDecorator customHeaderDecorator() {
return _customHeaderDecorator;
}
public String authScheme() {
public HttpAuthScheme authScheme() {
return _authScheme;
}

Expand Down Expand Up @@ -473,7 +474,7 @@ public static final class Builder {
private HashSet<String> _flagSetsFilter = new HashSet<>();
private int _invalidSetsCount = 0;
private CustomHeaderDecorator _customHeaderDecorator = null;
private String _authScheme = null;
private HttpAuthScheme _authScheme = null;

public Builder() {
}
Expand Down Expand Up @@ -974,7 +975,7 @@ public Builder customHeaderDecorator(CustomHeaderDecorator customHeaderDecorator
* @param authScheme
* @return this builder
*/
public Builder authScheme(String authScheme) {
public Builder authScheme(HttpAuthScheme authScheme) {
_authScheme = authScheme;
return this;
}
Expand Down Expand Up @@ -1087,13 +1088,6 @@ public SplitClientConfig build() {
_storageMode = StorageMode.PLUGGABLE;
}

if(_authScheme != null) {
if (!_authScheme.toLowerCase(Locale.ROOT).equals("kerberos")) {
throw new IllegalArgumentException("authScheme must be either null or `kerberos`.");
}
_authScheme = "kerberos";
}

return new SplitClientConfig(
_endpoint,
_eventsEndpoint,
Expand Down
3 changes: 2 additions & 1 deletion client/src/main/java/io/split/client/SplitFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import io.split.engine.segments.SegmentChangeFetcher;
import io.split.engine.segments.SegmentSynchronizationTaskImp;
import io.split.integrations.IntegrationsConfig;
import io.split.service.HttpAuthScheme;
import io.split.service.SplitHttpClient;
import io.split.service.SplitHttpClientImpl;
import io.split.service.SplitHttpClientKerberosImpl;
Expand Down Expand Up @@ -526,7 +527,7 @@ private static SplitHttpClient buildSplitHttpClient(String apiToken, SplitClient
httpClientbuilder = setupProxy(httpClientbuilder, config);
}

if (config.authScheme() != null) {
if (config.authScheme() == HttpAuthScheme.KERBEROS) {
return SplitHttpClientKerberosImpl.create(
requestDecorator,
apiToken,
Expand Down
5 changes: 5 additions & 0 deletions client/src/main/java/io/split/service/HttpAuthScheme.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.split.service;

public enum HttpAuthScheme {
KERBEROS
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class SplitHttpClientKerberosImpl implements SplitHttpClient {

private static final Logger _log = LoggerFactory.getLogger(SplitHttpClient.class);
private static final Logger _log = LoggerFactory.getLogger(SplitHttpClientKerberosImpl.class);
private static final String HEADER_CACHE_CONTROL_NAME = "Cache-Control";
private static final String HEADER_CACHE_CONTROL_VALUE = "no-cache";
private static final String HEADER_API_KEY = "Authorization";
Expand Down Expand Up @@ -54,7 +54,7 @@ public synchronized SplitHttpResponse get(URI uri, FetchOptions options, Map<Str
HttpURLConnection getHttpURLConnection = null;
try {
getHttpURLConnection = (HttpURLConnection) uri.toURL().openConnection();
return _get(getHttpURLConnection, options, additionalHeaders);
return doGet(getHttpURLConnection, options, additionalHeaders);
} catch (Exception e) {
throw new IllegalStateException(String.format("Problem in http get operation: %s", e), e);
} finally {
Expand All @@ -67,11 +67,10 @@ public synchronized SplitHttpResponse get(URI uri, FetchOptions options, Map<Str
}
}
}
public SplitHttpResponse _get(HttpURLConnection getHttpURLConnection, FetchOptions options, Map<String, List<String>> additionalHeaders) {
public SplitHttpResponse doGet(HttpURLConnection getHttpURLConnection, FetchOptions options, Map<String, List<String>> additionalHeaders) {
InputStreamReader inputStreamReader = null;
try {
getHttpURLConnection.setRequestMethod("GET");

setBasicHeaders(getHttpURLConnection);
setAdditionalAndDecoratedHeaders(getHttpURLConnection, additionalHeaders);

Expand Down Expand Up @@ -125,7 +124,7 @@ public synchronized SplitHttpResponse post(URI uri, HttpEntity entity, Map<Strin
HttpURLConnection postHttpURLConnection = null;
try {
postHttpURLConnection = (HttpURLConnection) uri.toURL().openConnection();
return _post(postHttpURLConnection, entity, additionalHeaders);
return doPost(postHttpURLConnection, entity, additionalHeaders);
} catch (Exception e) {
throw new IllegalStateException(String.format("Problem in http post operation: %s", e), e);
} finally {
Expand All @@ -139,18 +138,15 @@ public synchronized SplitHttpResponse post(URI uri, HttpEntity entity, Map<Strin
}
}

public SplitHttpResponse _post(HttpURLConnection postHttpURLConnection,
public SplitHttpResponse doPost(HttpURLConnection postHttpURLConnection,
HttpEntity entity,
Map<String, List<String>> additionalHeaders)
throws IOException {
Map<String, List<String>> additionalHeaders) {
try {
postHttpURLConnection.setRequestMethod("POST");
setBasicHeaders(postHttpURLConnection);
setAdditionalAndDecoratedHeaders(postHttpURLConnection, additionalHeaders);

if (postHttpURLConnection.getHeaderField("Accept-Encoding") == null) {
postHttpURLConnection.setRequestProperty("Accept-Encoding", "gzip");
}
postHttpURLConnection.setRequestProperty("Accept-Encoding", "gzip");
postHttpURLConnection.setRequestProperty("Content-Type", "application/json");
_log.debug(String.format("Request Headers: %s", postHttpURLConnection.getRequestProperties()));

Expand Down Expand Up @@ -198,7 +194,6 @@ private void setAdditionalAndDecoratedHeaders(HttpURLConnection urlConnection, M
for (Header header : request.getHeaders()) {
urlConnection.setRequestProperty(header.getName(), header.getValue());
}
request = null;
}

private Header[] getResponseHeaders(HttpURLConnection urlConnection) {
Expand All @@ -211,7 +206,6 @@ private Header[] getResponseHeaders(HttpURLConnection urlConnection) {
}
}
return responseHeaders.toArray(new Header[0]);

}
@Override
public void close() throws IOException {
Expand Down
18 changes: 3 additions & 15 deletions client/src/test/java/io/split/client/SplitClientConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.split.client.impressions.ImpressionsManager;
import io.split.client.dtos.RequestContext;
import io.split.integrations.IntegrationsConfig;
import io.split.service.HttpAuthScheme;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -258,24 +259,11 @@ public Map<String, List<String>> getHeaderOverrides(RequestContext context) {
@Test
public void checkExpectedAuthScheme() {
SplitClientConfig cfg = SplitClientConfig.builder()
.authScheme("kerberos")
.authScheme(HttpAuthScheme.KERBEROS)
.build();
Assert.assertEquals("kerberos", cfg.authScheme());
Assert.assertEquals(HttpAuthScheme.KERBEROS, cfg.authScheme());

cfg = SplitClientConfig.builder()
.authScheme("KERberos")
.build();
Assert.assertEquals("kerberos", cfg.authScheme());

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

@Test(expected = IllegalArgumentException.class)
public void checkUnexpectedAuthScheme() {
SplitClientConfig cfg = SplitClientConfig.builder()
.authScheme("proxy")
.build();
Assert.assertEquals(null, cfg.authScheme());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.split.client.utils.FileTypeEnum;
import io.split.client.utils.SDKMetadata;
import io.split.integrations.IntegrationsConfig;
import io.split.service.HttpAuthScheme;
import io.split.service.SplitHttpClientKerberosImpl;
import io.split.storages.enums.OperationMode;
import io.split.storages.pluggable.domain.UserStorageWrapper;
Expand Down Expand Up @@ -353,7 +354,7 @@ public void testLocalhosJsonInputStreamNullAndFileTypeNull() throws URISyntaxExc
public void testFactoryKerberosInstance() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
SplitClientConfig splitClientConfig = SplitClientConfig.builder()
.setBlockUntilReadyTimeout(10000)
.authScheme("kerberos")
.authScheme(HttpAuthScheme.KERBEROS)
.build();
SplitFactoryImpl splitFactory = new SplitFactoryImpl("asdf", splitClientConfig);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testGetWithSpecialCharacters() throws URISyntaxException, IOExceptio
Map<String, List<String>> additionalHeaders = Collections.singletonMap("AdditionalHeader",
Collections.singletonList("add"));

SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._get(mockHttpURLConnection,
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.doGet(mockHttpURLConnection,
new FetchOptions.Builder().cacheControlHeaders(true).build(), additionalHeaders);
SplitChange change = Json.fromJson(splitHttpResponse.body(), SplitChange.class);

Expand Down Expand Up @@ -84,7 +84,7 @@ public void testGetParameters() throws URISyntaxException, MalformedURLException
ArgumentCaptor<HttpURLConnection> connectionCaptor = ArgumentCaptor.forClass(HttpURLConnection.class);
ArgumentCaptor<FetchOptions> optionsCaptor = ArgumentCaptor.forClass(FetchOptions.class);
ArgumentCaptor<HashMap> headersCaptor = ArgumentCaptor.forClass(HashMap.class);
verify(splitHtpClientKerberos)._get(connectionCaptor.capture(), optionsCaptor.capture(), headersCaptor.capture());
verify(splitHtpClientKerberos).doGet(connectionCaptor.capture(), optionsCaptor.capture(), headersCaptor.capture());

assertThat(connectionCaptor.getValue().getRequestMethod(), is(equalTo("GET")));
assertThat(connectionCaptor.getValue().getURL().toString(), is(equalTo(new URL("https://api.split.io/splitChanges?since=1234567").toString())));
Expand All @@ -103,7 +103,7 @@ public void testGetError() throws URISyntaxException, IOException {
when(mockHttpURLConnection.getInputStream()).thenReturn(stubInputStream);

SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(decorator, "qwerty", metadata());
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._get(mockHttpURLConnection,
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.doGet(mockHttpURLConnection,
new FetchOptions.Builder().cacheControlHeaders(true).build(), null);
Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, (long) splitHttpResponse.statusCode());
}
Expand All @@ -120,7 +120,7 @@ public void testException() throws URISyntaxException, InvocationTargetException
when(mockHttpURLConnection.getInputStream()).thenReturn(stubInputStream);

SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(decorator, "qwerty", metadata());
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._get(mockHttpURLConnection,
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.doGet(mockHttpURLConnection,
new FetchOptions.Builder().cacheControlHeaders(true).build(), null);
}

Expand Down Expand Up @@ -149,7 +149,7 @@ public void testPost() throws URISyntaxException, IOException, ParseException {
ByteArrayOutputStream mockOs = Mockito.mock( ByteArrayOutputStream.class);
when(mockHttpURLConnection.getOutputStream()).thenReturn(mockOs);

SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._post(mockHttpURLConnection, Utils.toJsonEntity(toSend),
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.doPost(mockHttpURLConnection, Utils.toJsonEntity(toSend),
additionalHeaders);

// Capture outgoing request and validate it
Expand All @@ -176,7 +176,7 @@ public void testPotParameters() throws URISyntaxException, IOException {
ArgumentCaptor<HttpURLConnection> connectionCaptor = ArgumentCaptor.forClass(HttpURLConnection.class);
ArgumentCaptor<HttpEntity> entityCaptor = ArgumentCaptor.forClass(HttpEntity.class);
ArgumentCaptor<HashMap> headersCaptor = ArgumentCaptor.forClass(HashMap.class);
verify(splitHtpClientKerberos)._post(connectionCaptor.capture(), entityCaptor.capture(), headersCaptor.capture());
verify(splitHtpClientKerberos).doPost(connectionCaptor.capture(), entityCaptor.capture(), headersCaptor.capture());

assertThat(connectionCaptor.getValue().getURL().toString(), is(equalTo(new URL("https://kubernetesturl.com/split/api/testImpressions/bulk").toString())));
}
Expand All @@ -190,7 +190,7 @@ public void testPosttError() throws URISyntaxException, IOException {
when(mockHttpURLConnection.getOutputStream()).thenReturn(mockOs);

SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(decorator, "qwerty", metadata());
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._post(mockHttpURLConnection,
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.doPost(mockHttpURLConnection,
Utils.toJsonEntity(Arrays.asList(new String[] { "A", "B", "C", "D" })), null);

Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, (long) splitHttpResponse.statusCode());
Expand All @@ -203,7 +203,7 @@ public void testPosttException() throws URISyntaxException, IOException {
Mockito.when(mockHttpURLConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);

SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(decorator, "qwerty", metadata());
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._post(mockHttpURLConnection,
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.doPost(mockHttpURLConnection,
Utils.toJsonEntity(Arrays.asList(new String[] { "A", "B", "C", "D" })), null);
}

Expand Down

0 comments on commit 077accd

Please sign in to comment.