Retrieve secret value from openshift

Viewed 7240

I created a key/value secret in openshift. I want to retrieve the value of that key/value pair.

i tried using

oc describe secret ashish -n my-project

but it gave the value as shown below but i dont the value for my key it just shows 7bytes.

Name:         ashish
Namespace:    my-project
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
ashish:  7 bytes
3 Answers

You can get key and value using "oc get secret/SECRETNAME -o yaml" simply, but you should decode the value by base64. After you retrieve the key using "oc get -o yaml", the value can decode simply as follows.

oc get secret ashish -n my-project \
   -o go-template --template="{{.data.KEY|base64decode}}"
VALUE

For example,

oc get secret ashish -n my-project \
   -o go-template --template="{{.data.ashish|base64decode}}"
...value...

You can use oc get secrets/ashish -o yaml or -o json where you'll see the base64-encoded value. You can then copy the value and decode it with something like echo <ENCODED_VALUE> | base64 -d

You can get the decode secret value, single oc command.

oc get secret ashish -n my-project --template={{.data.ashish}} | base64 -d 

As noticed in other comments, Openshift JSONPath rendering (quite rightly) will not handle @ and . in key names. this is also mentioned in OCP domentation on Using Templates

There are two practical ways around this:

  1. Use jsonpath output:
oc get secret mysecret -n myproject -o jsonpath="{.data['tls\.key']}" | base64 -d
  1. Output the files into your current working directory:
oc extract secrets/mysecret -n myproject
Related