Skip to content

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

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 1 commit 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
26 changes: 26 additions & 0 deletions providers/go-feature-flag/pkg/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Provider struct {
httpClient HTTPClient
endpoint string
goFeatureFlagInstance *client.GoFeatureFlag
apiKey string
}

// HTTPClient is a custom interface to be able to override it by any implementation
Expand Down Expand Up @@ -64,6 +65,7 @@ func NewProvider(options ProviderOptions) (*Provider, error) {
}

return &Provider{
apiKey: options.APIKey,
endpoint: options.Endpoint,
httpClient: httpClient,
}, nil
Expand Down Expand Up @@ -276,6 +278,9 @@ func evaluateWithRelayProxy[T model.JsonType](provider *Provider, ctx context.Co
}
}
goffRequest.Header.Set("Content-Type", "application/json")
if provider.apiKey != "" {
goffRequest.Header.Set("Authorization", fmt.Sprintf("Bearer %s", provider.apiKey))
}

response, err := provider.httpClient.Do(goffRequest)
if err != nil {
Expand All @@ -298,6 +303,27 @@ func evaluateWithRelayProxy[T model.JsonType](provider *Provider, ctx context.Co
}
}

if response.StatusCode == http.StatusUnauthorized {
return model.GenericResolutionDetail[T]{
Value: defaultValue,
ProviderResolutionDetail: of.ProviderResolutionDetail{
ResolutionError: of.NewGeneralResolutionError(
"invalid token used to contact GO Feature Flag relay proxy instance"),
Reason: of.ErrorReason,
},
}
}
if response.StatusCode >= http.StatusBadRequest {
return model.GenericResolutionDetail[T]{
Value: defaultValue,
ProviderResolutionDetail: of.ProviderResolutionDetail{
ResolutionError: of.NewGeneralResolutionError(
"unexpected answer from the relay proxy"),
Reason: of.ErrorReason,
},
}
}

var evalResponse model.EvalResponse[T]
err = json.Unmarshal(responseStr, &evalResponse)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions providers/go-feature-flag/pkg/provider_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ type ProviderOptions struct {
// GOFeatureFlagConfig is the configuration struct for the GO Feature Flag module.
// If not nil we will launch the provider using the GO Feature Flag module.
GOFeatureFlagConfig *ffclient.Config

// APIKey (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
APIKey string
}
29 changes: 29 additions & 0 deletions providers/go-feature-flag/pkg/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ type mockClient struct{}
func (m *mockClient) Do(req *http.Request) (*http.Response, error) {
mockPath := "../testutils/mock_responses/%s.json"
flagName := strings.Replace(strings.Replace(req.URL.Path, "/v1/feature/", "", -1), "/eval", "", -1)

if flagName == "unauthorized" {
return &http.Response{
StatusCode: http.StatusUnauthorized,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}

content, err := os.ReadFile(fmt.Sprintf(mockPath, flagName))
if err != nil {
content, _ = os.ReadFile(fmt.Sprintf(mockPath, "flag_not_found"))
Expand Down Expand Up @@ -65,6 +73,27 @@ func TestProvider_BooleanEvaluation(t *testing.T) {
args args
want of.BooleanEvaluationDetails
}{
{
name: "unauthorized flag",
args: args{
flag: "unauthorized",
defaultValue: false,
evalCtx: defaultEvaluationCtx(),
},
want: of.BooleanEvaluationDetails{
Value: false,
EvaluationDetails: of.EvaluationDetails{
FlagKey: "unauthorized",
FlagType: of.Boolean,
ResolutionDetail: of.ResolutionDetail{
Variant: "",
Reason: of.ErrorReason,
ErrorCode: of.GeneralCode,
ErrorMessage: "invalid token used to contact GO Feature Flag relay proxy instance",
},
},
},
},
{
name: "should resolve a valid boolean flag with TARGETING_MATCH reason",
args: args{
Expand Down