-
Notifications
You must be signed in to change notification settings - Fork 4
/
grey_regular.py
297 lines (210 loc) · 10.2 KB
/
grey_regular.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
"""
BASICS
Returns user files or information about them
"""
from flask import Flask, request, jsonify, send_file, after_this_request
import os, sys, shutil
import tarfile
from werkzeug.utils import secure_filename
import base_functions as bf
import checksums as ch
app = Flask(__name__)
GREYFISH_FOLDER = os.environ['greyfish_path']+"/sandbox/"
CURDIR = dir_path = os.path.dirname(os.path.realpath(__file__)) # Current directory
# Checks if reef cloud storage is available
@app.route('/grey/status')
def api_operational():
return 'External Greyfish cloud storage is available'
# Returns a json object of all the files obtained from the user
@app.route('/grey/all_user_files/<gkey>/<toktok>')
def all_user_files(toktok, gkey):
IP_addr = request.environ['REMOTE_ADDR']
if not bf.valid_key(gkey, toktok):
bf.failed_login(gkey, IP_addr, toktok, "json-all-user-files")
return "INVALID key"
if str('DIR_'+toktok) not in os.listdir(GREYFISH_FOLDER):
return 'INVALID, User directory does not exist'
bf.greyfish_log(IP_addr, toktok, "json summary", "all files")
return jsonify(bf.structure_in_json(GREYFISH_FOLDER+'DIR_'+toktok))
# Returns the contents of an entire directory
@app.route('/grey/user_files/<gkey>/<toktok>/<DIR>')
def user_files(toktok, gkey, DIR=''):
IP_addr = request.environ['REMOTE_ADDR']
if not bf.valid_key(gkey, toktok):
bf.failed_login(gkey, IP_addr, toktok, "json-user-dir")
return "INVALID key"
if str('DIR_'+toktok) not in os.listdir(GREYFISH_FOLDER):
return 'INVALID, User directory does not exist'
# Accounts for users without a sandbox yet
try:
bf.greyfish_log(IP_addr, toktok, "json summary", "single dir", '/'.join(DIR.split('++')))
return jsonify(bf.structure_in_json(GREYFISH_FOLDER+'DIR_'+toktok+'/'+'/'.join(DIR.split('++'))))
except:
return 'Sandbox not set-up, create a sandbox first'
# Uploads one file
# Directories must be separated by ++
@app.route("/grey/upload/<gkey>/<toktok>/<DIR>", methods=['POST'])
def result_upload(toktok, gkey, DIR=''):
IP_addr = request.environ['REMOTE_ADDR']
if not bf.valid_key(gkey, toktok):
bf.failed_login(gkey, IP_addr, toktok, "upload-file")
return "INVALID key"
if str('DIR_'+toktok) not in os.listdir(GREYFISH_FOLDER):
return 'INVALID, User directory does not exist'
file = request.files['file']
fnam = file.filename
# Avoids empty filenames and those with commas
if fnam == '':
return 'INVALID, no file uploaded'
if ',' in fnam:
return "INVALID, no ',' allowed in filenames"
# Ensures no commands within the filename
new_name = secure_filename(fnam)
if not os.path.exists(GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++'))):
os.makedirs(GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++')))
bf.greyfish_log(IP_addr, toktok, "upload", "single file", '/'.join(DIR.split('++')), new_name)
file.save(os.path.join(GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++')), new_name))
return 'File succesfully uploaded to Greyfish'
# Deletes a file already present in the user
@app.route('/grey/delete_file/<gkey>/<toktok>/<FILE>/<DIR>')
def delete_file(toktok, gkey, FILE, DIR=''):
IP_addr = request.environ['REMOTE_ADDR']
if not bf.valid_key(gkey, toktok):
bf.failed_login(gkey, IP_addr, toktok, "delete-file")
return "INVALID key"
if str('DIR_'+toktok) not in os.listdir(GREYFISH_FOLDER):
return 'INVALID, User directory does not exist'
try:
os.remove(GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++'))+'/'+str(FILE))
bf.greyfish_log(IP_addr, toktok, "delete", "single file", '/'.join(DIR.split('++')), new_nam)
return 'File succesfully deleted from Greyfish storage'
except:
return 'File is not present in Greyfish'
# Deletes a directory
@app.route("/grey/delete_dir/<gkey>/<toktok>/<DIR>")
def delete_dir(toktok, gkey, DIR):
IP_addr = request.environ['REMOTE_ADDR']
if not bf.valid_key(gkey, toktok):
bf.failed_login(gkey, IP_addr, toktok, "delete-dir")
return "INVALID key"
try:
shutil.rmtree(GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++'))+'/')
bf.greyfish_log(IP_addr, toktok, "delete", "single dir", '/'.join(DIR.split('++')))
return "Directory deleted"
except:
return "User directory does not exist"
# Returns a file
@app.route('/grey/grey/<gkey>/<toktok>/<FIL>/<DIR>')
def grey_file(gkey, toktok, FIL, DIR=''):
IP_addr = request.environ['REMOTE_ADDR']
if not bf.valid_key(gkey, toktok):
bf.failed_login(gkey, IP_addr, toktok, "download-file")
return "INVALID key"
if str('DIR_'+toktok) not in os.listdir(GREYFISH_FOLDER):
return 'INVALID, User directory does not exist'
USER_DIR = GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++'))+'/'
if str(FIL) not in os.listdir(USER_DIR):
return 'INVALID, File not available'
bf.greyfish_log(IP_addr, toktok, "download", "single file", '/'.join(DIR.split('++')), FIL)
return send_file(USER_DIR+str(FIL), as_attachment=True)
# Uploads one directory, it the directory already exists, then it deletes it and uploads the new contents
# Must be a tar file
@app.route("/grey/upload_dir/<gkey>/<toktok>/<DIR>", methods=['POST'])
def upload_dir(gkey, toktok, DIR):
IP_addr = request.environ['REMOTE_ADDR']
if not bf.valid_key(gkey, toktok):
bf.failed_login(gkey, IP_addr, toktok, "upload-dir")
return "INVALID key"
if str('DIR_'+toktok) not in os.listdir(GREYFISH_FOLDER):
return 'INVALID, User directory does not exist'
file = request.files['file']
fnam = file.filename
# Avoids empty filenames and those with commas
if fnam == '':
return 'INVALID, no file uploaded'
if ',' in fnam:
return "INVALID, no ',' allowed in filenames"
# Untars the file, makes a directory if it does not exist
if ('.tar.gz' not in fnam) and ('.tgz' not in fnam):
return 'ERROR: Compression file not accepted, file must be .tgz or .tar.gz'
new_name = secure_filename(fnam)
try:
if os.path.exists(GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++'))):
shutil.rmtree(GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++')))
os.makedirs(GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++')))
file.save(os.path.join(GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++')), new_name))
tar = tarfile.open(GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++'))+'/'+new_name)
tar.extractall(GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++')))
tar.close()
os.remove(GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++'))+'/'+new_name)
except:
return "Could not open tar file"
bf.greyfish_log(IP_addr, toktok, "upload", "dir", '/'.join(DIR.split('++')))
return 'Directory succesfully uploaded to Greyfish'
# Downloads a directory
# Equivalent to downloading the tar file, since they are both equivalent
@app.route('/grey/grey_dir/<gkey>/<toktok>/<DIR>')
def grey_dir(gkey, toktok, DIR=''):
IP_addr = request.environ['REMOTE_ADDR']
if not bf.valid_key(gkey, toktok):
bf.failed_login(gkey, IP_addr, toktok, "download-dir")
return "INVALID key"
if str('DIR_'+toktok) not in os.listdir(GREYFISH_FOLDER):
return 'INVALID, User directory does not exist'
USER_DIR = GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++'))+'/'
if not os.path.exists(USER_DIR):
return 'INVALID, Directory not available'
os.chdir(USER_DIR)
tar = tarfile.open("summary.tar.gz", "w:gz")
for ff in os.listdir('.'):
tar.add(ff)
tar.close()
os.chdir(CURDIR)
bf.greyfish_log(IP_addr, toktok, "download", "dir", '/'.join(DIR.split('++')))
return send_file(USER_DIR+"summary.tar.gz")
# Downloads a directory with a checksum as a tar file
# The file is name after the first 8 characters of the checksum
# The tar file is moved onto a temporary directory afterwards, where it can be deleted if the checksum is correct
@app.route('/grey/download_checksum_dir/<gkey>/<toktok>/<DIR>')
def download_checksum_dir(gkey, toktok, DIR=''):
IP_addr = request.environ['REMOTE_ADDR']
if not bf.valid_key(gkey, toktok):
bf.failed_login(gkey, IP_addr, toktok, "download-dir")
return "INVALID key"
if str('DIR_'+toktok) not in os.listdir(GREYFISH_FOLDER):
return 'INVALID, User directory does not exist'
USER_DIR = GREYFISH_FOLDER+'DIR_'+str(toktok)+'/'+'/'.join(DIR.split('++'))+'/'
if not os.path.exists(USER_DIR):
return 'INVALID, Directory not available'
os.chdir(USER_DIR)
tar = tarfile.open("summary.tar.gz", "w:gz")
to_be_tarred = [x for x in os.listdir('.') if x != "summary.tar.gz"]
for ff in to_be_tarred:
tar.add(ff)
os.remove(ff)
tar.close()
checksum8_name = ch.sha256_checksum("summary.tar.gz")[:8]+".tar.gz"
checksum_dir = GREYFISH_FOLDER+'DIR_'+str(toktok)+'/checksum_files/'
# If the temporary directory does not exist, it creates it
if not os.path.isdir(checksum_dir):
os.makedirs(checksum_dir)
shutil.move("summary.tar.gz", checksum_dir + checksum8_name)
os.chdir(CURDIR)
bf.greyfish_log(IP_addr, toktok, "download checksum", "dir", '/'.join(DIR.split('++')))
return send_file(checksum_dir + checksum8_name, as_attachment=True)
# Deletes a checksum file given its file name
@app.route('/grey/delete_checksum_file/<gkey>/<toktok>/<FILE>')
def delete_checksum_file(toktok, gkey, FILE):
IP_addr = request.environ['REMOTE_ADDR']
checksum_dir = GREYFISH_FOLDER+'DIR_'+str(toktok)+'/checksum_files/'
if not bf.valid_key(gkey, toktok):
bf.failed_login(gkey, IP_addr, toktok, "delete-file")
return "INVALID key"
if not os.path.exists(checksum_dir+FILE):
return 'Checksum file is not present in Greyfish'
os.remove(checksum_dir+FILE)
bf.greyfish_log(IP_addr, toktok, "delete", "checksum file", FILE)
return 'Checksum file succesfully deleted from Greyfish storage'
if __name__ == '__main__':
app.run()