Inspecting and Extracting Kubernetes Kubeconfig Data
Scenario
An engineer moving to another team handed over their kubeconfig. It is at /home/laborant/admin-kubeconfig on dev-machine.
Before anyone uses it, the security team wants a record of what it gives access to: which contexts it defines, which one is active, which cluster that context reaches, and which identity it authenticates as.
The kubeconfig holds several clusters, users, and contexts. Some users authenticate with a client certificate and some with a token.
Work on dev-machine as the default laborant user. Root access is not needed. Do not change the kubeconfig.
Task
Every answer comes from /home/laborant/admin-kubeconfig, not from the default kubeconfig.
- Write the names of all contexts in
/home/laborant/admin-kubeconfigto/home/laborant/context-list, one name per line. - Write the name of the active context in
/home/laborant/admin-kubeconfigto/home/laborant/active-context. - Write the name of the cluster associated with that active context to
/home/laborant/active-cluster. - Write the client certificate of the user that active context authenticates as to
/home/laborant/client-cert, decoded to PEM format.
Hint 1 | How a kubeconfig is organised
A kubeconfig has three lists and one pointer:
| Field | What it holds |
|---|---|
clusters | API server addresses and their CA certificates |
users | Credentials, such as a client certificate or a token |
contexts | Named pairs of one cluster and one user, plus an optional namespace |
current-context | The name of the context kubectl uses by default |
kubectl config reads any kubeconfig file you point it at:
kubectl config --help
kubectl config <subcommand> --kubeconfig=<path>
Hint 2 | Looking only at the active context
The users list is not in the same order as the contexts list, so the position of an entry tells you nothing. kubectl config view has a flag that limits its output to the cluster, user, and context the active context uses:
kubectl config view --help
Hint 3 | Getting at the certificate data
kubectl config view hides certificate and key data unless asked to show it. The data it holds is base64-encoded, and a single field can be printed on its own:
kubectl config view --kubeconfig=<path> <flags> -o jsonpath='{<path-to-field>}'
base64 --help