Skip to content

Commit

Permalink
implement ns-authz
Browse files Browse the repository at this point in the history
  • Loading branch information
edobry committed Jan 31, 2025
1 parent 4b4258e commit 491cf57
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ns-authz/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v2
name: ns-authz
description: A Helm chart to automatically grant cluster-admin to the default ServiceAccount in every new namespace.
type: application
version: 0.1.0
appVersion: "1.0.0"
35 changes: 35 additions & 0 deletions ns-authz/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ns-authz Helm Chart

This chart deploys a lightweight namespace watcher that automatically grants the \`cluster-admin\` role to the default ServiceAccount in every new namespace.

## Features

- Automatic Namespace Detection
- RoleBinding Creation for each new namespace
- Idempotent operation
- Minimal logging
- Self-contained using a lightweight kubectl image
- Helm-based install/uninstall

## Installation

```bash
helm install ns-authz ./ns-authz --namespace kube-system
```

## Uninstallation

```bash
helm uninstall ns-authz --namespace kube-system
```

## Verification

1. Create a new namespace:
```bash
kubectl create namespace test-ns
```
2. Check the watcher pod logs:
```bash
kubectl logs -l app=ns-authz -n kube-system
```
12 changes: 12 additions & 0 deletions ns-authz/templates/clusterrolebinding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: ns-authz-crb
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: {{ .Values.serviceAccount.name }}
namespace: {{ .Release.Namespace }}
7 changes: 7 additions & 0 deletions ns-authz/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: ns-authz-script
data:
watcher.sh: |-
{{ (.Files.Get "watcher.sh") | indent 4 }}
32 changes: 32 additions & 0 deletions ns-authz/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: ns-authz
labels:
app: ns-authz
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: ns-authz
template:
metadata:
labels:
app: ns-authz
spec:
serviceAccountName: {{ .Values.serviceAccount.name }}
containers:
- name: ns-authz
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["/bin/sh", "/scripts/watcher.sh"]
volumeMounts:
- name: script-volume
mountPath: /scripts
volumes:
- name: script-volume
configMap:
name: ns-authz-script
nodeSelector: {{ toYaml .Values.nodeSelector | nindent 8 }}
tolerations: {{ toYaml .Values.tolerations | nindent 8 }}
affinity: {{ toYaml .Values.affinity | nindent 8 }}
7 changes: 7 additions & 0 deletions ns-authz/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ .Values.serviceAccount.name }}
namespace: {{ .Release.Namespace }}
{{- end }}
18 changes: 18 additions & 0 deletions ns-authz/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
replicaCount: 1

image:
repository: bitnami/kubectl
tag: latest
pullPolicy: IfNotPresent

serviceAccount:
name: ns-authz-sa
create: true

resources: {}

nodeSelector: {}

tolerations: []

affinity: {}
32 changes: 32 additions & 0 deletions ns-authz/watcher.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

ROLEBINDING_NAME="cluster-admin-binding"

echo "starting namespace watcher..."

ensureClusterRoleBinding() {
local ns="$1"
local sa="$2"

# Strip off the leading 'namespace/'
local nsName="${1#namespace/}"
local clusterRoleBindingName="${ROLEBINDING_NAME}-${nsName}-${sa}"

echo "ensuring CRB '$clusterRoleBindingName'..."

if kubectl get clusterrolebinding "$clusterRoleBindingName" -o name >/dev/null 2>&1; then
echo "CRB already exists, skipping"
return
fi

echo "creating CRB '$clusterRoleBindingName'..."

kubectl create clusterrolebinding "$clusterRoleBindingName" \
--clusterrole=cluster-admin \
--serviceaccount="${nsName}:${sa}"
}

kubectl get namespaces --watch -o name | while read ns; do
ensureClusterRoleBinding $ns "default"
ensureClusterRoleBinding $ns "kurtosis-api"
done

0 comments on commit 491cf57

Please sign in to comment.