OpenFeature: Fix bulk evaluation request postprocessing (#108155)
This commit is contained in:
@@ -1,15 +1,20 @@
|
||||
package ofrep
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/util/proxyutil"
|
||||
)
|
||||
|
||||
@@ -22,20 +27,31 @@ func (b *APIBuilder) proxyAllFlagReq(isAuthedUser bool, w http.ResponseWriter, r
|
||||
|
||||
proxy.ModifyResponse = func(resp *http.Response) error {
|
||||
if resp.StatusCode == http.StatusOK && !isAuthedUser {
|
||||
var result map[string]interface{}
|
||||
var result featuremgmt.OFREPBulkResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
|
||||
filtered := make(map[string]any)
|
||||
for k, v := range result {
|
||||
if isPublicFlag(k) {
|
||||
filtered[k] = v
|
||||
var filteredFlags []featuremgmt.OFREPFlag
|
||||
for _, f := range result.Flags {
|
||||
if isPublicFlag(f.Key) {
|
||||
filteredFlags = append(filteredFlags, f)
|
||||
}
|
||||
}
|
||||
|
||||
writeResponse(http.StatusOK, filtered, b.logger, w)
|
||||
result.Flags = filteredFlags
|
||||
newBodyBytes, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
logger.Error("Failed to encode filtered result", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Replace the body
|
||||
resp.Body = io.NopCloser(bytes.NewReader(newBodyBytes))
|
||||
resp.ContentLength = int64(len(newBodyBytes))
|
||||
resp.Header.Set("Content-Length", strconv.Itoa(len(newBodyBytes)))
|
||||
resp.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -98,7 +98,7 @@ type OFREPBulkResponse struct {
|
||||
// OFREPFlag represents a single flag in the bulk response
|
||||
type OFREPFlag struct {
|
||||
Key string `json:"key"`
|
||||
Value bool `json:"value"`
|
||||
Value any `json:"value"`
|
||||
Reason string `json:"reason"`
|
||||
Variant string `json:"variant,omitempty"`
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
|
||||
@@ -80,7 +80,7 @@ ABCD = true
|
||||
|
||||
openFeatureEnabledFlags := map[string]bool{}
|
||||
for _, flag := range allFlags.Flags {
|
||||
if flag.Value {
|
||||
if v, ok := flag.Value.(bool); ok && v {
|
||||
openFeatureEnabledFlags[flag.Key] = true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user