How to manage.env files in private repositories

·

Is committing config-only .env files (no secrets) to a private repo really a security sin?

When you operate multiple repositories, one of the most tedious tasks is manually copying the same configuration files. Every time you rebuild a server or add a team member, you paste values into place and lose track of which repository has the latest version. A private repository can provide change history and pull request reviews, so doing environment variable separation correctly can make configuration management easier.

Judge.env values individually instead of banning every.env commit

The original project is divided into a Docker repository, a backend repository, and an indexer repository, with each repository containing its own.env file. These files include secrets such as database passwords, API keys, and tokens, as well as non-secret values such as private IP addresses for fixed servers and backend tuning parameters that change with VPS resources and workload.

Keeping passwords out of Git and keeping non-secret configuration files out of private repositories are two different decisions. Secrets should be injected from outside the repository, while only non-secret configuration should be managed with Git. A.env file is a format for delivering configuration, not a security boundary. The risk depends on the values it contains and who can access them, not on its filename.

A private repository is not a fully isolated space. Broad repository permissions or weak account management can expose internal addresses and the operating structure. Private IP addresses are not credentials themselves, but they can reveal internal topology, so teams and individuals whose access may be exposed to the public must be identified first. Tuning parameters should also be classified after confirming that combining them with another authentication method does not expand access.

Separate values into three categories

Secrets include values that can lead to account takeover or data access if exposed, such as database passwords, API keys, and tokens. Do not store them in Git. Inject them as environment variables or files during deployment. Sensitive configuration includes values such as private IP addresses that reveal internal structure but do not grant access on their own, so limit access to authorized members. General configuration includes tuning parameters that are close to public information and would cause limited harm if exposed, so these can use Git history and pull request reviews.

For each value, record answers to three questions: who can read it, what harm would exposure cause, and which runtime component reads it. If any answer is unclear, do not classify the value as general configuration until the owning team and access scope have been agreed upon. Addresses and account information that become more sensitive when combined with secret values should receive the same treatment as secrets.

Split configuration across repositories into shared values and differences

When shared configuration is copied into each repository, a change may update only some files and leave values inconsistent. Collect shared configuration in a separate configuration repository or deployment artifact, and keep a.env.example file in each repository that lists only the required keys. Separate environment-specific differences with override files.

For example, a shared repository can version general configuration and environment-specific overrides. The Docker repository can own the compose files needed for deployment, the backend repository can own the application configuration template, and the indexer repository can own the collector configuration template. Actual secret values should not be placed in the shared repository either, and should instead be injected in each deployment environment.

git grep -nE '(PASSWORD|SECRET|TOKEN|API_KEY)' -- ':!*.example'
git ls-files --.env
git log --all --oneline --.env

These commands provide a basic workflow for checking for secret keys that may remain in a repository, whether.env files are tracked, and what changes were made in the past. Finding a value does not mean it has been removed automatically, so review both the current files and the historical record. Git retains committed content in its history, so a commit that deletes a secret does not erase the exposure history. If exposure is suspected, rotate the affected credentials and review access logs before proceeding with the history cleanup process.

End secret injection at the deployment boundary

Environment variable separation should do more than create a separate secret store. It should minimize access and deliver values only to the services that need them during deployment. Even if encrypted values are stored in Git, the decryption key must remain outside the repository. This approach also loses its protection if key management fails, so record the key storage location, authorized readers, rotation schedule, and responsible team in the operating procedure.

Moving every value into a secret injection system, on the other hand, complicates deployment pipelines and access management whenever the configuration changes. First create a classification table, review general configuration through pull requests, and inject only secrets through a separate path. This reduces the maintenance burden. The original configuration can be reviewed in the discussion about committing configuration-only.env files.

In practice, start by dividing the current.env values into secrets, sensitive configuration, and general configuration, and record the potential harm of exposure and the component that references each value. Then use git grep and Git history to check whether secrets have been exposed, and organize the configuration structure with.env.example files and environment-specific overrides. Limit private repository permissions to the members who need access and include the change procedure in pull request reviews. If secrets are found in the history, rotate the credentials and review access logs before deleting the values.

Leave a Reply

Your email address will not be published. Required fields are marked *