Skip to content

CKS: give each user a short-lived kubeconfig of their own instead of the cluster-admin one - #14908

Open
bhouse-nexthop wants to merge 1 commit into
apache:mainfrom
bhouse-nexthop:cks-user-kubeconfig
Open

bhouse-nexthop wants to merge 1 commit into
apache:mainfrom
bhouse-nexthop:cks-user-kubeconfig

Conversation

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator

Description

getKubernetesClusterConfig returns the cluster's admin kubeconfig to everyone who may operate the cluster. That includes roles that only allow get* APIs, such as the default read-only roles. The credential is cluster-admin and valid for a year. Every user shares it, and it can't be taken back from one of them.

With the new setting cloud.kubernetes.cluster.user.kubeconfig.enabled (domain scope, read for the cluster's domain, dynamic, default false), each caller gets a kubeconfig of their own:

Kubernetes user cloudstack:user:<user uuid> (certificate CN)
Groups cloudstack:account:<account uuid> and one of the two below (certificate O)
Caller may call scaleKubernetesCluster (role, project role and API key) cloudstack:cluster-admins, bound to ClusterRole cluster-admin
Others, e.g. Read-Only User - Default, Read-Only Admin - Default cloudstack:viewers, bound to ClusterRole view (no secrets; kube-system/cloudstack-secret holds the cluster's API key)
Validity cloud.kubernetes.cluster.user.kubeconfig.validity hours (domain scope, default 24, 1 to 8760)
kubeconfig entries cluster, user and context named cloudstack-<cluster uuid>, so kubeconfigs of several clusters merge

Root admins who may scale the cluster still get the admin kubeconfig; they already hold the SSH key to every node. With the setting off nothing changes, so tools that store the admin kubeconfig keep working.

How the certificate is made:

  1. The management server makes an RSA key and a certificate signing request. The key is only returned in the API response (responseHasSensitiveInfo); it is not logged and is never sent to the node.
  2. Over SSH, the control node applies (kubectl apply --server-side) the two ClusterRoleBindings and a CertificateSigningRequest (random name) with signer kubernetes.io/kube-apiserver-client and expirationSeconds, approves it, reads the certificate and deletes the request.
  3. The kubeconfig takes the API server address and CA from the stored kubeconfig.

expirationSeconds is honoured from Kubernetes 1.22, so older clusters are refused with an error that says so. The bindings are applied on every request, so clusters created before this change get them too.

AccountManager gets checkApiAccess(User, ...): the project role checker only applies to a user, so the Account variant would ignore a project role that denies scaling. It refuses an API key that is not the user's. Each issued kubeconfig is logged at INFO with the user, groups, validity and request name; the key never is.

The UI fetched the kubeconfig on every refresh of the cluster page. It now fetches it when the Access tab is opened, when the cluster changes, and on a state change while no kubeconfig is shown. Errors from the server are shown.

Not done:

  • Kubeconfigs handed out before the setting is turned on stay valid until they expire. Taking them back needs a new cluster CA.
  • Group membership is fixed when the certificate is issued. A user whose role changes keeps the old rights until the certificate expires, which is what the short validity is for.
  • The view role doesn't allow listing nodes. Cluster admins can bind more to cloudstack:viewers.
  • The node user data still holds the kubeadm join token, which never expires; Join CKS nodes with a fresh token and a pinned CA #14769 deals with that token.

Conflicts with #14651: both add settings at the end of KubernetesClusterService and getConfigKeys(). Whichever merges second keeps both.

Fixes #14907

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • Build/CI
  • Test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

How Has This Been Tested?

Unit tests, mvn install -pl server,plugins/integrations/kubernetes-service,plugins/network-elements/juniper-contrail -am:

  • KubernetesClusterUserKubeConfigWorkerTest (new, 12 tests): the subject names the user and the groups; the request asks the client signer for the configured validity; the private key and its bytes are not in the command sent to the control node; the returned key matches the signed certificate; entries and the request name don't contain the user UUID; the API server address and CA come from the stored kubeconfig and the admin key does not; output that is not a certificate is refused.
  • KubernetesClusterManagerImplUserKubeConfigTest (new, 8 tests): setting off returns the admin kubeconfig; a root admin who may scale gets it; a user who may scale gets cluster-admin; a read-only root admin gets view; the API key is passed to the check; validity outside 1 to 8760 hours and Kubernetes older than 1.22 are refused.
  • AccountManagerImplTest: the new checkApiAccess(User, ...) passes the user to the checkers, refuses another user's API key, and throws when a checker denies.

Results: Kubernetes*Test 148 passed (of 148), AccountManagerImplTest 129 passed (of 129). pre-commit clean. UI lint not run.

Not run yet: a live cluster. A lab check (a user and a read-only account against a 1.33 cluster: identity, allowed and refused actions, expiry, admin kubeconfig for root admin, no key in the logs) is ready and will be reported here when it passes.

How did you try to break this feature and the system with this change?

  • Read-only roles allow get*: they get view, not cluster-admin, even as root admin.
  • A scoped API key without scaleKubernetesCluster on a user whose role has it gets view.
  • A project role denying scaleKubernetesCluster gets view.
  • Two first requests at once: server-side apply, so neither fails creating the bindings.
  • A node that prints warnings on stderr: the certificate is found by its prefix anywhere in the output and checked to be a PEM certificate.
  • A user UUID with shell or YAML characters (it can be set on createUser): it only goes into the CSR subject, never into the command or the kubeconfig text.
  • Another user's API key in the request: refused, so the caller gets view.

getKubernetesClusterConfig returns the cluster's admin kubeconfig to everyone
who may operate the cluster, including roles that are only allowed get* APIs,
such as the default read-only roles. The credential is cluster-admin, valid
for a year, shared by every user and cannot be taken back from one of them,
so a user removed from the account keeps full access.

With cloud.kubernetes.cluster.user.kubeconfig.enabled (domain scope,
default false), each caller gets a kubeconfig with a client certificate of
their own for the Kubernetes user cloudstack:user:<user uuid>, in the groups
cloudstack:account:<account uuid> and either cloudstack:cluster-admins, when
the caller may call scaleKubernetesCluster (role, project role and API key),
or cloudstack:viewers otherwise. CKS binds these groups to the cluster-admin
and view cluster roles. The certificate is valid for
cloud.kubernetes.cluster.user.kubeconfig.validity hours (default 24).

The key pair is made on the management server and only returned in the API
response. The control node gets the certificate signing request, signs it
with the Kubernetes CSR API (signer kubernetes.io/kube-apiserver-client,
expirationSeconds), and the request is deleted. This needs Kubernetes 1.22,
the first release that honours expirationSeconds; older clusters are
refused. Root admins allowed to scale the cluster still get the admin
kubeconfig, as does everyone while the setting is off.

AccountManager gets a checkApiAccess for a User, as the project role checker
only applies to a user; it refuses an API key that is not the user's.

The UI fetched the kubeconfig on every refresh of the cluster page; it now
fetches it when the Access tab is opened, as each fetch can issue a
certificate.

Signed-off-by: Brad House <bhouse@nexthop.ai>
@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.62500% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 20.94%. Comparing base (58fda71) to head (116d9c5).

Files with missing lines Patch % Lines
...workers/KubernetesClusterUserKubeConfigWorker.java 93.02% 4 Missing and 2 partials ⚠️
...bernetes/cluster/KubernetesClusterManagerImpl.java 88.00% 2 Missing and 1 partial ⚠️
...c/main/java/com/cloud/user/AccountManagerImpl.java 76.92% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##              main   #14908       +/-   ##
============================================
+ Coverage     3.86%   20.94%   +17.08%     
- Complexity       0    21174    +21174     
============================================
  Files          494     6058     +5564     
  Lines        42888   549690   +506802     
  Branches      8141    73495    +65354     
============================================
+ Hits          1657   115132   +113475     
- Misses       40999   421102   +380103     
- Partials       232    13456    +13224     
Flag Coverage Δ
uitests 3.86% <ø> (-0.01%) ⬇️
unittests 22.39% <90.62%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

Tested on a KVM lab running a main (24.0) build that includes this change. Primary storage is Ceph RBD. The CKS cluster is Kubernetes 1.33.1, 1 control and 1 worker node, owned by an account with the User role in a subdomain. A second account in ROOT has Read-Only Admin - Default. Both settings were set on the subdomain only, with validity 1 hour.

Result: 24 of 24 checks passed.

Caller Certificate subject Checks
Owner (User role), setting off CN=kubernetes-admin gets the admin kubeconfig, as before
Owner (User role), setting on CN=cloudstack:user:<user uuid>, O=cloudstack:account:<account uuid>, O=cloudstack:cluster-admins entries named cloudstack-<cluster uuid>; kubectl auth whoami shows the same user and groups; can create a namespace; can list kube-system secrets
Read-Only Admin - Default CN=cloudstack:user:<user uuid>, O=cloudstack:account:<account uuid>, O=cloudstack:viewers lists pods in all namespaces; refused creating a namespace; refused reading kube-system secrets
Root admin (may scale) CN=kubernetes-admin still gets the admin kubeconfig

Also checked:

  • With validity 1 hour, the user certificate's notAfter was 59 minutes away.
  • No CertificateSigningRequest was left in the cluster.
  • Neither issued private key is in management-server.log or apilog.log, and no PRIVATE KEY line was written during the run. apilog.log has the getKubernetesClusterConfig calls, so the search read the right file.

The cluster, the accounts and the subdomain were deleted afterwards.

Not tested: waiting for the certificate to expire and checking that it is then refused, and the UI Access tab (when the kubeconfig is fetched).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CKS: every user who may operate a cluster gets its cluster-admin kubeconfig

1 participant