163

I inherited a Kubernetes/Docker setup, and I accidentally crashed the pod by changing something relating to the DB password.

I am trying to troubleshoot this.

I don't have much Kubernetes or Docker experience, so I'm still learning how to do things.

The value is contained inside the db-user-pass credential I believe, which is an Opaque type secret.

I'm describing it:

kubectl describe secrets/db-user-pass
Name:         db-user-pass
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
password:  16 bytes
username:  13 bytes

but I have no clue how to get any data from this secret. The example on the Kubernetes site seems to assume I'll have a base64 encoded string, but I can't even seem to get that. How do I get the value for this?

5
  • 13
    Try kubectl get secret db-user-pass -o yaml, which will dump it out in YAML form and usually includes the encoded secret values.
    – David Maze
    Jul 5, 2019 at 21:03
  • 1
    Perfect! Thank you. If you want to write this as an answer I'll accept it. Jul 5, 2019 at 21:03
  • 43
    kubectl get secret db-user-pass -o json | jq '.data | map_values(@base64d)'. This givest the nicest results but needs the jq dependency. Oct 28, 2020 at 14:43
  • for dots in json see this: github.com/kubernetes/kubernetes/issues/23386
    – Tilo
    Aug 25, 2021 at 18:29
  • kubectl get secrets --namespace=jhalokia my-jalokia-credentials -ojsonpath='{.data.password}' | base64 -d
    – Sanjeev
    Nov 21, 2022 at 16:26

18 Answers 18

201

You can use kubectl get secrets/db-user-pass -o yaml or -o json where you'll see the base64-encoded username and password. You can then copy the value and decode it with something like echo <ENCODED_VALUE> | base64 -D (Mac OS X).

A more compact one-liner for this:

kubectl get secrets/db-user-pass --template={{.data.password}} | base64 -D

and likewise for the username:

kubectl get secrets/db-user-pass --template={{.data.username}} | base64 -D

Note: on GNU/Linux, the base64 flag is -d, not -D.

5
  • 24
    according to base64 on centos7 the command is base64 -d not -D
    – TheSteve0
    Mar 12, 2020 at 19:29
  • 2
    -D is not available for ubuntu 18 as well Jul 28, 2020 at 5:28
  • 7
    This answer was posted by someone on a mac OS. -d is the appropriate argument for decode in linux. Sep 16, 2020 at 23:02
  • 14
    This template doesn't work when a key inside a secret contains a dot (e.g. credentials.json). The working variant is: kubectl get secrets/secret-name --template="{{index .data \"credentials.json\" | base64decode}}". Also note the usage of Go's base64decode instead of OS's base64 to make it work under any OS. Dec 5, 2020 at 5:36
  • There is no point making this work on any OS, as docker only really works in Linux. I've updated the answer to work with bash and made a note about OS/X and zsh. May 4, 2021 at 15:33
98

I would suggest using this handy command. It utilizes a power of go-templates. It iterates over all values, decodes them, and prints them along with the key. It also handles not set values.

kubectl get secret name-of-secret -o go-template='
{{range $k,$v := .data}}{{printf "%s: " $k}}{{if not $v}}{{$v}}{{else}}{{$v | base64decode}}{{end}}{{"\n"}}{{end}}'

## In your case it would output
# password: decoded_password
# username: decoded_username

If you don't like go-templates you can use different output formats e.g. yaml or json, but that will output secrets encoded by base64.

3
  • 8
    This is the most robust answer. Jan 11, 2020 at 12:29
  • 1
    Has anyone figured out the syntax to get this to work in PowerShell? If I put the template in a file, and use --go-template-file it works. But --go-template behaves differently (gives various errors about unexpected this or that). Even with properly escaping the string (which, frankly, is painful).
    – BrettJ
    Apr 19, 2020 at 16:34
  • 1
    Use: kubectl get secret name-of-secret -o go-template='{{range $k,$v := .data}}{{printf \"%s: \" $k}}{{if not $v}}{{$v}}{{else}}{{$v | base64decode}}{{end}}{{\"\n\"}}{{end}}' Note the escaped double quotes.
    – Rashack
    Jan 14, 2021 at 13:40
89

If you have jq (json query) this works:

kubectl get secret db-user-pass -o json | jq '.data | map_values(@base64d)'

NOTE:

  • db-user-pass is the name of the k8s secret
  • .data is the variable within that contains the secret value
3
  • 4
    This works! Note it requires jq 1.6 or higher (which is not installed baseline on some systems) and may have issues per github.com/stedolan/jq/issues/47
    – btown
    Mar 28, 2020 at 20:30
  • 4
    This gives the nicest result! Oct 28, 2020 at 14:41
  • Thanks for this nice command line, the result is magic
    – rasolog
    Mar 14, 2021 at 11:19
18

This should work on all platforms, with kubectl 1.11+

kubectl get secrets/db-user-pass --template='{{.data.password | base64decode}}'

If there is a "-" in the password, the following will work

kubectl get secrets/db-user-pass --template='{{ index .data "sql-password" | base64decode}}'

And if you want to get all keys, values

kubectl get secrets/db-user-pass --template='{{ range $key, $value := .data }}{{ printf "%s: %s\n" $key ($value | base64decode) }}{{ end }}'
3
  • and if there is a dash "-" in the password key the first example will not work, it will return an error: error parsing template {{ .data.sql-password |base64decode }}, template: output:1: bad character U+002D '-' , you can do the following kubectl get secrets/db-user-pass --template='{{ index .data "sql-password" | base64decode}}'
    – Maoz Zadok
    Apr 14, 2022 at 6:40
  • 1
    Pity that kubernetes.io/docs/tasks/configmap-secret/… has not been updated with this more portable idiom. BTW you can include {{"\n"}} at the end to get a trailing newline, nicer for interactive use. Feb 3 at 18:12
  • @JesseGlick thank you for clearing this point, I'll add it to the answer
    – Maoz Zadok
    Feb 4 at 6:06
15

If your secret keys contain dash (-) or dot (.):

kubectl get secret db-user-pass -o=go-template='{{index .data "password"}}' | base64 -d
1
  • 1
    As our argocd-cluster secret has a dot with admin.password inside it's data, this approach using a go-template was the only solution that worked for us. Here's our command: kubectl -n argocd get secret argocd-cluster -o=go-template='{{index .data "admin.password"}}' | base64 -d; echo.
    – jonashackt
    Feb 22, 2022 at 10:19
14

This jsonpath variation works for me on OSX.

kubectl get secrets/db-user-pass -o jsonpath="{.data.username}" | base64 -d

To get secret with dot in the name.

kubectl get secrets/tls -o jsonpath="{.data['tls\.crt']}" | base64 -d
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 22, 2021 at 8:40
9

This is the link you might be looking for.

Kubernetes secrets need the secrets to be given in base64 encoded format, which can be created using base64 binary in case of linux distributions.

Example:

echo "hello" | base64
aGVsbG8K

Kubernetes decodes the base64 encoding when we pass the secret key as environment variable or mounted as volume.

2
  • 2
    Append --decode parameter to decode the secret from base64.
    – Matze
    Jul 5, 2019 at 21:09
  • 3
    echo "aGVsbG8K" | base64 --decode will output hello
    – Malathi
    Jul 5, 2019 at 21:10
9

First, get the secret from the etcd by querying the api server using kubectl.

kubectl get secret db-user-pass -o yaml 

This will give you the base64 encoded secret in yaml format.

Once you have the yaml file decode them using

"base64 --decode"

Final command will look like this: Don't forget the -n flag in echo command

echo -n "jdddjdkkdkdmdl" | base64 --decode

3

For easier decoding you can use a tool like ksd that will do the base64 decoding for you

kubectl get secrets/db-user-pass -o yaml | ksd

or using https://github.com/elsesiy/kubectl-view-secret

kubectl view-secret secrets/db-user-pass
1
  • 1
    I love this plugin, and recommend it.
    – tdensmore
    Oct 8, 2021 at 18:51
2

on ubuntu 18+

kubectl get secrets/db-user-pass --template={{.data.password}} | base64 -d
2
  • Does not work for me. In version 1.18.5 I get "<no value>" when using --template={{.data.password}} as a parameter.
    – Dave
    May 6, 2021 at 20:54
  • I use the same everyday, I have different clusters of version 1,17, 1.18,1.19 and 1.20 and it is still working for me. May 7, 2021 at 21:10
1

Kubernetes 1.11+

kubectl get secrets/db-user-pass --template='{{.data.password | base64decode }}'
1

This one liner is used to get an encoded kubeconfig file from a secret, and generate a file from it to be used dynamically on a ci job for example:

kubectl get secret YOUR_SECRET -o json | grep -oP '(?<=\"YOUR_SECRET_KEY\": \")[^\"]*' | base64 --decode > ./YOUR_KUBECONFIG_FILE_NAME
0

Extending @Břetislav Hájek solution (thank you very much for that). If you need to get it by a label, then you'll need to add an extra range command to iterate over the returned items.

$ LABEL_FILTER="app.kubernetes.io/name=mysql-chart"

$ kubectl get secret  -l "$LABEL_FILTER"  -o go-template='
{{range $i := .items}}{{range $k,$v := $i.data}}{{printf "%s: " $k}}{{if not $v}}{{$v}}{{else}}{{$v | base64decode}}{{end}}{{"\n"}}{{end}}{{end}}'

mysql_password: ...
mysql_root_password: ...
mysql_user: ...
0

With bash. This is running ubuntu 18.04, and Kubernetes 1.18.5

kubectl -n metallb-system get secrets memberlist -o json | grep secretkey | grep -v f:s | awk -F '"' '{print$4}' |base64 --decode; echo
0

Minimal nodejs CLI tool (github)

npm i -g kusd
kubectl get secret your-secret -o yaml | kusd
0

This would help if you have yaml file for k8s secrets. You can use this intellij plugin to decode all base64 encoded values in a yaml file. https://plugins.jetbrains.com/plugin/19099-yaml-base64-decoder

0

Yet another approach. This one involves a kubectl plugin, kubectl-view-secret, the good part is it doesn't print all the secrets only the selected ones, which might be beneficial from time to time.

This is how it works.

Setup

#suppose you have krew installed
❯ kubectl krew install view-secret 

Working Example

❯ kubectl get secrets 
NAME                        TYPE                             DATA   AGE
cert-manager-webhook-ca     Opaque                           3      30d
harbor-global-docker-pull   kubernetes.io/dockerconfigjson   1      30d

❯ kubectl view-secret cert-manager-webhook-ca
Multiple sub keys found. Specify another argument, one of:
-> ca.crt
-> tls.crt
-> tls.key

❯ kubectl view-secret cert-manager-webhook-ca ca.crt
-----BEGIN CERTIFICATE-----
MIIBwj.......

0
$kubectl get secret grafana -n monitoring -o jsonpath='{.data}'

The output is the encoded string

Use the String in the echo command below

$echo "string" | base64 --decode
New contributor
Chandra S is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.