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
|
||||
|
||||
Reference in New Issue
Block a user