Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Cluster tokens with spaces not handled properly #708

Open
justledbetter opened this issue Jun 2, 2021 · 2 comments
Open

Cluster tokens with spaces not handled properly #708

justledbetter opened this issue Jun 2, 2021 · 2 comments
Labels
kind/bug Something isn't working

Comments

@justledbetter
Copy link

Version (k3OS / kernel)

k3os version v0.20.6-k3s1r0
5.4.0-72-generic #80 SMP Tue May 4 19:31:44 UTC 2021

Architecture

x86_64

Describe the bug
When specifying a passphrase as the k3s service token (containing spaces), the token is not properly quoted in the startup configuration, causing a heavily truncated token to be used by the cluster.

k3os-93130 [~]$ sudo cat /etc/rancher/k3s/k3s-service.env 
K3S_CLUSTER_SECRET=this is a silly little sentence of zest and fun

To Reproduce

  • Standalone install, no yaml file
  • Entered the following token manually when prompted
this is my cluster secret

Expected behavior

As this is a development environment, I wasn't expecting to provide a super secure token; but I wanted it to be more secure than a semi-random string of 15 characters. And without copy/paste, wanted something that was relatively easy to type.

Actual behavior

The following error messages appear in the k3os startup logs:

/lib/rc/sh/gendepends.sh: /etc/rancher/k3s/k3s-service.env: line 1: is: not found

Note that this causes the environment to run with the actual token this, which is critically insecure.

k3os-21591 [~]$ sudo cat /etc/rancher/k3s/k3s-service.env 
K3S_CLUSTER_SECRET=this is my cluster secret

Additional context

I'm not sure if anyone ever expected to see spaces in k3s service tokens, but didn't find any documentation to suggest that they're forbidden either here or in the k3s repo.

https://resources.infosecinstitute.com/topic/password-security-complexity-vs-length

I believe this behavior was caused by Issue #590.

This is roughly the line that is causing the problem:
https://github.com/dweomer/k3os/blob/master/pkg/cc/funcs.go#L132

A proper fix might be to change any line that creates an environment variable within a shell script to read:

vars = append(vars, fmt.Sprintf("ENV_VARNAME=%s", strconv.Quote(value))
@justledbetter justledbetter added the kind/bug Something isn't working label Jun 2, 2021
@dweomer
Copy link
Contributor

dweomer commented Jun 3, 2021

Was wondering how long this would take to break someone. Thanks for reporting and doing the investigation!

@dweomer
Copy link
Contributor

dweomer commented Jun 3, 2021

As mentioned in #590 we are relying on the k3s install.sh capture of k3s-related environment variables and that processing does not currently support whitespace nor shell-relevant special characters (such as !) in tokens. Until a fix lands with k3s the work-around will be to use dashes as separators instead of spaces.

dweomer added a commit to dweomer/k3os that referenced this issue Jun 3, 2021
Partially addresses rancher#708

Signed-off-by: Jacob Blain Christen <jacob@rancher.com>
dweomer added a commit to dweomer/k3s that referenced this issue Oct 22, 2021
Leverage posix builtins + grep (with regex matching) to emit quoted
environment variables to K3S_FILE_ENV, e.g.:

```shell
$ export CONTAINERD_TEST_1='one!1' K3S_TEST_2=two K3S_TEST_3='thr ee' TEST_4='!@#$%^&*()_+four!'
$ sh -c export | while read x v; do echo $v; done | grep -E '^(K3S|CONTAINERD)_'
CONTAINERD_TEST_1='one!1'
K3S_TEST_2='two'
K3S_TEST_3='thr ee'
```

Fixes k3s-io#3395
Addresses rancher/k3os#708

Signed-off-by: Jacob Blain Christen <jacob@rancher.com>
dweomer added a commit to k3s-io/k3s that referenced this issue Oct 22, 2021
Leverage posix builtins + grep (with regex matching) to emit quoted
environment variables to K3S_FILE_ENV, e.g.:

```shell
$ export CONTAINERD_TEST_1='one!1' K3S_TEST_2=two K3S_TEST_3='thr ee' TEST_4='!@#$%^&*()_+four!'
$ sh -c export | while read x v; do echo $v; done | grep -E '^(K3S|CONTAINERD)_'
CONTAINERD_TEST_1='one!1'
K3S_TEST_2='two'
K3S_TEST_3='thr ee'
```

Fixes #3395
Addresses rancher/k3os#708

Signed-off-by: Jacob Blain Christen <jacob@rancher.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants