-
Notifications
You must be signed in to change notification settings - Fork 15
/
bambleweeny.py
993 lines (789 loc) · 25.3 KB
/
bambleweeny.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
import json, os, uuid, base64, redis, time, hmac, hashlib, keyword, re
from multiprocessing import Process
from bottle import Bottle, request, response
# Settings
admin_password = u"changeme"
default_salt = "iKm4SyH6JCtA8l"
default_token_expiry_seconds = 3000
redis_datadir = '/data' # this is currently also set in b9y.sh
redis_maxmemory = '256mb'
b9y_release = "0.36"
b9y_version = "0.36.1"
app = Bottle()
# The default path renders a hello world JSON message
@app.get('/')
def get_home():
return(dict(msg="This is bambleweeny", release=str(b9y_release),\
version=str(b9y_version), instance=cluster_id))
# Default 404 handler
@app.error(404)
def error404(error):
#return 'Nothing here, sorry'
return("Nothing here.")
# Default 405 handler
@app.error(405)
def error405(error):
#return 'Nothing here, sorry'
return("Method not allowed for this endpoint.")
# Swagger
@app.get('/swagger')
def get_swagger():
with open("/app/swagger.json", mode='r', encoding='utf-8') as file_handle:
file_content = file_handle.read()
sw = json.loads(file_content)
return(dict(sw))
# Get Auth Token
@app.route('/auth/token', method='POST')
def get_token():
# Get JSON Payload
try:
payload = json.load(request.body)
username = payload["username"]
password = payload["password"]
pwhash = _get_password_hash(password)
except:
response.status = 400
return dict({"message":"No valid JSON found in post body or mandatory fields missing."})
user_list = rc.scan_iter("USER:*")
for user in user_list:
user_record = json.loads(rc.get(user))
if user_record["username"] == username and user_record["hash"] == pwhash:
user_token = _issue_token(user=username, id=user[5:], expiry=token_expiry_seconds)
if 'raw' in request.query:
return(user_token)
else:
return(dict(token_type="bearer", access_token=user_token))
response.status = 401
return(dict(info="could not authorize user"))
# Test Auth Token
@app.route('/auth/test', method='GET')
def validate_token():
d = _authenticate()
return(dict(d))
# Backend Info
@app.route('/info', method='GET')
def backend_info():
api_auth = _authenticate()
# Only Admin can do this
if api_auth["admin"] != "True" or api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
i = {}
i["redis_connection"] = {}
i["redis_connection"]["host"] = redis_host
i["redis_connection"]["port"] = redis_port
i["redis_dbsize"] = rc.dbsize()
i["redis_client_list"] = rc.client_list()
i["redis_info"] = rc.info()
return(i)
# Trigger Redis Save
@app.route('/save', method='GET')
def redis_save():
api_auth = _authenticate()
# Only Admin can do this
if api_auth["admin"] != "True" or api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
rc.save()
return(dict(info="ok"))
# Create User
@app.route('/users', method='POST')
def create_user():
api_auth = _authenticate()
# Only Admin can do this
if api_auth["admin"] != "True" or api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
try:
payload = json.load(request.body)
username = payload["username"]
password = payload["password"]
except:
response.status = 400
return dict({"info":"No valid JSON found in post body or mandatory fields missing."})
if _find_user(username) == "found":
response.status = 400
return dict({"info":"This user exists already."})
# Set ID and password hash for user
new_userid = rc.incr("_USERID_")
pwhash = _get_password_hash(password)
user_record = {}
user_record["username"] = username
user_record["hash"] = pwhash
user_record["quota"] = "0"
rc.set("USER:"+str(new_userid), json.dumps(user_record, ensure_ascii=False))
return(dict(info="created", id=new_userid))
# Read User
@app.route('/users/<id:int>', method='GET')
def get_user_details(id):
api_auth = _authenticate()
# Only Admin can do this
if api_auth["admin"] != "True" or api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Read from Redis
try:
user_record = json.loads(rc.get("USER:"+str(id)))
except:
response.status = 404
return dict({"info":"Not found."})
user_out = {}
user_out["username"] = user_record["username"]
user_out["quota"] = user_record["quota"]
return(dict(user_out))
# Set Quota for User
@app.route('/users/<id:int>', method='PUT')
def set_user_quota(id):
api_auth = _authenticate()
# Only Admin can do this
if api_auth["admin"] != "True" or api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Get Payload
try:
payload = json.load(request.body)
quota = payload["quota"]
except:
response.status = 400
return dict({"info":"No valid JSON found in post body or mandatory fields missing."})
# Read from Redis
try:
user_record = json.loads(rc.get("USER:"+str(id)))
except:
response.status = 404
return dict({"info":"Not found."})
user_record["quota"] = quota
rc.set("USER:"+str(id), json.dumps(user_record, ensure_ascii=False))
return dict({"info":"Quota updated for user."})
# Delete User
@app.route('/users/<id:int>', method='DELETE')
def delete_user(id):
api_auth = _authenticate()
# Only Admin can do this
if api_auth["admin"] != "True" or api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Do not allow deleting admin
if id == 0:
response.status = 400
return dict({"info":"Cannot delete admin user."})
# Does the user exist?
if rc.get("USER:"+str(id)) == None:
response.status = 404
return dict({"info":"Not found."})
# Delete user record
rc.delete("USER:"+str(id))
return(dict(info="user deleted"))
# List All Users
@app.route('/users', method='GET')
def list_user():
api_auth = _authenticate()
# Only Admin can do this
if api_auth["admin"] != "True" or api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
output = []
user_list = rc.scan_iter("USER:*")
for user in user_list:
user_record = json.loads(rc.get(user))
user_out = {}
user_out["id"] = user[5:]
user_out["username"] = user_record["username"]
user_out["quota"] = user_record["quota"]
output.append(user_out)
return(dict(users=output))
# Change Admin Password
@app.route('/config/admin', method='PUT')
def set_admin_password():
api_auth = _authenticate()
# Only Admin can do this
if api_auth["admin"] != "True" or api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
try:
payload = json.load(request.body)
new_password = payload["password"]
except:
response.status = 400
return dict({"info":"No valid JSON found in post body or mandatory fields missing."})
# Read record for admin user
admin_record = json.loads(rc.get("USER:0"))
admin_record["hash"] = _get_password_hash(new_password)
rc.set("USER:0", json.dumps(admin_record, ensure_ascii=False))
return(dict(info="updated"))
# Read Key
@app.route('/keys/<id>', method='GET')
def get_key(id):
api_auth = _authenticate()
# Authorization is needed for this endpoint
if api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Get User ID
user_id = api_auth["id"]
# Does the key have a valid format?
if _valid_identifier(str(id)) != True:
response.status = 400
return dict({"info":"Key format invalid."})
# Construct Resource Location from user_id and id
redis_key = "KEY:"+str(user_id)+"::"+str(id)
# Read from Redis
try:
key_content = rc.get(redis_key)
if key_content == None:
raise ValueError('not found')
except:
response.status = 404
return dict({"info":"Not found."})
response.content_type = 'text/plain'
return(str(key_content))
# Increase Key
@app.route('/incr/<id>', method='GET')
def incr_key(id):
api_auth = _authenticate()
# Authorization is needed for this endpoint
if api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Get User ID and quota
user_id = api_auth["id"]
user_quota = _get_user_quota(user_id)
current_number_of_resources = _user_resources_number(user_id)
# Does the key have a valid format?
if _valid_identifier(str(id)) != True:
response.status = 400
return dict({"info":"Key name is invalid."})
# Construct Resource Location from user_id and id
redis_key = "KEY:"+str(user_id)+"::"+str(id)
# Does the key exist already?
keyexists = rc.get(redis_key)
if keyexists == None:
if user_quota != 0 and current_number_of_resources >= user_quota:
response.status = 400
return dict({"info":"Quota exceeded."})
# Increase the counter of resources for this user_record
rc.incr("NUMRES:"+str(user_id))
# Read from Redis
try:
key_content = rc.incr(redis_key)
except:
response.status = 400
return dict({"info":"Error - key is probably not INT."})
response.content_type = 'text/plain'
return(str(key_content))
# Write Key
@app.route('/keys/<id>', method='PUT')
def write_key(id):
api_auth = _authenticate()
# Authorization is needed for this endpoint
if api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Get User ID and quota
user_id = api_auth["id"]
user_quota = _get_user_quota(user_id)
current_number_of_resources = _user_resources_number(user_id)
# Does the key have a valid format?
if _valid_identifier(str(id)) != True:
response.status = 400
return dict({"info":"Key name is invalid."})
# Construct Resource Location from user_id and id
redis_key = "KEY:"+str(user_id)+"::"+str(id)
# Does the key exist already?
keyexists = rc.get(redis_key)
if keyexists == None:
# Are we allowed to create more objects?
if user_quota != 0 and current_number_of_resources >= user_quota:
response.status = 400
return dict({"info":"Quota exceeded."})
# Increase the counter of resources for this user_record
rc.incr("NUMRES:"+str(user_id))
# Write to Redis
try:
rc.set(redis_key, request.body.read())
except:
response.status = 400
return dict({"info":"not a valid request"})
return(dict(info="ok"))
# Delete Key
@app.route('/keys/<id>', method='DELETE')
def delete_key(id):
api_auth = _authenticate()
# Authorization is needed for this endpoint
if api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Get User ID
user_id = api_auth["id"]
# Does the key have a valid format?
if _valid_identifier(str(id)) != True:
response.status = 400
return dict({"info":"Key name is invalid."})
# Construct Resource Location from user_id and id
redis_key = "KEY:"+str(user_id)+"::"+str(id)
# Does the key exist already?
keyexists = rc.get(redis_key)
if keyexists == None:
response.status = 404
return dict({"info":"Key not found."})
# Delete from Redis
try:
rc.delete(redis_key)
rc.decr("NUMRES:"+str(user_id))
except:
response.status = 400
return dict({"info":"not a valid request"})
return(dict(info="ok"))
# List all Keys
@app.route('/keys', method='GET')
def get_all_keys():
api_auth = _authenticate()
# Authorization is needed for this endpoint
if api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Get User ID
user_id = api_auth["id"]
# Construct Resource Location
redis_key = "KEY:"+str(user_id)+"::*"
# Resource Location is different for admin access
# Since in that case we want all resources
if api_auth["admin"] == "True":
redis_key = "KEY:*"
output = []
keys_list = rc.scan_iter(redis_key)
for res in keys_list:
res_obj = {}
details = _get_key_data(res)
res_obj["key"] = details["id"]
res_obj["owner"] = details["owner"]
output.append(res_obj)
return(dict(keys=output))
# List - Add Item
@app.route('/lists/<id>', method='POST')
def add_item_to_list(id):
api_auth = _authenticate()
# Authorization is needed for this endpoint
if api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Get User ID and quota
user_id = api_auth["id"]
user_quota = _get_user_quota(user_id)
current_number_of_resources = _user_resources_number(user_id)
# Does the key have a valid format?
if _valid_identifier_lists(str(id)) != True:
response.status = 400
return dict({"info":"List name is invalid."})
# Construct Resource Location from user_id and id
redis_key = "LIST:"+str(user_id)+"::"+str(id)
# Does the list exist already?
keyexists = rc.lrange(redis_key, 0, 0)
if not keyexists:
# Are we allowed to create more objects?
if user_quota != 0 and current_number_of_resources >= user_quota:
response.status = 400
return dict({"info":"Quota exceeded."})
# Increase the counter of resources for this user_record
rc.incr("NUMRES:"+str(user_id))
# Write to Redis
try:
rc.lpush(redis_key, request.body.read())
except:
response.status = 400
return dict({"info":"not a valid request"})
return(dict(info="ok"))
# Lists - Get Item
@app.route('/lists/<id>', method='GET')
def get_list_item(id):
api_auth = _authenticate()
# Authorization is needed for this endpoint
if api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Get User ID
user_id = api_auth["id"]
# Does the key have a valid format?
if _valid_identifier_lists(str(id)) != True:
response.status = 400
return dict({"info":"List name is invalid."})
# Construct Resource Location from user_id and id
redis_key = "LIST:"+str(user_id)+"::"+str(id)
# Read from Redis. If there's just one item left we need to
# decrease NUMRES
try:
num_items = rc.llen(redis_key)
if num_items == 1:
rc.decr("NUMRES:"+str(user_id))
key_content = rc.rpop(redis_key)
except:
response.status = 404
return dict({"info":"Not found."})
response.content_type = 'text/plain'
return(str(key_content))
# Lists - Delete
@app.route('/lists/<id>', method='DELETE')
def delete_list(id):
api_auth = _authenticate()
# Authorization is needed for this endpoint
if api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Get User ID
user_id = api_auth["id"]
# Does the key have a valid format?
if _valid_identifier(str(id)) != True:
response.status = 400
return dict({"info":"Key name is invalid."})
# Construct Resource Location from user_id and id
redis_key = "LIST:"+str(user_id)+"::"+str(id)
# Does the list exist already?
keyexists = rc.lrange(redis_key, 0, -1)
if not keyexists:
response.status = 404
return dict({"info":"List not found."})
# Delete from Redis
try:
rc.delete(redis_key)
rc.decr("NUMRES:"+str(user_id))
except:
response.status = 400
return dict({"info":"not a valid request"})
return(dict(info="ok"))
# Lists - Get All
@app.route('/lists', method='GET')
def get_all_lists():
api_auth = _authenticate()
# Authorization is needed for this endpoint
if api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Get User ID
user_id = api_auth["id"]
# Construct Resource Location
redis_key = "LIST:"+str(user_id)+"::*"
# Resource Location is different for admin access
# Since in that case we want all resources
if api_auth["admin"] == "True":
redis_key = "LIST:*"
output = []
keys_list = rc.scan_iter(redis_key)
for res in keys_list:
res_obj = {}
details = _get_key_data(res)
res_obj["key"] = details["id"]
res_obj["owner"] = details["owner"]
output.append(res_obj)
return(dict(lists=output))
# Create Route
@app.route('/routes', method='POST')
def create_route():
api_auth = _authenticate()
# Authorization is needed for this endpoint
if api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Get User ID
user_id = api_auth["id"]
try:
payload = json.load(request.body)
key = payload["key"]
content_type = payload["content_type"]
except:
response.status = 400
return dict({"info":"No valid JSON found in post body or mandatory fields missing."})
id = str(uuid.uuid4())
# Construct Resource Location
redis_key = "ROUTE:"+str(id)
route_record = {}
route_record["key"] = key
route_record["content_type"] = content_type
route_record["user_id"] = user_id
# Write to Redis
try:
rc.set(redis_key, json.dumps(route_record, ensure_ascii=False))
except:
response.status = 400
return dict({"info":"not a valid request"})
return(dict(info="ok", path="/routes/"+str(id)))
# Route - Read Key
@app.route('/routes/<id>', method='GET')
@app.route('/routes/<id>/<dummy>', method='GET')
@app.route('/routes/<id>/<dummy>/<dummy2>', method='GET')
@app.route('/routes/<id>', method='POST')
@app.route('/routes/<id>/<dummy>', method='POST')
@app.route('/routes/<id>/<dummy>/<dummy2>', method='POST')
def get_route(id, dummy=0, dummy2=0):
route_key = "ROUTE:"+str(id)
# Read Route from Redis
try:
route_content = rc.get(route_key)
if route_content == None:
raise ValueError('not found')
route_record = json.loads(route_content)
user_id = route_record["user_id"]
key = route_record["key"]
content_type = route_record["content_type"]
except:
response.status = 404
return dict({"info":"Not found."})
# Construct Resource Location from user_id and id
redis_key = "KEY:"+str(user_id)+"::"+str(key)
# Read from Redis
try:
key_content = rc.get(redis_key)
if key_content == None:
raise ValueError('not found.')
except:
response.status = 404
return dict({"info":"Not found."})
response.headers['Access-Control-Allow-Origin'] = '*'
response.content_type = content_type
return(parse_route(str(key_content), user_id))
# Create Bins
@app.route('/bins', method='POST')
def create_bin():
api_auth = _authenticate()
# Authorization is needed for this endpoint
if api_auth["authenticated"] == "False":
response.status = 401
return dict({"info":"Unauthorized."})
# Get User ID
user_id = api_auth["id"]
user_quota = _get_user_quota(user_id)
current_number_of_resources = _user_resources_number(user_id)
if user_quota != 0 and current_number_of_resources >= user_quota:
response.status = 400
return dict({"info":"Quota exceeded."})
try:
payload = json.load(request.body)
list = payload["list"]
except:
response.status = 400
return dict({"info":"No valid JSON found in post body or mandatory fields missing."})
if _valid_identifier_lists(str(list)) != True:
response.status = 400
return dict({"info":"List name is invalid."})
id = str(uuid.uuid4())
# Construct Resource Location
redis_key = "BIN:"+str(id)
route_record = {}
route_record["list"] = list
route_record["user_id"] = user_id
# Write to Redis
try:
rc.set(redis_key, json.dumps(route_record, ensure_ascii=False))
except:
response.status = 400
return dict({"info":"not a valid request"})
# Increase the counter of resources for this user_record
rc.incr("NUMRES:"+str(user_id))
return(dict(info="ok", path="/bins/"+str(id)))
# Post to Bin
@app.route('/bins/<id>', method='POST')
def post_to_bin(id):
bin_key = "BIN:"+str(id)
# Read Bin from Redis
try:
bin_content = rc.get(bin_key)
if bin_content == None:
raise ValueError('not found')
bin_record = json.loads(bin_content)
user_id = bin_record["user_id"]
list = bin_record["list"]
except:
response.status = 404
return dict({"info":"Not found."})
# Construct Resource Location from user_id and id
redis_key = "LIST:"+str(user_id)+"::"+str(list)
try:
rc.lpush(redis_key, request.body.read())
except:
response.status = 400
return dict({"info":"not a valid request"})
return("OK")
####### Helper functions
# Parse Routes for nested keys
def parse_route(content, user_id):
if re.search('!@\[[_a-zA-Z0-9:]*\]', content):
response.headers["B9Y-ROUTES"] = "parsed"
repl1 = re.sub('!@\[[_a-zA-Z0-9:]*\]', '_B9yPrsE_\\g<0>_B9yPrsE_', content)
items = repl1.split("_B9yPrsE_")
out = ""
for i in items:
if i.startswith("!@["):
key = re.sub('[^\w:]', "", i)
val = rc.get("KEY:"+str(user_id)+"::"+str(key))
if val != None:
out += val.decode('utf-8')
else:
out += str(i)
return(out)
else:
return(content)
# Key names must match this regex
def _valid_identifier(i):
return(re.match("[_A-Za-z:][_a-zA-Z0-9:]*$", i) and not keyword.iskeyword(i))
# List names must match this regex
def _valid_identifier_lists(i):
return(re.match("[_A-Za-z][_a-zA-Z0-9]*$", i) and not keyword.iskeyword(i))
def _get_key_data(k):
out = {}
pos = k.find("::")
pos1 = k.find(":")
out["id"] = k[pos+2:]
out["owner"] = k[pos1+1:pos]
return(out)
def _get_password_hash(pw):
hash_object = hashlib.sha1((pw+salt).encode('utf-8'))
return(hash_object.hexdigest())
def _create_admin():
pwhash = _get_password_hash(admin_password)
user_record = {}
user_record["username"] = "admin"
user_record["hash"] = pwhash
user_record["quota"] = "0"
rc.set("USER:0", json.dumps(user_record, ensure_ascii=False))
return
def _find_user(username):
user_list = rc.scan_iter("USER:*")
for user in user_list:
user_record = json.loads(rc.get(user))
if user_record["username"] == username:
return("found")
return("not found")
def _get_user_quota(uid):
user_record = json.loads(rc.get("USER:"+str(uid)))
return int(user_record["quota"])
def _user_resources_number(uid):
n = rc.get("NUMRES:"+str(uid))
if n == None:
return 0
else:
return(int(n))
def _b64_enc(s):
return(base64.urlsafe_b64encode(s.encode('utf-8')).decode('utf-8'))
def _b64_dec(s):
return(base64.urlsafe_b64decode(s.encode('utf-8')).decode('utf-8'))
def _issue_token(user, expiry, id):
# Dict containing user info
content = {}
content["u"] = user
content["t"] = str(int(time.time()))
content["i"] = id
content["c"] = str(cluster_id)
# Create base64 encoded version
c = _b64_enc(json.dumps(content))
# Get an hmac signature
hmac1 = hmac.new(salt.encode('utf-8'), c.encode('utf-8'), hashlib.sha256)
return(c + "." + hmac1.hexdigest()[:8])
def _get_token_data(token):
token_data = {}
token_data["error"] = "0"
token_data["admin"] = "False"
token_data["authenticated"] = "False"
try:
# Get base64 encoded content and the signature from the token
separator = token.find(".")
sig_token = token[separator+1:]
content_raw = _b64_dec(token[:separator])
content = json.loads(content_raw)
# Create signature
c = base64.urlsafe_b64encode(json.dumps(content).encode('utf-8')).decode('utf-8')
hmac1 = hmac.new(salt.encode('utf-8'), c.encode('utf-8'), hashlib.sha256 )
sig_check = hmac1.hexdigest()[:8]
# Only get the data if the signature is valid
if sig_token == sig_check:
token_data["timestamp"] = int(content["t"])
token_data["user"] = content["u"]
token_data["id"] = content["i"]
token_data["cluster_id"] = content["c"]
if content["u"] == 'admin':
token_data["admin"] = "True"
else:
token_data["error"] = "1"
except:
token_data["error"] = "1"
return(token_data)
def _authenticate():
# Token can be in query string or AUTH header
if 'token' in request.query:
access_token = request.query["token"]
else:
bearer = request.environ.get('HTTP_AUTHORIZATION','')
access_token=bearer[7:]
# Extract the data from the token
data = _get_token_data(token=access_token)
# If there was an error, end here
if data["error"] != "0":
return(dict(data))
# Was the token issued by this cluster?
if data["cluster_id"] != cluster_id:
return(dict(data))
# Is the access token still valid?
token_timestamp = data["timestamp"]
current_time = int(time.time())
delta = current_time - token_timestamp
if delta > token_expiry_seconds:
# expired
data["authenticated"] = "False"
data["info"] = "Token expired"
else:
# valid
data["authenticated"] = "True"
data["info"] = "Session expires in " + str(token_expiry_seconds - delta) + " seconds."
# Set response header: username
response.headers["B9Y-AUTHENTICATED-USER"] = data["user"]
return(dict(data))
def _cluster_init():
cid = str(uuid.uuid4())
rc.set("_B9Y_ID_", cid[:8])
rc.set("__SALT__", salt)
return(cid[:8])
def queue_housekeeping():
rc.set("_LASTRUN_", str(int(time.time())))
def processor():
while True:
time.sleep(60)
queue_housekeeping()
# Initialization
# We need a Redis connection
if not "redis_host" in os.environ or not "redis_port" in os.environ:
exit("ERROR: please set the environment variables for Redis host")
# Connect to the Redis backend
redis_host = os.environ['redis_host']
redis_port = os.environ['redis_port']
if "redis_password" in os.environ:
redis_password = os.environ['redis_password']
else:
redis_password = ""
rc = redis.StrictRedis(host=redis_host, port=redis_port, password=redis_password, db=0)
# Configure local Redis
if "redis_embedded" in os.environ:
rc.config_set('dir', redis_datadir)
rc.config_set('maxmemory', redis_maxmemory)
# Set Token expiry
if 'token_expiry' in os.environ:
token_expiry_seconds = int(os.environ['token_expiry'])
else:
token_expiry_seconds = default_token_expiry_seconds
# Set Salt
if 'salt' in os.environ:
salt = os.environ['salt']
else:
salt = default_salt
# Create record for admin user if it doesn't exist
try:
if rc.get("USER:0") == None:
_create_admin()
except:
print("ERROR: Unable to connect to Redis at " + str(redis_host) + ":" + str(redis_port))
exit(1)
# Read unique ID for this instance / cluster or initialize if it doesn't exist
cluster_id = rc.get("_B9Y_ID_")
if cluster_id == None:
cluster_id = _cluster_init()
else:
cluster_id = cluster_id.decode('utf-8')
multiproc = Process(target=processor)
multiproc.start()