> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kosli.com/llms.txt
> Use this file to discover all available pages before exploring further.

# kosli_rego_policy data source

> Fetches an existing Kosli Rego policy and the files of one of its versions, by default the current version that an evaluation runs.

Fetches an existing Kosli [Rego policy](https://docs.kosli.com/policy-reference/rego_policy.md) and the files of one of its versions. Without `version`, it reads the policy's current version: the highest-numbered valid version, which is the one an evaluation runs.

## Example usage

```terraform theme={"theme":"dracula","languages":{"custom":["/languages/rego.json"]}}
terraform {
  required_providers {
    kosli = {
      source = "kosli-dev/kosli"
    }
  }
}

# Read the current version of a policy: the one an evaluation runs
data "kosli_rego_policy" "artifact_provenance" {
  identifier = "artifact-provenance"
}

# Read a specific version, whatever its validation status
data "kosli_rego_policy" "artifact_provenance_v1" {
  identifier = "artifact-provenance"
  version    = 1
}

output "current_version" {
  description = "Number of the version an evaluation runs"
  value       = data.kosli_rego_policy.artifact_provenance.version
}

output "current_digest" {
  description = "Digest a decision made with the current version cites"
  value       = data.kosli_rego_policy.artifact_provenance.digest
}

output "policy_source" {
  description = "Source of the policy's main file"
  value       = data.kosli_rego_policy.artifact_provenance.files["policy.rego"]
}

# Copy a policy: publish the files read from one policy under another
# identifier. With a second provider configuration (an alias for another
# organization) on the data source, this promotes a policy between organizations.
resource "kosli_rego_policy" "artifact_provenance_copy" {
  identifier   = "artifact-provenance-copy"
  name         = "${data.kosli_rego_policy.artifact_provenance.name} (copy)"
  input_schema = data.kosli_rego_policy.artifact_provenance.input_schema
  comment      = "Copied from artifact-provenance version ${data.kosli_rego_policy.artifact_provenance.version}"
  files        = data.kosli_rego_policy.artifact_provenance.files
}

# Pin a version: warn on every plan and apply when the version an evaluation
# runs is no longer the reviewed one. The digest identifies the files, so it
# stays the same when the reviewed source is published again under a new
# version number.
check "artifact_provenance_is_reviewed_version" {
  assert {
    condition     = data.kosli_rego_policy.artifact_provenance.digest == data.kosli_rego_policy.artifact_provenance_v1.digest
    error_message = "The current version of artifact-provenance is not the reviewed version 1."
  }
}
```

## Which version is read

Without `version`, the data source reads the policy's **current version**: the highest-numbered valid version, which is the one an evaluation runs. A newer version that is pending, rejected or failed is not the current version. A policy with no valid version yet is an error. The error names the newest version, which you can then read by number.

With `version`, the data source reads that version whatever its status. `status` says whether it is `pending`, `valid`, `rejected` or `failed`, and `entrypoint` and `params_schema` are null unless it is `valid`.

`name`, `description`, `created_at` and `created_by` belong to the policy, not to a version, so they are the same for every version.

## Archived policies

Destroying a `kosli_rego_policy` archives the policy. By default, the data source reads only an active policy and reports an archived one as an error. Set `archived = true` to read an archived policy.

## Read-only access

Data sources provide read-only access. To publish or change a Rego policy, use the [`kosli_rego_policy` resource](https://docs.kosli.com/terraform-reference/resources/rego_policy).

## Schema

### Required

* `identifier` (String) The identifier of the policy to query. Kosli compares identifiers without regard to case.

### Optional

* `archived` (Boolean) Whether the policy is expected to be archived. Deleting a `kosli_rego_policy` archives it, so by default only an active policy is read and an archived one is an error. Set to `true` to read an archived policy; an active one is then an error.
* `version` (Number) The version to read. Any version can be read, whatever its status. When unset, the current version is read and its number is returned here.

### Read-only

* `comment` (String) Why the version was published.
* `created_at` (String) RFC3339 UTC timestamp of when the policy was created.
* `created_by` (String) Display name of the user who created the policy.
* `description` (String) The description of the policy. Null when it has none.
* `digest` (String) Digest of the version's files, as `sha256:` and a hex digest. A decision made with the version cites this value.
* `entrypoint` (String) Policy package Kosli found as the entrypoint. Null unless the version is `valid`.
* `files` (Map of String) The version's Rego bundle, as a map of each file's relative path to its source.
* `input_schema` (String) Shape of the input the version is written against, such as `trail` or `trails`.
* `name` (String) Human-readable display name of the policy.
* `params_schema` (String) JSON Schema, as a JSON string, that the version declares for its params. Null when it declares none or is not `valid`.
* `status` (String) Validation status of the version: `pending`, `valid`, `rejected` or `failed`. Always `valid` when `version` is unset.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.