-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·183 lines (157 loc) · 5.78 KB
/
Copy pathserver.py
File metadata and controls
executable file
·183 lines (157 loc) · 5.78 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
#!/usr/bin/env python3
import ssl
import json
from datetime import datetime
from http.server import HTTPServer, BaseHTTPRequestHandler
from pathlib import Path
# Certificate paths
CERT_DIR = Path(__file__).parent.parent.parent / "certs"
SERVER_CERT = CERT_DIR / "servers" / "localhost" / "server-cert.pem"
SERVER_KEY = CERT_DIR / "servers" / "localhost" / "server-key.pem"
CA_CERT = CERT_DIR / "ca" / "ca-cert.pem"
class MTLSRequestHandler(BaseHTTPRequestHandler):
def log_message(self, format, *args):
"""Override to add custom logging"""
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {format % args}")
def get_client_cert_info(self):
"""Extract client certificate information"""
try:
cert = self.connection.getpeercert()
if cert:
subject = dict(x[0] for x in cert.get('subject', []))
return {
'cn': subject.get('commonName', 'Unknown'),
'organization': subject.get('organizationName', 'N/A'),
'verified': True
}
except Exception as e:
print(f"Error getting client cert: {e}")
return {
'cn': 'Unknown',
'organization': 'N/A',
'verified': False
}
def send_json_response(self, status_code, data):
"""Send JSON response"""
self.send_response(status_code)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(data, indent=2).encode())
def send_text_response(self, status_code, text):
"""Send plain text response"""
self.send_response(status_code)
self.send_header('Content-Type', 'text/plain')
self.end_headers()
self.wfile.write(text.encode())
def do_GET(self):
"""Handle GET requests"""
if self.path == '/':
self.handle_root()
elif self.path == '/health':
self.handle_health()
elif self.path == '/api/data':
self.handle_data()
else:
self.send_json_response(404, {'error': 'Not found'})
def do_POST(self):
"""Handle POST requests"""
if self.path == '/api/echo':
self.handle_echo()
else:
self.send_json_response(404, {'error': 'Not found'})
def handle_root(self):
"""Main endpoint with client info"""
client_info = self.get_client_cert_info()
response = {
'status': 'success',
'message': 'mTLS Python Server',
'client_cert': client_info['cn'],
'server_time': datetime.now().isoformat(),
'verified': client_info['verified']
}
self.send_json_response(200, response)
def handle_health(self):
"""Health check endpoint"""
self.send_text_response(200, 'OK')
def handle_data(self):
"""API data endpoint"""
import platform
import sys
client_info = self.get_client_cert_info()
data = {
'timestamp': datetime.now().isoformat(),
'client': client_info,
'server': {
'name': 'python-mtls-server',
'version': '1.0.0',
'platform': platform.system(),
'python_version': sys.version.split()[0]
},
'data': {
'users': 42,
'requests': 1337,
'status': 'healthy'
}
}
self.send_json_response(200, data)
def handle_echo(self):
"""Echo endpoint"""
try:
content_length = int(self.headers.get('Content-Length', 0))
body = self.rfile.read(content_length)
received_data = json.loads(body.decode())
client_info = self.get_client_cert_info()
response = {
'echo': received_data,
'metadata': {
'received_at': datetime.now().isoformat(),
'client_cn': client_info['cn'],
'content_length': content_length
}
}
self.send_json_response(200, response)
except json.JSONDecodeError:
self.send_json_response(400, {'error': 'Invalid JSON'})
except Exception as e:
self.send_json_response(500, {'error': str(e)})
def create_ssl_context():
"""Create SSL context with mTLS configuration"""
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.minimum_version = ssl.TLSVersion.TLSv1_2
# Load server certificate and key
context.load_cert_chain(
certfile=str(SERVER_CERT),
keyfile=str(SERVER_KEY)
)
# Load CA certificate for client verification
context.load_verify_locations(cafile=str(CA_CERT))
# Require and verify client certificate
context.verify_mode = ssl.CERT_REQUIRED
return context
def main():
host = 'localhost'
port = 8443
# Create HTTP server
server = HTTPServer((host, port), MTLSRequestHandler)
# Wrap with SSL
server.socket = create_ssl_context().wrap_socket(
server.socket,
server_side=True
)
print('🔒 mTLS Python Server')
print('=====================')
print(f'Server running on https://{host}:{port}')
print('\nEndpoints:')
print(' GET / - Main endpoint with client info')
print(' GET /health - Health check')
print(' GET /api/data - Sample data endpoint')
print(' POST /api/echo - Echo endpoint')
print('\nPress Ctrl+C to stop')
try:
server.serve_forever()
except KeyboardInterrupt:
print('\n\nShutting down gracefully...')
server.shutdown()
print('Server closed')
if __name__ == '__main__':
main()