🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Web & Software Development › Deployment: Servers, CI/CD, and Cloud Publishing › Lesson 6
TEC-WEB46-07 · Lesson 6 · 12 clock hours · theory

Environment Variables and Secrets Management

Objective. Students explain the principle of keeping secrets out of source code and configure application environment variables using a .env file and a process-level method suitable for production.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

Why Secrets Must Stay Out of Source Code

Every application eventually needs sensitive values: database passwords, API keys, authentication tokens, and encryption secrets. The most dangerous place to store these values is directly in your source code. When code is committed to a Git repository, every secret embedded in it is recorded permanently in the project history. Even if you delete the file in a later commit, the value remains visible in earlier commits and can be retrieved by anyone with access to the repository.

Real-world data breaches have resulted from developers accidentally pushing credentials to public repositories on platforms such as GitHub. Automated scanning bots continuously search public repositories for patterns that look like API keys and passwords. Within minutes of an accidental push, those credentials can be harvested and misused. This is not a theoretical risk — it is a routine occurrence that costs organizations significant time, money, and reputation.

The core principle is simple: source code is for logic, not for secrets. Secrets belong in the environment, not in the codebase.

What Is an Environment Variable?

An environment variable is a named value stored in the operating system's process environment rather than in a file the application reads directly. Every process running on a system inherits a set of environment variables from its parent. Applications can read these values at runtime without the values ever being written into the source files.

On Linux (relevant to CompTIA Linux+), you set an environment variable in a shell session with:

```bash

export DATABASE_URL="postgres://user:password@host/dbname"

```

You read it in the same session with:

```bash

echo $DATABASE_URL

```

In Python, the `os` module provides access to these values:

```python

import os

db_url = os.environ.get("DATABASE_URL")

```

Because environment variables exist only in the running process, they are never written into your `.py` files and never committed to Git.

The .env File for Development

Typing `export` commands every time you open a terminal is impractical during development. A `.env` file (short for "environment") solves this by storing key-value pairs in a plain text file that lives on your local machine only. The format is straightforward:

```

DATABASE_URL=postgres://user:password@host/dbname

SECRET_KEY=a-long-random-string

DEBUG=True

```

The critical rule: the .env file must never be committed to version control. You enforce this by adding `.env` to your project's `.gitignore` file. Once listed there, Git will not track it, and it will not appear in `git status` output or in any commit.

The `python-dotenv` package reads a `.env` file and loads the values into the process environment automatically when your application starts. Install it with:

```bash

pip install python-dotenv

```

In a Flask application, you load the variables near the top of your entry point:

```python

from dotenv import load_dotenv

import os

load_dotenv() # reads .env and populates os.environ

app = Flask(__name__)

app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY")

```

This pattern keeps all sensitive values off the file system that Git watches.

Production: Process-Level Environment Variables

A `.env` file is a development convenience. In production, you should not copy a `.env` file to your server. Instead, set environment variables directly in the server's process environment or through your cloud platform's configuration interface.

Cloud platforms such as Heroku, Render, Railway, and AWS Elastic Beanstalk all provide a dashboard or command-line interface where you enter environment variables that are injected into your application's process at startup. Your code reads them the same way — through `os.environ.get()` — so no code changes are needed between development and production.

On a Linux server managed directly, you can set variables in the service unit file (systemd) or in a restricted configuration file that is not part of the repository. Only system administrators with appropriate access should be able to read these values.

Secrets Manager Services

For enterprise and production environments, dedicated secrets manager services provide an extra layer of control. Examples include AWS Secrets Manager, HashiCorp Vault, and Azure Key Vault. These services store secrets encrypted, audit every access, support automatic rotation of credentials, and allow fine-grained permissions. Applications retrieve secrets by calling an API rather than reading a file. This approach is beyond what a `.env` file offers and is the standard for professional production deployments.

Remediating an Accidental Leak

If a secret is accidentally committed to Git history, removing it from the latest file is not enough — the history must be rewritten. Remediation steps include:

1. Immediately revoke and rotate the exposed credential at the source (the database, the API provider, etc.). Assume it is already compromised.

2. Use tools such as `git filter-repo` or BFG Repo-Cleaner to remove the secret from history.

3. Force-push the cleaned history to the remote repository.

4. Notify any collaborators who have clones of the repository so they re-clone from the cleaned version.

Step one — rotating the credential — is the most important. Cleaning the history prevents future exposure but does not undo past exposure.

Safety Reminder

In web and software development, "safety" includes data security. Mishandling credentials can expose users' personal data, violate privacy regulations, and result in legal liability for your employer. Treat secrets with the same discipline that tradespeople apply to lockout/tagout procedures: a missed step causes harm that is difficult or impossible to reverse.

Key terms8

Environment variableA named value stored in the operating system process environment and accessible to running applications at runtime without being written into source code files.
.env fileA plain text file containing key-value pairs used to define environment variables locally during development; must be excluded from version control via .gitignore.
.gitignoreA Git configuration file that lists file names and patterns Git should not track, preventing sensitive or irrelevant files from being committed to a repository.
python-dotenvA Python package that reads a .env file and loads its key-value pairs into the process environment so they are accessible through os.environ.
Secrets managerA dedicated service (such as AWS Secrets Manager or HashiCorp Vault) that stores, encrypts, audits access to, and can automatically rotate application credentials.
Credential rotationThe practice of replacing an exposed or periodically expiring secret (such as a password or API key) with a new one to limit the window of potential misuse.
Git historyThe complete record of all commits in a repository; a secret committed at any point remains visible in earlier commits even after the file is later deleted or changed.
Process environmentThe collection of environment variables available to a running process, inherited from its parent shell or set by the operating system or platform at startup.
In class: Review a sample repository where a database password was accidentally committed to Git history; use git log and git show to locate the leak and discuss remediation steps as a class. · Create a .env file for a sample Python Flask application, add it to .gitignore, and load variables in the app using python-dotenv; demonstrate that git status no longer tracks the file. · Research and present a two-minute verbal summary of one additional secrets management approach (e.g., server-side environment variables set in the cloud console, or a secrets manager service) and record findings in student notes.
Materials: Sample Git repository with intentional credential leak, Python Flask sample app, python-dotenv package, .gitignore reference, student laptops, student notebooks
Check: Students demonstrate that their Flask app runs using .env variables and that .env does not appear in git status; instructor confirms .gitignore entry exists. Written summary graded pass/fail.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

Security fundamentals / CompTIA ITF+
1. A developer removes a database password from a Python file and pushes the change. Why is the password still a security risk?
Version control / CompTIA ITF+
2. Which file must be updated so that a .env file is never tracked by Git?
Python programming / PCEP Python
3. In a Python application, which standard library module provides access to environment variables set in the process environment?
Deployment and cloud / CompTIA ITF+
4. A Flask application uses python-dotenv in development. Which approach is most appropriate for supplying the same secrets in a cloud production environment?
Security fundamentals / CompTIA ITF+
5. What is the first and most critical step when a developer discovers that an API key was accidentally committed to a public GitHub repository?
Version control / CompTIA ITF+
6. A team wants to view the contents of a specific old Git commit to check whether it contains a leaked password. Which two Git commands would they use together?
Linux shell / CompTIA Linux+
7. On a Linux system, which shell command makes a variable called APP_SECRET available to child processes in the current session?
Security and cloud services / CompTIA ITF+
8. An organization requires automatic credential rotation, encrypted storage, and an audit log of every application that accessed a database password. Which solution best meets these requirements?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.