Skip to content
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

opa: pass headers to OPA service #62

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 18 additions & 0 deletions pkg/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,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
Loading