Skip to content

Join CKS nodes with a fresh token and a pinned CA - #14769

Open
bhouse-nexthop wants to merge 1 commit into
apache:4.22from
bhouse-nexthop:cks-secure-join
Open

bhouse-nexthop wants to merge 1 commit into
apache:4.22from
bhouse-nexthop:cks-secure-join

Conversation

@bhouse-nexthop

@bhouse-nexthop bhouse-nexthop commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Fixes #14768

Nodes join a CKS cluster with a never-expiring bootstrap token derived from the cluster UUID, and with --discovery-token-unsafe-skip-ca-verification. This gives every join a fresh, short-lived token and pins the cluster CA.

step how
create CloudStack generates the cluster CA, a random bootstrap token and a random HA certificate key. The token TTL is the start timeout
create, CA copied to the first control node over SSH, not put in userdata. kubeadm init waits for it. Every other node joins with --discovery-token-ca-cert-hash sha256:<hash>
create, cleanup once all nodes are Ready, the token and the kubeadm-certs secret are deleted
scale-up, add-node kubeadm token create --ttl <timeout> --print-join-command on the control node gives a new token and the CA hash, so clusters created before this change keep scaling. The output is parsed strictly (token and hash formats) before it goes into userdata. The token is deleted afterwards, and also when an add-node fails before any node was given it
API server certificate joins now verify the certificate, so it always lists the control node's own IP next to the public IP. With external etcd the control node advertises its own IP (as kubeadm already does for stacked etcd); the public IP stays the controlPlaneEndpoint and kubeconfig is unchanged
add-node target the control node's own IP, as before, unless the API server certificate served there is not valid for it (openssl x509 -checkip on the control node). That is the case for external etcd clusters created before this change, which then join on the address --print-join-command prints, which the certificate covers. If the check can't tell, the control node's IP is used

Not done here: deleting the old <forever> UUID token on existing clusters. That is a one-off migration step.

Related: conflicts with #14819 (fail add-node when a node isn't added) in KubernetesClusterAddWorker: failure block, outer catch and helpers. Whichever lands second takes #14819's failure path, keeps this PR's token create/delete, and deletes the join token after the Ready wait on a partial add. A reference resolution is available.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Feature/Enhancement Scale or Bug Severity

Bug Severity

  • Major

How Has This Been Tested?

Unit tests: KubernetesCluster*Test in the plugin pass, including the new token, certificate-key, CA-hash, join-command-parsing and token-delete tests.

Lab: a nested KVM lab (3 hosts, Ceph RBD). It runs a main (24.0.0-SNAPSHOT) build with this commit picked on top, so it has not been run on a 4.22 build. Kubernetes v1.33.13, plus v1.32.5 for the old cluster.

case joins kubeadm token list afterwards
create, 1 control + 2 workers workers: --discovery-token-ca-cert-hash sha256:…, which matches the hash of /etc/kubernetes/pki/ca.crt; no unsafe flag in any node's userdata no bootstrap token; kubeadm-certs deleted
create, 3 control + 1 worker (HA) kubeadm init --token-ttl 3600s … --certificate-key <random>; both control-plane joins and the worker join pin the same CA hash no bootstrap token; kubeadm-certs deleted
scale-up +1 worker (single control and HA) new node pins the CA hash the scale token was deleted
add-node, Ubuntu 24.04 VM pins the CA hash the add-node token was deleted
add-node that fails validation – no token left (an earlier revision of this change left a 59m token here)
cluster created by the old packages, scaled after the upgrade old node: unsafe flag (as created); new node: --discovery-token-ca-cert-hash only the old <forever> token remains, as before

The only entry that remains is kubeadm's own "Proxy for managing TTL for the kubeadm-certs secret". It has no usages, so it cannot be used to join, and it expires after 2h.

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

  • Scaled a cluster that was created by the old packages.
  • Ran add-node with a VM that fails validation, then checked kubeadm token list.

@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 42.63959% with 113 lines in your changes missing coverage. Please review.
✅ Project coverage is 18.33%. Comparing base (b56bbc2) to head (6fef2a0).
⚠️ Report is 2 commits behind head on 4.22.

Files with missing lines Patch % Lines
...er/actionworkers/KubernetesClusterStartWorker.java 16.25% 67 Missing ⚠️
...r/actionworkers/KubernetesClusterActionWorker.java 49.15% 27 Missing and 3 partials ⚠️
...ster/actionworkers/KubernetesClusterAddWorker.java 0.00% 10 Missing ⚠️
...er/actionworkers/KubernetesClusterScaleWorker.java 0.00% 6 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               4.22   #14769      +/-   ##
============================================
+ Coverage     18.32%   18.33%   +0.01%     
- Complexity    16644    16677      +33     
============================================
  Files          5939     5939              
  Lines        537787   537967     +180     
  Branches      65932    65947      +15     
============================================
+ Hits          98544    98650     +106     
- Misses       428033   428105      +72     
- Partials      11210    11212       +2     
Flag Coverage Δ
uitests 4.08% <ø> (ø)
unittests 19.42% <42.63%> (+0.01%) ⬆️

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 added a commit to bhouse-nexthop/cloudstack that referenced this pull request Nov 22, 2026
A recreated worker joined again with the token in its user data, which
only works while that token exists. With short-lived join tokens
(apache#14769) it is gone after the create or scale.

- before the drain of a batch with workers to recreate, the rollout
  runs kubeadm token create --print-join-command on a control node,
  with a TTL of the node timeout, and reads the token and CA hash.
- before the restore, the kubeadm join of the VM's user data gets that
  token and --discovery-token-ca-cert-hash, in place of the old token
  and --discovery-token-unsafe-skip-ca-verification or an older hash.
  The rest of the user data is kept. The VM is stopped, so the network
  gets it when it starts.
- a worker whose user data has no kubeadm join is refused before the
  drain.
- the token is deleted once the batch ends, unless a recreated VM that
  is not back is running and may still be joining; it then expires on
  its own. A token whose output can't be parsed is deleted at once.
- a restore refused before the root disk was replaced puts the old user
  data back.

Works with or without apache#14769: the token comes from kubeadm and the join
line is rewritten whichever form it has.

Signed-off-by: Brad House <bhouse@nexthop.ai>
bhouse-nexthop added a commit to bhouse-nexthop/cloudstack that referenced this pull request Nov 22, 2026
A recreated worker joined again with the token in its user data, which
only works while that token exists. With short-lived join tokens
(apache#14769) it is gone after the create or scale.

- before the drain of a batch with workers to recreate, the rollout
  runs kubeadm token create --print-join-command on a control node,
  with a TTL of the node timeout, and reads the token and CA hash.
- before the restore, the kubeadm join of the VM's user data gets that
  token and --discovery-token-ca-cert-hash, in place of the old token
  and --discovery-token-unsafe-skip-ca-verification or an older hash.
  The rest of the user data is kept. The VM is stopped, so the network
  gets it when it starts.
- a worker whose user data has no kubeadm join is refused before the
  drain.
- the token is deleted once the batch ends, unless a recreated VM that
  is not back is running and may still be joining; it then expires on
  its own. A token whose output can't be parsed is deleted at once.
- a restore refused before the root disk was replaced puts the old user
  data back.

Works with or without apache#14769: the token comes from kubeadm and the join
line is rewritten whichever form it has.

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

Copy link
Copy Markdown
Collaborator Author

Also passed on a 4.22-based build (4.22.1.1-nexthop with this change, Kubernetes v1.32.3, nested KVM lab):

  • Create with 1 control node + 2 workers: the workers joined with kubeadm join ... --token ... --discovery-token-ca-cert-hash sha256:..., and --discovery-token-unsafe-skip-ca-verification was not used.
  • After the create, the join token and the kubeadm-certs secret were gone. kubeadm token list shows only kubeadm's own "Proxy for managing TTL for the kubeadm-certs secret" token (usages none).
  • Scale 2 -> 3 and four add-node joins of Ubuntu 24.04 VMs all worked. On an added node the join line also used --discovery-token-ca-cert-hash. After every scale and add-node, kubeadm token list showed no new token.

bhouse-nexthop added a commit to bhouse-nexthop/cloudstack that referenced this pull request Nov 24, 2026
With external etcd the kubeadm config advertised the public IP, so the
API server certificate was valid only for the public IP and the service
IP, not for the control node's own IP:

| field | was | now |
|---|---|---|
| localAPIEndpoint.advertiseAddress (external etcd) | public IP | control node IP |
| apiServer.certSANs, --apiserver-cert-extra-sans | public IP | control node IP, public IP |
| controlPlaneEndpoint | public IP | public IP (unchanged) |

- Nodes join on the control node's own IP. Today they skip CA
  verification; once they pin the CA (apache#14769), kubeadm checks the
  certificate and fails with "x509: certificate is valid for
  10.96.0.1, <public IP>, not <control node IP>".
- Advertising the node's own IP matches the stacked case, where kubeadm
  picks it by default. The kubernetes Service endpoint and the
  apiserver probes then stay on the guest network instead of going out
  through the public IP's port forward.
- The control node IP is also listed explicitly in the SANs, in both
  the config and the flag path, so the certificate does not depend on
  which address kubeadm picks.
- kubeconfig and the API rule still use the public IP.

Signed-off-by: Brad House <bhouse@nexthop.ai>
bhouse-nexthop added a commit to bhouse-nexthop/cloudstack that referenced this pull request Nov 24, 2026
With external etcd the kubeadm config advertised the public IP, so the
API server certificate was valid only for the public IP and the service
IP, not for the control node's own IP:

| field | was | now |
|---|---|---|
| localAPIEndpoint.advertiseAddress (external etcd) | public IP | control node IP |
| apiServer.certSANs, --apiserver-cert-extra-sans | public IP | control node IP, public IP |
| controlPlaneEndpoint | public IP | public IP (unchanged) |

- Nodes join on the control node's own IP. Today they skip CA
  verification; once they pin the CA (apache#14769), kubeadm checks the
  certificate and fails with "x509: certificate is valid for
  10.96.0.1, <public IP>, not <control node IP>".
- Advertising the node's own IP matches the stacked case, where kubeadm
  picks it by default. The kubernetes Service endpoint and the
  apiserver probes then stay on the guest network instead of going out
  through the public IP's port forward.
- The control node IP is also listed explicitly in the SANs, in both
  the config and the flag path, so the certificate does not depend on
  which address kubeadm picks.
- kubeconfig and the API rule still use the public IP.

Signed-off-by: Brad House <bhouse@nexthop.ai>
bhouse-nexthop added a commit to bhouse-nexthop/cloudstack that referenced this pull request Nov 24, 2026
24.0 counterpart of the add-node part of apache#14769.

- Add-node joins on the address `kubeadm token create
  --print-join-command` prints (the controlPlaneEndpoint, else the
  advertise address), which the API server certificate always covers.
- External etcd clusters created before the certificate fix have only
  the public IP in their certificate, so a pinned-CA join on the control
  node's private IP fails there.

Signed-off-by: Brad House <bhouse@nexthop.ai>
bhouse-nexthop added a commit to bhouse-nexthop/cloudstack that referenced this pull request Nov 24, 2026
Follow-up to "Join CKS nodes with a fresh token and a pinned CA", the
changes amended into apache#14769.

- The API server certificate always lists the control node's own IP as
  well as the public IP, since nodes join on the former and now verify
  the certificate. The external etcd kubeadm config advertises the
  control node's own IP (as kubeadm does in the stacked case); the
  public IP stays the controlPlaneEndpoint.
- Add-node joins on the address kubeadm prints with the join command
  (the controlPlaneEndpoint, else the advertise address), which the
  certificate always covers. External etcd clusters created before
  this change have only the public IP in their certificate.

Signed-off-by: Brad House <bhouse@nexthop.ai>
Problem:
- The bootstrap token was derived from the cluster UUID (visible in the
  API) and never expired.
- Nodes joined with --discovery-token-unsafe-skip-ca-verification.
- The HA certificate key was also derived from the UUID.

Change:
- Create: CloudStack generates the cluster CA, a random token and a
  random certificate key. The token expires after the start timeout.
- Create: the CA is copied to the first control node over SSH, not put
  in userdata; kubeadm init waits for it. Every other node pins its hash.
- Create: once all nodes are ready, the token and the kubeadm-certs
  secret are deleted.
- Scale and add-node: `kubeadm token create --print-join-command` on the
  control node gives a new token and the CA hash, so clusters created
  before this change keep scaling. The token is deleted afterwards, and
  also when an add-node fails before any node was given it.
- Joins use --discovery-token-ca-cert-hash.
- The API server certificate always lists the control node's own IP as
  well as the public IP, since nodes join on the former and now verify
  the certificate. The external etcd kubeadm config advertises the
  control node's own IP (as kubeadm does in the stacked case); the
  public IP stays the controlPlaneEndpoint.
- Add-node joins on the control node's own IP, as before, unless the
  API server certificate served there is not valid for it (external
  etcd clusters created before this change). Then it joins on the
  address kubeadm prints with the join command, which the certificate
  covers. If the certificate can't be checked, the control node's IP
  is used.

Not done here: deleting the old UUID token on existing clusters.

Signed-off-by: Brad House <bhouse@nexthop.ai>
bhouse-nexthop added a commit to bhouse-nexthop/cloudstack that referenced this pull request Nov 24, 2026
…vers it

24.0 counterpart of the add-node part of the amended
apache#14769.

- Add-node joins on the control node's own IP, as before apache#14769, unless
  the API server certificate served there is not valid for it (openssl
  -checkip on the control node): external etcd clusters created before
  the certificate fix. Then it joins on the address kubeadm token
  create --print-join-command prints, which the certificate covers.
- If the check can't tell, the control node's IP is used.

Signed-off-by: Brad House <bhouse@nexthop.ai>
bhouse-nexthop added a commit to bhouse-nexthop/cloudstack that referenced this pull request Nov 24, 2026
…vers it

Follow-up to "Put the control node's IP in the CKS API server
certificate", matching the amended apache#14769.

- Add-node joins on the control node's own IP, as before apache#14769, unless
  the API server certificate served there is not valid for it (openssl
  -checkip on the control node): external etcd clusters created before
  the certificate fix. Then it joins on the address kubeadm token
  create --print-join-command prints, which the certificate covers.
- If the check can't tell, the control node's IP is used.

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

Copy link
Copy Markdown
Collaborator Author

Lab test of the join-address change: KVM, isolated network, a 4.22-based build (46372ad) and a main-based build (d385b9d). "Private" is the control node's own IP and "public" is the cluster's public IP.

4.22-based build, addNodesToKubernetesCluster:

case join in the added node's user data management server log
stacked cluster (1 control + 1 worker) kubeadm join <private>:6443 … --discovery-token-ca-cert-hash, Ready, no x509 errors no "not valid for"
external etcd cluster created on this build (served cert covers 10.96.0.1, private and public) <private>:6443, Ready no "not valid for"
same cluster with apiserver.crt regenerated without the private IP (as clusters created before the fix have it) <public>:6443, Ready, no x509 errors The API server certificate of Kubernetes cluster … is not valid for <private>, nodes join on <public>

Run by hand on the control nodes, the check prints IP <private> does match certificate, and does NOT match after the regeneration. openssl is present there (OpenSSL 3.0.13).

Main-based build, recreate rollouts (worker template change, rollout=true), upgraded in place from the 4.22-based build with the clusters above:

case recreated worker's join and ExecStartPre log
stacked cluster <private>:6443 kept in both, Ready no "not valid for"
external etcd cluster created on this build: add-node, then recreate <private>:6443 for both, Ready no "not valid for"
external etcd cluster with the cert lacking the private IP join and curl rewritten to <public>:6443, node rejoined Ready, no x509 errors … is not valid for <private>, node <worker> joins on <public>

This also covers the external-etcd results from my earlier run that I hadn't posted. On both build lines, clusters created with this change get the control node's private IP in the API server certificate and in certSANs. Their workers join on the private IP with a fresh token and the CA hash, and the add-node and scale tokens are deleted afterwards.

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.

1 participant