-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.py
More file actions
233 lines (191 loc) · 8.14 KB
/
proxy.py
File metadata and controls
233 lines (191 loc) · 8.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
"""
CipherGate - High-Performance Security Proxy with Zero-Trust Architecture
This module implements the main proxy server that intercepts and secures
data in transit using Zero-Trust principles.
"""
import asyncio
import logging
import time
from typing import Dict, Any, Optional, List
from fastapi import FastAPI, Request, HTTPException, Depends, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from pydantic import BaseModel, validator
import uvicorn
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import JSONResponse
from crypto_vault import CryptoVault
from masking_engine import MaskingEngine
from compliance_auditor import ComplianceAuditor
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Security configuration
security = HTTPBearer(auto_error=False)
class SecurityProxy:
"""Main security proxy implementing Zero-Trust Architecture"""
def __init__(self):
self.crypto_vault = CryptoVault()
self.masking_engine = MaskingEngine()
self.auditor = ComplianceAuditor()
self.app = self._create_app()
def _create_app(self) -> FastAPI:
"""Create and configure the FastAPI application"""
app = FastAPI(
title="CipherGate Security Proxy",
description="High-performance security proxy implementing Zero-Trust Architecture",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc"
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Configure based on your needs
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Add security middleware
app.add_middleware(SecurityMiddleware, proxy=self)
# Health check endpoint
@app.get("/health")
async def health_check():
return {"status": "healthy", "timestamp": time.time()}
# Main proxy endpoint
@app.post("/api/proxy/{service_path:path}")
async def proxy_endpoint(
service_path: str,
request: Request,
credentials: Optional[HTTPAuthorizationCredentials] = Depends(security)
):
return await self.handle_request(service_path, request, credentials)
return app
async def handle_request(
self,
service_path: str,
request: Request,
credentials: Optional[HTTPAuthorizationCredentials]
) -> Dict[str, Any]:
"""Handle incoming requests with Zero-Trust validation"""
start_time = time.time()
try:
# 1. Verify Explicitly - Authenticate and authorize
user_context = await self._verify_user(credentials)
# 2. Validate request schema
raw_payload = await request.json()
validated_payload = self._validate_payload(raw_payload)
# 3. Log access attempt
self.auditor.log_access_attempt(
user_id=user_context.get('user_id', 'anonymous'),
service_path=service_path,
action='proxy_request',
payload_size=len(str(raw_payload))
)
# 4. Apply data masking based on user role
masked_payload = self.masking_engine.apply_masking(
validated_payload,
user_context.get('role', 'guest')
)
# Ensure masked_payload is a dict for encryption
if not isinstance(masked_payload, dict):
masked_payload = {"data": masked_payload}
# 5. Encrypt sensitive data
encrypted_payload = self.crypto_vault.encrypt_payload(masked_payload)
# 6. Process the request (simulated service call)
response = await self._process_service_request(service_path, encrypted_payload)
# 7. Decrypt and mask response
decrypted_response = self.crypto_vault.decrypt_payload(response)
masked_response = self.masking_engine.apply_masking(
decrypted_response,
user_context.get('role', 'guest')
)
# Ensure final response is a dict
if not isinstance(masked_response, dict):
final_response = {"data": masked_response}
else:
final_response = masked_response
# 8. Log successful completion
self.auditor.log_access_completion(
user_id=user_context.get('user_id', 'anonymous'),
service_path=service_path,
duration=time.time() - start_time,
success=True,
session_id=None
)
return final_response
except Exception as e:
logger.error(f"Request processing failed: {str(e)}")
self.auditor.log_access_completion(
user_id='unknown',
service_path=service_path,
duration=time.time() - start_time,
success=False,
session_id=None,
error_message=str(e)
)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Request processing failed"
)
async def _verify_user(self, credentials: Optional[HTTPAuthorizationCredentials]) -> Dict[str, Any]:
"""Verify user authentication and extract context"""
if not credentials:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Authentication required"
)
# In a real implementation, this would validate against an auth service
token = credentials.credentials
user_context = self.crypto_vault.validate_token(token)
if not user_context:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication token"
)
return user_context
def _validate_payload(self, payload: Dict[str, Any]) -> Dict[str, Any]:
"""Validate incoming JSON payload schema"""
# Basic schema validation
if not isinstance(payload, dict):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid payload format"
)
# Add more sophisticated schema validation as needed
return payload
async def _process_service_request(self, service_path: str, payload: Dict[str, Any]) -> Dict[str, Any]:
"""Simulate processing the request with the target service"""
# In a real implementation, this would make HTTP requests to the actual service
await asyncio.sleep(0.1) # Simulate network delay
return {
"status": "success",
"service": service_path,
"processed_at": time.time(),
"data": payload
}
class SecurityMiddleware(BaseHTTPMiddleware):
"""Security middleware implementing Zero-Trust principles"""
def __init__(self, app, proxy: SecurityProxy):
super().__init__(app)
self.proxy = proxy
async def dispatch(self, request: Request, call_next):
# Add security headers
response = await call_next(request)
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["X-Frame-Options"] = "DENY"
response.headers["X-XSS-Protection"] = "1; mode=block"
response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
return response
if __name__ == "__main__":
proxy = SecurityProxy()
uvicorn.run(
proxy.app,
host="0.0.0.0",
port=8000,
log_level="info",
access_log=True
)