|
| 1 | +# Security Guidelines for API Keys |
| 2 | + |
| 3 | +## Overview |
| 4 | +This project requires EC3 and RSMeans API keys to function. It's critical to prevent accidental exposure of these credentials in version control or logs. |
| 5 | + |
| 6 | +## API Key Management |
| 7 | + |
| 8 | +### EC3 API Token |
| 9 | +- **Source**: https://buildingtransparency.org |
| 10 | +- **Handling**: |
| 11 | + - Store in `config.ini` (local development only - file is .gitignored) |
| 12 | + - Use environment variable `EC3_API_TOKEN` for CI/CD and production |
| 13 | + - Never commit real tokens to version control |
| 14 | + |
| 15 | +### RSMeans API Key |
| 16 | +- **Handling**: |
| 17 | + - Pass as measure argument (measure.py) - not stored in files |
| 18 | + - Use environment variable `RSMEANS_API_KEY` for CI/CD |
| 19 | + - Never hardcode in source files |
| 20 | + |
| 21 | +## Setup Instructions |
| 22 | + |
| 23 | +### Local Development |
| 24 | +1. Copy the template: `cp config.ini.template config.ini` |
| 25 | +2. Edit `config.ini` and add your real EC3 API token |
| 26 | +3. Never commit `config.ini` to version control (it's in .gitignore) |
| 27 | + |
| 28 | +### CI/CD and Production |
| 29 | +Use environment variables instead of config files: |
| 30 | + |
| 31 | +```bash |
| 32 | +# Set before running measures |
| 33 | +export EC3_API_TOKEN="your_token_here" |
| 34 | +export RSMEANS_API_KEY="your_key_here" |
| 35 | + |
| 36 | +# Or inline |
| 37 | +EC3_API_TOKEN="token" python apply_measure.py |
| 38 | +``` |
| 39 | + |
| 40 | +### Using with GitHub Actions |
| 41 | +```yaml |
| 42 | +env: |
| 43 | + EC3_API_TOKEN: ${{ secrets.EC3_API_TOKEN }} |
| 44 | + RSMEANS_API_KEY: ${{ secrets.RSMEANS_API_KEY }} |
| 45 | +``` |
| 46 | +
|
| 47 | +## .gitignore Configuration |
| 48 | +The following sensitive files are already excluded: |
| 49 | +- `config.ini` - Local configuration with API tokens |
| 50 | +- `.env` - Environment variable files |
| 51 | +- `.secrets` - Secret files |
| 52 | +- Workflow files with API keys |
| 53 | + |
| 54 | +Verify these entries in `.gitignore`: |
| 55 | +``` |
| 56 | +config.ini |
| 57 | +.env |
| 58 | +.env.* |
| 59 | +.secrets |
| 60 | +lib/measures/workflow_with_reporting.osw |
| 61 | +``` |
| 62 | + |
| 63 | +## Checking for Exposed Credentials |
| 64 | + |
| 65 | +### Before committing: |
| 66 | +```bash |
| 67 | +# Check for any patterns matching API keys |
| 68 | +git diff HEAD -- | grep -i "api_key\|token\|secret" |
| 69 | +``` |
| 70 | + |
| 71 | +### If credentials are accidentally committed: |
| 72 | +1. **Immediately rotate the credentials** - they are now exposed in Git history |
| 73 | +2. Create a new commit that removes the sensitive data (use `git filter-branch` or `BFG Repo Cleaner`) |
| 74 | +3. Force push to remove from history |
| 75 | +4. Notify your team and update all consumers of the exposed credentials |
| 76 | + |
| 77 | +## Code Review Checklist |
| 78 | +- [ ] No hardcoded API keys in `.py`, `.rb`, or `.json` files |
| 79 | +- [ ] No sensitive data in default values of measure arguments |
| 80 | +- [ ] Config files with templates included but not actual tokens |
| 81 | +- [ ] Environment variable fallbacks implemented where applicable |
| 82 | +- [ ] .gitignore properly covers all config and secrets files |
| 83 | + |
| 84 | +## Environment Variable Fallback Support |
| 85 | + |
| 86 | +The following components support environment variables: |
| 87 | + |
| 88 | +- **apply_measure.py**: Reads `EC3_API_TOKEN` if `config.ini` is not available |
| 89 | +- **measure.py**: Accepts API token as measure argument (passed at runtime) |
| 90 | +- **Resources**: EC3_lookup.py and call_rsmeans_api.py accept tokens as function parameters |
| 91 | + |
| 92 | +This allows flexibility: |
| 93 | +1. Local dev: Use `config.ini` |
| 94 | +2. CI/CD: Use environment variables |
| 95 | +3. Docker/containers: Use mounted secrets or env vars |
| 96 | + |
| 97 | +## References |
| 98 | +- [OWASP: Secrets Management](https://owasp.org/www-community/Secrets_Management) |
| 99 | +- [GitHub: Removing sensitive data from history](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository) |
| 100 | +- [EC3 BuildingTransparency API](https://buildingtransparency.org) |
0 commit comments