|
9 | 9 | - [Policy API](#policy-api) - manage policy loaded into the OPA instance. |
10 | 10 | - [Data API](#data-api) - evaluate rules and retrieve data. |
11 | 11 | - [Query API](#query-api) - execute adhoc queries. |
12 | | -- [Compile API](#compile-api) - access Rego's [Partial Evaluation](https://blog.openpolicyagent.org/partial-evaluation-162750eaf422) functionality. |
| 12 | +- [Compile API](#compile-api) - access Rego's [Partial Evaluation](https://blog.openpolicyagent.org/partial-evaluation-162750eaf422) and data filtering functionality. |
13 | 13 | - [Health API](#health-api) - access instance operational health information. |
14 | 14 | - [Config API](#config-api) - view instance configuration. |
15 | 15 | - [Status API](#status-api) - view instance [status](./management-status) state. |
@@ -1608,6 +1608,124 @@ The following table summarizes the behavior for partial evaluation results. |
1608 | 1608 |
|
1609 | 1609 | > The partially evaluated queries are represented as strings in the table above. The actual API response contains the JSON AST representation. |
1610 | 1610 |
|
| 1611 | + |
| 1612 | +### Compling a Rego policy (and query) into data filters |
| 1613 | + |
| 1614 | +```http |
| 1615 | +POST /v1/compile/{path:.+} |
| 1616 | +Content-Type: application/json |
| 1617 | +``` |
| 1618 | + |
| 1619 | +Where the `{path}` is the slash delimited filter rule to be compiled. |
| 1620 | +E.g., to compile the `data.filters.include` rule, query `/v1/compile/filters/include` |
| 1621 | + |
| 1622 | + |
| 1623 | +#### Request Headers |
| 1624 | + |
| 1625 | +| Name | Required | Accepted Values | Description | |
| 1626 | +| --- | --- | --- | --- | |
| 1627 | +| Content-Type | No | `application/json` | Indicates the request body is either a JSON encoded document. | |
| 1628 | +| Content-Encoding | No | gzip | Indicates the request body is a compressed gzip object. | |
| 1629 | +| Accept | Yes | See [below](#accept-header--controlling-the-target-response-format) | See [below](#accept-header--controlling-the-target-response-format) | |
| 1630 | + |
| 1631 | + |
| 1632 | +#### Accept Header – Controlling the Target Response Format |
| 1633 | + |
| 1634 | +The same request can generate filters that are representable in many different ways, such as raw SQL `WHERE` clauses or Universal Conditions AST (UCAST). |
| 1635 | + |
| 1636 | +OPA uses the `Accept` header to denote the target response format. |
| 1637 | + |
| 1638 | +| Value | Response Schema | Description | |
| 1639 | +| --- | --- | -- | |
| 1640 | +| Multitarget: `application/vnd.opa.multitarget+json` | `result.{ucast,sqlserver,mysql,postgresql,sqlite}` | The partially evaluated result of the query in each target dialect. Use the `options.targetDialects` field in the request body to control targets. | |
| 1641 | +| UCAST: `application/vnd.opa.ucast.all+json`, `application/vnd.opa.ucast.minimal+json`, `application/vnd.opa.ucast.linq+json`, `application/vnd.opa.ucast.prisma+json` | `result.query`| UCAST JSON object describing the conditions under which the query is true. | |
| 1642 | +| SQL: `application/vnd.opa.sql.sqlserver+json`, `application/vnd.opa.sql.mysql+json`, `application/vnd.opa.sql.postgresql+json`, `application/vnd.opa.sql.sqlite+json` | `result.query`| String representing the SQL equivalent of the conditions under which the query is true. | |
| 1643 | + |
| 1644 | + |
| 1645 | +#### Request Body |
| 1646 | + |
| 1647 | +| Field | Type | Required | Description | |
| 1648 | +| --- | --- | --- | --- | |
| 1649 | +| `input` | `any` | No | The input document to use during partial evaluation and during mask rule evaluation (default: undefined). | |
| 1650 | +| `options` | `object[string, any]` | No | Additional options to use during partial evaluation | |
| 1651 | +| `options.disableInlining` | `array[string]` | No. Default: undefined | A list of rule references. | |
| 1652 | +| `options.maskRule` | `string` | No | The rule to evaluate for generating column masks. Overrides any `mask_rule` annotations defined in the policy. | |
| 1653 | +| `options.targetDialects` | `array[string]`, one of `ucast+all`, `ucast+minimal`, `ucast+prisma`, `ucast+linq`, `sql+sqlserver`, `sql+mysql`, `sql+postgresql` | Yes, if using `multitarget`. **Ignored for all other targets** | The output targets for partial evaluation. Different targets will have different constraints. Use [`Accept` header](#accept-header--controlling-the-target-response-format) to request a single compilation target. | |
| 1654 | +| `options.targetSQLTableMappings` | `object[string, object[string, string]]` | No | A mapping between tables and columns. See the [example](#using-table-and-column-remappings) for the schema. | |
| 1655 | +| `unknowns` | `array[string]` | No | The terms to treat as unknown during partial evaluation (default: `["input"]`]). | |
| 1656 | + |
| 1657 | + |
| 1658 | +#### Example Request |
| 1659 | + |
| 1660 | +With OPA running with this policy, we'll compile the query `data.filters.include` into SQL filters: |
| 1661 | + |
| 1662 | +```rego |
| 1663 | +package filters |
| 1664 | +
|
| 1665 | +# METADATA |
| 1666 | +# scope: document |
| 1667 | +# custom: |
| 1668 | +# unknowns: [input.fruits] |
| 1669 | +include if input.fruits.name == input.favorite |
| 1670 | +
|
| 1671 | +include if { |
| 1672 | + input.fruits.name == "apple" |
| 1673 | + not input.favorite |
| 1674 | +} |
| 1675 | +``` |
| 1676 | + |
| 1677 | +```http |
| 1678 | +POST /v1/compile/filters/include HTTP/1.1 |
| 1679 | +Content-Type: application/json |
| 1680 | +Accept: application/vnd.opa.sql.postgresql+json |
| 1681 | +``` |
| 1682 | + |
| 1683 | +```json |
| 1684 | +{ |
| 1685 | + "input": { |
| 1686 | + "favorite": "pineapple" |
| 1687 | + } |
| 1688 | +} |
| 1689 | +``` |
| 1690 | + |
| 1691 | + |
| 1692 | +#### Example Response |
| 1693 | + |
| 1694 | +```http |
| 1695 | +HTTP/1.1 200 OK |
| 1696 | +Content-Type: application/vnd.opa.sql.postgresql+json |
| 1697 | +``` |
| 1698 | + |
| 1699 | +```json |
| 1700 | +{ |
| 1701 | + "result": { |
| 1702 | + "query": "WHERE fruits.name = E'pineapple'" |
| 1703 | + } |
| 1704 | +} |
| 1705 | +``` |
| 1706 | + |
| 1707 | + |
| 1708 | +#### Unconditional Results from Filters Generation |
| 1709 | + |
| 1710 | +An absent `query` in the `result` indicates **unconditional deny**: |
| 1711 | + |
| 1712 | +```json |
| 1713 | +{ |
| 1714 | + "result": {} |
| 1715 | +} |
| 1716 | +``` |
| 1717 | + |
| 1718 | +An empty string `query` indicates **unconditional include**: |
| 1719 | + |
| 1720 | +```json |
| 1721 | +{ |
| 1722 | + "result": { |
| 1723 | + "query": "" |
| 1724 | + } |
| 1725 | +} |
| 1726 | +``` |
| 1727 | + |
| 1728 | + |
1611 | 1729 | ## Health API |
1612 | 1730 |
|
1613 | 1731 | The `/health` API endpoint executes a simple built-in policy query to verify |
|
0 commit comments