-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
279 lines (211 loc) · 8.22 KB
/
app.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
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
import hashlib
import io
import json
import os
import re
from datetime import datetime
from io import BytesIO
import boto3
import botocore
from flask import Flask, redirect, request, jsonify
from flask.views import MethodView
from flask_httpauth import HTTPBasicAuth
from lupa import LuaRuntime
from werkzeug.utils import cached_property
app = Flask(__name__)
auth = HTTPBasicAuth()
S3_URL = os.environ.get("S3_URL")
S3_ROCKS_FOLDER = os.environ.get("S3_ROCKS_FOLDER", '')
S3_AUDIT_FOLDER = os.environ.get("S3_AUDIT_FOLDER", S3_ROCKS_FOLDER)
S3_ACCESS_KEY = os.environ.get("S3_ACCESS_KEY")
S3_SECRET_KEY = os.environ.get("S3_SECRET_KEY")
S3_REGION = os.environ.get("S3_REGION")
ROCKS_UPLOAD_BUCKET = os.environ.get("ROCKS_UPLOAD_BUCKET")
USER = os.environ.get("USERNAME")
PASSWORD = os.environ.get("PASSWORD")
PORT = os.environ.get("PORT", 5000)
TARANTOOL_IO_REDIRECT_URL = "https://www.tarantool.io/en/download/rocks"
MANIFEST_TARGETS = ['manifest-5.1']
MANIFEST = 'manifest'
MANIFEST_SCRIPT = 'make_manifest.lua'
supported_files_pattern = re.compile(r'.*(.rockspec|.src.rock|.all.rock)$')
def md5(f_obj):
hash_md5 = hashlib.md5()
f_obj.seek(0)
for chunk in iter(lambda: f_obj.read(4096), b""):
hash_md5.update(chunk)
f_obj.seek(0)
return hash_md5.hexdigest()
def int2byte(x):
return bytes((x,))
_text_characters = (
b''.join(bytes((i,)) for i in range(32, 127)) +
b'\n\r\t\f\b')
def istextfile(fileobj, blocksize=512):
""" Uses heuristics to guess whether the given file is text or binary,
by reading a single block of bytes from the file.
If more than 30% of the chars in the block are non-text, or there
are NUL ('\x00') bytes in the block, assume this is a binary file.
"""
block = fileobj.read(blocksize)
if b'\x00' in block:
# Files with null bytes are binary
return False
elif not block:
# An empty file is considered a valid text file
return True
# Use translate's 'deletechars' argument to efficiently remove all
# occurrences of _text_characters from the block
nontext = block.translate(None, _text_characters)
return float(len(nontext)) / len(block) <= 0.30
class InvalidUsage(RuntimeError):
status_code = 400
def __init__(self, message, status_code=None):
RuntimeError.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
def to_dict(self):
rv = {'message': self.message}
return rv
def __str__(self):
return self.message
@app.errorhandler(InvalidUsage)
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response
def response_message(message, status=201):
response = jsonify({'message': message})
response.status_code = status
return response
@auth.verify_password
def verify_password(user, password):
return USER == user and PASSWORD == password
lua = LuaRuntime(unpack_returned_tuples=True)
with open(MANIFEST_SCRIPT, 'r') as file:
patch_manifest_script = file.read()
patch_manifest_func = lua.eval(patch_manifest_script)
def patch_manifest(manifest: str, filename: str, rock_content: str = '', action: str = 'add') -> tuple:
return patch_manifest_func(manifest, filename, rock_content, action)
def file_name_is_valid(name):
if supported_files_pattern.match(name):
error = None
else:
error = f'File with name {name} is not supported. Rocks ' \
f'server can serve .rockspec, .src.rock and .all.rock' \
f' files only'
return error
class S3View(MethodView):
bucket = ROCKS_UPLOAD_BUCKET
expires_in = 24 * 60 * 60
@cached_property
def client(self):
return boto3.client(
's3',
endpoint_url=S3_URL,
aws_access_key_id=S3_ACCESS_KEY,
aws_secret_access_key=S3_SECRET_KEY,
region_name=S3_REGION
)
def presign_get(self, filename):
return self.client.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': self.bucket,
'Key': f'{S3_ROCKS_FOLDER}{filename}',
},
ExpiresIn=self.expires_in
)
@auth.login_required
def put(self):
manifest = self.download_manifest()
file = request.files.get('rockspec')
if not file:
msg = 'package file was not found in request data'
self.audit_log(msg)
raise InvalidUsage(msg)
file_name = file.filename
error = file_name_is_valid(file_name)
if error:
self.audit_log(error)
raise InvalidUsage(error)
is_text = istextfile(file)
file.seek(0)
package = file.read()
rockspec = package if is_text else ''
message, patched_manifest = patch_manifest(manifest, file_name,
rock_content=rockspec, action='add')
if patched_manifest:
self.upload_fileobj(BytesIO(package), file_name,
f'put {file_name} - {message}')
self.upload_fileobj(BytesIO(str.encode(patched_manifest)),
'manifest', 'update manifest')
else:
self.audit_log(f'manifest update error: {message}')
raise InvalidUsage(message)
return response_message(message)
def upload_fileobj(self, file_obj, file_path, message):
err = None
md5_hash = md5(file_obj)
try:
self.client.upload_fileobj(file_obj, self.bucket, f'{S3_ROCKS_FOLDER}{file_path}')
except Exception as e:
err = str(e)
self.audit_log(f'Upload failure: {err} {message}' if err else message, md5_hash)
def get(self, path='/'):
if path == '/':
return redirect(TARANTOOL_IO_REDIRECT_URL, code=301)
if not self.client:
return 'Server config does not exist'
path = path.strip('/')
if path in MANIFEST_TARGETS:
path = MANIFEST
url = self.presign_get(path)
return redirect(url)
def object_exists(self, filename, folder=None):
if filename == '':
return False
if folder is None:
folder = S3_ROCKS_FOLDER
try:
obj = self.client.get_object(
Bucket=self.bucket,
Key=f'{folder}{filename}'
)
except botocore.exceptions.ClientError as ex:
if ex.response['Error']['Code'] == 'NoSuchKey':
return False
else:
raise ex
headers = obj['ResponseMetadata']['HTTPHeaders']
if 'content-type' not in headers:
return False
return True
def download_manifest(self):
if not self.object_exists('manifest'):
raise InvalidUsage('manifest file was not found in the bucket')
manifest_io = BytesIO()
self.client.download_fileobj(self.bucket, f'{S3_ROCKS_FOLDER}manifest', manifest_io)
return manifest_io.getvalue().decode('utf-8')
def audit_log(self, event: str, md5_hash=''):
if md5_hash:
md5_hash = f' md5hash: {md5_hash} |'
log_data = f'{datetime.now()} | {event} |{md5_hash} ' \
f'{request.remote_addr} | {json.dumps(dict(request.headers))}\n'
audit_file_name = f'{datetime.today().strftime("%y-%m")}.log'
audit_file = BytesIO()
exists = self.object_exists(audit_file_name, S3_AUDIT_FOLDER)
if exists:
self.client.download_fileobj(self.bucket, f'{S3_AUDIT_FOLDER}{audit_file_name}', audit_file)
initial_size = audit_file.getbuffer().nbytes
audit_file.seek(0, io.SEEK_END)
audit_file.write(log_data.encode())
audit_file.seek(0)
if not exists or (exists and audit_file.getbuffer().nbytes > initial_size):
self.client.upload_fileobj(audit_file, self.bucket, f'{S3_AUDIT_FOLDER}{audit_file_name}')
s3_view = S3View.as_view('s3_view')
app.add_url_rule('/<path>', view_func=s3_view, methods=['GET'])
app.add_url_rule('/', view_func=s3_view, methods=['GET', 'PUT'])
if __name__ == '__main__':
app.run(port=PORT)