Skip to content

Commit ed98fa6

Browse files
authored
feat: add kyverno policy for denying SCR based on packages (#20)
1 parent 7da50fe commit ed98fa6

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ Part of how the operator works is the [skyhook-agent](agent/README.md). Packages
4848
└── config.json
4949
```
5050

51+
## Example Kyverno Policy
52+
53+
This repository includes an example Kyverno policy that demonstrates how to restrict the images that can be used in Skyhook packages. While this is not a complete policy, it serves as a template that end users can modify to fit their security needs.
54+
55+
The policy prevents the creation of Skyhook resources that contain packages with restricted image patterns. Specifically, it blocks:
56+
- Images containing 'shellscript:' anywhere in the image name
57+
- Images from Docker Hub (matching 'docker.io/*')
58+
59+
If you are going to use kyverno make sure to turn on the creation of the skyhook-viewer-role in the values file for the operator. (rbac.createSkyhookViewerRole: true) and then bind kyverno to that role. Example policy:
60+
```
61+
apiVersion: rbac.authorization.k8s.io/v1
62+
kind: ClusterRoleBinding
63+
metadata:
64+
name: kyverno-skyhook-binding
65+
roleRef:
66+
apiGroup: rbac.authorization.k8s.io
67+
kind: ClusterRole
68+
name: skyhook-viewer-role
69+
subjects:
70+
- kind: ServiceAccount
71+
name: kyverno-reports-controller
72+
namespace: kyverno
73+
```
74+
5175
## [Skyhook-Operator](operator/README.md)
5276
The operator is a kbuernetes operator that monitors cluster events and coordinates the installation and lifecycle of Skyhook packages.
5377

kyverno/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Skyhook Kyverno Policies
2+
3+
This directory contains example [Kyverno](https://kyverno.io/) policies for Skyhook. These policies can be used to enforce security and best practices for Skyhook packages.
4+
5+
## Prerequisites
6+
7+
Before applying any policies, you need to have Kyverno installed in your cluster. You can install it using one of the following methods:
8+
9+
### Helm Installation (Recommended)
10+
11+
```bash
12+
helm repo add kyverno https://kyverno.github.io/kyverno/
13+
helm install kyverno kyverno/kyverno -n kyverno --create-namespace
14+
```
15+
16+
### Manual Installation
17+
18+
```bash
19+
kubectl apply -f https://raw.githubusercontent.com/kyverno/kyverno/main/definitions/install.yaml
20+
```
21+
22+
## Available Policies
23+
24+
### Restrict Package Images
25+
The `disable_packages.yaml` policy demonstrates how to restrict which container images can be used in Skyhook packages. This is particularly useful for:
26+
- Preventing the use of potentially dangerous images (e.g., those containing shell scripts)
27+
- Enforcing the use of approved container registries
28+
- Maintaining security standards across your cluster
29+
30+
To apply the policy:
31+
32+
```bash
33+
kubectl apply -f disable_packages.yaml
34+
```
35+
36+
The policy will prevent the creation of Skyhook resources that contain packages with restricted image patterns. Currently, it blocks:
37+
- Images containing 'shellscript' anywhere in the image name
38+
- Images from Docker Hub (matching 'docker.io/*')
39+
40+
## Testing the Policy
41+
42+
You can test the policy by trying to create a Skyhook resource with a restricted image. For example:
43+
44+
```yaml
45+
apiVersion: skyhook.nvidia.com/v1alpha1
46+
kind: Skyhook
47+
metadata:
48+
labels:
49+
app.kubernetes.io/part-of: skyhook-operator
50+
app.kubernetes.io/created-by: skyhook-operator
51+
name: test-scr
52+
spec:
53+
packages:
54+
shellscript:
55+
configMap:
56+
config.sh: |-
57+
#!/bin/bash
58+
echo "hello"
59+
image: shellscript
60+
version: 1.3.2
61+
62+
# This will be blocked by the policy
63+
```
64+
65+
The creation will be denied with an appropriate error message.
66+
67+
## Customizing Policies
68+
69+
The example policies are templates that you can modify to fit your security needs. Common customizations include:
70+
- Adding additional restricted image patterns
71+
- Modifying the validation rules
72+
- Adjusting the failure action (warn vs enforce)
73+
74+
See the [Kyverno documentation](https://kyverno.io/docs/) for more details on policy customization.
75+

kyverno/disable_packages.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This is an example to show how to restrict the images that can be used in a Skyhook package.
2+
# It is not a complete policy and it is expected end users will alter rules to fit their security needs.
3+
apiVersion: kyverno.io/v1
4+
kind: ClusterPolicy
5+
metadata:
6+
name: restrict-skyhook-images
7+
annotations:
8+
policies.kyverno.io/title: Restrict Skyhook Package Images
9+
policies.kyverno.io/category: Security
10+
policies.kyverno.io/severity: medium
11+
policies.kyverno.io/description: >-
12+
This policy prevents the creation of Skyhook resources that contain packages with
13+
restricted image patterns. Specifically, it blocks images containing 'shellscript'
14+
and images coming from docker hub.
15+
spec:
16+
validationFailureAction: Enforce
17+
background: true
18+
rules:
19+
- name: validate-package-images
20+
match:
21+
any:
22+
- resources:
23+
kinds:
24+
- Skyhook
25+
operations:
26+
- CREATE
27+
- UPDATE
28+
validate:
29+
message: "Package image matches restricted pattern. Images containing 'shellscript' or starting with 'docker.io/' are not allowed."
30+
deny:
31+
conditions:
32+
any:
33+
- key: "{{ regex_match('nvcr.io/nvidian/swgpu-baseos/shellscript', '{{request.object.spec.packages.*.image}}' ) }}"
34+
operator: Equals
35+
value: true
36+
- key: "{{ regex_match('docker.io/.*', '{{request.object.spec.packages.*.image}}' ) }}"
37+
operator: Equals
38+
value: true
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRoleBinding
3+
metadata:
4+
name: kyverno-skyhook-binding
5+
roleRef:
6+
apiGroup: rbac.authorization.k8s.io
7+
kind: ClusterRole
8+
name: skyhook-viewer-role
9+
subjects:
10+
- kind: ServiceAccount
11+
name: kyverno-reports-controller
12+
namespace: kyverno

0 commit comments

Comments
 (0)