|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Generate secure API keys for production use |
| 4 | +""" |
| 5 | + |
| 6 | +import secrets |
| 7 | +import hashlib |
| 8 | +import string |
| 9 | +import json |
| 10 | +from datetime import datetime |
| 11 | + |
| 12 | +def generate_api_key(prefix="", length=32): |
| 13 | + """Generate a cryptographically secure API key.""" |
| 14 | + alphabet = string.ascii_letters + string.digits |
| 15 | + key = ''.join(secrets.choice(alphabet) for _ in range(length)) |
| 16 | + return f"{prefix}_{key}" if prefix else key |
| 17 | + |
| 18 | +def generate_tier_keys(): |
| 19 | + """Generate API keys for different tiers.""" |
| 20 | + keys = { |
| 21 | + "basic": { |
| 22 | + "key": generate_api_key("basic", 40), |
| 23 | + "tier": "basic", |
| 24 | + "rate_limit": "100/hour", |
| 25 | + "features": ["emissions", "countries", "basic_stats"] |
| 26 | + }, |
| 27 | + "premium": { |
| 28 | + "key": generate_api_key("premium", 40), |
| 29 | + "tier": "premium", |
| 30 | + "rate_limit": "1000/hour", |
| 31 | + "features": ["emissions", "countries", "stats", "analytics", "bulk_export"] |
| 32 | + }, |
| 33 | + "enterprise": { |
| 34 | + "key": generate_api_key("enterprise", 40), |
| 35 | + "tier": "enterprise", |
| 36 | + "rate_limit": "unlimited", |
| 37 | + "features": ["all"] |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + # Generate master key |
| 42 | + master_key = generate_api_key("master", 48) |
| 43 | + |
| 44 | + return keys, master_key |
| 45 | + |
| 46 | +def create_env_format(keys, master_key): |
| 47 | + """Create environment variable format.""" |
| 48 | + key_strings = [] |
| 49 | + for tier_name, tier_data in keys.items(): |
| 50 | + key_strings.append(f"{tier_data['key']}:Production{tier_name.title()}:{tier_name}") |
| 51 | + |
| 52 | + api_keys_env = ",".join(key_strings) |
| 53 | + |
| 54 | + return f"""# Production API Keys - Generated {datetime.now().isoformat()} |
| 55 | +API_KEYS={api_keys_env} |
| 56 | +MASTER_API_KEY={master_key} |
| 57 | +
|
| 58 | +# Individual Keys for Reference: |
| 59 | +# Basic Tier: {keys['basic']['key']} |
| 60 | +# Premium Tier: {keys['premium']['key']} |
| 61 | +# Enterprise Tier: {keys['enterprise']['key']} |
| 62 | +# Master Key: {master_key} |
| 63 | +""" |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + print("🔑 Generating secure API keys for production...") |
| 67 | + |
| 68 | + keys, master_key = generate_tier_keys() |
| 69 | + env_content = create_env_format(keys, master_key) |
| 70 | + |
| 71 | + # Save to file |
| 72 | + with open('.env.production.keys', 'w') as f: |
| 73 | + f.write(env_content) |
| 74 | + |
| 75 | + # Save detailed info to JSON |
| 76 | + key_info = { |
| 77 | + "generated_at": datetime.now().isoformat(), |
| 78 | + "keys": keys, |
| 79 | + "master_key": master_key, |
| 80 | + "usage_instructions": { |
| 81 | + "basic": "For standard API usage with rate limits", |
| 82 | + "premium": "For high-volume usage with extended features", |
| 83 | + "enterprise": "For unlimited usage with all features", |
| 84 | + "master": "For administrative access and service management" |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + with open('production_keys.json', 'w') as f: |
| 89 | + json.dump(key_info, f, indent=2) |
| 90 | + |
| 91 | + print("✅ API Keys generated successfully!") |
| 92 | + print("\n📁 Files created:") |
| 93 | + print("- .env.production.keys (Environment variables)") |
| 94 | + print("- production_keys.json (Detailed key information)") |
| 95 | + |
| 96 | + print("\n🔐 Security Notes:") |
| 97 | + print("- Store these keys securely") |
| 98 | + print("- Never commit keys to version control") |
| 99 | + print("- Update GitHub Secrets with new API_KEYS value") |
| 100 | + print("- Rotate keys regularly") |
| 101 | + |
| 102 | + print(f"\n📋 Quick Reference:") |
| 103 | + print(f"Basic Key: {keys['basic']['key']}") |
| 104 | + print(f"Premium Key: {keys['premium']['key']}") |
| 105 | + print(f"Master Key: {master_key}") |
0 commit comments