Team LBAC: Move middleware to enterprise (#76969)

* Team LBAC: Move middleware to enterprise

* Remove ds proxy part

* Move utils to enterprise
This commit is contained in:
Alexander Zobnin
2023-10-24 14:06:18 +03:00
committed by GitHub
parent 575981201c
commit cad3c43bb1
5 changed files with 2 additions and 303 deletions
-63
View File
@@ -4,13 +4,10 @@ import (
"fmt"
"net"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"github.com/grafana/grafana/pkg/services/auth/identity"
"github.com/grafana/grafana/pkg/services/datasources"
)
const (
@@ -133,63 +130,3 @@ func ApplyForwardIDHeader(req *http.Request, user identity.Requester) {
req.Header.Set(IDHeaderName, token)
}
}
func ApplyTeamHTTPHeaders(req *http.Request, ds *datasources.DataSource, teams []int64) error {
headers, err := GetTeamHTTPHeaders(ds, teams)
if err != nil {
return err
}
for header, value := range headers {
// check if headerv is already set in req.Header
if req.Header.Get(header) != "" {
req.Header.Add(header, value)
} else {
req.Header.Set(header, value)
}
}
return nil
}
func GetTeamHTTPHeaders(ds *datasources.DataSource, teams []int64) (map[string]string, error) {
teamHTTPHeadersMap := make(map[string]string)
teamHTTPHeaders, err := ds.TeamHTTPHeaders()
if err != nil {
return nil, err
}
for teamID, headers := range teamHTTPHeaders {
id, err := strconv.ParseInt(teamID, 10, 64)
if err != nil {
// FIXME: logging here
continue
}
if !contains(teams, id) {
continue
}
for _, header := range headers {
// Header values should be properly escaped.
if value, ok := teamHTTPHeadersMap[header.Header]; ok {
// Add multiple header values as a comma-separated strings according to RFC 7230
// https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.6
teamHTTPHeadersMap[header.Header] = fmt.Sprintf("%s,%s", value, url.PathEscape(header.Value))
} else {
teamHTTPHeadersMap[header.Header] = url.PathEscape(header.Value)
}
}
}
return teamHTTPHeadersMap, nil
}
func contains(slice []int64, value int64) bool {
for _, v := range slice {
if v == value {
return true
}
}
return false
}
-94
View File
@@ -6,8 +6,6 @@ import (
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/user"
)
@@ -205,95 +203,3 @@ func TestApplyUserHeader(t *testing.T) {
require.Equal(t, "admin", req.Header.Get("X-Grafana-User"))
})
}
func TestApplyteamHTTPHeaders(t *testing.T) {
testCases := []struct {
desc string
jsonData any
userTeams []int64
want map[string]string
}{
{
desc: "Should apply team headers for users teams",
jsonData: map[string]interface{}{
"1": []map[string]interface{}{
{
"header": "X-Team-Header",
"value": "1",
},
},
"2": []map[string]interface{}{
{
"header": "X-Prom-Label-Policy",
"value": "2",
},
},
// user is not part of this team
"3": []map[string]interface{}{
{
"header": "X-Custom-Label-Policy",
"value": "3",
},
},
},
userTeams: []int64{1, 2},
want: map[string]string{
"X-Team-Header": "1",
"X-Prom-Label-Policy": "2",
},
},
{
desc: "Should be able to parse header values with commas",
jsonData: map[string]interface{}{
"101": []map[string]interface{}{
{
"header": "X-Prom-Label-Policy",
"value": `1234:{ foo="bar", bar="baz" }`,
},
},
},
userTeams: []int64{101},
want: map[string]string{
"X-Prom-Label-Policy": "1234:%7B%20foo=%22bar%22%2C%20bar=%22baz%22%20%7D",
},
}, {
desc: "Should be able to handle multiple header values",
jsonData: map[string]interface{}{
"101": []map[string]interface{}{
{
"header": "X-Prom-Label-Policy",
"value": `1234:{ foo="bar" }`,
},
{
"header": "X-Prom-Label-Policy",
"value": `1234:{ bar="baz" }`,
},
},
},
userTeams: []int64{101},
want: map[string]string{
"X-Prom-Label-Policy": "1234:%7B%20foo=%22bar%22%20%7D,1234:%7B%20bar=%22baz%22%20%7D",
},
},
}
for _, testCase := range testCases {
t.Run("Should apply team headers for users teams", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
ds := &datasources.DataSource{
JsonData: simplejson.New(),
}
// add team headers
ds.JsonData.Set("teamHttpHeaders", testCase.jsonData)
err = ApplyTeamHTTPHeaders(req, ds, testCase.userTeams)
require.NoError(t, err)
for header, value := range testCase.want {
require.Contains(t, req.Header, header)
require.Equal(t, value, req.Header.Get(header))
}
})
}
}