|
| 1 | +# π‘οΈ API Security Guide |
| 2 | + |
| 3 | +## π¨ CRITICAL: API Key Security Best Practices |
| 4 | + |
| 5 | +This document outlines essential security practices for managing API keys in the Gemini Flow project. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## π API Key Management |
| 10 | + |
| 11 | +### 1. Never Hardcode API Keys |
| 12 | + |
| 13 | +**β WRONG:** |
| 14 | +```javascript |
| 15 | +const apiKey = "AIzaSyC8UgQSomeRealAPIKey123"; // NEVER DO THIS! |
| 16 | +const genAI = new GoogleGenerativeAI(apiKey); |
| 17 | +``` |
| 18 | + |
| 19 | +**β
CORRECT:** |
| 20 | +```javascript |
| 21 | +const apiKey = process.env.GEMINI_API_KEY; // Use environment variables |
| 22 | +const genAI = new GoogleGenerativeAI(apiKey); |
| 23 | +``` |
| 24 | + |
| 25 | +### 2. Use Environment Variables |
| 26 | + |
| 27 | +Create a `.env` file (never commit this): |
| 28 | +```bash |
| 29 | +GEMINI_API_KEY=your_actual_api_key_here |
| 30 | +``` |
| 31 | + |
| 32 | +Load it in your application: |
| 33 | +```javascript |
| 34 | +require('dotenv').config(); |
| 35 | +const apiKey = process.env.GEMINI_API_KEY; |
| 36 | +``` |
| 37 | + |
| 38 | +### 3. Gitignore Protection |
| 39 | + |
| 40 | +Ensure `.env` is in your `.gitignore`: |
| 41 | +```gitignore |
| 42 | +# Environment variables |
| 43 | +.env |
| 44 | +.env.local |
| 45 | +.env.production |
| 46 | +.env.development |
| 47 | +
|
| 48 | +# API keys |
| 49 | +*.key |
| 50 | +*.pem |
| 51 | +config/secrets.json |
| 52 | +``` |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## π Google Cloud Security Settings |
| 57 | + |
| 58 | +### API Key Restrictions |
| 59 | + |
| 60 | +1. **Go to Google Cloud Console** |
| 61 | + - Navigate to "APIs & Services" > "Credentials" |
| 62 | + - Click on your API key |
| 63 | + |
| 64 | +2. **Set Application Restrictions** |
| 65 | + - **HTTP referrers**: For web applications |
| 66 | + - **IP addresses**: For server applications |
| 67 | + - **Android apps**: For mobile applications |
| 68 | + - **iOS apps**: For iOS applications |
| 69 | + |
| 70 | +3. **Set API Restrictions** |
| 71 | + - Restrict to "Generative Language API" only |
| 72 | + - Don't allow access to other Google services |
| 73 | + |
| 74 | +### Example Restrictions: |
| 75 | +``` |
| 76 | +Application restrictions: |
| 77 | +βββ HTTP referrers: https://yourdomain.com/* |
| 78 | +βββ IP addresses: 203.0.113.0/24 |
| 79 | +βββ None (for development only) |
| 80 | +
|
| 81 | +API restrictions: |
| 82 | +βββ Generative Language API only |
| 83 | +``` |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## π¨ Common Security Mistakes |
| 88 | + |
| 89 | +### β What NOT to Do: |
| 90 | + |
| 91 | +1. **Committing keys to version control** |
| 92 | + ```bash |
| 93 | + git add .env # NEVER! |
| 94 | + git commit -m "Added API keys" # DISASTER! |
| 95 | + ``` |
| 96 | + |
| 97 | +2. **Sharing keys in plain text** |
| 98 | + - Slack messages |
| 99 | + - Email |
| 100 | + - Discord/Teams chat |
| 101 | + - Stack Overflow posts |
| 102 | + |
| 103 | +3. **Using production keys for development** |
| 104 | + ```javascript |
| 105 | + const apiKey = "prod_key_12345"; // Use separate dev keys! |
| 106 | + ``` |
| 107 | + |
| 108 | +4. **Logging API keys** |
| 109 | + ```javascript |
| 110 | + console.log(`API Key: ${apiKey}`); // Keys will appear in logs! |
| 111 | + ``` |
| 112 | + |
| 113 | +5. **Client-side exposure** |
| 114 | + ```html |
| 115 | + <script> |
| 116 | + const apiKey = "AIzaSyC..."; // Visible to everyone! |
| 117 | + </script> |
| 118 | + ``` |
| 119 | + |
| 120 | +### β
What TO Do: |
| 121 | + |
| 122 | +1. **Use separate keys for each environment** |
| 123 | + ```bash |
| 124 | + # Development |
| 125 | + GEMINI_API_KEY=dev_key_here |
| 126 | + |
| 127 | + # Production |
| 128 | + GEMINI_API_KEY=prod_key_here |
| 129 | + ``` |
| 130 | + |
| 131 | +2. **Implement key rotation** |
| 132 | + ```javascript |
| 133 | + // Rotate keys every 90 days |
| 134 | + const keyRotationDate = new Date('2024-04-01'); |
| 135 | + if (Date.now() > keyRotationDate) { |
| 136 | + console.warn('API key rotation needed!'); |
| 137 | + } |
| 138 | + ``` |
| 139 | + |
| 140 | +3. **Monitor usage** |
| 141 | + ```javascript |
| 142 | + // Track API calls |
| 143 | + let apiCallCount = 0; |
| 144 | + const MAX_CALLS_PER_HOUR = 1000; |
| 145 | + |
| 146 | + function makeAPICall() { |
| 147 | + if (apiCallCount >= MAX_CALLS_PER_HOUR) { |
| 148 | + throw new Error('Rate limit exceeded'); |
| 149 | + } |
| 150 | + apiCallCount++; |
| 151 | + // ... make API call |
| 152 | + } |
| 153 | + ``` |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +## π Security Auditing |
| 158 | + |
| 159 | +### Regular Security Checks |
| 160 | + |
| 161 | +1. **Scan for exposed keys** |
| 162 | + ```bash |
| 163 | + # Use tools like gitleaks |
| 164 | + gitleaks detect --source . --verbose |
| 165 | + |
| 166 | + # Or truffleHog |
| 167 | + truffleHog git file://. --json |
| 168 | + ``` |
| 169 | + |
| 170 | +2. **Check git history** |
| 171 | + ```bash |
| 172 | + # Search for potential keys in git history |
| 173 | + git log -p | grep -i "api.*key\|secret\|token" |
| 174 | + ``` |
| 175 | + |
| 176 | +3. **Review access logs** |
| 177 | + - Monitor Google Cloud Console logs |
| 178 | + - Check for unusual API usage patterns |
| 179 | + - Set up alerts for unexpected activity |
| 180 | + |
| 181 | +### Automated Security Tools |
| 182 | + |
| 183 | +Add these to your CI/CD pipeline: |
| 184 | + |
| 185 | +```yaml |
| 186 | +# .github/workflows/security.yml |
| 187 | +name: Security Scan |
| 188 | +on: [push, pull_request] |
| 189 | + |
| 190 | +jobs: |
| 191 | + security: |
| 192 | + runs-on: ubuntu-latest |
| 193 | + steps: |
| 194 | + - uses: actions/checkout@v3 |
| 195 | + with: |
| 196 | + fetch-depth: 0 |
| 197 | + |
| 198 | + - name: Run gitleaks |
| 199 | + uses: gitleaks/gitleaks-action@v2 |
| 200 | + env: |
| 201 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 202 | +``` |
| 203 | +
|
| 204 | +--- |
| 205 | +
|
| 206 | +## π Incident Response |
| 207 | +
|
| 208 | +### If Your API Key is Compromised: |
| 209 | +
|
| 210 | +1. **Immediate Actions:** |
| 211 | + ```bash |
| 212 | + # 1. Revoke the compromised key immediately |
| 213 | + # Go to Google Cloud Console > Credentials > Delete key |
| 214 | + |
| 215 | + # 2. Generate a new key |
| 216 | + # Create new API key with restrictions |
| 217 | + |
| 218 | + # 3. Update environment variables |
| 219 | + export GEMINI_API_KEY="new_secure_key_here" |
| 220 | + ``` |
| 221 | + |
| 222 | +2. **Investigate:** |
| 223 | + - Check API usage logs for unauthorized activity |
| 224 | + - Review recent commits for exposed keys |
| 225 | + - Audit team access to repositories |
| 226 | + |
| 227 | +3. **Prevent Future Incidents:** |
| 228 | + - Add pre-commit hooks to scan for secrets |
| 229 | + - Implement automated key rotation |
| 230 | + - Train team on security best practices |
| 231 | + |
| 232 | +### Pre-commit Hook Example: |
| 233 | +```bash |
| 234 | +#!/bin/sh |
| 235 | +# .git/hooks/pre-commit |
| 236 | + |
| 237 | +# Check for potential API keys |
| 238 | +if grep -r "AIza" . --exclude-dir=.git; then |
| 239 | + echo "β Potential API key found! Commit blocked." |
| 240 | + exit 1 |
| 241 | +fi |
| 242 | + |
| 243 | +echo "β
No API keys detected." |
| 244 | +exit 0 |
| 245 | +``` |
| 246 | + |
| 247 | +--- |
| 248 | + |
| 249 | +## π Monitoring & Alerts |
| 250 | + |
| 251 | +### Set Up Usage Monitoring: |
| 252 | + |
| 253 | +```javascript |
| 254 | +// monitoring.js |
| 255 | +const { GoogleAuth } = require('google-auth-library'); |
| 256 | + |
| 257 | +async function monitorAPIUsage() { |
| 258 | + // Check daily usage |
| 259 | + const usage = await getAPIUsage(); |
| 260 | + |
| 261 | + if (usage.requestsToday > 10000) { |
| 262 | + console.warn('π¨ High API usage detected!'); |
| 263 | + // Send alert to team |
| 264 | + } |
| 265 | + |
| 266 | + if (usage.errorRate > 0.05) { |
| 267 | + console.warn('π¨ High error rate detected!'); |
| 268 | + // Check for potential abuse |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +// Run monitoring every hour |
| 273 | +setInterval(monitorAPIUsage, 3600000); |
| 274 | +``` |
| 275 | + |
| 276 | +### Google Cloud Monitoring: |
| 277 | + |
| 278 | +1. **Set up billing alerts** |
| 279 | +2. **Configure usage quotas** |
| 280 | +3. **Enable audit logging** |
| 281 | +4. **Set up anomaly detection** |
| 282 | + |
| 283 | +--- |
| 284 | + |
| 285 | +## π Additional Resources |
| 286 | + |
| 287 | +### Security Tools: |
| 288 | +- **gitleaks**: https://github.com/gitleaks/gitleaks |
| 289 | +- **truffleHog**: https://github.com/trufflesecurity/truffleHog |
| 290 | +- **git-secrets**: https://github.com/awslabs/git-secrets |
| 291 | + |
| 292 | +### Google Cloud Security: |
| 293 | +- **Security Best Practices**: https://cloud.google.com/docs/security |
| 294 | +- **API Key Best Practices**: https://cloud.google.com/docs/authentication/api-keys |
| 295 | +- **Identity and Access Management**: https://cloud.google.com/iam/docs |
| 296 | + |
| 297 | +### General Security: |
| 298 | +- **OWASP API Security**: https://owasp.org/www-project-api-security/ |
| 299 | +- **Security Headers**: https://securityheaders.com/ |
| 300 | +- **SSL/TLS Configuration**: https://ssl-config.mozilla.org/ |
| 301 | + |
| 302 | +--- |
| 303 | + |
| 304 | +## β
Security Checklist |
| 305 | + |
| 306 | +Before deploying your application: |
| 307 | + |
| 308 | +- [ ] All API keys are in environment variables |
| 309 | +- [ ] `.env` files are in `.gitignore` |
| 310 | +- [ ] API keys have appropriate restrictions |
| 311 | +- [ ] Separate keys for dev/staging/production |
| 312 | +- [ ] Monitoring and alerts are configured |
| 313 | +- [ ] Team trained on security practices |
| 314 | +- [ ] Pre-commit hooks are installed |
| 315 | +- [ ] Regular security audits scheduled |
| 316 | +- [ ] Incident response plan documented |
| 317 | +- [ ] Key rotation schedule established |
| 318 | + |
| 319 | +--- |
| 320 | + |
| 321 | +**Remember: Security is everyone's responsibility! π‘οΈ** |
| 322 | + |
| 323 | +Report security issues to: [Your security contact] |
0 commit comments