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

   ### 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; #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)
   - [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
   
   #### 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`.
   


-- 
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