-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_server.py
234 lines (174 loc) · 8.68 KB
/
object_server.py
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
import gzip
import hashlib
import os
import time
import zlib
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qs
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from identity_layer import IdentityLayer
from object_server_cluster import ObjectServerCluster
from storage_backend import StorageBackend
class NetworkError(Exception):
pass
class StorageError(Exception):
pass
class ObjectServer(BaseHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.identity_layer = IdentityLayer('kriya.db')
self.object_server_cluster = ObjectServerCluster()
self.storage_backend = StorageBackend()
def do_GET(self):
parsed_url = urlparse(self.path)
query_params = parse_qs(parsed_url.query)
# validate request according to S3 protocol
if 'X-Amz-Content-Sha256' not in self.headers:
self.send_error(400, 'Bad Request', 'Missing required header: X-Amz-Content-Sha256')
return
# extract object key from request
object_key = parsed_url.path.lstrip('/')
# perform read operation on object
object_data = self.storage_backend.read_object(object_key)
# decrypt the object data using AES-256 with the encryption key
encryption_key = self.storage_backend.read_metadata(object_key, 'encryption_key')
cipher = AES.new(encryption_key, AES.MODE_CBC)
object_data = unpad(cipher.decrypt(object_data), AES.block_size)
# decompress the object data using GZIP
compressed_object_data = self.storage_backend.read_metadata(object_key, 'compressed_data')
object_data = gzip.decompress(compressed_object_data)
# verify the checksum of the object data using CRC32
checksum = zlib.crc32(object_data)
stored_checksum = self.storage_backend.read_metadata(object_key, 'checksum')
if checksum != stored_checksum:
self.send_error(500, 'Internal Server Error', 'Checksum verification failed.')
return
# return object data to client
self.send_response(200)
self.send_header('Content-Type', 'application/octet-stream')
self.send_header('Content-Length', str(len(object_data)))
self.end_headers()
self.wfile.write(object_data)
def do_PUT(self):
parsed_url = urlparse(self.path)
query_params = parse_qs(parsed_url.query)
# validate request according to S3 protocol
if 'X-Amz-Content-Sha256' not in self.headers:
self.send_error(400, 'Bad Request', 'Missing required header: X-Amz-Content-Sha256')
return
# extract object key from request
object_key = parsed_url.path.lstrip('/')
# extract access key and secret key from request headers
access_key = self.headers.get('X-Amz-Access-Key')
secret_key = self.headers.get('X-Amz-Secret-Key')
# verify access key and secret key using identity layer
if not self.identity_layer.verify_access_key(access_key, secret_key):
self.send_error(403, 'Forbidden', 'Invalid access key or secret key.')
return
# read object data from request body
object_data = self.rfile.read(int(self.headers['Content-Length']))
# retry write operation if it fails due to network or storage errors
max_retries = 3
retry_count = 0
while retry_count < max_retries:
try:
# generate a random encryption key for each object
encryption_key = os.urandom(32)
# encrypt the object data using AES-256 with the encryption key
cipher = AES.new(encryption_key, AES.MODE_CBC)
object_data = cipher.encrypt(pad(object_data, AES.block_size))
# compress the object data using GZIP
compressed_object_data = gzip.compress(object_data)
# store the compressed object data as metadata of the object
self.storage_backend.write_metadata(object_key, 'compressed_data', compressed_object_data)
# store the encrypted object data and the encryption key as metadata of the object
self.storage_backend.write_object(object_key, object_data)
self.storage_backend.write_metadata(object_key, 'encryption_key', encryption_key)
# compute the checksum of the object data using CRC32
checksum = zlib.crc32(object_data)
# store the checksum as metadata of the object
self.storage_backend.write_metadata(object_key, 'checksum', checksum)
# return success response to client
self.send_response(200)
self.end_headers()
# hash object data using SHA-256
object_hash = hashlib.sha256(object_data).hexdigest()
# store hash value as metadata of object
self.storage_backend.write_metadata(object_key, 'hash', object_hash)
# replicate object to other object servers
self.object_server_cluster.replicate_object(object_key, object_data)
# rebalance objects among object servers
self.object_server_cluster.rebalance_objects()
return
except (NetworkError, StorageError) as e:
# log error and retry after a delay
print(f"Error writing object: {e}. Retrying in 5 seconds...")
time.sleep(5)
retry_count += 1
# if all retries fail, return error response to client
self.send_error(500, 'Internal Server Error', 'Failed to write object after multiple retries.')
def do_DELETE(self):
parsed_url = urlparse(self.path)
query_params = parse_qs(parsed_url.query)
# validate request according to S3 protocol
if 'X-Amz-Content-Sha256' not in self.headers:
self.send_error(400, 'Bad Request', 'Missing required header: X-Amz-Content-Sha256')
return
# extract object key from request
object_key = parsed_url.path.lstrip('/')
# perform delete operation on object
self.storage_backend.delete_object(object_key)
# return success response to client
self.send_response(204)
self.end_headers()
# delete object from other object servers
self.object_server_cluster.delete_object(object_key)
def do_HEAD(self):
parsed_url = urlparse(self.path)
query_params = parse_qs(parsed_url.query)
# validate request according to S3 protocol
if 'X-Amz-Content-Sha256' not in self.headers:
self.send_error(400, 'Bad Request', 'Missing required header: X-Amz-Content-Sha256')
return
# extract object key from request
object_key = parsed_url.path.lstrip('/')
# check if object exists
if not self.storage_backend.object_exists(object_key):
self.send_error(404, 'Not Found', 'The specified key does not exist.')
return
# return success response to client
self.send_response(200)
self.send_header('Content-Type', 'application/octet-stream')
self.send_header('Content-Length', str(self.storage_backend.get_object_size(object_key)))
self.end_headers()
def do_POST(self):
parsed_url = urlparse(self.path)
query_params = parse_qs(parsed_url.query)
# validate request according to S3 protocol
if 'X-Amz-Content-Sha256' not in self.headers:
self.send_error(400, 'Bad Request', 'Missing required header: X-Amz-Content-Sha256')
return
# extract object key from request
object_key = parsed_url.path.lstrip('/')
# extract access key and secret key from request headers
access_key = self.headers.get('X-Amz-Access-Key')
secret_key = self.headers.get('X-Amz-Secret-Key')
# verify access key and secret key using identity layer
if not self.identity_layer.verify_access_key(access_key, secret_key):
self.send_error(403, 'Forbidden', 'Invalid access key or secret key.')
return
# perform create operation on object
self.storage_backend.create_object(object_key)
# return success response to client
self.send_response(200)
self.end_headers()
# rebalance objects among object servers
self.object_server_cluster.rebalance_objects()
def main():
# create object server instance
object_server = HTTPServer(('localhost', 8080), ObjectServer)
# start object server
object_server.serve_forever()
if __name__ == '__main__':
main()