diff --git a/README.md b/README.md index 8ebd4760568379c507ec3f3eebd0b21fa15b3916..a2fae4887d045376eb966c665fabe2928402a95f 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,15 @@ can be mapped and used for evaluating all kinds of different policies. Without a package naming rule, there's no way the service can automatically generate HTTP endpoints for working with arbitrary dynamically uploaded policies. +### Access HTTP Headers inside a policy + +HTTP Request Headers are passed to the evaluation runtime on each request. One could access any header by name within +the Rego source code using `input.header.name` or `input.header["name"]`. + +##### **Important:** +The key `header` is forbidden for request body on the `evaluation` endpoint. Sending a `header` key in +the request body results in `400 Bad Request` response status code and the server will not process the request. + ### Policy Extensions Functions A brief documentation for the available Rego extensions functions diff --git a/internal/service/policy/service.go b/internal/service/policy/service.go index 4f67535fb557c84917fa26bbfc88266feb97cb24..6eea0de53af3bee20a05ccb16173f6bad5fd7a04 100644 --- a/internal/service/policy/service.go +++ b/internal/service/policy/service.go @@ -21,7 +21,7 @@ import ( //go:generate counterfeiter . Storage //go:generate counterfeiter . RegoCache -const HeadersKey = "headers" +const HeadersKey = "header" type Cache interface { Set(ctx context.Context, key, namespace, scope string, value []byte, ttl int) error diff --git a/internal/service/policy/service_test.go b/internal/service/policy/service_test.go index e1fb1b6f9b32d22476e5d4ed219dd805209ceac7..0cb619a416b445f56ad7140a13b34dc1ed71d814 100644 --- a/internal/service/policy/service_test.go +++ b/internal/service/policy/service_test.go @@ -34,7 +34,7 @@ func TestService_Evaluate(t *testing.T) { testPolicyWithStaticData := `package testgroup.example default allow = false allow { data.msg == "hello world" }` // prepare test policy accessing headers during evaluation - testPolicyAccessingHeaders := `package testgroup.example token := input.headers["Authorization"]` + testPolicyAccessingHeaders := `package testgroup.example token := input.header["Authorization"]` // prepare test query that can be retrieved from rego queryCache testQuery, err := rego.New(