Repository navigation
CKS: give each cluster its own API key pair, limited to the APIs it calls, with rotateKubernetesClusterKeys - #14910
bhouse-nexthop wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #14910 +/- ##
============================================
+ Coverage 3.86% 20.94% +17.08%
- Complexity 0 21184 +21184
============================================
Files 494 6058 +5564
Lines 42888 549730 +506842
Branches 8141 78946 +70805
============================================
+ Hits 1657 115163 +113506
- Misses 40999 421104 +380105
- Partials 232 13463 +13231
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:
|
d389a0b to
6779762
Compare
|
Tested on a KVM lab running a main (24.0) build that includes this change, at the current head of this PR. Primary storage is Ceph RBD. Each CKS cluster is Kubernetes 1.34.7, 1 control and 1 worker node, without CSI. It was run twice: once with the cluster owned by an account, and once with the cluster in a project (service account Result: 31 of 31 checks passed for the account-owned cluster, and 32 of 32 for the project cluster.
The merge of the cloud-config on rotation is also covered by Not tested: a cluster with CSI enabled (the extra volume and snapshot rules), the cluster autoscaler on the new key, and a rotation with a node down. |
Every CKS cluster of an account or project used the same API key pair, the latest one of a shared service user, and it was never rotated. A key read from kube-system/cloudstack-secret of one cluster could not be revoked without breaking every other cluster of that owner, and with an account-owned cluster it carried all the owner's rights. The keys also went to the control node on the SSH command line, and the secret was only created if missing, so it could not be updated. A new cluster now gets its own key pair, named cks-<cluster uuid>, on the service user. Its rules allow only the APIs that the CloudStack Kubernetes provider and the cluster autoscaler call, plus those of the CloudStack CSI driver when the cluster has CSI enabled, and only those the service account's role grants. The rules are per API, not per resource, so the key can still act on the owner's other clusters. The key pair id is kept in the cluster details. The cloud-config is copied to the control node as a 0600 file and read on stdin. Creating, scaling and upgrading a cluster store it only if the secret is missing, as before. rotateKubernetesClusterKeys creates a new key pair, replaces the api-key and secret-key lines of the secret with kubectl apply --server-side, keeping its other lines such as ssl-no-verify, restarts the components that read the secret and waits for them, then deletes the cluster's other key pairs. If the secret is not stored, or a component does not restart, the key pairs that may still be in use stay until a rotation completes or the cluster is deleted. A cluster created before this change keeps the shared key pair until it is rotated; the shared key pair stays, as other clusters may still use it. Deleting a cluster deletes its key pairs, and a rotation that finds the cluster being deleted deletes the key pair it created. The Kubernetes cluster view gets a Rotate API keys action, shown while the cluster is Running, with a confirmation that says what the rotation does. The key pairs belong to the user <account>-kubeadmin of the owner account, or for a project to the user of the project's service account, and only to a user of that account. Signed-off-by: Brad House <bhouse@nexthop.ai>
6779762 to
01ab543
Compare
Description
Every CKS cluster of an account or project used the same API key pair: the latest key pair of a shared service user (
<account>-kubeadmininside the owner's account, orkubeadmin-<project>for a project). A key read fromkube-system/cloudstack-secretof one cluster could not be revoked without breaking every other cluster of that owner, and with an account-owned cluster it carried all the owner's rights. The keys went to the control node on the SSH command line, and the secret was created only if missing, so it could not be replaced.Building on the key pairs of #9504, each cluster now has its own key pair.
cks-<cluster uuid>on the same service user, one per cluster; its id is the cluster detailapiKeyPairIddeploy-cloudstack-secret -k <api key> -s <secret key>on the SSH command line, then/tmp/cloud-configdeploy-cloudstack-secretruns. Create, scale and upgrade still store it only if the secret is missing; a rotation replaces the secret withkubectl apply --server-siderotateKubernetesClusterKeys, and a "Rotate API keys" action in the UIcks-<cluster uuid>) deletedAllowed APIs, taken from the calls in the code of each component:
main:cloudstack.go,cloudstack_instances.go,cloudstack_loadbalancer.go)listVirtualMachines,listNetworks,listCapabilities,listPublicIpAddresses,associateIpAddress,disassociateIpAddress,listLoadBalancerRules,createLoadBalancerRule,updateLoadBalancerRule,deleteLoadBalancerRule,assignToLoadBalancerRule,removeFromLoadBalancerRule,listLoadBalancerRuleInstances,listLBStickinessPolicies,createLBStickinessPolicy,deleteLBStickinessPolicy,listFirewallRules,createFirewallRule,deleteFirewallRule,listNetworkACLLists,listNetworkACLs,createNetworkACL,updateNetworkACLItem,deleteNetworkACLcluster-autoscaler/cloudprovider/cloudstack/service/cks.go)listKubernetesClusters,scaleKubernetesClusterqueryAsyncJobResultpkg/cloud), only when the cluster has CSI enabledlistZones,listVolumes,createVolume,deleteVolume,attachVolume,detachVolume,resizeVolume,listSnapshots,createSnapshot,deleteSnapshotThe autoscaler APIs are always allowed, as autoscaling can be turned on after the cluster is created. The "Project Kubernetes Service Role" is now built from the same two lists. Compared with before, it gains
listCapabilities, the three LB stickiness APIs,listNetworkACLLists,updateNetworkACLItemandlistZones, which these components call, and dropsupdateFirewallRule, which none of them calls. Roles created before keep what they have.rotateKubernetesClusterKeys (cluster must be Running):
deploy-cloudstack-secretto the control node and replaces theapi-keyandsecret-keylines of the secret's cloud-config with the new keys (adding them under[Global]if missing); every other line is kept, so a setting an operator added, such asssl-no-verify = true, survives, and so doapi-urlandproject-id. The merge is done on the node in bash builtins, so the keys never appear in a process's arguments. If the secret cannot be read, it is left as it is and the rotation fails;cloud-controller-manager,cluster-autoscaler,cloudstack-csi-controllerandcloudstack-csi-node, whichever exist, and waits for their rollouts (3 minutes per deployment, 15 for the daemonset), so the old key pair is deleted only once they use the new one;If the secret is not stored, the cluster keeps its keys and the job fails. The new key pair is not deleted then, as the script may have stored it before the SSH session failed; it is deleted by the next rotation that completes, or with the cluster. If the secret is stored but the script does not report every component restarted, the new key pair is recorded, the previous ones stay for the pods still running with them, and the job fails with the script output; rotating again once the component runs completes it. The job is serialised per cluster. If the cluster turns out to be being deleted once the new key pair is created, the rotation deletes that key pair and fails, as the deletion may already have deleted the cluster's key pairs.
The rotation needs the cluster Running with every node up: it reaches the first control node over SSH and waits for the CSI node daemonset to restart on each node.
UI: the Kubernetes cluster view has a "Rotate API keys" action, shown while a CloudManaged cluster is Running and only to callers allowed
rotateKubernetesClusterKeys. Its confirmation says what happens: a new key pair is stored in the cluster, the provider, autoscaler and CSI driver (whichever run in it) are restarted, the old key pair is deleted while a shared one from before 24.0.0 is kept, a failed step keeps the old key pair so the rotation can be run again, and every node must be up. The API returns only success, so nothing new is shown in the cluster details and no key is shown anywhere.The key pairs belong to the service user
<account>-kubeadminof the owner account, or for a project to the user of the project's service account, and only to a user of that account: an account whose name starts withkubeadmin-is no longer taken for a project service account, and a user of that name in another account is refused.Clusters created before this change keep using the shared key pair, and scale and upgrade leave their secret as it is.
rotateKubernetesClusterKeysmoves such a cluster onto its own key pair; it does not delete the shared key pair, as other clusters may still use it. Moving every existing cluster, and then deleting the shared key pair, is not part of this PR.Decisions:
deleteLoadBalancerRuleon another cluster of the same owner. Separating two clusters fully needs separate accounts or projects.kubernetes_cluster_details.Fixes #14909
Types of changes
Feature/Enhancement Scale or Bug Severity
Feature/Enhancement Scale
Screenshots (if appropriate):
How Has This Been Tested?
Unit tests: 161 in the kubernetes-service module pass, 33 of them new:
KubernetesClusterManagerImplKeyPairTest(16): allowed APIs with and without CSI, APIs the role lacks left out, a role allowing none refused (a key pair without rules would have every permission of its account), the key pair name and rules, a second start reusing the key pair of a failed first one, rotate (new pair recorded and the others deleted; secret not stored keeps every pair; components not all restarted records the new pair but keeps the others; refused when Stopped), deleting by name all pairs of the cluster but the one kept, a rotation that finds the cluster being deleted deleting its new key pair, and the service user lookup (an account named like a project service account, a project, a user of another account refused).KubernetesClusterActionWorkerSecretTest(10): the keys go only in the copied 0600 file, under a name of its own, and never in the SSH command; an existing secret is accepted without--restartbut not with it;--restartonly for a rotation; the secret counts as stored only when the script reports it, so an older script that is still in place counts as failed; the keys file is removed by a second command only when the script did not run; a cluster without its own key pair keeps its secret;project-idonly for a project-owned cluster.DeployCloudStackSecretScriptTest(7): runsdeploy-cloudstack-secretagainst a stubkubectl: with--restart, an existing secret with extra lines (ssl-no-verify, a value holding=and spaces, a comment) keeps them and gets only the new keys; missing keys are added under[Global]; key names in any case are replaced; a missing secret gets the new cloud-config; a secret that cannot be read is left as it is and nothing is reported stored; no key appears in akubectlargument; without--restart, an existing secret is left and a missing one stored.UI:
npm run lintfinds no errors; theActionButtonandAutogenViewunit tests pass (137 of 137).The script was also run against a stub
kubectl: secret present and absent without--restart, the secret lookup failing (exits without storing),--restartwith every component restarting,kubectl applyfailing, a workload lookup failing, and a daemonset rollout timing out.Tested on a KVM lab with an account-owned and a project cluster, rotation included: see the test report in the comments.
How did you try to break this feature and the system with this change?
SshHelperlogs a failing command at ERROR, and the command holds no key.