<p>The issue was with the command used to encode the secret - <code>echo &quot;pasword&quot; | base64</code>. The echo adds a newline character at the end of the string. Using <code>echo -n &quot;password&quot; | base64</code> fixed the secret.
Closing the issue.</p>

CC BY-SA 4.0
2023-01-22T04:33:24.813Z
2023-01-22T04:33:24.813Z
Harshit Nagar
<p>I have a secret being used as env var in another env var as follows:</p>
<pre><code>- name: &quot;PWD&quot;
  valueFrom:
    secretKeyRef:
      name: &quot;credentials&quot;
      key: &quot;password&quot;
- name: HOST
  value: &quot;xyz.mongodb.net&quot;
- name: MONGODB_URI 
  value: &quot;mongodb+srv://user:$(PWD)@$(HOST)/db_name?&quot;
</code></pre>
<p>When i exec into the container and run <code>env</code> command to see the values of env i see -</p>
<pre><code>mongodb+srv://user:password123  
@xyz.mongodb.net/db_name?
</code></pre>
<p>The container logs show error as authentication failure.
Is this something that is expected to work in kubernetes ? There <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-interdependent-environment-variables/" rel="nofollow noreferrer">docs</a> talk about dependent env vars but do not give example using secrets. Did not find clear explanation on this after extensive search. Only found <a href="https://joeblogs.technology/2020/12/concatenating-kubernetes-secrets-for-environment-variables/" rel="nofollow noreferrer">this</a> one article doing something similar.</p>
<p>Some points to note -</p>
<ul>
<li>The secret is a sealed secret.</li>
<li>This is the final manifest's contents, but all this is templated using helm.</li>
<li>The value is being used inside a spring boot application</li>
</ul>
<p>Is the new line after <code>123</code> expected ?
If this evaluation of env from a secret in another env is possible then what am I doing wrong here ?</p>

<kubernetes><kubernetes-helm><environment-variables>
Using kubernetes secret env var inside another env var
Harshit Nagar
# Using kubernetes secret env var inside another env var

(Asked by: username_0 on 2023-01-19)

I have a secret being used as env var in another env var as follows:
- name: "PWD"
  valueFrom:
    secretKeyRef:
      name: "credentials"
      key: "password"
- name: HOST
  value: "xyz.mongodb.net"
- name: MONGODB_URI 
  value: "mongodb+srv://user:$(PWD)@$(HOST)/db_name?"

When i exec into the container and run env command to see the values of env i see -
mongodb+srv://user:password123  
@xyz.mongodb.net/db_name?

The container logs show error as authentication failure.
Is this something that is expected to work in kubernetes ? There docs talk about dependent env vars but do not give example using secrets. Did not find clear explanation on this after extensive search. Only found this one article doing something similar.
Some points to note -

The secret is a sealed secret.
This is the final manifest's contents, but all this is templated using helm.
The value is being used inside a spring boot application

Is the new line after 123 expected ?
If this evaluation of env from a secret in another env is possible then what am I doing wrong here ?

**Tags:** kubernetes, kubernetes-helm, environment-variables


---

## 1 answers

### Answer by username_0 on 2023-01-22. Score: 0 | Accepted answer

The issue was with the command used to encode the secret - `echo "pasword" | base64`. The echo adds a newline character at the end of the string. Using `echo -n "password" | base64` fixed the secret.
Closing the issue.


---

