Skip to content

Commit 0a6219a

Browse files
authored
Make cluster role optional (#38)
1 parent b1d2dc2 commit 0a6219a

File tree

10 files changed

+132
-88
lines changed

10 files changed

+132
-88
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ Multiple namespaces are supported and can be set as a comma-separated list: `ns1
2424

2525
If `watchNamespace` is set to the empty string value `""`, all namespaces will be watched.
2626

27+
- `rbac.create` controls if rbac resources are deployed.
28+
29+
- `rbac.clusterRole` controls if secrets generator has permission to watch secrets in namespaces other than where it has been deployed.
30+
31+
`rbac.clusterRole=false & watchNamespace=""` will result in `watchNamespace` being set to the current namespace as this is all the permissions will allow access to.
32+
2733
Afterwards, deploy the operator using:
2834

2935
1. Add the [Mittwald Charts Repo](https://github.com/mittwald/helm-charts/blob/master/README.md#usage):

deploy/helm-chart/kubernetes-secret-generator/templates/_helpers.tpl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,14 @@ Create the name of the service account to use
6161
{{ default "default" .Values.serviceAccount.name }}
6262
{{- end -}}
6363
{{- end -}}
64+
65+
{{/*
66+
Define the namespace to watch
67+
*/}}
68+
{{- define "kubernetes-secret-generator.watchNamespace" -}}
69+
{{- if and .Values.serviceAccount.create .Values.rbac.create (not .Values.rbac.clusterRole) -}}
70+
{{ default .Values.watchNamespace .Release.Namespace }}
71+
{{- else -}}
72+
{{ .Values.watchNamespace }}
73+
{{- end -}}
74+
{{- end -}}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{{- if and .Values.rbac.create .Values.rbac.clusterRole -}}
2+
kind: ClusterRole
3+
apiVersion: rbac.authorization.k8s.io/v1
4+
metadata:
5+
name: "mittwald:{{ include "kubernetes-secret-generator.serviceAccountName" . }}"
6+
labels:
7+
{{ include "kubernetes-secret-generator.labels" . | nindent 4 }}
8+
rules:
9+
# actual operator functionality
10+
- apiGroups:
11+
- ""
12+
resources:
13+
- secrets
14+
verbs:
15+
- get
16+
- list
17+
- watch
18+
- update
19+
{{- end -}}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{- if and .Values.rbac.create .Values.rbac.clusterRole -}}
2+
kind: ClusterRoleBinding
3+
apiVersion: rbac.authorization.k8s.io/v1
4+
metadata:
5+
name: "mittwald:{{ include "kubernetes-secret-generator.serviceAccountName" . }}"
6+
labels:
7+
{{ include "kubernetes-secret-generator.labels" . | nindent 4 }}
8+
roleRef:
9+
kind: ClusterRole
10+
name: "mittwald:{{ include "kubernetes-secret-generator.serviceAccountName" . }}"
11+
apiGroup: rbac.authorization.k8s.io
12+
subjects:
13+
- kind: ServiceAccount
14+
namespace: {{ .Release.Namespace | quote }}
15+
name: {{ include "kubernetes-secret-generator.serviceAccountName" . }}
16+
{{- end -}}

deploy/helm-chart/kubernetes-secret-generator/templates/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ spec:
4545
periodSeconds: 3
4646
env:
4747
- name: WATCH_NAMESPACE
48-
value: {{ .Values.watchNamespace }}
48+
value: {{ template "kubernetes-secret-generator.watchNamespace" . }}
4949
- name: POD_NAME
5050
valueFrom:
5151
fieldRef:

deploy/helm-chart/kubernetes-secret-generator/templates/rbac.yaml

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{{- if .Values.rbac.create -}}
2+
kind: Role
3+
apiVersion: rbac.authorization.k8s.io/v1
4+
metadata:
5+
name: "mittwald:{{ include "kubernetes-secret-generator.serviceAccountName" . }}"
6+
labels:
7+
{{ include "kubernetes-secret-generator.labels" . | nindent 4 }}
8+
rules:
9+
# leader election
10+
- apiGroups:
11+
- ""
12+
resources:
13+
- configmaps
14+
verbs:
15+
- create
16+
- delete
17+
- get
18+
- apiGroups:
19+
- ""
20+
resources:
21+
- pods
22+
verbs:
23+
- delete
24+
- get
25+
- apiGroups:
26+
- monitoring.coreos.com
27+
resources:
28+
- servicemonitors
29+
verbs:
30+
- "get"
31+
- "create"
32+
{{- if and .Values.rbac.create (not .Values.rbac.clusterRole) }}
33+
# Permissions to access secrets in this namespace if no cluster role is created.
34+
- apiGroups:
35+
- ""
36+
resources:
37+
- secrets
38+
verbs:
39+
- get
40+
- list
41+
- watch
42+
- update
43+
{{- end -}}
44+
{{- end -}}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{- if .Values.rbac.create -}}
2+
kind: RoleBinding
3+
apiVersion: rbac.authorization.k8s.io/v1
4+
metadata:
5+
name: "mittwald:{{ include "kubernetes-secret-generator.serviceAccountName" . }}"
6+
labels:
7+
{{ include "kubernetes-secret-generator.labels" . | nindent 4 }}
8+
roleRef:
9+
kind: Role
10+
name: "mittwald:{{ include "kubernetes-secret-generator.serviceAccountName" . }}"
11+
apiGroup: rbac.authorization.k8s.io
12+
subjects:
13+
- kind: ServiceAccount
14+
namespace: {{ .Release.Namespace | quote }}
15+
name: {{ include "kubernetes-secret-generator.serviceAccountName" . }}
16+
{{- end -}}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{{- if .Values.serviceAccount.create -}}
2+
apiVersion: v1
3+
kind: ServiceAccount
4+
metadata:
5+
name: {{ include "kubernetes-secret-generator.serviceAccountName" . }}
6+
labels:
7+
{{ include "kubernetes-secret-generator.labels" . | nindent 4 }}
8+
{{- end -}}

deploy/helm-chart/kubernetes-secret-generator/values.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,15 @@ secretLength: 40
5252
# Namespace that are watched for secret generation
5353
# Accepts a comma-separated list of namespaces: ns1,ns2
5454
# If set to "", all namespaces will be watched
55+
# Accessing secrets in namespaces other than the deployed one requires permissions via a cluster role (on by default)
5556
watchNamespace: ""
57+
58+
# RBAC parameteres
59+
# https://kubernetes.io/docs/reference/access-authn-authz/rbac/
60+
rbac:
61+
# Disables creation of rbac resources
62+
create: true
63+
# The cluster role allows access to all namespaces in the cluster.
64+
# Set to false to restrict access to the deployed namespace only.
65+
# ClusterRole is deployed by Default
66+
clusterRole: true

0 commit comments

Comments
 (0)