From def2eb0f42700b5b115de76451bd409c6430c42d Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Wed, 4 Sep 2024 15:14:50 +0200 Subject: [PATCH] Add passwords generation to FerretDB (#329) Signed-off-by: Andrei Kvapil ## Summary by CodeRabbit - **New Features** - Updated application version from 0.3.0 to 0.4.0, indicating a new release. - Introduced a new Kubernetes Role for managing access to dashboard-related resources, enhancing security. - Improved user credential management with dynamic password generation in the initialization script, enhancing security practices. Signed-off-by: Andrei Kvapil --- packages/apps/ferretdb/Chart.yaml | 2 +- .../templates/dashboard-resourcemap.yaml | 19 ++++++++++++ .../apps/ferretdb/templates/init-script.yaml | 29 ++++++++++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 packages/apps/ferretdb/templates/dashboard-resourcemap.yaml diff --git a/packages/apps/ferretdb/Chart.yaml b/packages/apps/ferretdb/Chart.yaml index 4e2a1a31..d23dfe5e 100644 --- a/packages/apps/ferretdb/Chart.yaml +++ b/packages/apps/ferretdb/Chart.yaml @@ -16,7 +16,7 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.3.0 +version: 0.4.0 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to diff --git a/packages/apps/ferretdb/templates/dashboard-resourcemap.yaml b/packages/apps/ferretdb/templates/dashboard-resourcemap.yaml new file mode 100644 index 00000000..a91a54ac --- /dev/null +++ b/packages/apps/ferretdb/templates/dashboard-resourcemap.yaml @@ -0,0 +1,19 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: {{ .Release.Name }}-dashboard-resources +rules: +- apiGroups: + - "" + resources: + - services + resourceNames: + - {{ .Release.Name }} + verbs: ["get", "list", "watch"] +- apiGroups: + - "" + resources: + - secrets + resourceNames: + - {{ .Release.Name }}-credentials + verbs: ["get", "list", "watch"] diff --git a/packages/apps/ferretdb/templates/init-script.yaml b/packages/apps/ferretdb/templates/init-script.yaml index 818ccf2d..beef5a5a 100644 --- a/packages/apps/ferretdb/templates/init-script.yaml +++ b/packages/apps/ferretdb/templates/init-script.yaml @@ -1,3 +1,30 @@ +{{- $existingSecret := lookup "v1" "Secret" .Release.Namespace (printf "%s-credentials" .Release.Name) }} +{{- $passwords := dict }} + +{{- with (index $existingSecret "data") }} + {{- range $k, $v := . }} + {{- $_ := set $passwords $k (b64dec $v) }} + {{- end }} +{{- end }} + +{{- range $user, $u := .Values.users }} + {{- if $u.password }} + {{- $_ := set $passwords $user $u.password }} + {{- else if not (index $passwords $user) }} + {{- $_ := set $passwords $user (randAlphaNum 16) }} + {{- end }} +{{- end }} + +{{- if .Values.users }} +apiVersion: v1 +kind: Secret +metadata: + name: {{ .Release.Name }}-credentials +stringData: + {{- range $user, $u := .Values.users }} + {{ quote $user }}: {{ quote (index $passwords $user) }} + {{- end }} +{{- end }} --- apiVersion: v1 kind: Secret @@ -13,7 +40,7 @@ stringData: {{- range $user, $u := .Values.users }} SELECT 'CREATE ROLE {{ $user }} LOGIN INHERIT;' WHERE NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '{{ $user }}')\gexec - ALTER ROLE {{ $user }} WITH PASSWORD '{{ $u.password }}' LOGIN INHERIT {{ ternary "REPLICATION" "NOREPLICATION" (default false $u.replication) }}; + ALTER ROLE {{ $user }} WITH PASSWORD '{{ index $passwords $user }}' LOGIN INHERIT {{ ternary "REPLICATION" "NOREPLICATION" (default false $u.replication) }}; COMMENT ON ROLE {{ $user }} IS 'user managed by helm'; {{- end }} EOT