Skip to content

Commit

Permalink
Merge pull request #42 from kcl-lang/psp-forbidden-sysctls
Browse files Browse the repository at this point in the history
feat: add psp-forbidden-sysctls modules
  • Loading branch information
Peefy authored Oct 31, 2023
2 parents cb4773d + 6fed26f commit f90bd16
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions psp-forbidden-sysctls/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Introduction

`psp-forbidden-sysctls` is a kcl PSP validation package.

## Resource

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

Empty file.
39 changes: 39 additions & 0 deletions psp-forbidden-sysctls/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Controls the `sysctl` profile used by containers. Corresponds to the
`allowedUnsafeSysctls` and `forbiddenSysctls` fields in a PodSecurityPolicy.
When specified, any sysctl not in the `allowedSysctls` parameter is considered to be forbidden.
The `forbiddenSysctls` parameter takes precedence over the `allowedSysctls` parameter.
For more information, see https://kubernetes.io/docs/tasks/administer-cluster/sysctl-cluster/
"""

schema Params:
allowedSysctls?: [str]
forbiddenSysctls?: [str]

params: Params = option("params")
allowedSysctls: [str] = params?.allowedSysctls or []
forbiddenSysctls: [str] = params?.forbiddenSysctls or []
forbiddenAll = any sysctl in forbiddenSysctls {
sysctl == "*"
}
allowAll = any sysctl in allowedSysctls {
sysctl == "*"
}

# Define the validation function
validate = lambda item: {str:} {
if item.kind == "Pod":
name = item.metadata.name
sysctl_names = [sysctl.name for sysctl in item.spec.securityContext.sysctls]
if any sysctl in sysctl_names {
sysctl in forbiddenSysctls
} or forbiddenAll:
assert False, "The sysctl {} is not allowed, pod: {}. Forbidden sysctls: {}".format(sysctl_names, name, forbiddenSysctls)
if not (all sysctl in sysctl_names {
sysctl in allowedSysctls
} or allowAll):
assert False, "The sysctl {} is not explicitly allowed, pod: {}. Allowed sysctls: {}".format(sysctl_names, name, allowedSysctls)
# Return the resource
item
}
# Validate All resource
items = [validate(i) for i in option("items")]

0 comments on commit f90bd16

Please sign in to comment.