Retrieve secret value from openshift
Asked Answered
F

3

7

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
Fadil answered 12/4, 2021 at 10:46 Comment(0)
C
9

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...
Chaudoin answered 12/4, 2021 at 12:9 Comment(0)
S
4

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
Seeley answered 20/6, 2022 at 9:18 Comment(0)
I
2

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 
Iraq answered 13/4, 2021 at 7:40 Comment(1)
This does not work if your key name contains a . character. Which is indeed likely for Secrets mapped as a volume. (the key becomes a file name)Abecedary

© 2022 - 2024 — McMap. All rights reserved.