Skip to content

Commit

Permalink
server: exposing disableInlining option via Compile API (#4378)
Browse files Browse the repository at this point in the history
This change enables consumers of Compile API to specify packages
those should not be inlined in partial evaluation response.

Fixes: #4357

Signed-off-by: skosunda <skosunda@adobe.com>
  • Loading branch information
srlk authored Feb 24, 2022
1 parent d7c17d1 commit 3ea03ea
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion docs/content/rest-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1281,10 +1281,11 @@ Evaluation in OPA, see [this post on blog.openpolicyagent.org](https://blog.open

Compile API requests contain the following fields:

| Field | Type | Requried | Description |
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `query` | `string` | Yes | The query to partially evaluate and compile. |
| `input` | `any` | No | The input document to use during partial evaluation (default: undefined). |
| `options` | `object[string, any]` | No | Additional options to use during partial evaluation. Only `disableInlining` option is supported. (default: undefined). |
| `unknowns` | `array[string]` | No | The terms to treat as unknown during partial evaluation (default: `["input"]`]). |

#### Query Parameters
Expand Down Expand Up @@ -1325,6 +1326,9 @@ Content-Type: application/json
"clearance_level": 4
}
},
"options": {
"disableInlining": []
},
"unknowns": [
"data.reports"
]
Expand Down
9 changes: 9 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,7 @@ func (s *Server) v1CompilePost(w http.ResponseWriter, r *http.Request) {
rego.ParsedQuery(request.Query),
rego.ParsedInput(request.Input),
rego.ParsedUnknowns(request.Unknowns),
rego.DisableInlining(request.Options.DisableInlining),
rego.QueryTracer(buf),
rego.Instrument(includeInstrumentation),
rego.Metrics(m),
Expand Down Expand Up @@ -2670,6 +2671,11 @@ type compileRequest struct {
Query ast.Body
Input ast.Value
Unknowns []*ast.Term
Options compileRequestOptions
}

type compileRequestOptions struct {
DisableInlining []string
}

func readInputCompilePostV1(r io.ReadCloser) (*compileRequest, *types.ErrorV1) {
Expand Down Expand Up @@ -2716,6 +2722,9 @@ func readInputCompilePostV1(r io.ReadCloser) (*compileRequest, *types.ErrorV1) {
Query: query,
Input: input,
Unknowns: unknowns,
Options: compileRequestOptions{
DisableInlining: request.Options.DisableInlining,
},
}

return result, nil
Expand Down
32 changes: 32 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,10 @@ func TestCompileV1(t *testing.T) {
default r = true
r { input.x = 1 }
custom_func(x) { data.a[i] == x }
s { custom_func(input.x) }
`

expQuery := func(s string) string {
Expand Down Expand Up @@ -915,6 +919,34 @@ func TestCompileV1(t *testing.T) {
`)},
},
},
{
note: "function without disableInlining",
trs: []tr{
{http.MethodPut, "/policies/test", mod, 200, ""},
{http.MethodPost, "/compile", `{
"unknowns": ["data.a"],
"query": "data.test.s = true",
"input": { "x": 1 }
}`, 200, expQuery("data.a[i2] = 1")},
},
},
{
note: "function with disableInlining",
trs: []tr{
{http.MethodPut, "/policies/test", mod, 200, ""},
{http.MethodPost, "/compile", `{
"unknowns": ["data.a"],
"query": "data.test.s = true",
"options": { "disableInlining": ["data.test"] },
"input": { "x": 1 }
}`, 200, expQueryAndSupport(
`data.partial.test.s = true`,
`package partial.test
s { data.partial.test.custom_func(1) }
custom_func(__local0__2) { data.a[i2] = __local0__2 }
`)},
},
},
{
note: "empty unknowns",
trs: []tr{
Expand Down
3 changes: 3 additions & 0 deletions server/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ type CompileRequestV1 struct {
Input *interface{} `json:"input"`
Query string `json:"query"`
Unknowns *[]string `json:"unknowns"`
Options struct {
DisableInlining []string `json:"disableInlining,omitempty"`
} `json:"options,omitempty"`
}

// CompileResponseV1 models the response message for Compile API operations.
Expand Down

0 comments on commit 3ea03ea

Please sign in to comment.