Environment Variables & Secrets — DevOps

Keep three kinds of environment variables apart: config (not secret), secrets (grant access: names with SECRET, PASSWORD, TOKEN, KEY, CREDENTIAL) and public…

You've just realised the notes-api repo (where its code lives on GitHub) was public for a whole day, with the .env file holding JWT_SECRET committed (saved in the git history) inside it. JWT_SECRET is the key the server uses to sign login tokens. First reflex: delete the file from git, push, done. Sadly, it isn't that simple.

A secret is a value that grants access: a database password, JWT_SECRET, an API key. On a server, managing secrets is like keeping a spare house key safe, and the secret is the key. You don't tape it to the front door (git), and you don't print it in a brochure that many people get (a frontend bundle or a Docker image, both covered below). You keep it in a locked box (a 600 file). If that key ever went missing, you change the lock (rotation: making a new secret) instead of only looking for the key. How to detect a leak and handle it as an incident is in the Environment Variables & Secrets lesson of the Security track. This is the operations side, on web-1.

Config, secret and public

Every value that differs between environments (the places the app runs, such as your laptop or web-1) is read from an environment variable, but each kind is handled differently:

Variable Kind Why
PORT, NODE_ENV, LOG_LEVEL config differs per server, but not secret
DATABASE_URL, JWT_SECRET secret grants access; DATABASE_URL holds a password
VITE_API_URL (the notes-web frontend) public the VITE_ prefix sends its value to the browser

On the server: one 600 file, read by systemd

All of notes-api's secrets live in one file, 600 and owned by root (the File System & Permissions lesson), read by systemd through EnvironmentFile= (the Systemd & Service Management lesson):

deploy@web-1:~$ ls -l /etc/notes-api.env
-rw------- 1 root root 152 Oct  8 17:20 /etc/notes-api.env
deploy@web-1:~$ sudo cat /etc/notes-api.env
DATABASE_URL=postgres://notes:83hQLRdx8M1klbAQvNcZPwBn@localhost:5432/notes
JWT_SECRET=0b08ce8c4914826707aa575513330eaf0e3783035d7eab9656197a23af0f6382

Not in git, not in the image, not in the logs

Secrets in CI/CD

The pipeline (the automatic path from a push to a deploy) that deploys notes-api needs secrets too, for example an SSH key to log in to web-1. In GitHub Actions (GitHub's automation feature), secrets are stored encrypted in the repo settings and read with ${{ secrets.DEPLOY_SSH_KEY }}. For a cloud provider such as AWS, OIDC is better still:

permissions:
  id-token: write
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: aws-actions/configure-aws-credentials@v6
        with:
          role-to-assume: arn:aws:iam::123456789012:role/gha-deploy
          aws-region: ap-southeast-1

With OIDC, GitHub proves the identity of the workflow (an automated list of steps) to AWS. Then AWS gives temporary credentials (access keys) that expire on their own after a short time. No permanent AWS_ACCESS_KEY_ID is stored, so there is nothing to leak. GitHub Actions is covered in full in the CI/CD lessons later.

Rotation: change the lock

git rm --cached .env only changes the next commit. The old commits on GitHub still hold JWT_SECRET, and more importantly, the server still uses it. So anyone who copied JWT_SECRET can still make a forged admin token, and the server accepts it. The only thing that stops it is rotation: change the secret, so the leaked value becomes useless.

deploy@web-1:~$ openssl rand -hex 32
4681783844dbb75403ab029609e5318d0bbcd6ed92fd6aa73605922c6414b322
deploy@web-1:~$ sudoedit /etc/notes-api.env
deploy@web-1:~$ sudo systemctl restart notes-api

Then from outside, try two tokens: a forged one, and Budi's old one (a real token made before the rotation). The first request tries to delete note 1 and prints only its status code. The second asks for the list of notes with Budi's token:

$ curl -s -o /dev/null -w "%{http_code}\n" -X DELETE https://notes.example.com/notes/1 -H "Authorization: Bearer $FORGED_TOKEN"
401
$ curl -s https://notes.example.com/notes -H "Authorization: Bearer $BUDI_TOKEN"
{"error":"Invalid token"}

Public, secret or config?

In the exercise, classifyEnvVar(name, value) returns { name, category, shouldGitignore }. Check in this order:

  1. A name starting with PUBLIC_, NEXT_PUBLIC_ or VITE_ (startsWith) is "public".
  2. Otherwise, a name containing SECRET, PASSWORD, TOKEN, KEY or CREDENTIAL (includes) is "secret".
  3. Anything else is "config".

shouldGitignore is true only for "secret".

⚠️ Mistakes that leave a secret wide open

TL;DR: Keep three kinds of environment variables apart: config (not secret), secrets (grant access: names with SECRET, PASSWORD, TOKEN, KEY, CREDENTIAL) and public values (prefixed VITE_, NEXT_PUBLIC_, PUBLIC_, they always reach the browser). On the server web-1, every secret lives in /etc/notes-api.env (600, owned by root), read by systemd through EnvironmentFile=. Secrets are never in a unit file's Environment=, in git (.env in .gitignore, .env.example committed), in a Docker image or in the logs. In CI (the server that runs the deploy automatically), use the repo's secrets (stored encrypted in the repo settings), and OIDC for the cloud so there is no permanent credential (access key). A leaked secret is handled by rotation (making a new secret): openssl rand -hex 32 for a new secret, sudoedit to write it into the file, systemctl restart, then everyone logs in again.

⚡ Config, secret or public

Write:

PORT, NODE_ENV, LOG_LEVEL      config
DATABASE_URL, JWT_SECRET       secret
VITE_API_URL                   public
Guess first: a variable prefixed VITE_ is sent to the browser. Is VITE_STRIPE_SECRET_KEY secret or public?

Public. The VITE_ prefix writes its value into the JavaScript bundle the browser downloads, whatever its name. Secrets belong on the backend (the code on the server) only.

Meaning: Environment variables (settings the app receives when it starts) come in three kinds. A variable prefixed VITE_, NEXT_PUBLIC_ or PUBLIC_ is always public. Of the rest, a name containing SECRET, PASSWORD, TOKEN, KEY or CREDENTIAL is a secret (a value that grants access), anything else is config (not secret).

⚡ .env stays out of git

Write:

git rm --cached .env

Meaning: .env (the file holding secrets) goes into .gitignore, the list of files that are not committed. What gets committed is .env.example, with the variable names only. .gitignore does not apply to a file that is already tracked (followed by git), so take it out of git first with git rm --cached.

⚡ On the server: one 600 file

Write:

deploy@web-1:~$ ls -l /etc/notes-api.env
-rw------- 1 root root 152 Oct  8 17:20 /etc/notes-api.env

Meaning: Every secret lives in one 600 file owned by root, so only root may read it. systemd (the program that starts the app) reads it through the EnvironmentFile= line of the unit file (the app's settings file for systemd). Secrets are never in the unit file's Environment= line (every user can read it), in a Docker image (a package holding the app) or in the logs.

⚡ Leaked? Change the lock

Write:

deploy@web-1:~$ openssl rand -hex 32
deploy@web-1:~$ sudoedit /etc/notes-api.env
deploy@web-1:~$ sudo systemctl restart notes-api
Guess first: .env, holding JWT_SECRET (the key for signing login tokens), was deleted from git and pushed. Is it safe now?

Not yet. The old commits on GitHub still hold JWT_SECRET, and the server still uses it. So anyone who copied it can make a forged admin token, and the server still accepts it.

Meaning: Only rotation, changing the secret, stops forged tokens: make a new secret (openssl rand -hex 32), write it into the file (sudoedit opens a root-owned file), then restart. The side effect: every old token is refused and everyone logs in again.

What you will learn