Skip to content

feat: Support apiKey for GO Feature Flag relay proxy v1.7.0 #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.openfeature.contrib.providers.gofeatureflag;

import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;

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

// apiKey contains the token to use while calling GO Feature Flag relay proxy
private String apiKey;

/**
* Constructor of the provider.
*
Expand Down Expand Up @@ -108,6 +112,7 @@ private void initializeProvider(GoFeatureFlagProviderOptions options) throws Inv
if (this.parsedEndpoint == null) {
throw new InvalidEndpoint();
}
this.apiKey = options.getApiKey();
}

@Override
Expand Down Expand Up @@ -179,15 +184,21 @@ private <T> ProviderEvaluation<T> resolveEvaluationGoFeatureFlagProxy(
.addEncodedPathSegment("eval")
.build();

Request request = new Request.Builder()
Request.Builder reqBuilder = new Request.Builder()
.url(url)
.addHeader("Content-Type", "application/json")
.post(RequestBody.create(
requestMapper.writeValueAsBytes(goffRequest),
MediaType.get("application/json; charset=utf-8")))
.build();
MediaType.get("application/json; charset=utf-8")));

try (Response response = this.httpClient.newCall(request).execute()) {
if (this.apiKey != null && !"".equals(this.apiKey)) {
reqBuilder.addHeader("Authorization", "Bearer " + this.apiKey);
}

try (Response response = this.httpClient.newCall(reqBuilder.build()).execute()) {
if (response.code() == HTTP_UNAUTHORIZED) {
throw new GeneralError("invalid token used to contact GO Feature Flag relay proxy instance");
}
if (response.code() >= HTTP_BAD_REQUEST) {
throw new GeneralError("impossible to contact GO Feature Flag relay proxy instance");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ public class GoFeatureFlagProviderOptions {
*/
@Getter
private Long keepAliveDuration;

/**
* (optional) If the relay proxy is configured to authenticate the requests, you should provide
* an API Key to the provider.
* Please ask the administrator of the relay proxy to provide an API Key.
* (This feature is available only if you are using GO Feature Flag relay proxy v1.7.0 or above)
* Default: null
*/
@Getter
private String apiKey;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public MockResponse dispatch(RecordedRequest request) {
if (request.getPath().contains("fail_500")) {
return new MockResponse().setResponseCode(500);
}
if (request.getPath().contains("fail_401")) {
return new MockResponse().setResponseCode(401);
}
if (request.getPath().startsWith("/v1/feature/")) {
String flagName = request.getPath().replace("/v1/feature/", "").replace("/eval", "");
return new MockResponse()
Expand Down Expand Up @@ -119,6 +122,17 @@ void should_throw_an_error_if_endpoint_not_available() throws InvalidOptions {
assertThrows(GeneralError.class, () -> g.getBooleanEvaluation("fail_500", false, this.evaluationContext));
}

@Test
void should_throw_an_error_if_invalid_api_key() throws InvalidOptions {
GoFeatureFlagProvider g = new GoFeatureFlagProvider(
GoFeatureFlagProviderOptions.builder()
.endpoint(this.baseUrl.toString())
.timeout(1000)
.apiKey("invalid_api_key")
.build());
assertThrows(GeneralError.class, () -> g.getBooleanEvaluation("fail_401", false, this.evaluationContext));
}

@Test
void should_throw_an_error_if_flag_does_not_exists() throws InvalidOptions {
GoFeatureFlagProvider g = new GoFeatureFlagProvider(GoFeatureFlagProviderOptions.builder().endpoint(this.baseUrl.toString()).timeout(1000).build());
Expand Down