nexusctl
Homelab
nexusctl runs kubectl against the cluster of the homelab setup with an access (a cluster account with limited rights and a fixed duration; see the glossary). It writes every call to a local journal, masks the values of Secrets in the output, and replaces SECRET: references with their values without passing them through the command line. Accesses are created and revoked with nexusadm. Messages of the command are in French.
Synopsis
nexusctl [--nexusconfig <name|path>] <kubectl arguments>
Every argument other than --nexusconfig goes to kubectl unchanged, except --from-literal values that hold a reference (see Secrets and references).
Accesses
| Source | Access used |
|---|---|
--nexusconfig <name> | The file <name>.json in the access folder |
--nexusconfig <path> | The file at that path (a value that contains /) |
NEXUSCONFIG environment variable | Same as --nexusconfig, when the option is absent |
| Neither | The access named read |
The access folder is NEXUS_CONFIG_DIR, otherwise $XDG_CONFIG_HOME/nexus, otherwise ~/.config/nexus. An access file holds the cluster address, its public certificate, the access name, its expiry date and the Secrets it lists. The token lives in the keyring of the desktop session (secret-tool), never in the file. --nexusconfig given twice, or without a value, ends the command with exit code 2.
For each call, nexusctl writes a temporary kubeconfig without any secret in a private folder (under $XDG_RUNTIME_DIR when it exists), and kubectl obtains the token from nexusctl through a Unix socket while the command runs. The token does not appear in a file, in the environment or in the arguments of kubectl. The folder is deleted when the command ends. nexusctl replaces KUBECONFIG in the environment of kubectl.
The read access
nexusadm grant, without arguments, creates the read access for 8 hours. It grants get, list and watch on the whole cluster through the Kubernetes view role, plus:
nodes,persistentvolumes;storageclasses,ingressclasses,customresourcedefinitions;prometheuses,servicemonitors,podmonitorsandprometheusrulesofmonitoring.coreos.com.
The view role does not include Secrets: with read, kubectl get secret returns Forbidden.
Other accesses
Any other access is created with nexusadm grant request <name>, with its rules, its duration (10 minutes to 168 hours, 8 hours by default, 1 hour at most when the access covers Secrets) and the references it lists. See nexusadm.
Missing or expired access
When the access file is missing or unreadable, or when the keyring holds no token for the access or the token has expired, nexusctl prints the cause with the command that creates the access (nexusadm grant request <name>) and exits with code 1. kubectl does not run and the journal records nothing.
Refused options and subcommand
These options would replace the account, the context or the server of the access. nexusctl ends with exit code 2 when one of them appears before --, in the --option value form or the --option=value form:
| Options |
|---|
--kubeconfig, --context, --cluster, --user |
--server, -s |
--token, --username, --password |
--as, --as-group, --as-uid |
--client-certificate, --client-key, --certificate-authority, --insecure-skip-tls-verify |
The config subcommand ends with exit code 2 as well, since it can print the token. Arguments after -- belong to the command run in the pod and are not checked.
A refused command is recorded in the journal with the decision refusee.
Journal
Each call writes JSON lines to infra/.nexusctl/journal.jsonl of the repository, or to the file named by NEXUSCTL_JOURNAL. The file is created with mode 0600. nexusadm log prints it.
| Event | Fields |
|---|---|
debut, before kubectl runs | Call identifier, tool (nexusctl), UTC time, system user, cluster account (system:serviceaccount:nexus-access:nexus-<access>), name of the access file, working directory, arguments |
decision | refusee when an option, the config subcommand or a reference is refused |
secret | The references resolved for the call, never their values |
fin | Exit code and duration in milliseconds |
In the recorded arguments, the values of --from-literal, --docker-password, --password and --token are replaced with ***.
When the debut line cannot be written, kubectl does not run and the exit code is 1. When the fin line cannot be written, nexusctl prints the error and keeps the exit code of kubectl.
The journal is a local file that the account running nexusctl can modify. The requests seen by the API server are in its audit log, which nexusadm audit reads.
Secrets and references
Masking in the output
The access file lists the namespaces whose Secrets the access can read (* for all) and the references it accepts. Before running kubectl, nexusctl reads those Secrets with the access, then replaces in the standard output and the standard error:
- each value of a cluster Secret, in base64 and decoded, with
SECRET:<namespace>/<name>#<key>; - each value of a listed keyring secret with
SECRET:keyring/<name>.
Values shorter than 8 bytes are not masked. Output is filtered as it arrives, so logs -f and get -w keep streaming. When the access cannot read any Secret, as with read, the output goes straight to the terminal. A Secret that the access fails to read is skipped without a message.
The access file can be modified by the account that owns it: masking prevents a value from being printed by accident and does not restrict what the access can read.
References in --from-literal
A reference designates a secret without containing its value:
| Reference | Value |
|---|---|
SECRET:<namespace>/<name>#<key> | Key <key> of the Secret <name> in <namespace>, read with the access |
SECRET:keyring/<name> | Secret stored in the keyring with nexusadm secret add <name> |
A reference is accepted only as the value of a --from-literal key:
nexusctl --nexusconfig deploy -n my-app create secret generic db \
--from-literal=password=SECRET:keyring/db-password
nexusctl writes the value to a private file of the access folder (mode 0600) and passes --from-file=password=<file> to kubectl.
The command ends with exit code 2, before kubectl runs, when:
- the reference is not the whole value (
--from-literal=url=postgres://SECRET:keyring/db-password); - the reference has another form than the two above;
- a
SECRET:reference appears in any other argument; - the access does not list the reference (add it with
--secrettonexusadm grant request); - the Secret, its key or the keyring entry cannot be read.
A cluster reference listed in the access also grants get on that one Secret.
Inside the sandbox
In the sandbox that infra/bin/nexus-sandbox starts (bubblewrap, without the keyring, the access files or the administration kubeconfig), NEXUS_BROKER is set and nexusctl sends the command to a broker that runs outside the sandbox. The broker runs the same command with the same standard streams and returns its exit code. It passes only the NEXUSCONFIG and TERM variables, and accepts only a working directory inside the project. When the broker cannot be reached, the command exits with code 2.
Exit codes
| Code | Meaning |
|---|---|
Code of kubectl | kubectl ran |
| 1 | Access missing, expired or unreadable; journal cannot be written |
| 2 | --nexusconfig repeated or empty; refused option or subcommand; refused or unreadable reference; broker unreachable |
| 127 | kubectl cannot start (missing from PATH) |
Examples
List the pods of a namespace with the read access:
nexusctl get pods -n my-app
Follow the logs of a deployment with the deploy access:
nexusctl --nexusconfig deploy logs -f deploy/my-app -n my-app
Pick the access for several commands:
export NEXUSCONFIG=deploy
nexusctl rollout restart deploy/my-app -n my-app
Forward a port, with an access created by nexusadm grant request pf --port-forward my-app:
nexusctl --nexusconfig pf port-forward svc/my-app 8080:80 -n my-app