Skip to content
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

Kyverno Policies used during demo #5141

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
39 changes: 39 additions & 0 deletions components/kyverno/policies/base/bootstrap-namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: bootstrap-namespace
spec:
admission: true
background: true
emitWarning: false
rules:
- generate:
apiVersion: rbac.authorization.k8s.io/v1
data:
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: tenant-admin-actions
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: '{{ request.object.metadata.annotations.username }}'
kind: RoleBinding
name: tenant-admin-actions-{{ request.object.metadata.annotations.username }}
namespace: '{{ request.object.metadata.name }}'
synchronize: true
match:
any:
- resources:
kinds:
- Namespace
operations:
- CREATE
name: give-admin-permissions
preconditions:
all:
- key: '{{ request.object.metadata.annotations.username_hash || '''' }}'
operator: NotEquals
value: ""
skipBackgroundRequests: true
validationFailureAction: Audit
65 changes: 65 additions & 0 deletions components/kyverno/policies/base/created-by-label-hash.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: label-namespace-with-creator
spec:
background: false
rules:
- name: add-created-by-label
match:
resources:
kinds:
- Namespace
operations:
- CREATE
preconditions:
any:
- key: "{{ konfluxConfig.data.sre_group_name }}"
operator: AllNotIn
value: "{{ request.userInfo.groups }}"
- key: "{{ request.object.metadata.annotations.username || '' }}"
operator: NotEquals
value: ""
all:
- key: "system:serviceaccounts"
operator: AllNotIn
value: "{{ request.userInfo.groups }}"
context:
- name: konfluxConfig
configMap:
name: konflux-config
namespace: kyverno
- name: isGroupMember
variable:
value: "{{ contains(request.userInfo.groups, konfluxConfig.data.sre_group_name) }}"
- name: userName
variable:
value: "{{ (isGroupMember &&
(request.object.metadata.annotations.username || request.userInfo.username)) ||
request.userInfo.username }}"
- name: userNameHash
variable:
value: "{{ sha256(userName) }}"
- name: userNameHash1
variable:
value: "{{ truncate(userNameHash, `32`) }}"
- name: userNameHash2
variable:
value: "{{ trim_prefix(userNameHash, userNameHash1) }}"
mutate:
patchesJson6902: |
- op: add
path: "/metadata/labels/username-hash-1"
value: "{{ userNameHash1 }}"
- op: add
path: "/metadata/labels/username-hash-2"
value: "{{ userNameHash2 }}"
- op: add
path: "/metadata/annotations/username"
value: "{{ userName }}"
- op: add
path: "/metadata/annotations/username_hash"
value: "{{ userNameHash }}"
- op: add
path: "/metadata/annotations/created-by"
value: "{{ request.userInfo.username }}"
8 changes: 8 additions & 0 deletions components/kyverno/policies/base/konflux-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: konflux-config
namespace: kyverno
data:
namespace_limit: '3'
sre_group_name: 'cluster-admins'
7 changes: 7 additions & 0 deletions components/kyverno/policies/base/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- bootstrap-namespace.yaml
- created-by-label-hash.yaml
- namespaces-limit.yaml
- konflux-config.yaml
53 changes: 53 additions & 0 deletions components/kyverno/policies/base/namespaces-limit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: namespace-limit
spec:
background: false
validationFailureAction: Enforce
rules:
- name: limit-user-namespaces
match:
resources:
kinds:
- Namespace
operations:
- CREATE
preconditions:
all:
- key: "{{ konfluxConfig.data.sre_group_name }}"
operator: NotIn
value: "{{ request.userInfo.groups }}"
- key: "system:serviceaccounts"
operator: NotIn
value: "{{ request.userInfo.groups }}"
context:
- name: konfluxConfig
configMap:
name: konflux-config
namespace: kyverno
- name: userName
variable:
value: "{{ request.userInfo.username }}"
- name: userNameHash
variable:
value: "{{ sha256(userName) }}"
- name: userNameHash1
variable:
value: "{{ truncate(userNameHash, `32`) }}"
- name: userNameHash2
variable:
value: "{{ trim_prefix(userNameHash, userNameHash1) }}"
- name: ownedNamespaces
apiCall:
urlPath: "/api/v1/namespaces?labelSelector=username-hash-1%3D{{ userNameHash1 }}%2Cusername-hash-2%3D{{ userNameHash2 }}"
- name: namespacesLimit
variable:
value: "{{ konfluxConfig.data.namespace_limit }}"
validate:
message: "Maximum number of namespaces ({{ namespacesLimit }}) reached for {{ userName }}"
deny:
conditions:
- key: "{{ length(ownedNamespaces.items) }}"
operator: GreaterThanOrEquals
value: "{{ namespacesLimit }}"
Loading