Skip to content

feat: add psp-privileged-containers module #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2023
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
7 changes: 7 additions & 0 deletions psp-privileged-containers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Introduction

`psp-privileged-containers` is a kcl PSP validation package.

## Resource

Code source and document is [here](https://github.com/kcl-lang/artifacthub/tree/main/psp-privileged-containers)
5 changes: 5 additions & 0 deletions psp-privileged-containers/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "psp-privileged-containers"
version = "0.1.0"
description = "`psp-privileged-containers` is a kcl validation package"

Empty file.
42 changes: 42 additions & 0 deletions psp-privileged-containers/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Controls the ability of any container to enable privileged mode.
Corresponds to the `privileged` field in a PodSecurityPolicy. For more
information, see
https://kubernetes.io/docs/concepts/policy/pod-security-policy/#privileged
"""

schema Params:
exemptImages?: [str]

params: Params = option("params")
exemptImages: [str] = params?.exemptImages or []

is_exempt = lambda image: str -> bool {
result = False
if exemptImages:
result = any exempt_image in exemptImages {
(image.startswith(exempt_image.removesuffix("*")) if exempt_image.endswith("*") else exempt_image == image)
}
result
}

violation = lambda container: {str:} {
msg = "only read-only root filesystem container is allowed: ${container.name}"
assert not container.securityContext?.privileged, msg
msg
}

# Define the validation function
validate = lambda item: {str:} {
containers: [{str:}] = []
if item.kind == "Pod":
containers = (item.spec.containers or []) + (item.spec.initContainers or []) + (item.spec.ephemeralContainers or [])
elif item.kind == "Deployment":
containers = (item.spec.template.spec.containers or []) + (item.spec.template.spec.initContainers or []) + (item.spec.template.spec.ephemeralContainers or [])
if containers:
containers = [c for c in containers if not is_exempt(c.image)]
container_list_disallow = [c.name for c in containers if not violation(c)]
# Return the resource
item
}
# Validate All resource
items = [validate(i) for i in option("items")]