Skip to content

Commit 36215c9

Browse files
committed
feat: adding a script to generate the kyverno policies (probably needs to be moved into a partial)
1 parent 28031e2 commit 36215c9

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

scripts/generate-policies.sh

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env bash
2+
#
3+
## This script generates the policies for the kyverno policies directory
4+
#
5+
set -e
6+
7+
# Set colors for terminal output
8+
GREEN='\033[0;32m'
9+
NC='\033[0m' # No Color
10+
11+
cat <<EOF
12+
# Kyverno Policies
13+
14+
## Overview
15+
16+
Kyverno is a policy engine designed for Kubernetes that validates, mutates, and generates configurations using policies as Kubernetes resources. It provides key features like:
17+
18+
- Policy validation and enforcement
19+
- Resource mutation and generation
20+
- Image verification and security controls
21+
- Audit logging and reporting
22+
- Admission control webhooks
23+
24+
The following policies are shipped by default in this platform to enforce security best practices, resource management, and operational standards.
25+
26+
For detailed information about Kyverno's capabilities, refer to the [official documentation](https://kyverno.io/docs/) or [policy library](https://kyverno.io/policies/).
27+
28+
---
29+
EOF
30+
31+
# Find all policy.yaml files in the kyverno directory
32+
POLICY_FILES=$(find addons/kustomize/oss/kyverno -name "policy.yaml")
33+
34+
if [ -z "$POLICY_FILES" ]; then
35+
echo "No policy files found at addons/kustomize/oss/kyverno/**/policy.yaml"
36+
exit 1
37+
fi
38+
39+
# Counter for policies
40+
COUNT=0
41+
42+
for POLICY_FILE in $POLICY_FILES; do
43+
# Get policy directory name which often indicates the purpose
44+
POLICY_DIR=$(dirname "$POLICY_FILE" | xargs basename)
45+
46+
# Extract policy information using yq
47+
POLICY_NAME=$(yq e '.metadata.name' "$POLICY_FILE")
48+
POLICY_KIND=$(yq e '.kind' "$POLICY_FILE")
49+
POLICY_DESCRIPTION=$(yq e '.metadata.annotations["policies.kyverno.io/description"] // "No description provided"' "$POLICY_FILE")
50+
POLICY_CATEGORY=$(yq e '.metadata.annotations["policies.kyverno.io/category"] // "Uncategorized"' "$POLICY_FILE")
51+
POLICY_SEVERITY=$(yq e '.metadata.annotations["policies.kyverno.io/severity"] // "medium"' "$POLICY_FILE")
52+
POLICY_RULES=$(yq e '.spec.rules[].name' "$POLICY_FILE")
53+
54+
# Determine if this is a cluster or namespaced policy
55+
POLICY_SCOPE="Namespaced"
56+
if [[ "$POLICY_KIND" == "ClusterPolicy" ]]; then
57+
POLICY_SCOPE="Cluster-wide"
58+
fi
59+
60+
# Output policy as markdown
61+
echo "## :material-shield-lock: Rule: $POLICY_NAME"
62+
echo ""
63+
echo "**Category:** $POLICY_CATEGORY | **Severity:** $POLICY_SEVERITY | **Scope:** $POLICY_SCOPE"
64+
echo ""
65+
echo "$POLICY_DESCRIPTION"
66+
echo ""
67+
68+
# Section for rules
69+
echo "**Rules**"
70+
echo ""
71+
for RULE in $POLICY_RULES; do
72+
RULE_TYPE=""
73+
if yq e '.spec.rules[] | select(.name == "'"$RULE"'") | has("validate")' "$POLICY_FILE" | grep -q "true"; then
74+
RULE_TYPE="Validation"
75+
elif yq e '.spec.rules[] | select(.name == "'"$RULE"'") | has("mutate")' "$POLICY_FILE" | grep -q "true"; then
76+
RULE_TYPE="Mutation"
77+
elif yq e '.spec.rules[] | select(.name == "'"$RULE"'") | has("generate")' "$POLICY_FILE" | grep -q "true"; then
78+
RULE_TYPE="Generation"
79+
fi
80+
81+
echo "- **$RULE** ($RULE_TYPE)"
82+
83+
# Try to extract match resources for additional context
84+
RESOURCES=$(yq e '.spec.rules[] | select(.name == "'"$RULE"'") | .match.resources.kinds[]' "$POLICY_FILE" 2>/dev/null || echo "")
85+
if [ ! -z "$RESOURCES" ]; then
86+
echo " - Applies to: $RESOURCES"
87+
fi
88+
89+
echo ""
90+
done
91+
92+
echo "---"
93+
echo ""
94+
95+
COUNT=$((COUNT + 1))
96+
done
97+
98+
echo "**Total Policies: $COUNT**"

0 commit comments

Comments
 (0)