diff --git a/client/src/main/java/io/split/client/SplitClientConfig.java b/client/src/main/java/io/split/client/SplitClientConfig.java index 43695d8d..2c305a13 100644 --- a/client/src/main/java/io/split/client/SplitClientConfig.java +++ b/client/src/main/java/io/split/client/SplitClientConfig.java @@ -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; @@ -92,7 +93,7 @@ public class SplitClientConfig { private final HashSet _flagSetsFilter; private final int _invalidSets; private final CustomHeaderDecorator _customHeaderDecorator; - private final String _authScheme; + private final HttpAuthScheme _authScheme; public static Builder builder() { @@ -151,7 +152,7 @@ private SplitClientConfig(String endpoint, HashSet flagSetsFilter, int invalidSets, CustomHeaderDecorator customHeaderDecorator, - String authScheme) { + HttpAuthScheme authScheme) { _endpoint = endpoint; _eventsEndpoint = eventsEndpoint; _featuresRefreshRate = pollForFeatureChangesEveryNSeconds; @@ -412,7 +413,7 @@ public int getInvalidSets() { public CustomHeaderDecorator customHeaderDecorator() { return _customHeaderDecorator; } - public String authScheme() { + public HttpAuthScheme authScheme() { return _authScheme; } @@ -473,7 +474,7 @@ public static final class Builder { private HashSet _flagSetsFilter = new HashSet<>(); private int _invalidSetsCount = 0; private CustomHeaderDecorator _customHeaderDecorator = null; - private String _authScheme = null; + private HttpAuthScheme _authScheme = null; public Builder() { } @@ -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; } @@ -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, diff --git a/client/src/main/java/io/split/client/SplitFactoryImpl.java b/client/src/main/java/io/split/client/SplitFactoryImpl.java index 0dc96730..68eeba67 100644 --- a/client/src/main/java/io/split/client/SplitFactoryImpl.java +++ b/client/src/main/java/io/split/client/SplitFactoryImpl.java @@ -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; @@ -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, diff --git a/client/src/main/java/io/split/service/HttpAuthScheme.java b/client/src/main/java/io/split/service/HttpAuthScheme.java new file mode 100644 index 00000000..1753f736 --- /dev/null +++ b/client/src/main/java/io/split/service/HttpAuthScheme.java @@ -0,0 +1,5 @@ +package io.split.service; + +public enum HttpAuthScheme { + KERBEROS +} diff --git a/client/src/main/java/io/split/service/SplitHttpClientKerberosImpl.java b/client/src/main/java/io/split/service/SplitHttpClientKerberosImpl.java index 3536aa0a..83f31ee7 100644 --- a/client/src/main/java/io/split/service/SplitHttpClientKerberosImpl.java +++ b/client/src/main/java/io/split/service/SplitHttpClientKerberosImpl.java @@ -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"; @@ -54,7 +54,7 @@ public synchronized SplitHttpResponse get(URI uri, FetchOptions options, Map> additionalHeaders) { + public SplitHttpResponse doGet(HttpURLConnection getHttpURLConnection, FetchOptions options, Map> additionalHeaders) { InputStreamReader inputStreamReader = null; try { getHttpURLConnection.setRequestMethod("GET"); - setBasicHeaders(getHttpURLConnection); setAdditionalAndDecoratedHeaders(getHttpURLConnection, additionalHeaders); @@ -125,7 +124,7 @@ public synchronized SplitHttpResponse post(URI uri, HttpEntity entity, Map> additionalHeaders) - throws IOException { + Map> 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())); @@ -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) { @@ -211,7 +206,6 @@ private Header[] getResponseHeaders(HttpURLConnection urlConnection) { } } return responseHeaders.toArray(new Header[0]); - } @Override public void close() throws IOException { diff --git a/client/src/test/java/io/split/client/SplitClientConfigTest.java b/client/src/test/java/io/split/client/SplitClientConfigTest.java index d323ebe2..86b18541 100644 --- a/client/src/test/java/io/split/client/SplitClientConfigTest.java +++ b/client/src/test/java/io/split/client/SplitClientConfigTest.java @@ -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; @@ -258,24 +259,11 @@ public Map> 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()); } diff --git a/client/src/test/java/io/split/client/SplitFactoryImplTest.java b/client/src/test/java/io/split/client/SplitFactoryImplTest.java index 00e9f626..57441ced 100644 --- a/client/src/test/java/io/split/client/SplitFactoryImplTest.java +++ b/client/src/test/java/io/split/client/SplitFactoryImplTest.java @@ -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; @@ -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); diff --git a/client/src/test/java/io/split/service/HttpSplitClientKerberosTest.java b/client/src/test/java/io/split/service/HttpSplitClientKerberosTest.java index 74f95874..4f814c31 100644 --- a/client/src/test/java/io/split/service/HttpSplitClientKerberosTest.java +++ b/client/src/test/java/io/split/service/HttpSplitClientKerberosTest.java @@ -53,7 +53,7 @@ public void testGetWithSpecialCharacters() throws URISyntaxException, IOExceptio Map> 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); @@ -84,7 +84,7 @@ public void testGetParameters() throws URISyntaxException, MalformedURLException ArgumentCaptor connectionCaptor = ArgumentCaptor.forClass(HttpURLConnection.class); ArgumentCaptor optionsCaptor = ArgumentCaptor.forClass(FetchOptions.class); ArgumentCaptor 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()))); @@ -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()); } @@ -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); } @@ -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 @@ -176,7 +176,7 @@ public void testPotParameters() throws URISyntaxException, IOException { ArgumentCaptor connectionCaptor = ArgumentCaptor.forClass(HttpURLConnection.class); ArgumentCaptor entityCaptor = ArgumentCaptor.forClass(HttpEntity.class); ArgumentCaptor 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()))); } @@ -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()); @@ -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); }