forked from rossman22590/machine-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudflare.py
More file actions
356 lines (289 loc) · 14 KB
/
cloudflare.py
File metadata and controls
356 lines (289 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import os
import json
import getpass
from pathlib import Path
class CloudflareCredentialsManager:
"""Manage Cloudflare API credentials securely"""
def __init__(self):
self.config_dir = Path.home() / '.cloudflare'
self.config_file = self.config_dir / 'credentials.json'
self.env_file = Path('.env')
# Your pre-configured API key
self.default_api_token = "Your_Key"
def setup_config_directory(self):
"""Create config directory if it doesn't exist"""
self.config_dir.mkdir(exist_ok=True)
def auto_setup_with_your_key(self):
"""Automatically set up with your provided API key"""
print("🔐 Setting up with your Cloudflare API Key")
print("=" * 45)
credentials = {
'CLOUDFLARE_API_TOKEN': self.default_api_token
}
# Ask for additional optional credentials
print(f"✅ API Token: {self.default_api_token[:8]}...{self.default_api_token[-4:]}")
# Account ID
print("\n📋 Optional: Add your Cloudflare Account ID for better functionality")
print(" - Found in the right sidebar of your Cloudflare dashboard")
account_id = input(" Enter your Cloudflare Account ID (press Enter to skip): ").strip()
if account_id:
credentials['CLOUDFLARE_ACCOUNT_ID'] = account_id
# Zone ID (for custom domain)
print("\n🌐 Optional: Add Zone ID for custom domains")
print(" - Go to your domain in Cloudflare dashboard")
print(" - Found in the right sidebar under 'API'")
zone_id = input(" Enter your Cloudflare Zone ID (press Enter to skip): ").strip()
if zone_id:
credentials['CLOUDFLARE_ZONE_ID'] = zone_id
# Custom Domain
print("\n🏠 Optional: Add custom domain")
print(" - Your domain name for custom subdomains (e.g., 'yourdomain.com')")
custom_domain = input(" Enter your custom domain (press Enter to skip): ").strip()
if custom_domain:
credentials['CUSTOM_DOMAIN'] = custom_domain
return credentials
def input_credentials(self):
"""Prompt user to input Cloudflare credentials"""
print("🔐 Cloudflare Credentials Setup")
print("=" * 40)
credentials = {}
# API Token with your key as default
print("\n1. Cloudflare API Token:")
print(f" Default: {self.default_api_token[:8]}...{self.default_api_token[-4:]}")
use_default = input(" Use your pre-configured API token? (Y/n): ").strip().lower()
if use_default != 'n':
credentials['CLOUDFLARE_API_TOKEN'] = self.default_api_token
print(" ✅ Using your pre-configured API token")
else:
print(" - Go to https://dash.cloudflare.com/profile/api-tokens")
print(" - Create a token with 'Cloudflare Pages:Edit' and 'Zone:Read' permissions")
api_token = getpass.getpass(" Enter your Cloudflare API Token: ").strip()
if not api_token:
print("❌ API Token is required!")
return None
credentials['CLOUDFLARE_API_TOKEN'] = api_token
# Account ID
print("\n2. Cloudflare Account ID:")
print(" - Found in the right sidebar of your Cloudflare dashboard")
account_id = input(" Enter your Cloudflare Account ID: ").strip()
if account_id:
credentials['CLOUDFLARE_ACCOUNT_ID'] = account_id
# Zone ID (for custom domain)
print("\n3. Cloudflare Zone ID (Optional - for custom domains):")
print(" - Go to your domain in Cloudflare dashboard")
print(" - Found in the right sidebar under 'API'")
zone_id = input(" Enter your Cloudflare Zone ID (press Enter to skip): ").strip()
if zone_id:
credentials['CLOUDFLARE_ZONE_ID'] = zone_id
# Custom Domain
print("\n4. Custom Domain (Optional):")
print(" - Your domain name for custom subdomains (e.g., 'yourdomain.com')")
custom_domain = input(" Enter your custom domain (press Enter to skip): ").strip()
if custom_domain:
credentials['CUSTOM_DOMAIN'] = custom_domain
return credentials
def quick_setup(self):
"""Quick setup with just your API key"""
credentials = {
'CLOUDFLARE_API_TOKEN': self.default_api_token
}
# Save to both JSON and .env
self.save_credentials_to_json(credentials)
self.save_credentials_to_env(credentials)
print("🚀 Quick setup complete!")
print(f"✅ API Token configured: {self.default_api_token[:8]}...{self.default_api_token[-4:]}")
return credentials
def save_credentials_to_json(self, credentials):
"""Save credentials to JSON file"""
self.setup_config_directory()
try:
with open(self.config_file, 'w') as f:
json.dump(credentials, f, indent=2)
# Set restrictive permissions on the file
os.chmod(self.config_file, 0o600)
print(f"✅ Credentials saved to {self.config_file}")
return True
except Exception as e:
print(f"❌ Error saving credentials: {e}")
return False
def save_credentials_to_env(self, credentials):
"""Save credentials to .env file"""
try:
env_content = []
# Read existing .env file if it exists
if self.env_file.exists():
with open(self.env_file, 'r') as f:
env_content = f.readlines()
# Remove existing Cloudflare entries
env_content = [line for line in env_content
if not any(line.startswith(key) for key in credentials.keys())]
# Add new credentials
for key, value in credentials.items():
env_content.append(f"{key}={value}\n")
# Write back to .env file
with open(self.env_file, 'w') as f:
f.writelines(env_content)
print(f"✅ Credentials saved to {self.env_file}")
return True
except Exception as e:
print(f"❌ Error saving to .env file: {e}")
return False
def load_credentials_from_json(self):
"""Load credentials from JSON file"""
try:
if self.config_file.exists():
with open(self.config_file, 'r') as f:
return json.load(f)
return None
except Exception as e:
print(f"❌ Error loading credentials: {e}")
return None
def load_credentials_from_env(self):
"""Load credentials from environment variables"""
credentials = {}
env_vars = ['CLOUDFLARE_API_TOKEN', 'CLOUDFLARE_ACCOUNT_ID',
'CLOUDFLARE_ZONE_ID', 'CUSTOM_DOMAIN']
for var in env_vars:
value = os.getenv(var)
if value:
credentials[var] = value
return credentials if credentials else None
def display_credentials(self, credentials):
"""Display credentials (masked for security)"""
print("\n📋 Current Credentials:")
print("=" * 30)
for key, value in credentials.items():
if 'TOKEN' in key:
# Mask the token for security
masked_value = value[:8] + '*' * (len(value) - 12) + value[-4:] if len(value) > 12 else '*' * len(value)
print(f" {key}: {masked_value}")
else:
print(f" {key}: {value}")
def test_credentials(self, credentials):
"""Test if credentials work with Cloudflare API"""
try:
import requests
api_token = credentials.get('CLOUDFLARE_API_TOKEN')
if not api_token:
return False, "No API token provided"
headers = {
'Authorization': f'Bearer {api_token}',
'Content-Type': 'application/json'
}
# Test API token by getting user info
response = requests.get('https://api.cloudflare.com/client/v4/user', headers=headers)
if response.status_code == 200:
data = response.json()
if data.get('success'):
user_email = data.get('result', {}).get('email', 'Unknown')
return True, f"✅ API token valid for user: {user_email}"
else:
return False, f"❌ API returned error: {data.get('errors', 'Unknown error')}"
else:
return False, f"❌ HTTP {response.status_code}: {response.text}"
except ImportError:
return None, "⚠️ 'requests' library not installed. Run: pip install requests"
except Exception as e:
return False, f"❌ Error testing credentials: {e}"
def set_environment_variables(self):
"""Set environment variables for current session"""
credentials = self.load_credentials_from_json()
if not credentials:
credentials = {'CLOUDFLARE_API_TOKEN': self.default_api_token}
for key, value in credentials.items():
os.environ[key] = value
print(f"✅ Set {key}")
print("🌟 Environment variables set for current session!")
return credentials
def main():
"""Main function to handle credential management"""
manager = CloudflareCredentialsManager()
# Check if credentials already exist
existing_creds = manager.load_credentials_from_json()
if not existing_creds:
existing_creds = manager.load_credentials_from_env()
if not existing_creds:
print("🎉 Welcome! No existing credentials found.")
print("Let's set up your Cloudflare credentials quickly!")
quick_setup = input("\nDo quick setup with your API key? (Y/n): ").strip().lower()
if quick_setup != 'n':
manager.quick_setup()
print("\n🎯 Setup complete! You can now use the deployment script.")
return
while True:
print("\n🌐 Cloudflare Credentials Manager")
print("=" * 35)
print("1. Quick setup (use your API key)")
print("2. Full setup (add Account ID, Zone ID, etc.)")
print("3. View current credentials")
print("4. Test credentials")
print("5. Set environment variables")
print("6. Delete credentials")
print("7. Export commands")
print("8. Exit")
choice = input("\nSelect an option (1-8): ").strip()
if choice == '1':
# Quick setup
manager.quick_setup()
elif choice == '2':
# Full setup
credentials = manager.auto_setup_with_your_key()
if credentials:
print("\nSave credentials to:")
print("1. JSON file (recommended)")
print("2. .env file")
print("3. Both")
save_choice = input("Select option (1-3): ").strip()
if save_choice in ['1', '3']:
manager.save_credentials_to_json(credentials)
if save_choice in ['2', '3']:
manager.save_credentials_to_env(credentials)
elif choice == '3':
# View current credentials
credentials = manager.load_credentials_from_json()
if not credentials:
credentials = manager.load_credentials_from_env()
if credentials:
manager.display_credentials(credentials)
else:
print("ℹ️ No credentials found")
elif choice == '4':
# Test credentials
credentials = manager.load_credentials_from_json()
if not credentials:
credentials = manager.load_credentials_from_env()
if credentials:
print("\n🧪 Testing credentials...")
success, message = manager.test_credentials(credentials)
print(message)
else:
print("❌ No credentials found to test")
elif choice == '5':
# Set environment variables
manager.set_environment_variables()
elif choice == '6':
# Delete credentials
confirm = input("Are you sure you want to delete all credentials? (y/N): ").strip().lower()
if confirm == 'y':
manager.delete_credentials()
elif choice == '7':
# Export to environment variables
credentials = manager.load_credentials_from_json()
if not credentials:
credentials = manager.load_credentials_from_env()
if credentials:
print("\n📤 Export commands:")
print("Copy and paste these commands in your terminal:")
print("-" * 50)
for key, value in credentials.items():
print(f"export {key}='{value}'")
print("-" * 50)
else:
print("❌ No credentials found to export")
elif choice == '8':
print("👋 Goodbye!")
break
else:
print("❌ Invalid option. Please select 1-8.")
if __name__ == "__main__":
main()