diff --git a/README.md b/README.md index be39783fcdcf100fd1d898ff1cb6563aeb47d019..78aa797f28d7def85568d3f7b2033bce6109938b 100644 --- a/README.md +++ b/README.md @@ -172,14 +172,14 @@ 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. They can be -accessed through a built-in extension function named `get_header()`. It accepts as argument +accessed through a built-in extension function named `external.http.header()`. It accepts as argument the name of the header in [Canonical](https://golangbyexample.com/canonical-http-header-key/) format. For example, inside Rego the value of a header named `Authorization` can be retrieved as follows: ``` package example.example -auth := get_header("Authorization") +auth := external.http.header("Authorization") ``` >Header names are passed to the Rego runtime in Canonical format. This means that the @@ -195,10 +195,10 @@ x-loCATion: Baz ``` Inside a policy these headers could be accessed as follows: ``` -accept_encoding := get_header("Accept-Encoding") -accept_language := get_header("Accept-Language") -foo := get_header("Foo") -location := get_header("X-Location") +accept_encoding := external.http.header("Accept-Encoding") +accept_language := external.http.header("Accept-Language") +foo := external.http.header("Foo") +location := external.http.header("X-Location") ``` ### Policy Extensions Functions diff --git a/doc/policy_development.md b/doc/policy_development.md index bffe48eb45289f15975881faedb53db4a1210c87..1daccecb4a75e2331187f1c63ff516328c4c5f89 100644 --- a/doc/policy_development.md +++ b/doc/policy_development.md @@ -69,7 +69,7 @@ A number of Rego extension functions are developed and injected in the policy service Rego runtime. Here is a list with brief description for each one of them. -#### get_header +#### external.http.header The function retrieves an HTTP header value taken from the incoming request during the current policy evaluation. The header name is in Canonical format @@ -80,7 +80,7 @@ as follows: ``` package example.example -auth := get_header("Authorization") +auth := external.http.header("Authorization") ``` #### cache.get diff --git a/internal/regocache/regocache_test.go b/internal/regocache/regocache_test.go index a83b91c2fdaa639f47265fa42a25d32e97057338..944a0e3bd7f85c0e7981a58752475f4e745fa71f 100644 --- a/internal/regocache/regocache_test.go +++ b/internal/regocache/regocache_test.go @@ -22,7 +22,7 @@ func TestCache_SetAndGet(t *testing.T) { Name: "example", Group: "example", Version: "1.0", - Rego: `package example.example _ = get_header("Authorization")`, + Rego: `package example.example _ = external.http.header("Authorization")`, Data: `{"hello":"world"}`, Locked: false, LastUpdate: time.Now(), @@ -42,7 +42,7 @@ func TestCache_Purge(t *testing.T) { Name: "example", Group: "example", Version: "1.0", - Rego: `package example.example _ = get_header("Authorization")`, + Rego: `package example.example _ = external.http.header("Authorization")`, Data: `{"hello":"world"}`, Locked: false, LastUpdate: time.Now(), @@ -63,7 +63,7 @@ func TestCache_PolicyDataChange(t *testing.T) { Name: "example", Group: "example", Version: "1.0", - Rego: `package example.example _ = get_header("Authorization")`, + Rego: `package example.example _ = external.http.header("Authorization")`, Data: `{"hello":"world"}`, Locked: false, LastUpdate: time.Now(), diff --git a/internal/regofunc/get_header.go b/internal/regofunc/http_header.go similarity index 95% rename from internal/regofunc/get_header.go rename to internal/regofunc/http_header.go index 37aedf40d4d7351951646b7c9920358a48917dfb..749ce285eba2f95ba1446da4b806cd34883c745f 100644 --- a/internal/regofunc/get_header.go +++ b/internal/regofunc/http_header.go @@ -10,7 +10,7 @@ import ( func GetHeaderFunc(headers map[string]string) (*rego.Function, rego.Builtin1) { return ®o.Function{ - Name: "get_header", + Name: "external.http.header", Decl: types.NewFunction(types.Args(types.S), types.S), Memoize: true, }, diff --git a/internal/regofunc/http_header_test.go b/internal/regofunc/http_header_test.go new file mode 100644 index 0000000000000000000000000000000000000000..374ffdabef19342064a3777f905048330e37c0b1 --- /dev/null +++ b/internal/regofunc/http_header_test.go @@ -0,0 +1 @@ +package regofunc_test diff --git a/internal/service/policy/service.go b/internal/service/policy/service.go index fb7a7103374dd9c110d91c989e9879332ef0aa91..6d87f7acbb414ff1716eb0bc740835d3301ff7ba 100644 --- a/internal/service/policy/service.go +++ b/internal/service/policy/service.go @@ -232,11 +232,9 @@ func (s *Service) prepareQuery(ctx context.Context, group, policyName, version s return nil, errors.New("error building rego runtime functions", err) } - // Append dynamically the get_header function on every request, + // Append dynamically the external.http.header function on every request, // because it is populated with different headers each time. - if len(headers) > 0 { - regoArgs = append(regoArgs, rego.Function1(regofunc.GetHeaderFunc(headers))) - } + regoArgs = append(regoArgs, rego.Function1(regofunc.GetHeaderFunc(headers))) newQuery, err := rego.New( regoArgs..., diff --git a/internal/service/policy/service_test.go b/internal/service/policy/service_test.go index dcf5521be4383cbbe4c8d05932835d8d80946e1e..f27f09611dc1cbc7ae2b99e099de5fb25cf7579c 100644 --- a/internal/service/policy/service_test.go +++ b/internal/service/policy/service_test.go @@ -43,7 +43,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 := get_header("Authorization")` + testPolicyAccessingHeaders := `package testgroup.example token := external.http.header("Authorization")` // prepare test request to be used in tests testReq := func() *goapolicy.EvaluateRequest {