Skip to content

Commit 1f66456

Browse files
committed
fix(monitoring): promote oidc.users[].role to required + regenerate schema
The `OIDCUser` typedef in `packages/extra/monitoring/values.yaml` declared only `@field {string} email`; `cozyvalues-gen` regenerated `values.schema.json`, `README.md`, and the mirrored `openAPISchema` on the `Monitoring` ApplicationDefinition WITHOUT a `role` field. Consequences: - The `@enum OIDCRole` declaration was orphan — referenced from nowhere, so the schema's items object had `required: ["email"]` and one property. - A dashboard-driven CR would offer a form without any role picker; a schema-conformant `{email}` submission would render into the users-Job with `role: null`, `jq --raw-output` yields the string `"null"`, the `POST /api/orgs/1/users` body carries `"role":"null"`, Grafana returns HTTP 400, and the post-install hook exhausts `backoffLimit: 6` — HelmRelease never becomes Ready. - Unit + e2e tests all hardcoded `role: Admin` / `role: Viewer`, so the contract mismatch never surfaced in CI. Fix: - Add `@field {OIDCRole} role - ...` to the `OIDCUser` typedef in `packages/extra/monitoring/values.yaml` so cozyvalues-gen materialises the field with enum + required. - Regenerate `values.schema.json`, `README.md`, and mirrored `openAPISchema` via `make generate`. - Add `monitoring.oidc.assertUsersRoleShape` template-level guard (same shape as `assertUsersEmailShape`) so a stale-schema CR or a direct-helm invocation fails at render time with a legible error instead of at hook execution with a Grafana 400. - Consolidate all `spec.oidc.users` guards into a single `monitoring.oidc.assertUsersPrologue` include and call it once from `oidc-users-job.yaml`, ordering the incompat check (`assertCustomSecretRefUsersEmpty`) before the per-entry role / email regexes so operators see the more actionable message first. Drop the duplicate include from `grafana.yaml` (Job template renders in every mode anyway). - Two new helm-unittest cases lock the regression: `oidc.users entry missing role fails the render` and `oidc.users entry with unknown role fails the render`. - Point the previous `secretRef + users` incompat unit test at the users-Job template since that's where the prologue now fires. - `docs/oidc-grafana.md`: correct the failed-hook post-mortem window claim — with `ttlSecondsAfterFinished: 3600` the Kubernetes TTL controller reaps the Job + Pod 1h after the terminal condition, not "until the next helm-upgrade". Signed-off-by: IvanHunters <[email protected]>
1 parent ee2f1a3 commit 1f66456

8 files changed

Lines changed: 99 additions & 5 deletions

File tree

‎packages/extra/monitoring/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,5 @@
114114
| `oidc.customConfig.secretRef.name` | Name of an existing Secret in the release namespace whose `auth.ini` key holds a ready-made `[auth.generic_oauth]` block. Mutually exclusive with `customConfig.config`. | `string` | `""` |
115115
| `oidc.users` | Users granted access to this Grafana instance's org. Each entry is reconciled into Grafana org membership + role by a chart-owned post-install/post-upgrade Job. Works for both `System` and `CustomConfig` modes; ignored when `mode: None`. Users not listed here get nothing (no default org role from group mapping). | `[]object` | `[]` |
116116
| `oidc.users[i].email` | Email address matched against the `email` claim from the issuer. The chart pre-provisions a Grafana account with this email so the users-Job can set the org role before the operator's first login (`oauth_allow_insecure_email_lookup = true` glues the two together). Must match a conservative pattern: local part is `[A-Za-z0-9._%+-]+`, domain is `[A-Za-z0-9.-]+\.[A-Za-z]{2,}`. Whitespace, quoted literals, and unusual URL-reserved characters are rejected on schema level so the reconcile-Job's shell handling never has to defend against malformed input. | `string` | `""` |
117+
| `oidc.users[i].role` | Grafana org role to bind: `Admin`, `Editor`, or `Viewer`. Applied to the release's Grafana org (`Main Org.`); server-level `GrafanaAdmin` promotion is out of scope for Phase 1. Required — the users-Job POSTs `{loginOrEmail, role}` to `/api/orgs/1/users` and PATCHes `{role}` to converge across re-runs; omitting the field would render `role: "null"` into the body and fail with HTTP 400. | `string` | `{}` |
117118

‎packages/extra/monitoring/values.schema.json‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,22 @@
645645
"items": {
646646
"type": "object",
647647
"required": [
648-
"email"
648+
"email",
649+
"role"
649650
],
650651
"properties": {
651652
"email": {
652653
"description": "Email address matched against the `email` claim from the issuer. The chart pre-provisions a Grafana account with this email so the users-Job can set the org role before the operator's first login (`oauth_allow_insecure_email_lookup = true` glues the two together). Must match a conservative pattern: local part is `[A-Za-z0-9._%+-]+`, domain is `[A-Za-z0-9.-]+\\.[A-Za-z]{2,}`. Whitespace, quoted literals, and unusual URL-reserved characters are rejected on schema level so the reconcile-Job's shell handling never has to defend against malformed input.",
653654
"type": "string"
655+
},
656+
"role": {
657+
"description": "Grafana org role to bind: `Admin`, `Editor`, or `Viewer`. Applied to the release's Grafana org (`Main Org.`); server-level `GrafanaAdmin` promotion is out of scope for Phase 1. Required — the users-Job POSTs `{loginOrEmail, role}` to `/api/orgs/1/users` and PATCHes `{role}` to converge across re-runs; omitting the field would render `role: \"null\"` into the body and fail with HTTP 400.",
658+
"type": "string",
659+
"enum": [
660+
"Admin",
661+
"Editor",
662+
"Viewer"
663+
]
654664
}
655665
}
656666
}

‎packages/extra/monitoring/values.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ vmagent:
209209

210210
## @typedef {struct} OIDCUser - Grafana user reconciled into the release's org.
211211
## @field {string} email - Email address matched against the `email` claim from the issuer. The chart pre-provisions a Grafana account with this email so the users-Job can set the org role before the operator's first login (`oauth_allow_insecure_email_lookup = true` glues the two together). Must match a conservative pattern: local part is `[A-Za-z0-9._%+-]+`, domain is `[A-Za-z0-9.-]+\.[A-Za-z]{2,}`. Whitespace, quoted literals, and unusual URL-reserved characters are rejected on schema level so the reconcile-Job's shell handling never has to defend against malformed input.
212+
## @field {OIDCRole} role - Grafana org role to bind: `Admin`, `Editor`, or `Viewer`. Applied to the release's Grafana org (`Main Org.`); server-level `GrafanaAdmin` promotion is out of scope for Phase 1. Required — the users-Job POSTs `{loginOrEmail, role}` to `/api/orgs/1/users` and PATCHes `{role}` to converge across re-runs; omitting the field would render `role: "null"` into the body and fail with HTTP 400.
212213

213214
## @typedef {struct} OIDC - OIDC selector for the Grafana instance (Phase 1 — per-instance audience binding; no per-tenant Keycloak realm).
214215
## @field {OIDCMode} mode - Identity mode. `None`: no OIDC, only the `grafana-admin-password` Secret works. `System`: trust the platform `cozy` realm via a per-instance confidential client and audience binding; zero-config default. `CustomConfig`: trust a tenant-supplied issuer directly (BYO); `cozy` is not in the path.

‎packages/system/monitoring-rd/cozyrds/monitoring.yaml‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎packages/system/monitoring/templates/_helpers.tpl‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,45 @@
111111
{{- toYaml $users -}}
112112
{{- end -}}
113113

114+
{{- /*
115+
Prologue that all templates touching spec.oidc.users should include
116+
ONCE, near the top. Runs the incompatibility check (secretRef + users)
117+
before the per-entry regexes so operators see the most actionable
118+
error first — combining incompatible top-level fields is a
119+
configuration bug the operator can fix immediately; a per-user
120+
malformed email is a data-entry issue in the CR body.
121+
*/}}
122+
{{- define "monitoring.oidc.assertUsersPrologue" -}}
123+
{{- include "monitoring.oidc.assertCustomSecretRefUsersEmpty" . }}
124+
{{- include "monitoring.oidc.assertUsersRoleShape" . }}
125+
{{- include "monitoring.oidc.assertUsersEmailShape" . }}
126+
{{- end -}}
127+
128+
{{- /*
129+
Reject `spec.oidc.users[]` entries missing `role` or carrying an
130+
unknown role. openAPISchema also marks `role` required, but a stale
131+
schema (dashboard-emitted CR generated before the field was added, or
132+
a direct-helm invocation that bypasses the API validation) would
133+
otherwise render `role: "null"` into the users-Job's Grafana admin API
134+
body and fail with HTTP 400 at hook execution. Failing at render
135+
time surfaces the root cause instead of a mysterious backoffLimit
136+
exhaustion.
137+
*/}}
138+
{{- define "monitoring.oidc.assertUsersRoleShape" -}}
139+
{{- $oidc := .Values.oidc | default dict -}}
140+
{{- $users := $oidc.users | default (list) -}}
141+
{{- $allowed := list "Admin" "Editor" "Viewer" -}}
142+
{{- range $i, $u := $users -}}
143+
{{- $role := $u.role | default "" -}}
144+
{{- if eq $role "" -}}
145+
{{- fail (printf "spec.oidc.users[%d].role: is required and must be one of Admin, Editor, Viewer. Omitting the field would render null into the Grafana admin API body and fail the post-install users-Job with HTTP 400." $i) -}}
146+
{{- end -}}
147+
{{- if not (has $role $allowed) -}}
148+
{{- fail (printf "spec.oidc.users[%d].role: %q is not one of the allowed values Admin, Editor, Viewer." $i $role) -}}
149+
{{- end -}}
150+
{{- end -}}
151+
{{- end -}}
152+
114153
{{- /*
115154
Reject malformed emails in `spec.oidc.users`. Grafana passes the
116155
string verbatim into a query string (/api/users/lookup?loginOrEmail=)

‎packages/system/monitoring/templates/grafana/grafana.yaml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@
2929
*/}}
3030
{{- $oidc := .Values.oidc | default dict }}
3131
{{- $oidcMode := $oidc.mode | default "None" }}
32+
{{- /*
33+
Users-map prologue is included from oidc-users-job.yaml (rendered in
34+
every mode), so the secretRef+users incompat is caught even when
35+
oidc-users-job.yaml is disabled downstream. Only the CustomConfig-XOR
36+
check is grafana.yaml-scoped.
37+
*/}}
3238
{{- if eq $oidcMode "CustomConfig" }}
3339
{{- include "monitoring.oidc.assertCustomConfigXor" . }}
34-
{{- include "monitoring.oidc.assertCustomSecretRefUsersEmpty" . }}
3540
{{- end }}
3641
{{- /*
3742
Symmetric assertions with templates/grafana/oidc-keycloak.yaml so a

‎packages/system/monitoring/templates/grafana/oidc-users-job.yaml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@
4141
{{- $oidc := .Values.oidc | default dict }}
4242
{{- $mode := $oidc.mode | default "None" }}
4343
{{- $users := $oidc.users | default (list) }}
44-
{{- include "monitoring.oidc.assertUsersEmailShape" . }}
44+
{{- /*
45+
Prologue: incompat (secretRef+users) first, then per-entry role +
46+
email validation. See monitoring.oidc.assertUsersPrologue.
47+
*/}}
48+
{{- include "monitoring.oidc.assertUsersPrologue" . }}
4549
---
4650
apiVersion: batch/v1
4751
kind: Job

‎packages/system/monitoring/tests/oidc_test.yaml‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,9 @@ tests:
503503
errorMessage: "spec.oidc.mode: CustomConfig requires either spec.oidc.customConfig.config (inline generic_oauth map) or spec.oidc.customConfig.secretRef.name (Secret with an `auth.ini` key)."
504504

505505
- it: mode=CustomConfig secretRef + non-empty users fails the render (contract incompat)
506-
template: templates/grafana/grafana.yaml
506+
# Prologue fires from the users-Job template — that's where all
507+
# spec.oidc.users assertions live, so the fail surfaces there.
508+
template: templates/grafana/oidc-users-job.yaml
507509
set:
508510
oidc.mode: CustomConfig
509511
oidc.customConfig.secretRef.name: my-oauth-secret
@@ -524,3 +526,35 @@ tests:
524526
# matching the earlier secretRef test — this is the compatible path.
525527
- notExists:
526528
path: spec.config["auth.generic_oauth"]
529+
530+
##########################################################
531+
# spec.oidc.users[].role — schema contract regression guard
532+
##########################################################
533+
534+
# Even though the openAPISchema now marks `role` as required, downstream
535+
# clients (Flux HelmRelease valuesFrom, direct helm CLI, dashboard-
536+
# emitted CRs during a stale schema window) may still pass a
537+
# role-less entry. The template must reject that at render time so the
538+
# users-Job never fires with `role: "null"` on Grafana's admin API.
539+
- it: oidc.users entry missing role fails the render (contract regression guard)
540+
template: templates/grafana/oidc-users-job.yaml
541+
set:
542+
oidc.mode: System
543+
_cluster.oidc-enabled: "true"
544+
oidc.users:
545+
546+
asserts:
547+
- failedTemplate:
548+
errorMessage: "spec.oidc.users[0].role: is required and must be one of Admin, Editor, Viewer. Omitting the field would render null into the Grafana admin API body and fail the post-install users-Job with HTTP 400."
549+
550+
- it: oidc.users entry with unknown role fails the render
551+
template: templates/grafana/oidc-users-job.yaml
552+
set:
553+
oidc.mode: System
554+
_cluster.oidc-enabled: "true"
555+
oidc.users:
556+
557+
role: Superuser
558+
asserts:
559+
- failedTemplate:
560+
errorMessage: "spec.oidc.users[0].role: \"Superuser\" is not one of the allowed values Admin, Editor, Viewer."

0 commit comments

Comments
 (0)