8000 🔒 Harden the dashboard and make provider failures visible by abahmed · Pull Request #602 · abahmed/kwatch · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
17 changes: 7 additions & 10 deletions deploy/chart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,26 +204,24 @@ Karpenter annotation with `--set-string`.
| `networkPolicy.healthIngressFrom` | Extra peers (`namespaceSelector`, `podSelector` or `ipBlock`) that may reach the health port (`/health`, `/metrics`, `/status`), such as your Prometheus namespace. Without it only pods in the release namespace (and `kubeletCIDRs`) can | `[]` |
| `dashboard.enabled` | Serve the read-only dashboard (web UI and `/api/*`) on its own port and ClusterIP Service `<release>-dashboard`. Pro only; Community never opens the port | `false` |
| `dashboard.port` | Dashboard port; must not equal the health port `config.healthCheck.port` (the render fails when they are equal) | `8061` |
| `dashboard.mode` | `local`: reached through `kubectl port-forward`; the cookie works on `http://localhost`. `shared`: https only; kwatch refuses sign-in over plain http and marks the cookie `Secure`. Needs `dashboard.trustProxy` or `dashboard.tls.secretName`, or the render fails | `local` |
| `dashboard.trustProxy` | Trust `X-Forwarded-Proto`/`X-Forwarded-For` from the Ingress or proxy that terminates TLS in front of kwatch. Without it the header is ignored | `false` |
| `dashboard.trustedProxies` | CIDRs or addresses of the proxies that terminate TLS in front of kwatch (Ingress controller, Gateway, load balancer), for example `["10.0.0.0/8"]`. `X-Forwarded-For` and `X-Forwarded-Proto` count only from these peers, and the client is the first hop from the right that is not itself listed. Any entry (or `dashboard.tls.secretName`) makes the dashboard https only: sign-in over plain http is refused and the cookie is `Secure`. Empty means local (port-forward over http); behind a proxy, sign-in failures are then limited per connecting address, so set it | `[]` |
| `dashboard.tls.secretName` | `kubernetes.io/tls` Secret kwatch serves https from itself, for a LoadBalancer or NodePort with nothing in front. Renewals are picked up without a restart | `""` |
| `dashboard.existingSecret` | Existing Secret with the keys `password` (sign-in) and `token` (API bearer token). Without it the chart creates `<release>-dashboard` with a generated password (`XXXX-XXXX-XXXX-XXXX`) and token, and keeps both across upgrades. Changing the password signs everyone out | `""` |
| `dashboard.existingSecret` | Existing Secret with the keys `password` (sign-in) and `token` (API bearer token). Without it the chart ships `<release>-dashboard` empty and kwatch writes a generated password (`XXXX-XXXX-XXXX-XXXX`) and token into it once, so every renderer (`helm template`, Argo CD, Flux) sees the same manifest and the values never change. kwatch gets and updates only that Secret. Edits take effect after a restart; changing the password signs everyone out | `""` |
| `config.investigation.logs` | Pro evidence reads container logs (redacted, bounded). `false` reads none and the dashboard shows the log section as turned off | `true` |
| `dashboard.networkPolicyFrom` | With `networkPolicy.enabled`: extra peers (your ingress controller, `ipBlock`s for load balancer clients) allowed to reach the dashboard port. The release namespace and `healthIngressFrom` always may | `[]` |

Sign-ins last 12 hours. A password change or a kwatch restart (upgrade,
rollout, rescheduling) signs everyone out. Behind an Ingress, set
`dashboard.trustProxy`: without it every client shares the Ingress's
`dashboard.trustedProxies`: without it every client shares the Ingress's
address, and ten wrong passwords in a minute from anyone lock everyone
out of sign-in for that minute.

The generated `<release>-dashboard` Secret carries
The `<release>-dashboard` Secret carries
`helm.sh/resource-policy: keep`: uninstalling or turning the dashboard off
leaves it, so a reinstall keeps the saved password. Delete it to forget the
password. The password and token are kept across upgrades through Helm
`lookup`, which offline renders (`helm template`, Argo CD, Flux with
post-rendering) cannot do: each sync would make a new password and sign
everyone out. With GitOps, create the Secret yourself (keys `password` and
password. It is shipped empty and kwatch fills it, so the manifest is the
same on every render (including `helm template` and GitOps controllers) and
the password never changes by itself. To use your own values create the Secret yourself (keys `password` and
`token`, for example with a sealed or external secret) and set
`dashboard.existingSecret`.

Expand All @@ -232,7 +230,6 @@ run the kwatch.sh Dashboard menu or create your own Ingress or route
pointing at Service `<release>-dashboard` (port name `dashboard`).

| `image.repository` / `image.tag` / `image.pullPolicy` | Container image; an empty tag uses the chart's `appVersion` | `ghcr.io/abahmed/kwatch` / `""` / `IfNotPresent` |
| `rbac.readyz` | Grant `get` on the API server's `/readyz`, which the prober and Pro evidence read. `false` removes the grant and the control plane evidence then shows that kwatch may not read it | `true` |
| `crd.install` | Install the shared `KwatchConfig` CRD with this release; skipped automatically when another release owns it. `config.crd.enabled` is separate: it turns the live overlay on | `true` |
| `rbac.mode` | `least-privilege` grants an explicit read-only `list`/`watch` set, and kinds outside it (custom resources included) show as `permission_denied` in `/health` coverage (kwatch logs one warning about them and the install notes say so); `full` grants `list`/`watch` on every resource, except that `full` with `watch.secrets: false` renders the least-privilege list. Both grant `get` only on namespaces, nodes, `nodes/stats`, `nodes/metrics` and `pods/log`. Set `full` to watch custom resources | `least-privilege` |
| `persistence.size` | Size of the state volume (PVC). The state file is capped at 512 MiB of logical data (evidence 128 MiB), is rewritten at startup when it grows more than a quarter past that, and no backups are kept; the file and the temporary rewrite copy share the volume | `2Gi` |
Expand Down
10 changes: 10 additions & 0 deletions deploy/chart/templates/NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ port other than 443 need the same.
DASHBOARD (Pro): reach it with a port-forward, then open the URL:
kubectl -n {{ .Release.Namespace }} port-forward svc/{{ include "kwatch.fullname" . }}-dashboard {{ $dp }}:{{ $dp }}
{{ if (default dict $dashN.tls).secretName }}https{{ else }}http{{ end }}://localhost:{{ $dp }}
{{- if or $dashN.trustedProxies (default dict $dashN.tls).secretName }}
With dashboard.trustedProxies or dashboard.tls set, sign-in needs https; a
plain http port-forward can only use the API token.
{{- else }}
NOTE: dashboard.trustedProxies is empty. Behind an Ingress, Gateway or load
balancer set it to that proxy's address range (for example ["10.0.0.0/8"]):
without it kwatch cannot see the real client or the https the proxy ended, so
all clients share one sign-in failure limit and sign-in is not limited to
https.
{{- end }}
{{- if $dashN.existingSecret }}
The sign-in password (key password) and the API bearer token (key token) are
in the Secret {{ $dashN.existingSecret }}.
Expand Down
9 changes: 3 additions & 6 deletions deploy/chart/templates/_dashboard.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
Dashboard settings. The chart never exposes the dashbo 10BC8 ard outside the
cluster: the kwatch.sh Dashboard menu, or you, point an Ingress, route
or Service at the ClusterIP Service <release>-dashboard.
mode local (port-forward) or shared (https only, Secure cookie)
trustProxy TLS ends at a proxy that sets X-Forwarded-Proto
tls kwatch serves https itself from a kubernetes.io/tls Secret
trustedProxies proxies that end TLS and set X-Forwarded-*; any entry
(or tls) makes the dashboard https only
tls kwatch serves https itself from a kubernetes.io/tls Secret
*/}}

{{- define "kwatch.dashboardCheck" -}}
{{- $d := default dict .Values.dashboard -}}
{{- $tls := default dict $d.tls -}}
{{- if and (eq (default "local" $d.mode) "shared") (not $d.trustProxy) (not $tls.secretName) }}
{{- fail "dashboard.mode=shared is https only: set dashboard.trustProxy=true (TLS ends at your Ingress or proxy) or dashboard.tls.secretName (kwatch serves https itself)" }}
{{- end }}
{{- if $d.enabled }}
{{- $hp := include "kwatch.healthPort" . | int }}
{{- $dp := $d.port | default 8061 | int }}
Expand Down
68 changes: 38 additions & 30 deletions deploy/chart/templates/dashboard-secret.yaml
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
{{- $dash := default dict .Values.dashboard -}}
{{- if and $dash.enabled (not $dash.existingSecret) }}
{{- $name := include "kwatch.dashboardSecretName" . }}
{{- /* Keep the token and the password across upgrades: reuse the
existing Secret's values. A new password is 16 characters from an
alphabet without 0/O/1/I/L, in four groups of four (about 80 bits). */}}
{{- $existing := lookup "v1" "Secret" .Release.Namespace $name }}
{{- $data := dict }}
{{- if and $existing $existing.data }}
{{- $data = $existing.data }}
{{- end }}
{{- $token := index $data "token" | default (randAlphaNum 32 | b64enc) }}
{{- $password := index $data "password" }}
{{- if not $password }}
{{- /* crypto/rand bytes, each mapped onto the 32-letter alphabet: 256 is
a multiple of 32, so every letter is equally likely. */}}
{{- $alphabet := "ABCDEFGHJKLMNPQRSTUVWXYZ23456789" }}
{{- $raw := randBytes 16 | b64dec }}
{{- $plain := "" }}
{{- range $i := until 16 }}
{{- if and $i (eq (mod $i 4) 0) }}
{{- $plain = printf "%s-" $plain }}
{{- end }}
{{- $n := mod (int (index $raw $i)) 32 }}
{{- $plain = printf "%s%s" $plain (substr (int $n) (int (add1 $n)) $alphabet) }}
{{- end }}
{{- $password = $plain | b64enc }}
{{- end }}
{{- /*
The dashboard Secret is shipped pre-created and empty. The template never
renders `data` and uses no `lookup` or random function, so every render
(helm template, Argo CD, Flux, CI) is identical. kwatch writes the sign-in
password and the API token into the live object the first time it starts;
Helm's three-way merge leaves keys that neither the previous nor the new
manifest has alone, so upgrades keep them. RBAC cannot limit create by
name, so kwatch only gets and updates this one Secret.
*/ -}}
apiVersion: v1
kind: Secret
metadata:
Expand All @@ -38,9 +22,33 @@ metadata:
# keeps the password people saved. Delete it to forget the password.
helm.sh/resource-policy: keep
type: Opaque
data:
# The bearer token for API clients such as the installer.
token: {{ $token }}
# The shared sign-in password of the web dashboard.
password: {{ $password }}
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ .Release.Name }}-dashboard-credentials
namespace: {{ .Release.Namespace }}
labels:
{{- include "kwatch.labels" . | nindent 4 }}
rules:
- apiGroups: [""]
resources: ["secrets"]
resourceNames: [{{ $name | quote }}]
verbs: ["get", "update"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ .Release.Name }}-dashboard-credentials
namespace: {{ .Release.Namespace }}
labels:
{{- include "kwatch.labels" . | nindent 4 }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ .Release.Name }}-dashboard-credentials
subjects:
- kind: ServiceAccount
name: {{ .Release.Name }}
namespace: {{ .Release.Namespace }}
{{- end }}
32 changes: 13 additions & 19 deletions deploy/chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ spec:
{{- else if $license.key }}
checksum/license: {{ $license.key | sha256sum }}
{{- end }}
{{- /* Roll the pod when the dashboard Secret changes (password and
token are read at start). Same lookup limits as the license Secret:
an offline render hashes the name only. */}}
{{- if (default dict .Values.dashboard).enabled }}
{{- $dashSecret := lookup "v1" "Secret" .Release.Namespace (include "kwatch.dashboardSecretName" .) }}
{{- if $dashSecret }}
checksum/dashboard-secret: {{ toJson $dashSecret.data | sha256sum }}
{{- else }}
checksum/dashboard-secret: {{ include "kwatch.dashboardSecretName" . | sha256sum }}
{{- end }}
{{- end }}
{{- with .Values.podAnnotations }}
{{- toYaml . | nindent 8 }}
{{- end }}
Expand Down Expand Up @@ -164,18 +153,16 @@ spec:
# the pod start without the dashboard (Community, or the
# dashboard disabled).
{{- $dashEnv := default dict .Values.dashboard }}
# ENABLED and TRUST_PROXY are always set, true or false: the
# ENABLED and TRUSTED_PROXIES are always set, on or off: the
# environment wins over the config file, so the chart values
# decide both ways.
- name: KWATCH_DASHBOARD_ENABLED
value: {{ if $dashEnv.enabled }}"true"{{ else }}"false"{{ end }}
- name: KWATCH_DASHBOARD_TRUST_PROXY
value: {{ if and $dashEnv.enabled $dashEnv.trustProxy }}"true"{{ else }}"false"{{ end }}
- name: KWATCH_DASHBOARD_TRUSTED_PROXIES
value: {{ if $dashEnv.enabled }}{{ join "," ($dashEnv.trustedProxies | default list) | quote }}{{ else }}""{{ end }}
{{- if $dashEnv.enabled }}
- name: KWATCH_DASHBOARD_PORT
value: {{ $dashEnv.port | default 8061 | quote }}
- name: KWATCH_DASHBOARD_MODE
value: {{ $dashEnv.mode | default "local" | quote }}
{{- if (default dict $dashEnv.tls).secretName }}
- name: KWATCH_DASHBOARD_TLS_CERT
value: /etc/kwatch/dashboard-tls/tls.crt
Expand All @@ -184,19 +171,26 @@ spec:
{{- end }}
- name: KWATCH_DEPLOYMENT_NAME
value: {{ include "kwatch.fullname" . | quote }}
{{- end }}
{{- if $dashEnv.existingSecret }}
- name: KWATCH_DASHBOARD_TOKEN
valueFrom:
secretKeyRef:
name: {{ include "kwatch.dashboardSecretName" . }}
name: {{ $dashEnv.existingSecret }}
key: token
optional: true
- name: KWATCH_DASHBOARD_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "kwatch.dashboardSecretName" . }}
name: {{ $dashEnv.existingSecret }}
key: password
optional: true
{{- else }}
# kwatch fills the chart's empty Secret itself (get/update on
# that one name) and reads the values through the API.
- name: KWATCH_DASHBOARD_SECRET
value: {{ include "kwatch.dashboardSecretName" . | quote }}
{{- end }}
{{- end }}
{{- if eq (include "kwatch.watchSecrets" .) "false" }}
# watch.secrets=false: matches the RBAC, which grants no
# Secret access, even when config.yaml comes from a Secret.
Expand Down
14 changes: 2 additions & 12 deletions deploy/chart/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ rules:
- apiGroups: [""]
resources: ["nodes/stats", "nodes/metrics", "pods/log"]
verbs: ["get"]
{{- if ne .Values.rbac.readyz false }}
# /readyz: the prober and Pro evidence read the API server's readiness
# checks (rbac.readyz).
# /readyz: the prober and Pro evidence read the API server readiness checks.
- nonResourceURLs: ["/readyz", "/metrics"]
{{- else }}
- nonResourceURLs: ["/metrics"]
{{- end }}
verbs: ["get"]
- apiGroups: ["authorization.k8s.io"]
resources: ["selfsubjectaccessreviews"]
Expand Down Expand Up @@ -63,13 +58,8 @@ rules:
- apiGroups: [""]
resources: ["nodes/stats", "nodes/metrics", "pods/log"]
verbs: ["get"]
{{- if ne .Values.rbac.readyz false }}
# /readyz: the prober and Pro evidence read the API server's readiness
# checks (rbac.readyz).
# /readyz: the prober and Pro evidence read the API server readiness checks.
- nonResourceURLs: ["/readyz", "/metrics"]
{{- else }}
- nonResourceURLs: ["/metrics"]
{{- end }}
verbs: ["get"]
- apiGroups: ["apps"]
resources: ["deployments", "replicasets", "statefulsets", "daemonsets", "controllerrevisions"]
Expand Down
41 changes: 26 additions & 15 deletions deploy/chart/test_helm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -598,20 +598,28 @@ grep -A1 'name: KWATCH_DASHBOARD_ENABLED' <<<"$OUT1" | grep -Fq 'value: "false"'
echo "FAIL: KWATCH_DASHBOARD_ENABLED must be false by default"; exit 1
}
OUT_DASH=$(helm template kwatch . --set dashboard.enabled=true 2>&1)
for want in 'name: KWATCH_DASHBOARD_PASSWORD' 'key: password' \
'name: KWATCH_DASHBOARD_MODE' 'value: "local"' \
'name: KWATCH_DEPLOYMENT_NAME' ' password: ' ' token: '; do
for want in 'name: KWATCH_DASHBOARD_SECRET' 'value: "kwatch-dashboard"' \
'nam F885 e: KWATCH_DASHBOARD_TRUSTED_PROXIES' 'name: KWATCH_DEPLOYMENT_NAME' \
'name: kwatch-dashboard-credentials' 'resourceNames: ["kwatch-dashboard"]' \
'verbs: ["get", "update"]'; do
grep -Fq -- "$want" <<<"$OUT_DASH" || {
echo "FAIL: dashboard render lacks $want"; exit 1;
}
done
if grep -Fq "kind: Ingress" <<<"$OUT_DASH"; then
echo "FAIL: local mode must not render an Ingress"; exit 1
fi
PW=$(grep -E '^ password: ' <<<"$OUT_DASH" | awk '{print $2}' |
base64 --decode)
grep -Eq '^([A-HJ-NP-Z2-9]{4}-){3}[A-HJ-NP-Z2-9]{4}$' <<<"$PW" || {
echo "FAIL: generated password has the wrong shape: $PW"; exit 1;
# The Secret ships empty: no password or token in any render, and two
# renders (and a GitOps controller's) are byte for byte the same.
if grep -Eq '^ (password|token): ' <<<"$OUT_DASH"; then
echo "FAIL: the dashboard Secret must ship without credentials"; exit 1
fi
grep -Eq 'name: KWATCH_DASHBOARD_(PASSWORD|TOKEN)' <<<"$OUT_DASH" && {
echo "FAIL: chart-managed credentials must not use env refs"; exit 1
}
OUT_DASH2=$(helm template kwatch . --set dashboard.enabled=true 2>&1)
[ "$OUT_DASH" = "$OUT_DASH2" ] || {
echo "FAIL: two renders of the dashboard differ"; exit 1
}
# expose_ok renders with dashboard.enabled and the given flags and checks
# every following string is present; expose_fail checks the render fails
Expand Down Expand Up @@ -640,12 +648,12 @@ expose_fail() {
echo "FAIL: $label: message lacks '$want': $out"; exit 1;
}
}
expose_fail "unknown mode" "must be one of" --set dashboard.mode=public
expose_fail "shared without https" "https only" --set dashboard.mode=shared
expose_ok "shared behind a proxy" --set dashboard.mode=shared \
--set dashboard.trustProxy=true -- \
'value: "shared"' 'name: KWATCH_DASHBOARD_TRUST_PROXY' "type: ClusterIP"
expose_ok "shared with own TLS" --set dashboard.mode=shared \
expose_ok "behind a proxy" \
--set 'dashboard.trustedProxies[0]=10.0.0.0/8' \
--set 'dashboard.trustedProxies[1]=192.168.0.7' -- \
'name: KWATCH_DASHBOARD_TRUSTED_PROXIES' \
'value: "10.0.0.0/8,192.168.0.7"' "type: ClusterIP"
expose_ok "own TLS" \
--set dashboard.tls.secretName=kwatch-tls -- \
"name: KWATCH_DASHBOARD_TLS_CERT" "secretName: kwatch-tls" \
"mountPath: /etc/kwatch/dashboard-tls"
Expand All @@ -661,9 +669,12 @@ for kind in "kind: Ingress" "kind: HTTPRoute" "kind: Route" \
done
OUT_EXIST=$(helm template kwatch . --set dashboard.enabled=true \
--set dashboard.existingSecret=mine 2>&1)
if grep -Eq '^ password: ' <<<"$OUT_EXIST"; then
echo "FAIL: existingSecret must not render a password Secret"; exit 1
if grep -Fq "kind: Secret" <<<"$(sed -n '/dashboard-secret.yaml/,/^---/p' <<<"$OUT_EXIST")"; then
echo "FAIL: existingSecret must not render a dashboard Secret"; exit 1
fi
grep -Fq "name: kwatch-dashboard-credentials" <<<"$OUT_EXIST" && {
echo "FAIL: existingSecret must not grant access to a chart Secret"; exit 1
}
grep -Fq "name: mine" <<<"$OUT_EXIST" || {
echo "FAIL: existingSecret must feed the dashboard env"; exit 1;
}
Expand Down
Loading
0