Skip to content

Commit 83d9000

Browse files
feat: Support apiKey for GO Feature Flag relay proxy v1.7.0 (#270)
1 parent 2d2c67e commit 83d9000

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

providers/go-feature-flag/src/main/java/dev/openfeature/contrib/providers/gofeatureflag/GoFeatureFlagProvider.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.openfeature.contrib.providers.gofeatureflag;
22

33
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
4+
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
45

56
import com.fasterxml.jackson.databind.DeserializationFeature;
67
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -52,6 +53,9 @@ public class GoFeatureFlagProvider implements FeatureProvider {
5253
// httpClient is the instance of the OkHttpClient used by the provider
5354
private OkHttpClient httpClient;
5455

56+
// apiKey contains the token to use while calling GO Feature Flag relay proxy
57+
private String apiKey;
58+
5559
/**
5660
* Constructor of the provider.
5761
*
@@ -108,6 +112,7 @@ private void initializeProvider(GoFeatureFlagProviderOptions options) throws Inv
108112
if (this.parsedEndpoint == null) {
109113
throw new InvalidEndpoint();
110114
}
115+
this.apiKey = options.getApiKey();
111116
}
112117

113118
@Override
@@ -179,15 +184,21 @@ private <T> ProviderEvaluation<T> resolveEvaluationGoFeatureFlagProxy(
179184
.addEncodedPathSegment("eval")
180185
.build();
181186

182-
Request request = new Request.Builder()
187+
Request.Builder reqBuilder = new Request.Builder()
183188
.url(url)
184189
.addHeader("Content-Type", "application/json")
185190
.post(RequestBody.create(
186191
requestMapper.writeValueAsBytes(goffRequest),
187-
MediaType.get("application/json; charset=utf-8")))
188-
.build();
192+
MediaType.get("application/json; charset=utf-8")));
189193

190-
try (Response response = this.httpClient.newCall(request).execute()) {
194+
if (this.apiKey != null && !"".equals(this.apiKey)) {
195+
reqBuilder.addHeader("Authorization", "Bearer " + this.apiKey);
196+
}
197+
198+
try (Response response = this.httpClient.newCall(reqBuilder.build()).execute()) {
199+
if (response.code() == HTTP_UNAUTHORIZED) {
200+
throw new GeneralError("invalid token used to contact GO Feature Flag relay proxy instance");
201+
}
191202
if (response.code() >= HTTP_BAD_REQUEST) {
192203
throw new GeneralError("impossible to contact GO Feature Flag relay proxy instance");
193204
}

providers/go-feature-flag/src/main/java/dev/openfeature/contrib/providers/gofeatureflag/GoFeatureFlagProviderOptions.java

+10
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,14 @@ public class GoFeatureFlagProviderOptions {
3838
*/
3939
@Getter
4040
private Long keepAliveDuration;
41+
42+
/**
43+
* (optional) If the relay proxy is configured to authenticate the requests, you should provide
44+
* an API Key to the provider.
45+
* Please ask the administrator of the relay proxy to provide an API Key.
46+
* (This feature is available only if you are using GO Feature Flag relay proxy v1.7.0 or above)
47+
* Default: null
48+
*/
49+
@Getter
50+
private String apiKey;
4151
}

providers/go-feature-flag/src/test/java/dev/openfeature/contrib/providers/gofeatureflag/GoFeatureFlagProviderTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public MockResponse dispatch(RecordedRequest request) {
4040
if (request.getPath().contains("fail_500")) {
4141
return new MockResponse().setResponseCode(500);
4242
}
43+
if (request.getPath().contains("fail_401")) {
44+
return new MockResponse().setResponseCode(401);
45+
}
4346
if (request.getPath().startsWith("/v1/feature/")) {
4447
String flagName = request.getPath().replace("/v1/feature/", "").replace("/eval", "");
4548
return new MockResponse()
@@ -119,6 +122,17 @@ void should_throw_an_error_if_endpoint_not_available() throws InvalidOptions {
119122
assertThrows(GeneralError.class, () -> g.getBooleanEvaluation("fail_500", false, this.evaluationContext));
120123
}
121124

125+
@Test
126+
void should_throw_an_error_if_invalid_api_key() throws InvalidOptions {
127+
GoFeatureFlagProvider g = new GoFeatureFlagProvider(
128+
GoFeatureFlagProviderOptions.builder()
129+
.endpoint(this.baseUrl.toString())
130+
.timeout(1000)
131+
.apiKey("invalid_api_key")
132+
.build());
133+
assertThrows(GeneralError.class, () -> g.getBooleanEvaluation("fail_401", false, this.evaluationContext));
134+
}
135+
122136
@Test
123137
void should_throw_an_error_if_flag_does_not_exists() throws InvalidOptions {
124138
GoFeatureFlagProvider g = new GoFeatureFlagProvider(GoFeatureFlagProviderOptions.builder().endpoint(this.baseUrl.toString()).timeout(1000).build());

0 commit comments

Comments
 (0)