Skip to content

Commit

Permalink
Make cluster role optional (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
day1118 committed Jun 18, 2021
1 parent b1d2dc2 commit 0a6219a
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 88 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ Multiple namespaces are supported and can be set as a comma-separated list: `ns1

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

- `rbac.create` controls if rbac resources are deployed.

- `rbac.clusterRole` controls if secrets generator has permission to watch secrets in namespaces other than where it has been deployed.

`rbac.clusterRole=false & watchNamespace=""` will result in `watchNamespace` being set to the current namespace as this is all the permissions will allow access to.

Afterwards, deploy the operator using:

1. Add the [Mittwald Charts Repo](https://github.com/mittwald/helm-charts/blob/master/README.md#usage):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,14 @@ Create the name of the service account to use
{{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}

{{/*
Define the namespace to watch
*/}}
{{- define "kubernetes-secret-generator.watchNamespace" -}}
{{- if and .Values.serviceAccount.create .Values.rbac.create (not .Values.rbac.clusterRole) -}}
{{ default .Values.watchNamespace .Release.Namespace }}
{{- else -}}
{{ .Values.watchNamespace }}
{{- end -}}
{{- end -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{- if and .Values.rbac.create .Values.rbac.clusterRole -}}
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: "mittwald:{{ include "kubernetes-secret-generator.serviceAccountName" . }}"
labels:
{{ include "kubernetes-secret-generator.labels" . | nindent 4 }}
rules:
# actual operator functionality
- apiGroups:
- ""
resources:
- secrets
verbs:
- get
- list
- watch
- update
{{- end -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{- if and .Values.rbac.create .Values.rbac.clusterRole -}}
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: "mittwald:{{ include "kubernetes-secret-generator.serviceAccountName" . }}"
labels:
{{ include "kubernetes-secret-generator.labels" . | nindent 4 }}
roleRef:
kind: ClusterRole
name: "mittwald:{{ include "kubernetes-secret-generator.serviceAccountName" . }}"
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
namespace: {{ .Release.Namespace | quote }}
name: {{ include "kubernetes-secret-generator.serviceAccountName" . }}
{{- end -}}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ spec:
periodSeconds: 3
env:
- name: WATCH_NAMESPACE
value: {{ .Values.watchNamespace }}
value: {{ template "kubernetes-secret-generator.watchNamespace" . }}
- name: POD_NAME
valueFrom:
fieldRef:
Expand Down
87 changes: 0 additions & 87 deletions deploy/helm-chart/kubernetes-secret-generator/templates/rbac.yaml

This file was deleted.

44 changes: 44 additions & 0 deletions deploy/helm-chart/kubernetes-secret-generator/templates/role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{{- if .Values.rbac.create -}}
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: "mittwald:{{ include "kubernetes-secret-generator.serviceAccountName" . }}"
labels:
{{ include "kubernetes-secret-generator.labels" . | nindent 4 }}
rules:
# leader election
- apiGroups:
- ""
resources:
- configmaps
verbs:
- create
- delete
- get
- apiGroups:
- ""
resources:
- pods
verbs:
- delete
- get
- apiGroups:
- monitoring.coreos.com
resources:
- servicemonitors
verbs:
- "get"
- "create"
{{- if and .Values.rbac.create (not .Values.rbac.clusterRole) }}
# Permissions to access secrets in this namespace if no cluster role is created.
- apiGroups:
- ""
resources:
- secrets
verbs:
- get
- list
- watch
- update
{{- end -}}
{{- end -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{- if .Values.rbac.create -}}
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: "mittwald:{{ include "kubernetes-secret-generator.serviceAccountName" . }}"
labels:
{{ include "kubernetes-secret-generator.labels" . | nindent 4 }}
roleRef:
kind: Role
name: "mittwald:{{ include "kubernetes-secret-generator.serviceAccountName" . }}"
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
namespace: {{ .Release.Namespace | quote }}
name: {{ include "kubernetes-secret-generator.serviceAccountName" . }}
{{- end -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "kubernetes-secret-generator.serviceAccountName" . }}
labels:
{{ include "kubernetes-secret-generator.labels" . | nindent 4 }}
{{- end -}}
11 changes: 11 additions & 0 deletions deploy/helm-chart/kubernetes-secret-generator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,15 @@ secretLength: 40
# Namespace that are watched for secret generation
# Accepts a comma-separated list of namespaces: ns1,ns2
# If set to "", all namespaces will be watched
# Accessing secrets in namespaces other than the deployed one requires permissions via a cluster role (on by default)
watchNamespace: ""

# RBAC parameteres
# https://kubernetes.io/docs/reference/access-authn-authz/rbac/
rbac:
# Disables creation of rbac resources
create: true
# The cluster role allows access to all namespaces in the cluster.
# Set to false to restrict access to the deployed namespace only.
# ClusterRole is deployed by Default
clusterRole: true

0 comments on commit 0a6219a

Please sign in to comment.