Discovery

OPA can be configured to download bundles of policy and data, report status, and upload decision logs to remote endpoints. The discovery feature helps you centrally manage the OPA configuration for these features. You should use the discovery feature if you want to avoid managing OPA configuration updates in number of different locations.

When the discovery feature is enabled, OPA will periodically download a discovery bundle. Like regular bundles, the discovery bundle may contain JSON and Rego files. OPA will evaluate the data and policies contained in the discovery bundle to generate the rest of the configuration. There are two main ways to structure the discovery bundle:

  1. Include static JSON configuration files that define the OPA configuration.
  2. Include Rego files that can be evaluated to produce the OPA configuration.

If you need OPA to select which policy to download dynamically (e.g., based on environment variables like the region where OPA is running), use the second option.

If discovery is enabled, other features like bundle downloading and status reporting cannot be configured manually. Similarly, discovered configuration cannot override the original discovery settings in the configuration file that OPA was booted with.

See the Configuration Reference for configuration details.

Discovery Service API

OPA expects the service to expose an API endpoint that serves bundles.

  1. GET /<service_url>/<discovery.prefix>/<discovery.name> HTTP/1.1

If the bundle exists, the server should respond with an HTTP 200 OK status followed by a gzipped tarball in the message body.

  1. HTTP/1.1 200 OK
  2. Content-Type: application/gzip

You can enable discovery with an OPA configuration file similar to the example below. In some places in the documentation, the initial configuration provided to OPA is referred to as the “boot configuration”.

  1. services:
  2. - name: acmecorp
  3. url: https://example.com/control-plane-api/v1
  4. credentials:
  5. bearer:
  6. token: "bGFza2RqZmxha3NkamZsa2Fqc2Rsa2ZqYWtsc2RqZmtramRmYWxkc2tm"
  7. discovery:
  8. name: /example/discovery
  9. prefix: configuration

Using the boot configuration above, OPA will fetch discovery bundles from:

  1. https://example.com/control-plane-api/v1/configuration/example/discovery
  2. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
  3. services[0].url | |
  4. + discovery.prefix |
  5. + discovery.name

The discovery.prefix field defaults to bundles. The default is convenient if you want to serve discovery bundles and normal bundles from the same API endpoint.

OPA generates it’s subsequent configuration by querying the Rego and JSON files contained inside the discovery bundle. The query is defined by the discovery.name field from the boot configuration: data.<discovery.name>. For example. with the boot configuration above, OPA executes the following query:

  1. data.example.discovery

If the discovery bundle contained the following Rego file:

  1. package example
  2. discovery = {
  3. "bundle": {"name": bundle_name},
  4. "default_decision": "acmecorp/httpauthz/allow"
  5. }
  6. bundle_name = "acmecorp/httpauthz"

The subsequent configuration would be:

  1. {
  2. "bundle": {
  3. "name": "acmecorp/httpauthz"
  4. },
  5. "default_decision": "acmecorp/httpauthz/allow"
  6. }

The discovery bundle contents above are essentially static. The same result could be achieved by constructing the discovery bundle with a static JSON file:

  1. {
  2. "example": {
  3. "discovery": {
  4. "bundle": {
  5. "name": "acmecorp/httpauthz"
  6. },
  7. "default_decision": "acmecorp/httpauthz/allow"
  8. }
  9. }
  10. }

For an example of how to configure OPA dynamically see the Example section below.

The subsequent configuration does not have to specify services or include a reference to a service in the bundle, status, or decision_log sections. If the either the services or references to services are missing, OPA will default them to the value from the boot configuration.

Example

Let’s see an example of how the discovery feature can be used to dynamically configure an OPA to download one of two bundles based on a label in the boot configuration. Let’s say the label region indicates the region in which the OPA is running and it’s value will decide the bundle to download.

Below is a policy file which generates an OPA congfiguration.

example.rego

  1. package example
  2. discovery = {
  3. "bundle": {
  4. "name": bundle_name # line 5
  5. }
  6. }
  7. rt = opa.runtime()
  8. region = rt.config.labels.region
  9. bundle_name = region_bundle[region]
  10. # region-bundle information
  11. region_bundle = {
  12. "US": "example/test1/p",
  13. "UK": "example/test2/p"
  14. }

The bundle_name variable in line 5 of the above policy will be dynamically selected based on the value of the label region. So if an OPA was started with region: "US", then the bundle_name will be example/test1/p.

Start an OPA with a boot configuration as shown below:

config.yaml

  1. services:
  2. - name: acmecorp
  3. url: https://example.com/control-plane-api/v1
  4. credentials:
  5. bearer:
  6. token: "bGFza2RqZmxha3NkamZsa2Fqc2Rsa2ZqYWtsc2RqZmtramRmYWxkc2tm"
  7. discovery:
  8. name: /example/discovery
  9. labels:
  10. region: "US"

Run OPA:

  1. opa run -s -c config.yaml

You should see a log like below, which shows the bundle being downloaded. In this case, the bundle name is example/test1/p as region is US.

  1. INFO Bundle downloaded and activated successfully. name=example/test1/p plugin=bundle

Now start another OPA with a boot configuration as shown below. Notice the region is UK:

config.yaml

  1. services:
  2. - name: acmecorp
  3. url: https://example.com/control-plane-api/v1
  4. credentials:
  5. bearer:
  6. token: "bGFza2RqZmxha3NkamZsa2Fqc2Rsa2ZqYWtsc2RqZmtramRmYWxkc2tm"
  7. discovery:
  8. name: /example/discovery
  9. labels:
  10. region: "UK"

Run OPA:

  1. opa run -s -c config.yaml

In this case, the bundle being downloaded is example/test2/p as region is UK.

  1. INFO Bundle downloaded and activated successfully. name=example/test2/p plugin=bundle

This shows how the discovery feature can help in centrally managing the bundle to be downloaded by an OPA based on a configuration label. You can use the same strategy to dynamically configure other plugins based on the running OPA’s configuration labels or environment variables.

Limitations

The discovery feature cannot be used to dynamically modify services, labels and discovery. This means that these configuration settings should be included in the bootup configuration file provided to OPA.