bhouse-nexthop opened a new pull request, #14910:
URL: https://github.com/apache/cloudstack/pull/14910

   ### 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>-kubeadmin` inside the 
owner's account, or `kubeadmin-<project>` for a project). 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 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.
   
   | | Before | After |
   |---|---|---|
   | Key pair | Latest key pair of the shared service user, shared by every 
cluster of the owner | `cks-<cluster uuid>` on the same service user, one per 
cluster; its id is the cluster detail `apiKeyPairId` |
   | Allowed APIs | Everything the service user's role allows | Only the APIs 
below, and only those the role also grants (a WARN lists any the role lacks) |
   | Delivery | `deploy-cloudstack-secret -k <api key> -s <secret key>` on the 
SSH command line, then `/tmp/cloud-config` | The cloud-config is copied as a 
0600 file with a name of its own, opened as stdin and deleted before 
`deploy-cloudstack-secret` runs. Create, scale and upgrade still store it only 
if the secret is missing; a rotation replaces the secret with `kubectl apply 
--server-side` |
   | Rotation | Not possible | New API `rotateKubernetesClusterKeys` |
   | Cluster delete | Key pair stays | The cluster's key pairs (`cks-<cluster 
uuid>`) deleted |
   
   **Allowed APIs**, taken from the calls in the code of each component:
   
   | Component | APIs |
   |---|---|
   | CloudStack Kubernetes provider (apache/cloudstack-kubernetes-provider 
`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`, `deleteNetworkACL` |
   | Cluster autoscaler (kubernetes/autoscaler 
`cluster-autoscaler/cloudprovider/cloudstack/service/cks.go`) | 
`listKubernetesClusters`, `scaleKubernetesCluster` |
   | Both, for async jobs | `queryAsyncJobResult` |
   | CloudStack CSI driver v3.0.0 (`pkg/cloud`), only when the cluster has CSI 
enabled | `listZones`, `listVolumes`, `createVolume`, `deleteVolume`, 
`attachVolume`, `detachVolume`, `resizeVolume`, `listSnapshots`, 
`createSnapshot`, `deleteSnapshot` |
   
   The 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`, 
`updateNetworkACLItem` and `listZones`, which these components call, and drops 
`updateFirewallRule`, which none of them calls. Roles created before keep what 
they have.
   
   **rotateKubernetesClusterKeys** (cluster must be Running):
   
   1. creates a new key pair for the cluster;
   2. copies the current `deploy-cloudstack-secret` to the control node and 
replaces the secret with a cloud-config carrying the new keys, generated as for 
a new cluster;
   3. restarts the components that read the secret: `cloud-controller-manager`, 
`cluster-autoscaler`, `cloudstack-csi-controller` and `cloudstack-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;
   4. records the new key pair and deletes the cluster's other key pairs, found 
by name.
   
   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.
   
   **Clusters created before this change** keep using the shared key pair, and 
scale and upgrade leave their secret as it is. `rotateKubernetesClusterKeys` 
moves 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:**
   
   - No end date and no automatic rotation: a key pair that expires on its own 
would stop the cluster's load balancers and autoscaling. Rotation is the 
explicit API, which an operator can schedule.
   - The rules are per API name, as #9504 defines them. A cluster's key can 
still call `deleteLoadBalancerRule` on another cluster of the same owner. 
Separating two clusters fully needs separate accounts or projects.
   - No schema change: the key pairs are in the #9504 tables and the id in 
`kubernetes_cluster_details`.
   
   Fixes #14909
   
   ### Types of changes
   
   - [ ] Breaking change (fix or feature that would cause existing 
functionality to change)
   - [x] 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
   - [x] Minor
   
   ### Screenshots (if appropriate):
   
   ### How Has This Been Tested?
   
   Unit tests: 150 in the kubernetes-service module pass, 22 of them new:
   
   - `KubernetesClusterManagerImplKeyPairTest` (12): 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.
   - `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 `--restart` but not with it; `--restart` 
only 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-id` only for a 
project-owned cluster.
   
   The script was also run against a stub `kubectl`: secret present and absent 
without `--restart`, the secret lookup failing (exits without storing), 
`--restart` with every component restarting, `kubectl apply` failing, a 
workload lookup failing, and a daemonset rollout timing out.
   
   Not yet tested on a lab; a test report will follow.
   
   #### How did you try to break this feature and the system with this change?
   
   - A role that lacks some of the APIs (a project role created by an older 
version): the key pair gets the rest and a WARN names the missing ones, instead 
of the create failing.
   - A rotate whose secret update fails, or whose SSH session drops: the old 
key pair stays in use; a rotate where a component does not restart, or the 
session drops while the script waits for one: the old key pair stays for its 
pods.
   - A first start that fails after creating the key pair: the next start 
reuses it instead of leaving it behind.
   - An API server error while checking for the secret during scale or upgrade: 
the script stops instead of replacing the secret; every kubectl call but the 
rollout waits has a 60 second request timeout, so the SSH session cannot hang.
   - The SSH command: `SshHelper` logs a failing command at ERROR, and the 
command holds no key.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to