Skip to content

CKS: keep registry credentials out of node logs and safe from special characters - #14918

Open
bhouse-nexthop wants to merge 1 commit into
apache:4.22from
bhouse-nexthop:cks-registry-secrets
Open

bhouse-nexthop wants to merge 1 commit into
apache:4.22from
bhouse-nexthop:cks-registry-secrets

Conversation

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator

Description

With a private registry, every CKS node ran its registry script as bash -x /opt/bin/setup-containerd, so the
registry user name and password, in plain text and as the base64 auth string, were written to
/var/log/cloud-init-output.log on every worker and control node. The URL, user name and password were also
substituted into the cloud-config as plain text: a double quote, backslash, $ or newline broke the script or the
YAML, and {{ or {% made cloud-init fail to render the control node template (it is a Jinja template), so it
dropped the whole cloud-config and the control node was never set up.

Now:

  • The script runs without -x. It prints its own progress lines, and the credentials are the reason it exists, so
    tracing it can't be made safe by turning tracing off around some lines.
  • The URL and its endpoint go into the cloud-config base64-encoded and the script decodes them. The user name and
    password are no longer substituted at all: containerd gets only auth = "<base64 of user:password>", which it
    reads the same way as the username/password pair. No character in the values can change the cloud-config, the
    Jinja template or the script.
  • /opt/bin/setup-containerd is 0700 and /etc/containerd/config.toml 0600, as both hold the auth string.
  • createKubernetesCluster refuses a registry URL that is not a valid URI (double quotes, backslashes, whitespace),
    which would break the containerd config. new URL() accepted them.

Other secrets in the same templates, checked: the kubeadm join token and certificate keys are generated values with
safe characters and their scripts are not traced; the API server key is written 0600. Not changed.

Not changed here: on 4.22 the control node template only writes the registry script under
{% if registry is defined %}, which is never true, so the control node gets no registry (#14408 fixes that).

Fixes: #14917

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • 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
  • Minor

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

How Has This Been Tested?

Unit tests in KubernetesClusterActionWorkerTest, for the worker, control and added control node templates, with a
password containing {{, {%, quotes, a backslash, $(...) and a newline and a URL with ', $(...) and &: no
plain value is left in the rendered cloud-config, the URL, endpoint and auth are there base64-encoded, the runcmd is
bash /opt/bin/setup-containerd without -x, and the script is 0700. KubernetesClusterManagerImplTest: the URL
check refuses a double quote, a newline and a missing scheme. mvn -pl plugins/integrations/kubernetes-service -am test: all tests pass.

The rendered worker script was also run against a containerd 1.7 default config with those values: the resulting
config parses as TOML, the auth string decodes to the original user name and password, and the config is 0600.

A lab test on a KVM zone follows in a comment.

… characters

The private registry script on every node ran under bash -x, so the
registry user name and password, in plain text and base64, were written
to the cloud-init output log. The registry URL, user name and password
were also put into the cloud-config as they are: a quote, backslash, $
or newline broke the script or the cloud-config, and {{ or {% made
cloud-init fail to render the control node template, so it dropped the
whole cloud-config and the node was never set up.

Run the script without tracing. Pass the URL and its endpoint
base64-encoded and decode them in the script, and give containerd only
the base64 auth string instead of the user name and password, so no
character in them can change the cloud-config, the template or the
script. Make the script and the containerd config readable by root
only, as they hold the credentials. Refuse a registry URL that is not a
valid URI, such as one with double quotes, backslashes or spaces, which
would break the containerd config.

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

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 4.08%. Comparing base (a8735b2) to head (c3835b2).

❗ There is a different number of reports uploaded between BASE (a8735b2) and HEAD (c3835b2). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (a8735b2) HEAD (c3835b2)
unittests 1 0
Additional details and impacted files
@@              Coverage Diff              @@
##               4.22   #14918       +/-   ##
=============================================
- Coverage     18.34%    4.08%   -14.26%     
=============================================
  Files          5939      449     -5490     
  Lines        537817    38282   -499535     
  Branches      65938     7083    -58855     
=============================================
- Hits          98640     1564    -97076     
+ Misses       427954    36503   -391451     
+ Partials      11223      215    -11008     
Flag Coverage Δ
uitests 4.08% <ø> (ø)
unittests ?

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

Copy link
Copy Markdown
Collaborator Author

Tested on a KVM lab with a main (24.0) build, first without and then with this change. Primary storage is Ceph RBD. The build also carries #14408 (so the control node gets the registry config too) and #14898 (the hosts.toml version of setup-containerd), and the change is applied on top of both. The private registry is registry:2 with htpasswd basic auth over HTTP, and it refuses anonymous pulls (401). Its test user has a fake password containing {{ }}, {% %}, a double quote and a single quote. Each cluster runs Kubernetes 1.33.1 from an ISO, with 1 control node and 1 worker node. A DaemonSet pulls an image that exists only in that registry, so a Running pod proves the node sent the credentials.

Once setup finished, every node was searched for the password, the base64 user:password and the base64 password: in /var/log, and in any world-readable file under /etc, /opt, /var/lib/cloud, /run/cloud-init, /home, /root and /tmp. As a positive control, the same search had to find the auth string in config.toml and setup-containerd, with the modes this change sets.

Check Without the change With the change
Cluster state Running Running, both with stacked etcd and with 1 external etcd node
Credentials in /var/log/cloud-init-output.log on the control and worker nodes yes (from the bash -x trace) no
Credentials anywhere else under /var/log, or in a world-readable file not checked none
/etc/containerd/config.toml holds the auth 0644 (as the template sets it) 0600 root:root
/opt/bin/setup-containerd holds the auth 0755 (as the template sets it) 0700 root:root
Pull from the registry on the control and worker nodes Ready 2/2
Registry URL with a single quote accepted. The script dies on a syntax error, so there is no auth table and the pull fails (0/2) not run in the lab. A single quote is valid in a URI, so it is accepted, and the URL now reaches the script base64-encoded; the unit tests cover a URL with '
Registry URL with a double quote, or with braces refused: "Invalid docker registry url", and no cluster is created

Runs without the change: the leak check passed 9 of 9 and the broken-URL check 10 of 10. Runs with the change: invalid URLs were refused in 2 of 2 runs (5 of 5 checks each), and the stacked etcd and external etcd clusters each passed 17 of 17.

All clusters were deleted afterwards, and the test user was removed from the registry.

Not tested: 4.22, where the base has no #14408 so the control node gets no registry config. Also not tested: a registry over HTTPS, and the external etcd node itself, which gets no registry config.

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