xenfra 0.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- xenfra/__init__.py +0 -0
- xenfra/commands/__init__.py +3 -0
- xenfra/commands/auth.py +137 -0
- xenfra/commands/auth_device.py +159 -0
- xenfra/commands/deployments.py +666 -0
- xenfra/commands/intelligence.py +343 -0
- xenfra/commands/projects.py +204 -0
- xenfra/commands/security_cmd.py +233 -0
- xenfra/main.py +75 -0
- xenfra/utils/__init__.py +3 -0
- xenfra/utils/auth.py +243 -0
- xenfra/utils/codebase.py +126 -0
- xenfra/utils/config.py +363 -0
- xenfra/utils/security.py +336 -0
- xenfra/utils/validation.py +234 -0
- xenfra-0.3.1.dist-info/METADATA +116 -0
- xenfra-0.3.1.dist-info/RECORD +19 -0
- xenfra-0.3.1.dist-info/WHEEL +4 -0
- xenfra-0.3.1.dist-info/entry_points.txt +3 -0
xenfra/utils/config.py
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Configuration file generation utilities.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import shutil
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import click
|
|
11
|
+
import yaml
|
|
12
|
+
from rich.console import Console
|
|
13
|
+
from rich.prompt import Confirm, IntPrompt, Prompt
|
|
14
|
+
from xenfra_sdk import CodebaseAnalysisResponse
|
|
15
|
+
|
|
16
|
+
console = Console()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def read_xenfra_yaml(filename: str = "xenfra.yaml") -> dict:
|
|
20
|
+
"""
|
|
21
|
+
Read and parse xenfra.yaml configuration file.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
filename: Path to the config file (default: xenfra.yaml)
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
Dictionary containing the configuration
|
|
28
|
+
|
|
29
|
+
Raises:
|
|
30
|
+
FileNotFoundError: If the config file doesn't exist
|
|
31
|
+
yaml.YAMLError: If the YAML is malformed
|
|
32
|
+
"""
|
|
33
|
+
"""
|
|
34
|
+
Read and parse xenfra.yaml configuration file.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
filename: Path to the config file (default: xenfra.yaml)
|
|
38
|
+
|
|
39
|
+
Returns:
|
|
40
|
+
Dictionary containing the configuration
|
|
41
|
+
|
|
42
|
+
Raises:
|
|
43
|
+
FileNotFoundError: If the config file doesn't exist
|
|
44
|
+
"""
|
|
45
|
+
if not Path(filename).exists():
|
|
46
|
+
raise FileNotFoundError(
|
|
47
|
+
f"Configuration file '{filename}' not found. Run 'xenfra init' first."
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
try:
|
|
51
|
+
with open(filename, "r") as f:
|
|
52
|
+
return yaml.safe_load(f) or {}
|
|
53
|
+
except yaml.YAMLError as e:
|
|
54
|
+
raise ValueError(f"Invalid YAML in {filename}: {e}")
|
|
55
|
+
except Exception as e:
|
|
56
|
+
raise IOError(f"Failed to read {filename}: {e}")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def generate_xenfra_yaml(analysis: CodebaseAnalysisResponse, filename: str = "xenfra.yaml") -> str:
|
|
60
|
+
"""
|
|
61
|
+
Generate xenfra.yaml from AI codebase analysis.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
analysis: CodebaseAnalysisResponse from Intelligence Service
|
|
65
|
+
filename: Output filename (default: xenfra.yaml)
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
Path to the generated file
|
|
69
|
+
"""
|
|
70
|
+
# Build configuration dictionary
|
|
71
|
+
config = {
|
|
72
|
+
"name": os.path.basename(os.getcwd()),
|
|
73
|
+
"framework": analysis.framework,
|
|
74
|
+
"port": analysis.port,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
# Add database configuration if detected
|
|
78
|
+
if analysis.database and analysis.database != "none":
|
|
79
|
+
config["database"] = {"type": analysis.database, "env_var": "DATABASE_URL"}
|
|
80
|
+
|
|
81
|
+
# Add cache configuration if detected
|
|
82
|
+
if analysis.cache and analysis.cache != "none":
|
|
83
|
+
config["cache"] = {"type": analysis.cache, "env_var": f"{analysis.cache.upper()}_URL"}
|
|
84
|
+
|
|
85
|
+
# Add worker configuration if detected
|
|
86
|
+
if analysis.workers and len(analysis.workers) > 0:
|
|
87
|
+
config["workers"] = analysis.workers
|
|
88
|
+
|
|
89
|
+
# Add environment variables
|
|
90
|
+
if analysis.env_vars and len(analysis.env_vars) > 0:
|
|
91
|
+
config["env_vars"] = analysis.env_vars
|
|
92
|
+
|
|
93
|
+
# Add instance size
|
|
94
|
+
config["instance_size"] = analysis.instance_size
|
|
95
|
+
|
|
96
|
+
# Add package manager info (for intelligent diagnosis)
|
|
97
|
+
if analysis.package_manager:
|
|
98
|
+
config["package_manager"] = analysis.package_manager
|
|
99
|
+
if analysis.dependency_file:
|
|
100
|
+
config["dependency_file"] = analysis.dependency_file
|
|
101
|
+
|
|
102
|
+
# Write to file
|
|
103
|
+
with open(filename, "w") as f:
|
|
104
|
+
yaml.dump(config, f, sort_keys=False, default_flow_style=False)
|
|
105
|
+
|
|
106
|
+
return filename
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def create_backup(file_path: str) -> str:
|
|
110
|
+
"""
|
|
111
|
+
Create a timestamped backup of a file in .xenfra/backups/ directory.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
file_path: Path to the file to backup
|
|
115
|
+
|
|
116
|
+
Returns:
|
|
117
|
+
Path to the backup file
|
|
118
|
+
"""
|
|
119
|
+
# Create .xenfra/backups directory if it doesn't exist
|
|
120
|
+
backup_dir = Path(".xenfra") / "backups"
|
|
121
|
+
backup_dir.mkdir(parents=True, exist_ok=True)
|
|
122
|
+
|
|
123
|
+
# Generate timestamped backup filename
|
|
124
|
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
125
|
+
file_name = Path(file_path).name
|
|
126
|
+
backup_path = backup_dir / f"{file_name}.{timestamp}.backup"
|
|
127
|
+
|
|
128
|
+
# Copy file to backup location
|
|
129
|
+
shutil.copy2(file_path, backup_path)
|
|
130
|
+
|
|
131
|
+
return str(backup_path)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def apply_patch(patch: dict, target_file: str = None, create_backup_file: bool = True):
|
|
135
|
+
"""
|
|
136
|
+
Apply a JSON patch to a configuration file with automatic backup.
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
patch: Patch object with file, operation, path, value
|
|
140
|
+
target_file: Optional override for the file to patch
|
|
141
|
+
create_backup_file: Whether to create a backup before patching (default: True)
|
|
142
|
+
|
|
143
|
+
Returns:
|
|
144
|
+
Path to the backup file if created, None otherwise
|
|
145
|
+
|
|
146
|
+
Raises:
|
|
147
|
+
ValueError: If patch structure is invalid
|
|
148
|
+
FileNotFoundError: If target file doesn't exist
|
|
149
|
+
NotImplementedError: If file type is not supported
|
|
150
|
+
"""
|
|
151
|
+
# Validate patch structure
|
|
152
|
+
if not isinstance(patch, dict):
|
|
153
|
+
raise ValueError("Patch must be a dictionary")
|
|
154
|
+
|
|
155
|
+
required_fields = ["file", "operation"]
|
|
156
|
+
for field in required_fields:
|
|
157
|
+
if field not in patch:
|
|
158
|
+
raise ValueError(f"Patch missing required field: {field}")
|
|
159
|
+
|
|
160
|
+
operation = patch.get("operation")
|
|
161
|
+
if operation not in ["add", "replace", "remove"]:
|
|
162
|
+
raise ValueError(
|
|
163
|
+
f"Invalid patch operation: {operation}. Must be 'add', 'replace', or 'remove'"
|
|
164
|
+
)
|
|
165
|
+
"""
|
|
166
|
+
Apply a JSON patch to a configuration file with automatic backup.
|
|
167
|
+
|
|
168
|
+
Args:
|
|
169
|
+
patch: Patch object with file, operation, path, value
|
|
170
|
+
target_file: Optional override for the file to patch
|
|
171
|
+
create_backup_file: Whether to create a backup before patching (default: True)
|
|
172
|
+
|
|
173
|
+
Returns:
|
|
174
|
+
Path to the backup file if created, None otherwise
|
|
175
|
+
"""
|
|
176
|
+
file_to_patch = target_file or patch.get("file")
|
|
177
|
+
|
|
178
|
+
if not file_to_patch:
|
|
179
|
+
raise ValueError("No target file specified in patch")
|
|
180
|
+
|
|
181
|
+
if not os.path.exists(file_to_patch):
|
|
182
|
+
raise FileNotFoundError(f"File '{file_to_patch}' not found")
|
|
183
|
+
|
|
184
|
+
# Create backup before modifying
|
|
185
|
+
backup_path = None
|
|
186
|
+
if create_backup_file:
|
|
187
|
+
backup_path = create_backup(file_to_patch)
|
|
188
|
+
|
|
189
|
+
# For YAML files
|
|
190
|
+
if file_to_patch.endswith((".yaml", ".yml")):
|
|
191
|
+
with open(file_to_patch, "r") as f:
|
|
192
|
+
config_data = yaml.safe_load(f) or {}
|
|
193
|
+
|
|
194
|
+
# Apply patch based on operation
|
|
195
|
+
operation = patch.get("operation")
|
|
196
|
+
path = patch.get("path", "").strip("/")
|
|
197
|
+
value = patch.get("value")
|
|
198
|
+
|
|
199
|
+
if operation == "add":
|
|
200
|
+
# For simple paths, add to root
|
|
201
|
+
if path:
|
|
202
|
+
path_parts = path.split("/")
|
|
203
|
+
current = config_data
|
|
204
|
+
for part in path_parts[:-1]:
|
|
205
|
+
if part not in current:
|
|
206
|
+
current[part] = {}
|
|
207
|
+
current = current[part]
|
|
208
|
+
current[path_parts[-1]] = value
|
|
209
|
+
else:
|
|
210
|
+
# Add to root level
|
|
211
|
+
if isinstance(value, dict):
|
|
212
|
+
config_data.update(value)
|
|
213
|
+
else:
|
|
214
|
+
config_data = value
|
|
215
|
+
|
|
216
|
+
elif operation == "replace":
|
|
217
|
+
if path:
|
|
218
|
+
path_parts = path.split("/")
|
|
219
|
+
current = config_data
|
|
220
|
+
for part in path_parts[:-1]:
|
|
221
|
+
current = current[part]
|
|
222
|
+
current[path_parts[-1]] = value
|
|
223
|
+
else:
|
|
224
|
+
config_data = value
|
|
225
|
+
|
|
226
|
+
# Write back
|
|
227
|
+
with open(file_to_patch, "w") as f:
|
|
228
|
+
yaml.dump(config_data, f, sort_keys=False, default_flow_style=False)
|
|
229
|
+
|
|
230
|
+
# For JSON files
|
|
231
|
+
elif file_to_patch.endswith(".json"):
|
|
232
|
+
import json
|
|
233
|
+
|
|
234
|
+
with open(file_to_patch, "r") as f:
|
|
235
|
+
config_data = json.load(f)
|
|
236
|
+
|
|
237
|
+
operation = patch.get("operation")
|
|
238
|
+
path = patch.get("path", "").strip("/")
|
|
239
|
+
value = patch.get("value")
|
|
240
|
+
|
|
241
|
+
if operation == "add":
|
|
242
|
+
if path:
|
|
243
|
+
path_parts = path.split("/")
|
|
244
|
+
current = config_data
|
|
245
|
+
for part in path_parts[:-1]:
|
|
246
|
+
if part not in current:
|
|
247
|
+
current[part] = {}
|
|
248
|
+
current = current[part]
|
|
249
|
+
current[path_parts[-1]] = value
|
|
250
|
+
else:
|
|
251
|
+
if isinstance(value, dict):
|
|
252
|
+
config_data.update(value)
|
|
253
|
+
else:
|
|
254
|
+
config_data = value
|
|
255
|
+
|
|
256
|
+
elif operation == "replace":
|
|
257
|
+
if path:
|
|
258
|
+
path_parts = path.split("/")
|
|
259
|
+
current = config_data
|
|
260
|
+
for part in path_parts[:-1]:
|
|
261
|
+
current = current[part]
|
|
262
|
+
current[path_parts[-1]] = value
|
|
263
|
+
else:
|
|
264
|
+
config_data = value
|
|
265
|
+
|
|
266
|
+
# Write back
|
|
267
|
+
with open(file_to_patch, "w") as f:
|
|
268
|
+
json.dump(config_data, f, indent=2)
|
|
269
|
+
|
|
270
|
+
# For text files (like requirements.txt)
|
|
271
|
+
elif file_to_patch.endswith(".txt"):
|
|
272
|
+
operation = patch.get("operation")
|
|
273
|
+
value = patch.get("value")
|
|
274
|
+
|
|
275
|
+
if operation == "add":
|
|
276
|
+
# Append to file
|
|
277
|
+
with open(file_to_patch, "a") as f:
|
|
278
|
+
f.write(f"\n{value}\n")
|
|
279
|
+
elif operation == "replace":
|
|
280
|
+
# Replace entire file
|
|
281
|
+
with open(file_to_patch, "w") as f:
|
|
282
|
+
f.write(str(value))
|
|
283
|
+
else:
|
|
284
|
+
# Design decision: Only support auto-patching for common dependency files
|
|
285
|
+
# Other file types should be manually edited to avoid data loss
|
|
286
|
+
# See docs/future-enhancements.md #4 for potential extensions
|
|
287
|
+
raise NotImplementedError(f"Patching not supported for file type: {file_to_patch}")
|
|
288
|
+
|
|
289
|
+
return backup_path
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def manual_prompt_for_config(filename: str = "xenfra.yaml") -> str:
|
|
293
|
+
"""
|
|
294
|
+
Prompt user interactively for configuration details and generate xenfra.yaml.
|
|
295
|
+
|
|
296
|
+
Args:
|
|
297
|
+
filename: Output filename (default: xenfra.yaml)
|
|
298
|
+
|
|
299
|
+
Returns:
|
|
300
|
+
Path to the generated file
|
|
301
|
+
"""
|
|
302
|
+
config = {}
|
|
303
|
+
|
|
304
|
+
# Project name (default to directory name)
|
|
305
|
+
default_name = os.path.basename(os.getcwd())
|
|
306
|
+
config["name"] = Prompt.ask("Project name", default=default_name)
|
|
307
|
+
|
|
308
|
+
# Framework
|
|
309
|
+
framework = Prompt.ask(
|
|
310
|
+
"Framework", choices=["fastapi", "flask", "django", "other"], default="fastapi"
|
|
311
|
+
)
|
|
312
|
+
config["framework"] = framework
|
|
313
|
+
|
|
314
|
+
# Port
|
|
315
|
+
port = IntPrompt.ask("Application port", default=8000)
|
|
316
|
+
# Validate port
|
|
317
|
+
from .validation import validate_port
|
|
318
|
+
|
|
319
|
+
is_valid, error_msg = validate_port(port)
|
|
320
|
+
if not is_valid:
|
|
321
|
+
console.print(f"[bold red]Invalid port: {error_msg}[/bold red]")
|
|
322
|
+
raise click.Abort()
|
|
323
|
+
config["port"] = port
|
|
324
|
+
|
|
325
|
+
# Database
|
|
326
|
+
use_database = Confirm.ask("Does your app use a database?", default=False)
|
|
327
|
+
if use_database:
|
|
328
|
+
db_type = Prompt.ask(
|
|
329
|
+
"Database type",
|
|
330
|
+
choices=["postgresql", "mysql", "sqlite", "mongodb"],
|
|
331
|
+
default="postgresql",
|
|
332
|
+
)
|
|
333
|
+
config["database"] = {"type": db_type, "env_var": "DATABASE_URL"}
|
|
334
|
+
|
|
335
|
+
# Cache
|
|
336
|
+
use_cache = Confirm.ask("Does your app use caching?", default=False)
|
|
337
|
+
if use_cache:
|
|
338
|
+
cache_type = Prompt.ask("Cache type", choices=["redis", "memcached"], default="redis")
|
|
339
|
+
config["cache"] = {"type": cache_type, "env_var": f"{cache_type.upper()}_URL"}
|
|
340
|
+
|
|
341
|
+
# Instance size
|
|
342
|
+
instance_size = Prompt.ask(
|
|
343
|
+
"Instance size", choices=["basic", "standard", "premium"], default="basic"
|
|
344
|
+
)
|
|
345
|
+
config["instance_size"] = instance_size
|
|
346
|
+
|
|
347
|
+
# Environment variables
|
|
348
|
+
add_env = Confirm.ask("Add environment variables?", default=False)
|
|
349
|
+
if add_env:
|
|
350
|
+
env_vars = []
|
|
351
|
+
while True:
|
|
352
|
+
env_var = Prompt.ask("Environment variable name (blank to finish)", default="")
|
|
353
|
+
if not env_var:
|
|
354
|
+
break
|
|
355
|
+
env_vars.append(env_var)
|
|
356
|
+
if env_vars:
|
|
357
|
+
config["env_vars"] = env_vars
|
|
358
|
+
|
|
359
|
+
# Write to file
|
|
360
|
+
with open(filename, "w") as f:
|
|
361
|
+
yaml.dump(config, f, sort_keys=False, default_flow_style=False)
|
|
362
|
+
|
|
363
|
+
return filename
|
xenfra/utils/security.py
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Security utilities for Xenfra CLI.
|
|
3
|
+
Implements comprehensive URL validation, domain whitelisting, HTTPS enforcement, and certificate pinning.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
import ssl
|
|
8
|
+
from urllib.parse import urlparse
|
|
9
|
+
|
|
10
|
+
import certifi
|
|
11
|
+
import click
|
|
12
|
+
import httpx
|
|
13
|
+
from rich.console import Console
|
|
14
|
+
|
|
15
|
+
console = Console()
|
|
16
|
+
|
|
17
|
+
# Production API URL
|
|
18
|
+
PRODUCTION_API_URL = "https://api.xenfra.tech"
|
|
19
|
+
|
|
20
|
+
# Allowed domains (whitelist) - Solution 2
|
|
21
|
+
ALLOWED_DOMAINS = [
|
|
22
|
+
"api.xenfra.tech", # Production
|
|
23
|
+
"api-staging.xenfra.tech", # Staging
|
|
24
|
+
"localhost", # Local development
|
|
25
|
+
"127.0.0.1", # Local development (IP)
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
# Certificate fingerprints for pinning - Solution 4
|
|
29
|
+
# These should be updated when certificates are rotated
|
|
30
|
+
PINNED_CERTIFICATES = {
|
|
31
|
+
"api.xenfra.tech": {
|
|
32
|
+
# SHA256 fingerprint of the expected certificate
|
|
33
|
+
# Example: "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
|
|
34
|
+
# To get fingerprint, run:
|
|
35
|
+
# openssl s_client -connect api.xenfra.tech:443 | openssl x509 -pubkey -noout \
|
|
36
|
+
# | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | base64
|
|
37
|
+
"fingerprints": [], # Add actual fingerprints when you have production cert
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class SecurityConfig:
|
|
43
|
+
"""Security configuration for CLI."""
|
|
44
|
+
|
|
45
|
+
def __init__(self):
|
|
46
|
+
"""Initialize security configuration from environment."""
|
|
47
|
+
# PRODUCTION-ONLY: Default to production settings
|
|
48
|
+
# Environment variable only used for self-hosted instances
|
|
49
|
+
self.environment = "production"
|
|
50
|
+
|
|
51
|
+
# Security settings - ALWAYS enforced for production safety
|
|
52
|
+
self.enforce_https = True # Always require HTTPS
|
|
53
|
+
self.enforce_whitelist = False # Allow self-hosted instances
|
|
54
|
+
self.enable_cert_pinning = False # Disabled (see future-enhancements.md #3)
|
|
55
|
+
self.warn_on_http = True # Always warn on HTTP
|
|
56
|
+
|
|
57
|
+
def is_production(self) -> bool:
|
|
58
|
+
"""Check if running in production environment."""
|
|
59
|
+
return self.environment == "production"
|
|
60
|
+
|
|
61
|
+
def is_development(self) -> bool:
|
|
62
|
+
"""Check if running in development environment."""
|
|
63
|
+
return self.environment in ["development", "dev", "local"]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# Global security configuration
|
|
67
|
+
security_config = SecurityConfig()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def validate_url_format(url: str) -> dict:
|
|
71
|
+
"""
|
|
72
|
+
Solution 1: Basic URL validation.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
url: The URL to validate
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
Parsed URL components
|
|
79
|
+
|
|
80
|
+
Raises:
|
|
81
|
+
ValueError: If URL format is invalid
|
|
82
|
+
"""
|
|
83
|
+
try:
|
|
84
|
+
parsed = urlparse(url)
|
|
85
|
+
|
|
86
|
+
# Check scheme
|
|
87
|
+
if parsed.scheme not in ["http", "https"]:
|
|
88
|
+
raise ValueError(
|
|
89
|
+
f"Invalid URL scheme '{parsed.scheme}'. " f"Only 'http' and 'https' are allowed."
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
# Check hostname exists
|
|
93
|
+
if not parsed.hostname:
|
|
94
|
+
raise ValueError("URL must include a hostname")
|
|
95
|
+
|
|
96
|
+
# Check for malicious patterns
|
|
97
|
+
if ".." in url or "@" in url:
|
|
98
|
+
raise ValueError("URL contains suspicious characters")
|
|
99
|
+
|
|
100
|
+
# Prevent URL with credentials (http://user:pass@host)
|
|
101
|
+
if parsed.username or parsed.password:
|
|
102
|
+
raise ValueError("URLs with embedded credentials are not allowed")
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
"scheme": parsed.scheme,
|
|
106
|
+
"hostname": parsed.hostname,
|
|
107
|
+
"port": parsed.port,
|
|
108
|
+
"url": url,
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
except Exception as e:
|
|
112
|
+
raise ValueError(f"Invalid URL format: {e}")
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def check_domain_whitelist(hostname: str) -> bool:
|
|
116
|
+
"""
|
|
117
|
+
Solution 2: Domain whitelist validation.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
hostname: The hostname to check
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
True if domain is whitelisted
|
|
124
|
+
|
|
125
|
+
Raises:
|
|
126
|
+
ValueError: If domain is not whitelisted and enforcement is enabled
|
|
127
|
+
"""
|
|
128
|
+
is_whitelisted = hostname in ALLOWED_DOMAINS
|
|
129
|
+
|
|
130
|
+
if not is_whitelisted:
|
|
131
|
+
if security_config.enforce_whitelist:
|
|
132
|
+
# Hard block in strict mode
|
|
133
|
+
raise ValueError(
|
|
134
|
+
f"Domain '{hostname}' is not in the whitelist.\n"
|
|
135
|
+
f"Allowed domains: {', '.join(ALLOWED_DOMAINS)}\n\n"
|
|
136
|
+
f"If you're using a self-hosted Xenfra instance:\n"
|
|
137
|
+
f"1. Set XENFRA_ENFORCE_WHITELIST=false\n"
|
|
138
|
+
f"2. Or contact support to whitelist your domain"
|
|
139
|
+
)
|
|
140
|
+
else:
|
|
141
|
+
# Soft warning in permissive mode
|
|
142
|
+
console.print(
|
|
143
|
+
f"[yellow]⚠️ Warning: Domain '{hostname}' is not in the official whitelist.[/yellow]"
|
|
144
|
+
)
|
|
145
|
+
console.print(f"[dim]Whitelisted domains: {', '.join(ALLOWED_DOMAINS)}[/dim]")
|
|
146
|
+
console.print("[yellow]Are you sure you want to connect to this API?[/yellow]")
|
|
147
|
+
|
|
148
|
+
if not click.confirm("Continue?", default=False):
|
|
149
|
+
raise click.Abort()
|
|
150
|
+
|
|
151
|
+
return is_whitelisted
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def enforce_https(scheme: str, hostname: str) -> None:
|
|
155
|
+
"""
|
|
156
|
+
Solution 3: HTTPS enforcement.
|
|
157
|
+
|
|
158
|
+
Args:
|
|
159
|
+
scheme: URL scheme (http/https)
|
|
160
|
+
hostname: The hostname
|
|
161
|
+
|
|
162
|
+
Raises:
|
|
163
|
+
ValueError: If HTTPS is required but HTTP is used
|
|
164
|
+
"""
|
|
165
|
+
# Development exception: localhost is OK with HTTP
|
|
166
|
+
is_localhost = hostname in ["localhost", "127.0.0.1"]
|
|
167
|
+
|
|
168
|
+
if scheme == "http" and not is_localhost:
|
|
169
|
+
if security_config.enforce_https:
|
|
170
|
+
# Hard block in production/strict mode
|
|
171
|
+
raise ValueError(
|
|
172
|
+
f"HTTPS is required (environment: {security_config.environment}).\n"
|
|
173
|
+
f"Current URL uses insecure HTTP.\n\n"
|
|
174
|
+
f"To fix:\n"
|
|
175
|
+
f"1. Update XENFRA_API_URL to use https://\n"
|
|
176
|
+
f"2. Or set XENFRA_ENFORCE_HTTPS=false (not recommended)"
|
|
177
|
+
)
|
|
178
|
+
elif security_config.warn_on_http:
|
|
179
|
+
# Soft warning
|
|
180
|
+
console.print("[bold yellow]⚠️ Security Warning: Using unencrypted HTTP![/bold yellow]")
|
|
181
|
+
console.print(f"[yellow]Connecting to: {scheme}://{hostname}[/yellow]")
|
|
182
|
+
console.print("[yellow]Your credentials and data will be sent in plain text.[/yellow]")
|
|
183
|
+
console.print("[yellow]This should ONLY be used for local development.[/yellow]\n")
|
|
184
|
+
|
|
185
|
+
if not click.confirm("Continue with insecure connection?", default=False):
|
|
186
|
+
raise click.Abort()
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def create_secure_client(url: str, token: str = None) -> httpx.Client:
|
|
190
|
+
"""
|
|
191
|
+
Solution 4: Create HTTP client with optional certificate pinning.
|
|
192
|
+
|
|
193
|
+
Args:
|
|
194
|
+
url: The base URL
|
|
195
|
+
token: Optional authentication token
|
|
196
|
+
|
|
197
|
+
Returns:
|
|
198
|
+
Configured httpx.Client with security settings
|
|
199
|
+
"""
|
|
200
|
+
parsed = urlparse(url)
|
|
201
|
+
headers = {"Content-Type": "application/json"}
|
|
202
|
+
|
|
203
|
+
if token:
|
|
204
|
+
headers["Authorization"] = f"Bearer {token}"
|
|
205
|
+
|
|
206
|
+
# Certificate pinning for production domains (if enabled)
|
|
207
|
+
if security_config.enable_cert_pinning and parsed.hostname in PINNED_CERTIFICATES:
|
|
208
|
+
console.print(f"[dim]Enabling certificate pinning for {parsed.hostname}[/dim]")
|
|
209
|
+
|
|
210
|
+
# Create SSL context with certificate verification
|
|
211
|
+
ssl_context = ssl.create_default_context(cafile=certifi.where())
|
|
212
|
+
ssl_context.check_hostname = True
|
|
213
|
+
ssl_context.verify_mode = ssl.CERT_REQUIRED
|
|
214
|
+
|
|
215
|
+
# Note: Full certificate pinning implementation would require custom verification
|
|
216
|
+
# For now, we use strict certificate validation with system CA bundle
|
|
217
|
+
# Future enhancement: Certificate fingerprint pinning (see docs/future-enhancements.md #3)
|
|
218
|
+
|
|
219
|
+
return httpx.Client(
|
|
220
|
+
base_url=url,
|
|
221
|
+
headers=headers,
|
|
222
|
+
verify=ssl_context,
|
|
223
|
+
timeout=30.0,
|
|
224
|
+
)
|
|
225
|
+
else:
|
|
226
|
+
# Standard client with default certificate verification
|
|
227
|
+
return httpx.Client(
|
|
228
|
+
base_url=url,
|
|
229
|
+
headers=headers,
|
|
230
|
+
timeout=30.0,
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def validate_and_get_api_url(url: str = None) -> str:
|
|
235
|
+
"""
|
|
236
|
+
Comprehensive API URL validation (combines all 4 solutions).
|
|
237
|
+
|
|
238
|
+
Args:
|
|
239
|
+
url: Optional URL override (only for self-hosted instances)
|
|
240
|
+
|
|
241
|
+
Returns:
|
|
242
|
+
Validated API URL (defaults to https://api.xenfra.tech)
|
|
243
|
+
|
|
244
|
+
Raises:
|
|
245
|
+
ValueError: If URL fails validation
|
|
246
|
+
click.Abort: If user cancels security prompts
|
|
247
|
+
"""
|
|
248
|
+
# PRODUCTION DEFAULT: Use hardcoded production URL
|
|
249
|
+
# Only check environment variable for self-hosted overrides
|
|
250
|
+
if url is None:
|
|
251
|
+
url = os.getenv("XENFRA_API_URL", PRODUCTION_API_URL)
|
|
252
|
+
|
|
253
|
+
try:
|
|
254
|
+
# Solution 1: Validate URL format
|
|
255
|
+
parsed = validate_url_format(url)
|
|
256
|
+
|
|
257
|
+
# Solution 2: Check domain whitelist
|
|
258
|
+
check_domain_whitelist(parsed["hostname"])
|
|
259
|
+
|
|
260
|
+
# Solution 3: Enforce HTTPS
|
|
261
|
+
enforce_https(parsed["scheme"], parsed["hostname"])
|
|
262
|
+
|
|
263
|
+
# Display security info ONLY in debug mode
|
|
264
|
+
# Normal users shouldn't see this
|
|
265
|
+
if os.getenv("DEBUG") or os.getenv("XENFRA_DEBUG"):
|
|
266
|
+
console.print(f"[dim]🔒 Security: API URL validated: {url}[/dim]")
|
|
267
|
+
console.print(f"[dim] Environment: {security_config.environment}[/dim]")
|
|
268
|
+
console.print(f"[dim] HTTPS enforced: {security_config.enforce_https}[/dim]")
|
|
269
|
+
console.print(f"[dim] Whitelist enforced: {security_config.enforce_whitelist}[/dim]")
|
|
270
|
+
console.print(f"[dim] Cert pinning: {security_config.enable_cert_pinning}[/dim]")
|
|
271
|
+
|
|
272
|
+
return url
|
|
273
|
+
|
|
274
|
+
except ValueError as e:
|
|
275
|
+
console.print("[bold red]🔒 Security Validation Failed:[/bold red]")
|
|
276
|
+
console.print(f"[red]{e}[/red]")
|
|
277
|
+
raise click.Abort()
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def display_security_info():
|
|
281
|
+
"""Display current security configuration."""
|
|
282
|
+
console.print("\n[bold cyan]🔒 Security Configuration:[/bold cyan]")
|
|
283
|
+
|
|
284
|
+
table_data = [
|
|
285
|
+
("Environment", security_config.environment),
|
|
286
|
+
("HTTPS Enforcement", "✅ Enabled" if security_config.enforce_https else "⚠️ Disabled"),
|
|
287
|
+
(
|
|
288
|
+
"Domain Whitelist",
|
|
289
|
+
"✅ Enforced" if security_config.enforce_whitelist else "⚠️ Warning Only",
|
|
290
|
+
),
|
|
291
|
+
("HTTP Warning", "✅ Enabled" if security_config.warn_on_http else "❌ Disabled"),
|
|
292
|
+
(
|
|
293
|
+
"Certificate Pinning",
|
|
294
|
+
"✅ Enabled" if security_config.enable_cert_pinning else "❌ Disabled",
|
|
295
|
+
),
|
|
296
|
+
]
|
|
297
|
+
|
|
298
|
+
for key, value in table_data:
|
|
299
|
+
console.print(f" {key}: {value}")
|
|
300
|
+
|
|
301
|
+
console.print()
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
# Environment variable documentation
|
|
305
|
+
"""
|
|
306
|
+
PRODUCTION-FIRST DESIGN:
|
|
307
|
+
The CLI defaults to production (api.xenfra.tech) with HTTPS enforcement.
|
|
308
|
+
No configuration needed for normal users.
|
|
309
|
+
|
|
310
|
+
Environment variables (for developers/self-hosted only):
|
|
311
|
+
|
|
312
|
+
XENFRA_ENV=development
|
|
313
|
+
- Enables local development mode
|
|
314
|
+
- Allows HTTP, relaxes security
|
|
315
|
+
- Default: production (safe by default)
|
|
316
|
+
|
|
317
|
+
XENFRA_API_URL=https://your-instance.com
|
|
318
|
+
- Override API URL for self-hosted instances
|
|
319
|
+
- Default: https://api.xenfra.tech
|
|
320
|
+
|
|
321
|
+
XENFRA_ENFORCE_HTTPS=true|false
|
|
322
|
+
- Require HTTPS for all connections
|
|
323
|
+
- Default: true (production), false (development)
|
|
324
|
+
|
|
325
|
+
Example usage:
|
|
326
|
+
|
|
327
|
+
# Production users (zero config):
|
|
328
|
+
xenfra auth login
|
|
329
|
+
xenfra deploy
|
|
330
|
+
|
|
331
|
+
# Local development:
|
|
332
|
+
XENFRA_ENV=development xenfra auth login
|
|
333
|
+
|
|
334
|
+
# Self-hosted instance:
|
|
335
|
+
XENFRA_API_URL=https://xenfra.mycompany.com xenfra login
|
|
336
|
+
"""
|