diff --git a/files/k0s/manifests/kubestellar/40-kubestellar-console.yaml b/files/k0s/manifests/kubestellar/40-kubestellar-console.yaml index fc502bdf..be226493 100644 --- a/files/k0s/manifests/kubestellar/40-kubestellar-console.yaml +++ b/files/k0s/manifests/kubestellar/40-kubestellar-console.yaml @@ -38,6 +38,7 @@ spec: labels: app.kubernetes.io/name: kubestellar-console spec: + serviceAccountName: kubestellar-console containers: - name: console image: ghcr.io/kubestellar/console:v0.3.34 @@ -48,6 +49,21 @@ spec: value: "true" - name: DEV_USER_LOGIN value: "admin" + # First-boot fix (#193): on a freshly installed single-node k0s cluster + # no client kc-agent is connected yet, so the console must read the local + # embedded k0s API directly instead of defaulting to mock/demo cards. + # The fix is the ServiceAccount above plus the read-only ClusterRole in + # 42-kubestellar-console-rbac.yaml; NO_LOCAL_AGENT is set only to make + # that intent explicit, since the console backend already forces it on + # when it detects it is running in-cluster. SKIP_ONBOARDING skips the + # onboarding questionnaire so the kiosk shows the dashboard immediately + # (sign-in is bypassed separately by DEV_MODE/DEV_USER_LOGIN above). + - name: SKIP_ONBOARDING + value: "true" + - name: NO_LOCAL_AGENT + value: "true" + - name: POD_NAMESPACE + value: "kubestellar-console" - name: GITHUB_CLIENT_ID valueFrom: secretKeyRef: diff --git a/files/k0s/manifests/kubestellar/42-kubestellar-console-rbac.yaml b/files/k0s/manifests/kubestellar/42-kubestellar-console-rbac.yaml new file mode 100644 index 00000000..efbc1708 --- /dev/null +++ b/files/k0s/manifests/kubestellar/42-kubestellar-console-rbac.yaml @@ -0,0 +1,72 @@ +# First-boot fix (#193): the console pod reads the local embedded k0s cluster +# directly, so it needs read access to render live nodes, namespaces and pods +# instead of falling back to demo cards. This ServiceAccount plus the read-only +# ClusterRole below is the actual mechanism behind the fix. Bound to the +# console's own ServiceAccount so the default account stays unprivileged. +# +# Scope note: the resource list mirrors what the console dashboard renders -- +# the namespace overview and ConfigMap cards read configmaps, and the resource +# cards read events, limitranges and resourcequotas. Verbs are read-only and +# the console is only reachable through the 127.0.0.1-bound kiosk proxy +# (41-kubestellar-kiosk-proxy.yaml), never from the network. Secrets are +# deliberately excluded. Resource names must be exact: Kubernetes silently +# ignores unknown names in RBAC rules, so a typo removes the grant without any +# error (see tests/unit/test_kubestellar_kiosk.py). +apiVersion: v1 +kind: ServiceAccount +metadata: + name: kubestellar-console + namespace: kubestellar-console + labels: + app.kubernetes.io/name: kubestellar-console +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: kubestellar-console-reader + labels: + app.kubernetes.io/name: kubestellar-console +rules: +- apiGroups: [""] + resources: + - nodes + - namespaces + - pods + - services + - configmaps + - events + - persistentvolumes + - persistentvolumeclaims + - serviceaccounts + - endpoints + - limitranges + - resourcequotas + - replicationcontrollers + verbs: ["get", "list", "watch"] +- apiGroups: ["apps"] + resources: + - deployments + - statefulsets + - daemonsets + - replicasets + verbs: ["get", "list", "watch"] +- apiGroups: ["batch"] + resources: + - jobs + - cronjobs + verbs: ["get", "list", "watch"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: kubestellar-console-reader + labels: + app.kubernetes.io/name: kubestellar-console +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: kubestellar-console-reader +subjects: +- kind: ServiceAccount + name: kubestellar-console + namespace: kubestellar-console diff --git a/tests/unit/test_k0s_manifests.py b/tests/unit/test_k0s_manifests.py index 149ca421..36d65c73 100644 --- a/tests/unit/test_k0s_manifests.py +++ b/tests/unit/test_k0s_manifests.py @@ -38,6 +38,7 @@ def test_k0s_manifest_files(): assert (ks_dir / "30-kubestellar-core.yaml").is_file() assert (ks_dir / "40-kubestellar-console.yaml").is_file() assert (ks_dir / "41-kubestellar-kiosk-proxy.yaml").is_file() + assert (ks_dir / "42-kubestellar-console-rbac.yaml").is_file() def test_postgres_password_not_hardcoded(): diff --git a/tests/unit/test_kubestellar_kiosk.py b/tests/unit/test_kubestellar_kiosk.py index ec608c72..1da8f45e 100644 --- a/tests/unit/test_kubestellar_kiosk.py +++ b/tests/unit/test_kubestellar_kiosk.py @@ -2,6 +2,8 @@ from pathlib import Path +import yaml + ROOT = Path(__file__).resolve().parents[2] KIOSK = ROOT / "files" / "k0s" / "kiosk" KIOSK_CONF = KIOSK / "nginx.conf" @@ -87,6 +89,14 @@ def test_console_provides_local_and_oauth_login_options() -> None: assert 'value: "true"' in console assert "name: ALLOW_DEV_MODE_IN_CLUSTER" in console assert "hostPort:" not in console + # #193: on first boot the console must read the local k0s cluster directly + # (no connected kc-agent yet) and skip the onboarding questionnaire. + # SKIP_ONBOARDING skips the questionnaire, not sign-in; sign-in is bypassed + # by DEV_MODE/DEV_USER_LOGIN. + assert "name: SKIP_ONBOARDING" in console + assert "name: NO_LOCAL_AGENT" in console + assert "name: POD_NAMESPACE" in console + assert "serviceAccountName: kubestellar-console" in console assert "name: GITHUB_CLIENT_ID" in console assert "name: GITHUB_CLIENT_SECRET" in console assert "name: kubestellar-console-github-oauth" in console @@ -107,3 +117,68 @@ def test_proxy_is_the_only_public_console_endpoint() -> None: "nginx@sha256:62223d644fa234c3a1cc785ee14242ec47a77364226f1c811d2f669f96dc2ac8" in proxy ) + + +def test_console_rbac_reads_only_the_local_cluster() -> None: + rbac = ( + ROOT + / "files" + / "k0s" + / "manifests" + / "kubestellar" + / "42-kubestellar-console-rbac.yaml" + ).read_text(encoding="utf-8") + docs = list(yaml.safe_load_all(rbac)) + kinds = [d.get("kind") for d in docs if d] + assert kinds == [ + "ServiceAccount", + "ClusterRole", + "ClusterRoleBinding", + ] + + sa = next(d for d in docs if d["kind"] == "ServiceAccount") + assert sa["metadata"]["namespace"] == "kubestellar-console" + + role = next(d for d in docs if d["kind"] == "ClusterRole") + # Read-only: never grant write verbs to the console's account. + for rule in role["rules"]: + assert set(rule["verbs"]) <= {"get", "list", "watch"} + resources = {r for rule in role["rules"] for r in rule["resources"]} + assert {"nodes", "namespaces", "pods"} <= resources + # Kubernetes silently ignores resource names that do not exist in the named + # apiGroup, so a typo (e.g. "limitquotas") drops the grant with no error. + # Pin every name to the real resource for its group. + known_resources = { + "": { + "configmaps", + "endpoints", + "events", + "limitranges", + "namespaces", + "nodes", + "persistentvolumeclaims", + "persistentvolumes", + "pods", + "replicationcontrollers", + "resourcequotas", + "serviceaccounts", + "services", + }, + "apps": {"daemonsets", "deployments", "replicasets", "statefulsets"}, + "batch": {"cronjobs", "jobs"}, + } + for rule in role["rules"]: + for group in rule["apiGroups"]: + assert group in known_resources, f"unexpected apiGroup {group!r}" + unknown = set(rule["resources"]) - known_resources[group] + assert not unknown, f"not real resources in apiGroup {group!r}: {unknown}" + # The kiosk dashboard never needs secrets; keep them out of the grant. + assert "secrets" not in resources + + binding = next(d for d in docs if d["kind"] == "ClusterRoleBinding") + assert binding["roleRef"]["kind"] == "ClusterRole" + assert binding["roleRef"]["name"] == role["metadata"]["name"] + subject = binding["subjects"][0] + assert subject["kind"] == "ServiceAccount" + assert subject["name"] == "kubestellar-console" + assert subject["namespace"] == "kubestellar-console"