Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions config/navigation.json
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,8 @@
"terraform-reference/resources/service_account_api_key",
"terraform-reference/resources/control",
"terraform-reference/resources/notification_config",
"terraform-reference/resources/repo_tags"
"terraform-reference/resources/repo_tags",
"terraform-reference/resources/rego_policy"
]
},
{
Expand All @@ -545,7 +546,8 @@
"terraform-reference/data-sources/control",
"terraform-reference/data-sources/notification_config",
"terraform-reference/data-sources/repo",
"terraform-reference/data-sources/repos"
"terraform-reference/data-sources/repos",
"terraform-reference/data-sources/rego_policy"
]
},
{
Expand Down
2 changes: 2 additions & 0 deletions policy-reference/rego_policy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ See the [Ergo documentation](https://ergo.kosli.com) for subjects, sentences, an

`kosli publish rego-policy IDENTIFIER` stores a new version of the policy. Only org admins can publish, so the team whose trails are evaluated cannot write the policy that judges them. Publishing the same files and input schema as the newest version stores nothing new.

To manage a policy as code, use the [`kosli_rego_policy` Terraform resource](/terraform-reference/resources/rego_policy). It publishes a new version when the files change and archives the policy on destroy.

`--input-schema` declares the input the policy reads: `trail` for a single trail, or `trails` for several. Kosli then validates the version: it compiles the policy in OPA's strict mode against the schema of that input, so a policy that reads a field the input cannot have does not compile. A `trails` policy that reads `input.trail` fails here rather than at evaluation.

To catch these errors before you publish, download the schema Kosli validates against and type-check the policy with `opa check`. Kosli serves one schema for each input: `evaluation-input/trail/v1` for `--input-schema trail`, and `evaluation-input/trails/v1` for `--input-schema trails`. Pass the [capabilities file](https://docs.kosli.com/rego/capabilities/v1.json) Kosli compiles against too, so the same check fails on a [refused built-in function](#restrictions).
Expand Down
8 changes: 2 additions & 6 deletions terraform-reference/data-sources/control.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ icon: "database"

Fetches details of an existing Kosli control. Use this data source to reference controls and access metadata such as the version, tags, and referencing policies.

<Warning>
Controls is a **beta** feature and must be enabled for your organization; API requests return `403 Forbidden` otherwise.
</Warning>

Use this data source to:

- Reference an existing control managed outside Terraform
Expand Down Expand Up @@ -74,8 +70,8 @@ Data sources provide read-only access to control metadata. To create or modify c

### Read-only

- `created_at` (String) RFC3339 UTC timestamp of when the control was created. When `version` is set, this is when that version was created.
- `created_by` (String) Identifier of the user who created the control. When `version` is set, this is who created that version.
- `created_at` (String) RFC3339 UTC timestamp of when the latest version of the control was created, or the requested version when `version` is set.
- `created_by` (String) Identifier of the user who created the latest version of the control, or the requested version when `version` is set.
- `description` (String) The description of the control.
- `links` (Map of String) Named links related to the control, as a map of link name to URL.
- `name` (String) Human-readable display name of the control.
Expand Down
108 changes: 108 additions & 0 deletions terraform-reference/data-sources/rego_policy.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
title: "kosli_rego_policy data source"
description: "Fetches an existing Kosli Rego policy and the files of one of its versions, by default the current version that an evaluation runs."
icon: "database"
---

Fetches an existing Kosli [Rego policy](/policy-reference/rego_policy) 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
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](/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.
2 changes: 1 addition & 1 deletion terraform-reference/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ API tokens grant full access to your Kosli organization. Store them securely and

Use a token that belongs to a user or service account with the **Admin** role.

Some provider operations are restricted to admins. Destroying a `kosli_environment` or `kosli_logical_environment` archives the environment in Kosli, and archiving requires the Admin role. With a Member token, `terraform apply` can create and update environments. However, `terraform destroy` (or removing the resource from your configuration) fails with `403 Forbidden` and the resource stays in state.
Some provider operations are restricted to admins. Destroying a `kosli_environment` or `kosli_logical_environment` archives the environment in Kosli, and archiving requires the Admin role. With a Member token, `terraform apply` can create and update environments. However, `terraform destroy` (or removing the resource from your configuration) fails with `403 Forbidden` and the resource stays in state. Publishing, archiving and unarchiving a Rego policy also require the Admin role, so creating, updating and destroying a [`kosli_rego_policy`](/terraform-reference/resources/rego_policy) all fail with a Member token.

The recommended setup is a dedicated service account with the `admin` privilege and an API key minted for it. You can create both in the Kosli UI under **Settings** → **Service Accounts**, or with the [`kosli_service_account`](/terraform-reference/resources/service_account) and [`kosli_service_account_api_key`](/terraform-reference/resources/service_account_api_key) resources. The initial bootstrap with these resources must itself run with an Admin token, because a service account cannot be granted a higher privilege than the caller's.

Expand Down
4 changes: 0 additions & 4 deletions terraform-reference/list-resources/control.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ icon: "list"

Lists Kosli controls in the organization for use with `terraform query` (requires Terraform >= 1.14), so existing controls can be discovered and brought under Terraform management without hand-writing one `import` block per control.

<Warning>
Controls is a **beta** feature and must be enabled for your organization; API requests return `403 Forbidden` otherwise.
</Warning>

<Note>
The Kosli list endpoint does not return `policies_referencing`, so when resource data is included in query results that attribute is always reported as an empty list. Archived controls are only returned when `archived = true` is set.
</Note>
Expand Down
8 changes: 2 additions & 6 deletions terraform-reference/resources/control.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ icon: "cube"

Manages a Kosli control. Controls are org-level definitions of SDLC requirements (for example `SDLC-001` "Binary provenance") whose compliance is evaluated from attestations and enforced through environment policies.

<Warning>
Controls is a **beta** feature and must be enabled for your organization; API requests return `403 Forbidden` otherwise.
</Warning>

<Note>
Deleting this resource **archives** the control in Kosli rather than hard-deleting it. Creating a new control with the identifier of an archived control fails with a conflict; unarchive the control in Kosli and import it instead.
</Note>
Expand Down Expand Up @@ -76,7 +72,7 @@ To reference an existing control and access metadata such as the current version

### Read-only

- `created_at` (String) RFC3339 UTC timestamp of when the control was created.
- `created_by` (String) Identifier of the user who created the control.
- `created_at` (String) RFC3339 UTC timestamp of when the current version of the control was created. Changes on every update.
- `created_by` (String) Identifier of the user who created the current version of the control. Set again on every update, so it is the author of the latest change, not of the control.
- `policies_referencing` (List of String) Names of the environment policies that reference this control.
- `version` (Number) Version number of the control, assigned by the server and incremented on every update.
133 changes: 133 additions & 0 deletions terraform-reference/resources/rego_policy.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
title: "kosli_rego_policy resource"
description: "Manages a Kosli Rego policy: a versioned bundle of Rego files that Kosli evaluates trails against. Each change to the files publishes a new version that Kosli validates."
icon: "cube"
---

Manages a Kosli [Rego policy](/policy-reference/rego_policy): a versioned bundle of Rego files that Kosli evaluates trails against. Each change to `files` or `input_schema` publishes a new version, and the apply waits until Kosli has validated it.

<Warning>
Publishing, archiving and unarchiving a Rego policy all require a service account with **Admin** permissions, so creating, updating and destroying this resource do too.
</Warning>

<Note>
Deleting this resource **archives** the policy in Kosli rather than hard-deleting it: the policy leaves the policy list, while its versions stay readable and an evaluation by its identifier still runs. Creating the resource again with the same `identifier` publishes to the archived policy and unarchives it.
</Note>

## Example usage

```terraform
terraform {
required_providers {
kosli = {
source = "kosli-dev/kosli"
}
}
}

# A single-file policy, read from a .rego file kept next to the configuration
resource "kosli_rego_policy" "artifact_provenance" {
identifier = "artifact-provenance"
name = "Artifact provenance"
description = "Requires the trail to hold at least one artifact"
input_schema = "trail"
comment = "Initial version"

files = {
"policy.rego" = file("${path.module}/policies/artifact_provenance.rego")
}
}

# A bundle of every .rego file in a directory, keyed by its path in the bundle
resource "kosli_rego_policy" "release_checks" {
identifier = "release-checks"
name = "Release checks"
input_schema = "trail"

files = {
for name in fileset("${path.module}/policies", "**/*.rego") :
name => file("${path.module}/policies/${name}")
}
}

output "artifact_provenance_digest" {
description = "Digest of the version an evaluation runs"
value = kosli_rego_policy.artifact_provenance.digest
}
```

## Versions and validation

Kosli stores a policy as a series of immutable versions. Publishing returns a version in status `pending`, and Kosli validates it asynchronously:

| Status | Meaning | What the apply does |
|---|---|---|
| `valid` | Kosli accepted the bundle | Succeeds; the version becomes the policy's current version |
| `rejected` | The policy is at fault, for example a Rego syntax error | Fails with the validator's message |
| `failed` | Kosli could not validate the version | Fails; apply again to publish the files for another validation |
| `pending` | Validation has not finished | Waits up to 5 minutes, then fails; apply again to keep waiting |

Only a change to `files` or `input_schema` creates a new version. A change to `name` or `description` updates the policy in place, and a change to `comment` alone publishes nothing. Every update sends the whole bundle again, because Kosli has one publish call for both the files and the metadata.

`version` shows as known after apply on every update. Kosli never reuses a rejected or failed version, so publishing the current files again can still store a new version.

The resource tracks the policy's **current version**: the highest-numbered valid version, which is the one an evaluation runs. After an apply that ends with a rejected version, the policy keeps running its previous valid version and the next plan still shows the pending change.

A version published outside Terraform shows up as a change to `files` on the next plan, once it is valid.

## Destroying

Kosli never hard-deletes a Rego policy. `terraform destroy`, or removing a `kosli_rego_policy` from the configuration, **archives** the policy:

- It leaves the default policy list in Kosli.
- Its versions, files and past evaluations stay readable.
- An evaluation by its identifier still runs.
- Its identifier stays taken.

Creating a `kosli_rego_policy` with the identifier of an archived policy publishes to that policy and unarchives it, so the version numbers continue from where they stopped.

A policy whose first version is rejected, or is still pending at the timeout, exists in Kosli but is not in the Terraform state. Applying again publishes to it. If you remove the resource from the configuration instead, the policy stays in Kosli and you have to archive it there.

A policy archived outside Terraform reads as deleted: the next plan proposes to create it, and the apply unarchives it.

<Warning>
Archiving requires a Kosli server with the Rego policy archive API. On an older server, the destroy removes the resource from the Terraform state, leaves the policy active, and says so in a warning.
</Warning>

## Import

Rego policies can be imported using their identifier:

```shell
# Import an existing rego policy by identifier
terraform import kosli_rego_policy.artifact_provenance artifact-provenance
```

The import reads the files of the current version into the state. `comment` is not read back from Kosli. You can't import an archived policy; unarchive it in Kosli first.

## Querying Rego policies

To read a policy's current version or a specific version, including its files and digest, use the [`kosli_rego_policy` data source](/terraform-reference/data-sources/rego_policy).

## Schema

### Required

- `files` (Map of String) The Rego bundle, as a map of each file's relative path to its source. A single-file policy is one entry. Kosli limits a bundle to 100 files and 1 MiB in total, and rejects paths that leave the bundle.
- `identifier` (String) Unique identifier of the policy within the organization. Must start with a letter or number and contain only letters, numbers, periods (`.`), hyphens (`-`), underscores (`_`), and tildes (`~`). Kosli compares identifiers without regard to case. Changing this to a different policy will force recreation of the resource.
- `input_schema` (String) Shape of the input the policy is written against. Known values: `trail` (the policy evaluates one trail) and `trails` (a set of trails). The Kosli API validates the value.
- `name` (String) Human-readable display name of the policy. Can be changed without publishing a new version.

### Optional

- `comment` (String) Why this version is published. Kosli stores the comment with the version an apply creates; changing only the comment publishes nothing, and the value is not read back from Kosli.
- `description` (String) Free-form description of the policy. Can be changed without publishing a new version.

### Read-only

- `created_at` (String) RFC3339 UTC timestamp of when the policy was created.
- `created_by` (String) Display name of the user who created the policy.
- `digest` (String) Digest of the current version's files, as `sha256:` and a hex digest. A decision made with the policy cites this value.
- `entrypoint` (String) Policy package Kosli found as the entrypoint of the current version.
- `params_schema` (String) JSON Schema, as a JSON string, that the current version declares for its params. Null when the policy declares none.
- `version` (Number) Number of the policy's current version: the highest-numbered valid version, which is the one an evaluation runs.
Loading