|
2 | 2 | title: Open Policy Agent Integration |
3 | 3 | --- |
4 | 4 |
|
| 5 | +!!! note |
| 6 | + |
| 7 | + The [Open Policy Agent](https://www.openpolicyagent.org/docs/latest/) (OPA, pronounced “oh-pa”) is an open source, |
| 8 | + general-purpose policy engine that unifies policy enforcement across the stack. |
| 9 | + |
| 10 | +OPA allows you to create arbitrary rules within Concourse without having to add a new feature to Concourse. You could |
| 11 | +even recreate Concourse's [RBAC system](../auth-and-teams/user-roles.md) using OPA. |
| 12 | + |
| 13 | +More likely use-cases are to enforce rules your organization may have, such as not using certain container images or |
| 14 | +disallowing the use of privileged workloads. With OPA you can be as general or fine-grained as you want, enforcing these |
| 15 | +rules at the team or pipeline level. |
| 16 | + |
| 17 | +The next few sections explain how to configure Concourse to talk to an OPA server and how to write OPA rules for |
| 18 | +Concourse. |
| 19 | + |
5 | 20 | ## Configuring Concourse |
6 | 21 |
|
| 22 | +There are four configuration options you need to set on the `concourse web` nodes to have them interact with OPA. |
| 23 | + |
| 24 | +`CONCOURSE_OPA_URL`: The OPA policy check endpoint. |
| 25 | + |
| 26 | +: Should point to a specific `package/rule` that contains all Concourse rules for your cluster. |
| 27 | + |
| 28 | +: _Example_: `http://opa-endpoint.com/v1/data/concourse/decision` |
| 29 | + |
| 30 | +`CONCOURSE_POLICY_CHECK_FILTER_HTTP_METHOD`: API http methods to go through policy check. |
| 31 | + |
| 32 | +: You will need to make sure these match up with an API action in the next two configuration options. |
| 33 | + |
| 34 | +: _Example_: `PUT,POST` |
| 35 | + |
| 36 | +`CONCOURSE_POLICY_CHECK_FILTER_ACTION`: Actions in this list will go through policy check. |
| 37 | + |
| 38 | +: _Example_: `ListWorkers,ListContainers` |
| 39 | + |
| 40 | +`CONCOURSE_POLICY_CHECK_FILTER_ACTION_SKIP`: Actions in this list will not go through policy check |
| 41 | + |
| 42 | +: _Example_: `PausePipeline,UnpausePipeline` |
| 43 | + |
| 44 | +For the last three configuration options you can refer |
| 45 | +to [this list of routes](https://github.com/concourse/concourse/blob/master/atc/routes.go) for a list of API actions and |
| 46 | +their respective HTTP method. There are also some [Special Actions](#special-actions) not directly in the API. |
| 47 | + |
7 | 48 | ## Writing OPA Rules |
8 | 49 |
|
| 50 | +On the OPA server you'll need to create a package and policy for Concourse. This should match up with the endpoint |
| 51 | +provided to Concourse. The [OPA documentation](https://www.openpolicyagent.org/docs/latest/) has a good guide explaining |
| 52 | +how to generally write OPA rules and set up an OPA server. |
| 53 | + |
| 54 | +For any actions that Concourse has been configured to filter it will send a JSON request to the OPA server with the |
| 55 | +following details. Top-level data directly under the `input` key will be present for most actions. The information under |
| 56 | +the `data` key will differ based on the action being checked. |
| 57 | + |
| 58 | +This sample JSON payload is what OPA is sent when a user sets a pipeline. The `data` key contains the pipeline in JSON |
| 59 | +format. |
| 60 | + |
| 61 | +```json |
| 62 | +{ |
| 63 | + "input": { |
| 64 | + "service": "concourse", |
| 65 | + "cluster_name": "dev", |
| 66 | + "cluster_version": "7.4.0", |
| 67 | + "http_method": "PUT", |
| 68 | + "action": "SaveConfig", |
| 69 | + "user": "test", |
| 70 | + "team": "main", |
| 71 | + "pipeline": "check-pipeline", |
| 72 | + "data": { |
| 73 | + "jobs": [ |
| 74 | + { |
| 75 | + "name": "test", |
| 76 | + "plan": [ |
| 77 | + { |
| 78 | + "get": "tiny" |
| 79 | + }, |
| 80 | + { |
| 81 | + "config": { |
| 82 | + "image_resource": { |
| 83 | + "source": { |
| 84 | + "repository": "busybox" |
| 85 | + }, |
| 86 | + "type": "registry-image" |
| 87 | + }, |
| 88 | + "platform": "linux", |
| 89 | + "run": { |
| 90 | + "args": [ |
| 91 | + "-exc", |
| 92 | + "echo hello" |
| 93 | + ], |
| 94 | + "path": "sh" |
| 95 | + } |
| 96 | + }, |
| 97 | + "task": "a-task" |
| 98 | + } |
| 99 | + ] |
| 100 | + } |
| 101 | + ] |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +An OPA rule can respond to Concourse with three fields: |
| 108 | + |
| 109 | +<div class="annotate" markdown> |
| 110 | + |
| 111 | +* `allowed` (_required_): Boolean type. Setting to `False` will deny the action unless the `block` field is `False`. |
| 112 | +* `block` (_optional_): Boolean type. If set to `False` and `allowed` is `True` this creates a soft-policy enforcement. |
| 113 | + The action will be allowed and the `reasons` will still be printed to the web UI like a warning message. (1) |
| 114 | +* `reasons` (_optional_): List of string type. If an action is denied based on the `allowed` field then the reason(s) |
| 115 | + will be displayed in the UI. |
| 116 | + |
| 117 | +</div> |
| 118 | + |
| 119 | +1. Not setting `block` is the same as setting `"block": true`. |
| 120 | + |
| 121 | +Here is an example OPA policy. By default, it will allow whatever action it has been sent. It will deny the action if |
| 122 | +one or more of the three deny rules are true. |
| 123 | + |
| 124 | +```rego title="concourse.rego" linenums="1" |
| 125 | +package concourse |
| 126 | +
|
| 127 | +default decision = {"allowed": true} |
| 128 | +
|
| 129 | +decision = {"allowed": false, "reasons": reasons} { |
| 130 | + count(deny) > 0 |
| 131 | + reasons := deny |
| 132 | +} |
| 133 | +
|
| 134 | +deny["cannot use docker-image types"] { |
| 135 | + input.action == "UseImage" |
| 136 | + input.data.image_type == "docker-image" |
| 137 | +} |
| 138 | +
|
| 139 | +deny["cannot run privileged tasks"] { |
| 140 | + input.action == "SaveConfig" |
| 141 | + input.data.jobs[_].plan[_].privileged |
| 142 | +} |
| 143 | +
|
| 144 | +deny["cannot use privileged resource types"] { |
| 145 | + input.action == "SaveConfig" |
| 146 | + input.data.resource_types[_].privileged |
| 147 | +} |
| 148 | +``` |
| 149 | + |
9 | 150 | ## Special Actions |
10 | 151 |
|
| 152 | +Most of the actions you can filter for come directly from the list |
| 153 | +of [API actions](../auth-and-teams/user-roles.md#action-matrix). There are currently two special actions you can also |
| 154 | +filter on. |
| 155 | + |
11 | 156 | ### `UseImage` |
12 | 157 |
|
13 | | -### `SetPipeline` |
| 158 | +Before Concourse starts a container you can check what image it is going to use to create the container. Depending on |
| 159 | +the `image_type` the `image_source` field may contain other fields. The JSON payload for this action will look similar |
| 160 | +to the following example: |
| 161 | + |
| 162 | +```json |
| 163 | +{ |
| 164 | + "input": { |
| 165 | + "service": "concourse", |
| 166 | + "cluster_name": "dev", |
| 167 | + "cluster_version": "7.4.0", |
| 168 | + "action": "UseImage", |
| 169 | + "team": "main", |
| 170 | + "pipeline": "simple", |
| 171 | + "data": { |
| 172 | + "image_type": "registry-image", |
| 173 | + "privileged": true, |
| 174 | + "image_source": { |
| 175 | + "repository": "alpine", |
| 176 | + "tag": "latest" |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +### `SetPipeline` |
| 184 | + |
| 185 | +This action occurs whenever a [`set_pipeline` step](../steps/set-pipeline.md) is run. The JSON payload for this action |
| 186 | +will contain the pipeline config in JSON format under the `data` key: |
| 187 | + |
| 188 | +```json |
| 189 | +{ |
| 190 | + "input": { |
| 191 | + "service": "concourse", |
| 192 | + "cluster_name": "dev", |
| 193 | + "cluster_version": "7.4.0", |
| 194 | + "action": "SetPipeline", |
| 195 | + "team": "main", |
| 196 | + "pipeline": "simple", |
| 197 | + "data": { |
| 198 | + "jobs": [ |
| 199 | + { |
| 200 | + "name": "test", |
| 201 | + "plan": [ |
| 202 | + { |
| 203 | + "get": "tiny" |
| 204 | + }, |
| 205 | + { |
| 206 | + "config": { |
| 207 | + "image_resource": { |
| 208 | + "source": { |
| 209 | + "repository": "busybox" |
| 210 | + }, |
| 211 | + "type": "registry-image" |
| 212 | + }, |
| 213 | + "platform": "linux", |
| 214 | + "run": { |
| 215 | + "args": [ |
| 216 | + "-exc", |
| 217 | + "echo hello" |
| 218 | + ], |
| 219 | + "path": "sh" |
| 220 | + } |
| 221 | + }, |
| 222 | + "task": "a-task" |
| 223 | + } |
| 224 | + ] |
| 225 | + } |
| 226 | + ] |
| 227 | + } |
| 228 | + } |
| 229 | +} |
| 230 | +``` |
0 commit comments