Repository navigation
CKS: give each user a short-lived kubeconfig of their own instead of the cluster-admin one - #14908
bhouse-nexthop wants to merge 1 commit into
Conversation
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 Report❌ Patch coverage is 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
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 Result: 24 of 24 checks passed.
Also checked:
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). |
Description
getKubernetesClusterConfigreturns the cluster's admin kubeconfig to everyone who may operate the cluster. That includes roles that only allowget*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, defaultfalse), each caller gets a kubeconfig of their own:cloudstack:user:<user uuid>(certificate CN)cloudstack:account:<account uuid>and one of the two below (certificate O)scaleKubernetesCluster(role, project role and API key)cloudstack:cluster-admins, bound to ClusterRolecluster-adminRead-Only User - Default,Read-Only Admin - Defaultcloudstack:viewers, bound to ClusterRoleview(no secrets;kube-system/cloudstack-secretholds the cluster's API key)cloud.kubernetes.cluster.user.kubeconfig.validityhours (domain scope, default 24, 1 to 8760)cloudstack-<cluster uuid>, so kubeconfigs of several clusters mergeRoot 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:
responseHasSensitiveInfo); it is not logged and is never sent to the node.kubectl apply --server-side) the two ClusterRoleBindings and aCertificateSigningRequest(random name) with signerkubernetes.io/kube-apiserver-clientandexpirationSeconds, approves it, reads the certificate and deletes the request.expirationSecondsis 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.AccountManagergetscheckApiAccess(User, ...): the project role checker only applies to a user, so theAccountvariant 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:
viewrole doesn't allow listing nodes. Cluster admins can bind more tocloudstack:viewers.Conflicts with #14651: both add settings at the end of
KubernetesClusterServiceandgetConfigKeys(). Whichever merges second keeps both.Fixes #14907
Types of changes
Feature/Enhancement Scale or Bug Severity
Feature/Enhancement Scale
Bug Severity
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 newcheckApiAccess(User, ...)passes the user to the checkers, refuses another user's API key, and throws when a checker denies.Results:
Kubernetes*Test148 passed (of 148),AccountManagerImplTest129 passed (of 129).pre-commitclean. 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?
get*: they getview, not cluster-admin, even as root admin.scaleKubernetesClusteron a user whose role has it getsview.scaleKubernetesClustergetsview.view.