<p>I want to do the opposite of this question:</p>

<p><a href="https://stackoverflow.com/questions/46763148/how-to-create-secrets-using-kubernetes-python-client">How to create secrets using Kubernetes Python client?</a></p>

<p>i.e.:</p>

<p><strong>How do I read an existing secret from a kubernetes cluster via the kubernetes-python API?</strong></p>

<p>The use case is: I want to authenticate to mongodb (running in my cluster) from a jupyter notebook (also running in my cluster) without, for obvious reasons, saving the mongodb auth password inside the jupyter notebook.</p>

<p>Thanks!</p>

jtlz2
<ol>
<li>Install <a href="https://github.com/kubernetes-client/python#kubernetes-python-client" rel="noreferrer">Kubernetes client</a> for python</li>
<li>Now you can pull the secret. For example secret name - <code>mysql-pass</code>, namespace - <code>default</code></li>
</ol>
<pre><code>from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
secret = v1.read_namespaced_secret(&quot;mysql-pass&quot;, &quot;default&quot;)
print(secret)
</code></pre>
<ol start="3">
<li>If you need to extract decoded password from the secret</li>
</ol>
<pre><code>from kubernetes import client, config
import base64
import sys    
config.load_kube_config()
v1 = client.CoreV1Api()
sec = str(v1.read_namespaced_secret(&quot;mysql-pass&quot;, &quot;default&quot;).data)
pas = base64.b64decode(sec.strip().split()[1].translate(None, '}\''))
print(pas)
</code></pre>
<p>Hope this will help.</p>
