Secrets and access
Absolute rule: no password, no key, no token is written in the repository, nor in its history.
Where the secrets are
| Secret | Stored in | Used by | Ends up in |
|---|---|---|---|
POSTGRES_PASSWORD | GitLab CI/CD variable | deploy-dev, deploy-prod | db (POSTGRES_PASSWORD) and api (DB_PASSWORD) containers |
DJANGO_SECRET_KEY | GitLab CI/CD variable | deploy-dev, deploy-prod | api container (SECRET_KEY) |
CI_REGISTRY_PASSWORD | provided by GitLab, temporary | *:build, deploy-prod | docker login |
gitlab_runner_token | Ansible Vault | gitlab-runner role | the dev server's /etc/gitlab-runner/config.toml |
deploy_runner_tokens | Ansible Vault | gitlab-runner role | each server's config.toml |
| Vault password | person to person, outside Git | ansible-playbook --ask-vault-pass | — |
| Administrators' SSH keys | their machines | Ansible, manual connections | the servers' ~/.ssh/authorized_keys |
| Datadog API key | GitLab's Datadog integration, encrypted | GitLab.com | sending pipelines to Datadog |
| Mirror SSH key | generated and kept by GitLab; its public part is on Elias's GitHub account | GitLab.com | push to the school's GitHub repository |
How a secret reaches a container
- GitLab injects
POSTGRES_PASSWORDandDJANGO_SECRET_KEYas the job's environment variables. - The Compose file references them:
SECRET_KEY: ${DJANGO_SECRET_KEY}. docker composeordocker stack deployreplaces${…}with the value at deployment time.
The value is never written in a file of the repository.
GitLab variable settings
In Settings → CI/CD → Variables, each secret must be:
| Option | Why |
|---|---|
| Masked | the value is replaced by [MASKED] if it shows up in a log |
| Protected | the variable is only given to pipelines of protected branches (develop, main). A feature/* branch cannot read it, even with an echo |
Never print a secret in a job
No echo $DJANGO_SECRET_KEY, no env or printenv in a script. Masking does not protect against everything (an encoded or split value…).
Other security measures
| Measure | Effect |
|---|---|
| UFW firewall: 22, 80, 443 only | PostgreSQL, Docker and the applications are not reachable from the Internet |
internal network | the database is only reachable by the API |
| HTTPS everywhere, port 80 redirected | exchanges are encrypted |
| Non-root prod containers | a flaw in the application does not give root inside the container |
DEBUG=False and mandatory SECRET_KEY in prod | no detailed error page, no default key |
| Local deploy runners | no SSH deploy key to store in GitLab |
no_log: true on runner registration | tokens do not appear in Ansible's output |
Root .gitignore | excludes .env, *.pem, *.key, .vault_pass* |
If a secret leaked
- Change it right away: new value in GitLab or in the Vault. Deleting the commit is not enough: the history, clones and forks already have it.
- Redeploy, so the containers pick up the new value.
- For a runner token: revoke the runner in GitLab, create a new one, put the token in the Vault, re-run the
gitlab-runnerrole.
Changing POSTGRES_PASSWORD does not change the database password
The PostgreSQL image only uses this variable when the volume is created. You must also change the password inside PostgreSQL (ALTER USER trinity PASSWORD '…'), then redeploy.
Known limits (no automatic secret scanning, API without authentication) are in Limits and improvements.