<p>I've a nodejs application which needs to read secret in RT</p>
<p>This is the secret</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: secert1
  namespace: trail
type: Opaque
data:
  TOKEN1: cmVhbGx5X3NlY3JldF92YWx1ZTE=
</code></pre>
<p>I have used a volume to mount the secret as I've many fields that I need to read and I don't want to use the var option.</p>
<p>I've added volume to the deployment as following:</p>
<pre><code>          volumeMounts:
            - name: secret-volume
              mountPath: /etc/secret-volume
      volumes:
        - name: secret-volume
          secret:
            secretName: secert1
</code></pre>
<p>My question is how should I access the secret from the nodejs app?</p>
<p>I've tried with the following and didnt get any data, any idea?</p>
<pre><code>const fs = require('fs');
fs.readFile('/etc/secret-volume', function read(err, data) {
    if (err) {
        throw err;
    }
    const content = data;


});


</code></pre>

<p>Each <code>data</code> item in a secret will become a file in the <code>mountPath</code> of the secret based volume.</p>
<p>To read lots of tokens you can trawl the directory with <a href="https://nodejs.org/api/fs.html#fs_fspromises_readdir_path_options" rel="nofollow noreferrer"><code>readdir</code></a> and <a href="https://nodejs.org/api/fs.html#fs_fspromises_readfile_path_options" rel="nofollow noreferrer"><code>readFile</code></a></p>
<pre><code>const fsp = require('fs').promises
const path = require('path')

async function readTokens(token_path) {
  const tokens = {}
  const entries = await fsp.readdir(token_path, { withFileTypes: true })
  for (const entry of entries) {
    if (!entry.isFile()) continue
    const buf = await fsp.readFile(path.join(token_path, entry.name), 'utf8')
    tokens[file] = buf.toString()
  }
  return tokens
}

readTokens('/etc/secret-volume').then(console.log).catch(console.err)
</code></pre>
