Datasets:
File size: 14,788 Bytes
df0899b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | ---
id: users
title: Allowed Users
---
# Allowed Users
**Bundles:** `pod-security-restricted`
## Description
Controls the user and group IDs of the container and some volumes. Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and `fsGroup` fields in a PodSecurityPolicy. For more information, see https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups
## Template
```yaml
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: k8spspallowedusers
annotations:
metadata.gatekeeper.sh/title: "Allowed Users"
metadata.gatekeeper.sh/version: 1.0.3
metadata.gatekeeper.sh/bundle: "pod-security-restricted"
description: >-
Controls the user and group IDs of the container and some volumes.
Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and
`fsGroup` fields in a PodSecurityPolicy. For more information, see
https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups
spec:
crd:
spec:
names:
kind: K8sPSPAllowedUsers
validation:
openAPIV3Schema:
type: object
description: >-
Controls the user and group IDs of the container and some volumes.
Corresponds to the `runAsUser`, `runAsGroup`, `supplementalGroups`, and
`fsGroup` fields in a PodSecurityPolicy. For more information, see
https://kubernetes.io/docs/concepts/policy/pod-security-policy/#users-and-groups
properties:
exemptImages:
description: >-
Any container that uses an image that matches an entry in this list will be excluded
from enforcement. Prefix-matching can be signified with `*`. For example: `my-image-*`.
It is recommended that users use the fully-qualified Docker image name (e.g. start with a domain name)
in order to avoid unexpectedly exempting images from an untrusted repository.
type: array
items:
type: string
runAsUser:
type: object
description: "Controls which user ID values are allowed in a Pod or container-level SecurityContext."
properties:
rule:
type: string
description: "A strategy for applying the runAsUser restriction."
enum:
- MustRunAs
- MustRunAsNonRoot
- RunAsAny
ranges:
type: array
description: "A list of user ID ranges affected by the rule."
items:
type: object
description: "The range of user IDs affected by the rule."
properties:
min:
type: integer
description: "The minimum user ID in the range, inclusive."
max:
type: integer
description: "The maximum user ID in the range, inclusive."
runAsGroup:
type: object
description: "Controls which group ID values are allowed in a Pod or container-level SecurityContext."
properties:
rule:
type: string
description: "A strategy for applying the runAsGroup restriction."
enum:
- MustRunAs
- MayRunAs
- RunAsAny
ranges:
type: array
description: "A list of group ID ranges affected by the rule."
items:
type: object
description: "The range of group IDs affected by the rule."
properties:
min:
type: integer
description: "The minimum group ID in the range, inclusive."
max:
type: integer
description: "The maximum group ID in the range, inclusive."
supplementalGroups:
type: object
description: "Controls the supplementalGroups values that are allowed in a Pod or container-level SecurityContext."
properties:
rule:
type: string
description: "A strategy for applying the supplementalGroups restriction."
enum:
- MustRunAs
- MayRunAs
- RunAsAny
ranges:
type: array
description: "A list of group ID ranges affected by the rule."
items:
type: object
description: "The range of group IDs affected by the rule."
properties:
min:
type: integer
description: "The minimum group ID in the range, inclusive."
max:
type: integer
description: "The maximum group ID in the range, inclusive."
fsGroup:
type: object
description: "Controls the fsGroup values that are allowed in a Pod or container-level SecurityContext."
properties:
rule:
type: string
description: "A strategy for applying the fsGroup restriction."
enum:
- MustRunAs
- MayRunAs
- RunAsAny
ranges:
type: array
description: "A list of group ID ranges affected by the rule."
items:
type: object
description: "The range of group IDs affected by the rule."
properties:
min:
type: integer
description: "The minimum group ID in the range, inclusive."
max:
type: integer
description: "The maximum group ID in the range, inclusive."
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8spspallowedusers
import data.lib.exclude_update.is_update
import data.lib.exempt_container.is_exempt
violation[{"msg": msg}] {
# runAsUser, runAsGroup, supplementalGroups, fsGroup fields are immutable.
not is_update(input.review)
fields := ["runAsUser", "runAsGroup", "supplementalGroups", "fsGroup"]
field := fields[_]
container := input_containers[_]
not is_exempt(container)
msg := get_type_violation(field, container)
}
get_type_violation(field, container) = msg {
field == "runAsUser"
params := input.parameters[field]
msg := get_user_violation(params, container)
}
get_type_violation(field, container) = msg {
field != "runAsUser"
params := input.parameters[field]
msg := get_violation(field, params, container)
}
# RunAsUser (separate due to "MustRunAsNonRoot")
get_user_violation(params, container) = msg {
rule := params.rule
provided_user := get_field_value("runAsUser", container, input.review)
not accept_users(rule, provided_user)
msg := sprintf("Container %v is attempting to run as disallowed user %v. Allowed runAsUser: %v", [container.name, provided_user, params])
}
get_user_violation(params, container) = msg {
not get_field_value("runAsUser", container, input.review)
params.rule = "MustRunAs"
msg := sprintf("Container %v is attempting to run without a required securityContext/runAsUser", [container.name])
}
get_user_violation(params, container) = msg {
params.rule = "MustRunAsNonRoot"
not get_field_value("runAsUser", container, input.review)
not get_field_value("runAsNonRoot", container, input.review)
msg := sprintf("Container %v is attempting to run without a required securityContext/runAsNonRoot or securityContext/runAsUser != 0", [container.name])
}
accept_users("RunAsAny", _)
accept_users("MustRunAsNonRoot", provided_user) := provided_user != 0
accept_users("MustRunAs", provided_user) := res {
ranges := input.parameters.runAsUser.ranges
res := is_in_range(provided_user, ranges)
}
# Group Options
get_violation(field, params, container) = msg {
rule := params.rule
provided_value := get_field_value(field, container, input.review)
not is_array(provided_value)
not accept_value(rule, provided_value, params.ranges)
msg := sprintf("Container %v is attempting to run as disallowed group %v. Allowed %v: %v", [container.name, provided_value, field, params])
}
# SupplementalGroups is array value
get_violation(field, params, container) = msg {
rule := params.rule
array_value := get_field_value(field, container, input.review)
is_array(array_value)
provided_value := array_value[_]
not accept_value(rule, provided_value, params.ranges)
msg := sprintf("Container %v is attempting to run with disallowed supplementalGroups %v. Allowed %v: %v", [container.name, array_value, field, params])
}
get_violation(field, params, container) = msg {
not get_field_value(field, container, input.review)
params.rule == "MustRunAs"
msg := sprintf("Container %v is attempting to run without a required securityContext/%v. Allowed %v: %v", [container.name, field, field, params])
}
accept_value("RunAsAny", _, _)
accept_value("MayRunAs", provided_value, ranges) := is_in_range(provided_value, ranges)
accept_value("MustRunAs", provided_value, ranges) := is_in_range(provided_value, ranges)
# If container level is provided, that takes precedence
get_field_value(field, container, _) := get_seccontext_field(field, container)
# If no container level exists, use pod level
get_field_value(field, container, review) = out {
not has_seccontext_field(field, container)
review.kind.kind == "Pod"
pod_value := get_seccontext_field(field, review.object.spec)
out := pod_value
}
# Helper Functions
is_in_range(val, ranges) = res {
matching := {1 | val >= ranges[j].min; val <= ranges[j].max}
res := count(matching) > 0
}
has_seccontext_field(field, obj) {
get_seccontext_field(field, obj)
}
has_seccontext_field(field, obj) {
get_seccontext_field(field, obj) == false
}
get_seccontext_field(field, obj) = out {
out = obj.securityContext[field]
}
input_containers[c] {
c := input.review.object.spec.containers[_]
}
input_containers[c] {
c := input.review.object.spec.initContainers[_]
}
input_containers[c] {
c := input.review.object.spec.ephemeralContainers[_]
}
libs:
- |
package lib.exclude_update
is_update(review) {
review.operation == "UPDATE"
}
- |
package lib.exempt_container
is_exempt(container) {
exempt_images := object.get(object.get(input, "parameters", {}), "exemptImages", [])
img := container.image
exemption := exempt_images[_]
_matches_exemption(img, exemption)
}
_matches_exemption(img, exemption) {
not endswith(exemption, "*")
exemption == img
}
_matches_exemption(img, exemption) {
endswith(exemption, "*")
prefix := trim_suffix(exemption, "*")
startswith(img, prefix)
}
```
### Usage
```shell
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/library/pod-security-policy/users/template.yaml
```
## Examples
<details>
<summary>users-and-groups-together</summary>
<details>
<summary>constraint</summary>
```yaml
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPAllowedUsers
metadata:
name: psp-pods-allowed-user-ranges
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
runAsUser:
rule: MustRunAsNonRoot
runAsGroup:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
fsGroup:
rule: RunAsAny
```
Usage
```shell
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/library/pod-security-policy/users/samples/psp-pods-allowed-user-ranges/constraint.yaml
```
</details>
<details>
<summary>example-disallowed</summary>
```yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-users-disallowed
labels:
app: nginx-users
spec:
containers:
- name: nginx
image: nginx
securityContext:
runAsUser: 0
runAsGroup: 0
```
Usage
```shell
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/library/pod-security-policy/users/samples/psp-pods-allowed-user-ranges/example_disallowed.yaml
```
</details>
<details>
<summary>example-allowed</summary>
```yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-users-allowed
labels:
app: nginx-users
spec:
securityContext:
runAsNonRoot: true
containers:
- name: nginx
image: nginx
securityContext:
runAsUser: 1000
runAsGroup: 1000
```
Usage
```shell
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/library/pod-security-policy/users/samples/psp-pods-allowed-user-ranges/example_allowed.yaml
```
</details>
<details>
<summary>disallowed-ephemeral</summary>
```yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-users-disallowed
labels:
app: nginx-users
spec:
ephemeralContainers:
- name: nginx
image: nginx
securityContext:
runAsUser: 0
runAsGroup: 0
```
Usage
```shell
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/library/pod-security-policy/users/samples/psp-pods-allowed-user-ranges/disallowed_ephemeral.yaml
```
</details>
</details> |