@@ -5,8 +5,10 @@ policies and publishing them to Plural with
55[ Terraform] ( https://github.com/pluralsh/terraform-provider-plural ) .
66
77The included workbench policy denies Kubernetes deletes in the ` kube-system `
8- namespace. A binding policy automatically attaches it to workbenches whose
9- names begin with ` demo- ` .
8+ namespace unless the actor belongs to the ` sre ` group. It also automatically
9+ approves Kubernetes updates by SREs outside ` kube-system ` . A binding policy
10+ automatically attaches these guardrails to workbenches whose names begin with
11+ ` demo- ` .
1012
1113## Repository layout
1214
@@ -32,19 +34,24 @@ to either the `deny` or `approve` set:
3234package plrl.wb.admission
3335
3436deny[{"msg": "a useful reason for the denial"}] if {
35- input.input.some_field == "some-value"
37+ input.tool_name == "some_tool"
38+ input.tool.some_field == "some-value"
3639}
3740```
3841
39- Plural passes workbench tool arguments under ` input.input ` . It may also provide
40- the current user under ` input.actor ` . Any value added to ` deny ` blocks the tool
41- call. Denials must be objects with a ` msg ` string, which Plural presents as the
42- reason. Values added to ` approve ` require an approval before the tool runs.
42+ Plural provides:
4343
44- The example policy is attached only to the workbench ` delete_k8s_resource `
45- tool. The Terraform binding uses the tool match expression
46- ` ^delete_k8s_resource$ ` . Tool matching determines when a policy runs; the Rego
47- file receives the tool arguments, not the tool name.
44+ - ` input.tool_name ` : the name of the tool being evaluated
45+ - ` input.tool ` : the tool arguments
46+ - ` input.actor ` : the current user, including a ` groups ` array when available
47+
48+ Any value added to ` deny ` blocks the tool call. Denials must contain a ` msg `
49+ string. Values added to ` approve ` automatically approve tools that support
50+ approval and use the decision's ` reason ` in the audit trail.
51+
52+ The example explicitly checks ` delete_k8s_resource ` and
53+ ` update_k8s_resource ` . The Terraform binding includes both exact tool-name
54+ matches so the policy is evaluated for both operations.
4855
4956### Binding policies
5057
@@ -55,7 +62,7 @@ workbench itself:
5562package plrl.binding
5663
5764bind if {
58- startswith(input.name, "demo-")
65+ startswith(input.workbench. name, "demo-")
5966}
6067```
6168
@@ -64,52 +71,59 @@ true result attaches the associated workbench policy; a false result removes
6471it. The Terraform ` plural_binding_policy ` resource connects the workbench
6572policy, binding policy, and tool match expressions.
6673
67- ## Test policies locally
68-
69- Install the [ OPA CLI] ( https://www.openpolicyagent.org/docs/latest/#running-opa )
70- and run:
71-
72- ``` sh
73- opa fmt --fail policies
74- opa test --verbose policies
74+ ## Test policies
75+
76+ See [ ` .github/workflows/test.yaml ` ] ( .github/workflows/test.yaml ) for the OPA
77+ version, formatting check, and test command used by this repository. Tests live
78+ beside each policy and end in ` _test.rego ` .
79+
80+ ## Deploy as a Plural stack
81+
82+ The configuration in ` terraform/main.tf ` loads the Rego files and creates the
83+ workbench policy, binding policy, and binding. The recommended deployment is an
84+ ` InfrastructureStack ` , which gives the Terraform configuration managed state,
85+ plans, approvals, and Plural credentials at runtime.
86+
87+ Create or reuse ` GitRepository ` and ` Cluster ` resources, then point an
88+ ` InfrastructureStack ` at this repository's ` terraform ` directory:
89+
90+ ``` yaml
91+ apiVersion : deployments.plural.sh/v1alpha1
92+ kind : GitRepository
93+ metadata :
94+ name : policy-examples
95+ namespace : infra
96+ spec :
97+ url : https://github.com/your-org/policy-examples.git
98+ ---
99+ apiVersion : deployments.plural.sh/v1alpha1
100+ kind : InfrastructureStack
101+ metadata :
102+ name : policy-examples
103+ namespace : infra
104+ spec :
105+ name : policy-examples
106+ type : TERRAFORM
107+ approval : true
108+ manageState : true
109+ repositoryRef :
110+ name : policy-examples
111+ namespace : infra
112+ clusterRef :
113+ name : mgmt
114+ namespace : infra
115+ git :
116+ ref : main
117+ folder : terraform
75118` ` `
76119
77- Tests live beside each policy and end in ` _test.rego ` . Add both denied and
78- allowed cases whenever a policy changes.
79-
80- ## Publish policies with Terraform
81-
82- The configuration in ` terraform/main.tf ` uses the ` plural_policy ` resource and
83- loads the Rego source directly from ` policies/ ` . Authenticate with environment
84- variables so credentials do not enter Terraform source or variable files:
85-
86- ``` sh
87- export PLURAL_CONSOLE_URL=" https://console.example.com"
88- export PLURAL_ACCESS_TOKEN=" ..."
89-
90- terraform -chdir=terraform init
91- terraform -chdir=terraform fmt -check
92- terraform -chdir=terraform plan
93- terraform -chdir=terraform apply
94- ```
95-
96- The access token needs permission to manage policies in the selected project.
97- For local use, the provider can alternatively read credentials from `plural cd
98- login` by setting ` PLURAL_USE_CLI=true`.
120+ Replace the repository URL and ` clusterRef` with resources from your management
121+ cluster. The stack runner supplies `PLURAL_CONSOLE_URL` and
122+ ` PLURAL_ACCESS_TOKEN` ; do not commit them to Terraform variables or manifests.
99123
100124The example looks up the Plural project named `default`. Change the
101- ` plural_project ` data source if policies belong to another project. Terraform
102- creates both policies and reconciles the workbench attachments hourly. Before
103- using this repository with a team, configure a remote Terraform backend so
104- state is shared and protected.
105-
106- ## CI
107-
108- ` .github/workflows/test.yaml ` runs on every pull request and on pushes to
109- ` main ` . It checks Rego formatting and runs all OPA tests.
110-
111- CI does not apply Terraform and therefore needs no Plural credentials. Apply
112- from your normal infrastructure delivery workflow after review.
125+ ` plural_project` data source in `terraform/main.tf` if the policies belong to
126+ another project. The binding is reconciled hourly.
113127
114128# # Add another policy
115129
@@ -119,4 +133,5 @@ from your normal infrastructure delivery workflow after review.
119133 reads the new file.
1201344. Reuse or add a policy under `policies/binding/`, then connect the two with a
121135 ` plural_binding_policy` resource and the appropriate tool regexes.
122- 5 . Run the OPA and Terraform checks locally.
136+ 5. Add denied, allowed, and approval cases to the policy tests; use the
137+ repository workflow as the source of truth.
0 commit comments