Skip to content

Commit

Permalink
opa: pass headers to OPA service (#64)
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander authored Nov 28, 2023
1 parent 1a5b0bb commit af79a4b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
18 changes: 18 additions & 0 deletions pkg/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ func GetUrl(header api.RequestHeaderMap) *url.URL {
}
return uri
}

// GetHeaders returns a plain map represents the headers. The returned headers won't
// contain any pseudo header like `:authority`.
func GetHeaders(header api.RequestHeaderMap) map[string][]string {
hdr := map[string][]string{}
header.Range(func(k, v string) bool {
if k[0] == ':' {
return true
}
if entry, ok := hdr[k]; !ok {
hdr[k] = []string{v}
} else {
hdr[k] = append(entry, v)
}
return true
})
return hdr
}
10 changes: 8 additions & 2 deletions plugins/opa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@ Here is the JSON data Moe sends to the OPA:
"b": [""]
},
"method": "GET",
"host": "localhost"
"host": "localhost:10000",
"headers": {
"fruit": ["apple", "banana"],
"pet": ["dog"]
}
}
}
}
```

Note that:

* `method` is always uppercase, while `host` and `scheme` are always lowecase.
* `method` is always uppercase, while `host`, `headers` and `scheme` are always lowecase.
* `host` will contain the port if the `:authority` header sent by the client has the port.
* Multiple `headers` and `query` in the same name will be passed in an array.

The data can be read as `input` document in OPA.

Expand Down
10 changes: 6 additions & 4 deletions plugins/opa/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ var opaResponse struct {

func (f *filter) buildInput(header api.RequestHeaderMap) map[string]interface{} {
uri := request.GetUrl(header)
headers := request.GetHeaders(header)
req := map[string]interface{}{
"method": header.Method(),
"scheme": header.Scheme(),
"host": header.Host(),
"path": uri.Path,
"method": header.Method(),
"scheme": header.Scheme(),
"host": header.Host(),
"path": uri.Path,
"headers": headers,
}
if uri.RawQuery != "" {
req["query"] = map[string][]string(uri.Query())
Expand Down
6 changes: 6 additions & 0 deletions plugins/opa/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func TestOpaRemote(t *testing.T) {
})(cb)
hdr := envoy.NewRequestHeaderMap(http.Header(map[string][]string{
":path": {"/?a=1&b&c=true&c=foo"},
"pet": {"cat"},
"fruit": {"apple", "banana"},
}))

tests := []struct {
Expand All @@ -53,6 +55,10 @@ func TestOpaRemote(t *testing.T) {
"b": []interface{}{""},
"c": []interface{}{"true", "foo"},
},
"headers": map[string]interface{}{
"pet": []interface{}{"cat"},
"fruit": []interface{}{"apple", "banana"},
},
}, input["input"].(map[string]interface{})["request"])
},
},
Expand Down

0 comments on commit af79a4b

Please sign in to comment.