Skip to content

Commit

Permalink
Merge pull request #1 from kubewarden/implement-policy
Browse files Browse the repository at this point in the history
Initial implementation of the policy
  • Loading branch information
flavio authored Dec 1, 2021
2 parents c81b36f + 49e2b4d commit 2168f80
Show file tree
Hide file tree
Showing 11 changed files with 642 additions and 231 deletions.
File renamed without changes.
428 changes: 428 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ crate-type = ["cdylib"]
[dependencies]
k8s-openapi = { version = "0.11.0", features = ["v1_20"] }
kubewarden-policy-sdk = "0.2.3"
lazy_static = "1.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
slog = "2.7"
wapc-guest = "0.4.0"
wapc-guest = "0.4.0"
52 changes: 27 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
Please, note well: this file and the scaffold were generated from [a
template](https://github.com/kubewarden/policy-rust-template). Make
this project yours!
Continuous integration | License
-----------------------|--------
![Continuous integration](https://github.com/kubewarden/readonly-root-filesystem-psp-policy/workflows/Continuous%20integration/badge.svg) | [![License: Apache 2.0](https://img.shields.io/badge/License-Apache2.0-brightgreen.svg)](https://opensource.org/licenses/Apache-2.0)

# Kubewarden policy readonly-root-filesystem-psp-policy

## Description
This Kubewarden Policy is a replacement for the Kubernetes Pod Security Policy
that enforces the usage of [`ReadOnlyRootFilesystems`](https://kubernetes.io/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems).

This policy will reject pods that have a name `invalid-pod-name`. If
the pod to be validated has a different name, or if a different type
of resource is evaluated, it will be accepted.
# How the policy works

## Settings
The policy inspects the `securityContext` of each container defined inside of
a Pod and ensures all the containers have the `readOnlyRootFilesystem` attribute
set to `true`.

This policy has no configurable settings. This would be a good place
to document if yours does, and what behaviors can be configured by
tweaking them.
The policy checks the both the `pod.spec.containers` and the init containers
too.

## License
Containers that do not have a `securityContext` defined are rejected too.
That happens because, by default, the root filesystem of a container is
considered to be writable.

```
Copyright (C) 2021 Flavio Castelli <[email protected]>
Ephemeral containers are not checked because, by Kubernetes definition, they
cannot have a `securityContext`.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
# Configuration

http://www.apache.org/licenses/LICENSE-2.0
The policy doesn't have any configuration.

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```
# Obtain policy

The policy is automatically published as an OCI artifact inside of
[this](https://github.com/orgs/kubewarden/packages/container/package/policies%2Freadonly-root-filesystem-psp-policy)
container registry.

# Using the policy

The easiest way to use this policy is through the [kubewarden-controller](https://github.com/kubewarden/kubewarden-controller).
7 changes: 5 additions & 2 deletions hub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ download:
# Important: leave the __TAG__ around: this is automatically replaced with the value of the git tag
registry: ghcr.io/kubewarden/policies/readonly-root-filesystem-psp-policy:__TAG__
# url is optional
url: https://github.com/yourorg/readonly-root-filesystem-psp-policy/releases/download/__TAG__/policy.wasm
url: https://github.com/kubewarden/readonly-root-filesystem-psp-policy/releases/download/__TAG__/policy.wasm
keywords:
- this is freeform
- PSP
- Container
- filesystem
- Volume
resources:
- Pod
mutation: false
Expand Down
26 changes: 20 additions & 6 deletions metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@ rules:
- apiGroups: [""]
apiVersions: ["v1"]
resources: ["pods"]
operations: ["CREATE"]
operations: ["CREATE", "UPDATE"]
mutating: false
contextAware: false
executionMode: kubewarden-wapc
annotations:
io.kubewarden.policy.title: readonly-root-filesystem-psp-policy
io.kubewarden.policy.description: Short description
io.kubewarden.policy.description: Enforce all the containers to have a readonly root filesystem
io.kubewarden.policy.author: Flavio Castelli <[email protected]>
io.kubewarden.policy.url: https://github.com/yourorg/readonly-root-filesystem-psp-policy
io.kubewarden.policy.source: https://github.com/yourorg/readonly-root-filesystem-psp-policy
io.kubewarden.policy.url: https://github.com/kubewarden/readonly-root-filesystem-psp-policy
io.kubewarden.policy.source: https://github.com/kubewarden/readonly-root-filesystem-psp-policy
io.kubewarden.policy.license: Apache-2.0
io.kubewarden.policy.usage: |
Long explaination.
The policy inspects the `securityContext` of each container defined inside of
a Pod and ensures all the containers have the `readOnlyRootFilesystem` attribute
set to `true`.
**Note well:** this can be Markdown text
The policy checks the both the `pod.spec.containers` and the init containers
too.
Containers that do not have a `securityContext` defined are rejected too.
That happens because, by default, the root filesystem of a container is
considered to be writable.
Ephemeral containers are not checked because, by Kubernetes definition, they
cannot have a `securityContext`.
## Configuration
The policy doesn't have any configuration.
Loading

0 comments on commit 2168f80

Please sign in to comment.