-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·896 lines (729 loc) · 35.5 KB
/
main.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
"""
Copyright 2020-2021, Institute for Systems Biology
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from flask import Flask, abort, Response, stream_with_context, request, g, jsonify, make_response
from werkzeug.middleware.proxy_fix import ProxyFix
from config import settings
import logging
import time
import requests
import ipaddress
from google.auth import default as get_credentials
from google.auth.transport.requests import AuthorizedSession
import datetime
import redis
import json
import re
from random import random
from urllib.parse import urlparse
#
# Configuration
#
REDIS_HOST = settings['REDIS_HOST']
REDIS_PORT = int(settings['REDIS_PORT'])
DISABLE = (settings['DISABLE'].lower() == 'true')
CHUNK_SIZE = int(settings['CHUNK_SIZE'])
GOOGLE_HC_URL = settings['GOOGLE_HC_URL']
ALLOWED_HOST = settings['ALLOWED_HOST']
DEGRADATION_LEVEL_ONE = int(settings['DEGRADATION_LEVEL_ONE'])
DEGRADATION_LEVEL_ONE_PAUSE = float(settings['DEGRADATION_LEVEL_ONE_PAUSE'])
DEGRADATION_LEVEL_TWO = int(settings['DEGRADATION_LEVEL_TWO'])
DEGRADATION_LEVEL_TWO_PAUSE = float(settings['DEGRADATION_LEVEL_TWO_PAUSE'])
MAX_PER_IP_PER_DAY = int(settings['MAX_PER_IP_PER_DAY'])
MAX_TOTAL_PER_DAY = int(settings['MAX_TOTAL_PER_DAY'])
FREE_CLOUD_REGION = settings['FREE_CLOUD_REGION']
ALLOWED_LIST = settings['ALLOWED_LIST']
DENY_LIST = settings['DENY_LIST']
UA_SECRET = settings['UA_SECRET']
RESTRICT_LIST = settings['RESTRICT_LIST']
RESTRICT_MULTIPLIER = float(settings['RESTRICT_MULTIPLIER'])
HSTS_AGE = int(settings['HSTS_AGE'])
HSTS_PRELOAD = (settings['HSTS_PRELOAD'].lower() == 'true')
USAGE_DECORATION = settings['USAGE_DECORATION']
CURRENT_STORE_PATH = settings['CURRENT_STORE_PATH']
PATH_TAIL = settings['PATH_TAIL']
ALLOWED_LEGACY_PREFIX = settings['ALLOWED_LEGACY_PREFIX']
GLOBAL_IP_ADDRESS = "192.168.255.255"
CLOUD_IP_URL = 'https://www.gstatic.com/ipranges/cloud.json'
RAND_500_RATE = float(settings['RAND_500_RATE'])
BULK_PATH_PREFIX = settings['BULK_PATH_PREFIX']
IS_BULK = (settings['IS_BULK'].lower() == 'true')
BACKOFF_COUNT = 3
ABANDON_COUNT = 10
FIX_COUNT = 3
BULK_LOG_TAG = "(BULK) " if IS_BULK else ""
SUPPRESS_BULK = (settings['SUPPRESS_BULK'].lower() == 'true')
HUGE_STUDIES = settings['HUGE_STUDIES'] if 'HUGE_STUDIES' in settings else None
HUGE_STUDIES_LIST = []
if HUGE_STUDIES is not None:
if HUGE_STUDIES.lower() != 'none':
HUGE_STUDIES_LIST = HUGE_STUDIES.split(';')
app = Flask(__name__)
#
# We need to be able to extract the IP address of the actual caller, despite passing through the
# load balancer. This helps us do that cleanly:
#
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=FIX_COUNT)
#
# Logging:
#
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("main.py")
#
# Ok, this is the client we use for this server. Note that it is backed by a connection pool that is managed in
# a way that does not require us to explicitly release the connection on teardown!
#
redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT)
#
# This function does everything we wish to do inside a redis transaction.
# See: https://github.com/andymccurdy/redis-py/blob/master/README.rst#pipelines
#
def increment_ips(pipe):
try:
curr_use_per_ip_str = pipe.get(g.proxy_ip_addr)
curr_use_global_str = pipe.get(GLOBAL_IP_ADDRESS)
curr_use_per_ip = json.loads(curr_use_per_ip_str) if curr_use_per_ip_str is not None else None
curr_use_global = json.loads(curr_use_global_str) if curr_use_global_str is not None else None
if curr_use_per_ip is not None:
if curr_use_per_ip['day'] != g.proxy_date:
curr_use_per_ip['day'] = g.proxy_date
curr_use_per_ip['bytes'] = 0
curr_use_per_ip['bytes'] += g.proxy_byte_count
else:
curr_use_per_ip = {
'day': g.proxy_date,
'bytes': g.proxy_byte_count
}
if curr_use_global is not None:
if curr_use_global['day'] != g.proxy_date:
curr_use_global['day'] = g.proxy_date
curr_use_global['bytes'] = 0
curr_use_global['bytes'] += g.proxy_byte_count
else:
curr_use_global = {
'day': g.proxy_date,
'bytes': g.proxy_byte_count
}
pipe.multi()
pipe.set(g.proxy_ip_addr, json.dumps(curr_use_per_ip))
pipe.set(GLOBAL_IP_ADDRESS, json.dumps(curr_use_global))
return curr_use_per_ip, curr_use_global
except Exception as e:
logging.error("Exception in increment_ips: {}".format(str(e)))
logging.exception(e)
raise e
#
# Redis is being flaky, with lots of "connection reset by peer" errors. Do retrys inside a wrapper:
#
def redis_retry_wrapper(get_arg):
count = 0
retval = None
need_answer = True
while need_answer:
try:
retval = redis_client.get(get_arg)
need_answer = False
except Exception as e:
logging.error("Exception in redis_retry: {}".format(str(e)))
logging.exception(e)
if count > ABANDON_COUNT:
raise e
if count > BACKOFF_COUNT:
time.sleep(0.01 * (count - BACKOFF_COUNT))
count += 1
return retval
#
# Redis is being flaky, with lots of "connection reset by peer" errors. Do retrys inside a wrapper:
#
def redis_transaction_wrapper():
count = 0
curr_use_per_ip = None
curr_use_global = None
need_answer = True
while need_answer:
try:
curr_use_per_ip, curr_use_global = \
redis_client.transaction(increment_ips, g.proxy_ip_addr, GLOBAL_IP_ADDRESS, value_from_callable=True)
need_answer = False
except Exception as e:
logging.error("Exception in redis_transaction_wrapper: {}".format(str(e)))
logging.exception(e)
if count > ABANDON_COUNT:
raise e
if count > BACKOFF_COUNT:
time.sleep(0.01 * (count - BACKOFF_COUNT))
count += 1
return curr_use_per_ip, curr_use_global
#
# We only want to do one redis transaction per request. So we store up the data on the size and
# only update the db atomically when we are done:
#
@app.teardown_request
def teardown(request):
try:
if not hasattr(g, 'proxy_ip_addr'):
if hasattr(g, 'proxy_byte_count'):
logger.info("Skipping teardown: cloud access of %i bytes" % g.proxy_byte_count)
return
#logger.info("teardown_request start")
pre_millis = int(round(time.time() * 1000))
curr_use_per_ip, curr_use_global = redis_transaction_wrapper()
post_millis = int(round(time.time() * 1000))
logger.info("{}DAILY USAGE ON {} FOR IP {} is now {} bytes".format(BULK_LOG_TAG, curr_use_per_ip['day'],
g.proxy_ip_addr, curr_use_per_ip['bytes'] ))
logger.info("{}DAILY GLOBAL USAGE ON {} is now {} bytes".format(BULK_LOG_TAG, curr_use_global['day'], curr_use_global['bytes'] ))
end_gb = curr_use_per_ip['bytes'] // 10737418240 # Integer divison by 10 GB
#
# We want to track rapid egress without flooding the system with each log message. So we look for
# this message to send to pubsub. Note that *each* instance of this AppEngine service is going to
# issue this message when it goes over the 10 GB thresholds.
#
if end_gb > g.start_gb:
logger.info("{}DATE {} IP {} BYTES {} just chewed thru another 10 GB".format(BULK_LOG_TAG, curr_use_per_ip['day'],
g.proxy_ip_addr,
curr_use_per_ip['bytes'] ))
logger.info("{}Transaction length ms: {}".format(BULK_LOG_TAG, str(post_millis - pre_millis)))
logger.info("{}Chunk size was {}".format(BULK_LOG_TAG, CHUNK_SIZE))
logger.info("{}reported bytes {}".format(BULK_LOG_TAG, g.proxy_byte_count))
#logger.info("teardown_request done")
return
except Exception as e:
logging.error("Exception in teardown: {}".format(str(e)))
logging.exception(e)
raise e
#
# We can optionally impose a "delay" time as the user gets close to the limit by providing
# values for these constants > 0. Note, however, that this will require Google to spin up other
# instances through the load balancer to keep up with traffic, as this just sleeps this instance:
#
def calc_delay(byte_count):
if (DEGRADATION_LEVEL_TWO > 0) and (byte_count > DEGRADATION_LEVEL_TWO):
delay_time = DEGRADATION_LEVEL_TWO_PAUSE
elif (DEGRADATION_LEVEL_ONE > 0) and (byte_count > DEGRADATION_LEVEL_ONE):
delay_time = DEGRADATION_LEVEL_ONE_PAUSE
else:
delay_time = 0.0
return delay_time
#
# This is streaming content, so we count the bytes as they go out the door, based on our streaming chunk size. This
# slightly overcounts, since we don't know how many bytes go out on the last call:
#
def counting_wrapper(req, delay_time):
# This is too simple; current Python 3 uses "yield from". But we need to
# do stuff on each call. So should implement full yield from semantics shown at
# https://www.python.org/dev/peps/pep-0380/
# Originally used this structure:
#for v in req.iter_content(chunk_size=CHUNK_SIZE):
# yield v
# but it turns out that Requests decodes the stream from GZIP with that call, not
# allowing us to pass on the gzipped stream to the caller. So, we dig down into
# iter_content() and pull out what appears to be the relevant line from that call,
# and change the hardwired decode_content=True argument.
# See "def iter_content()" in https://github.com/psf/requests/blob/master/requests/models.py
# There is some more code in there to "simulate reading small chunks of the content", which
# appears to be irrelevant in our use case.
# Note the comment in that function that the actual bytes returned could be different than
# chunk size due to decoding. So by going with raw, we appear to be doing a better job of
# tracking what goes out the door.
#
# (see https://requests.readthedocs.io/en/master/user/quickstart/#raw-response-content
# and https://requests.readthedocs.io/en/master/community/faq/#encoded-data)
try:
for chunk in req.raw.stream(CHUNK_SIZE, decode_content=False):
yield chunk
g.proxy_byte_count += len(chunk)
if delay_time > 0.0:
time.sleep(delay_time)
except Exception as e:
logging.error("Exception in wrapper: {}".format(str(e)))
logging.exception(e)
raise e
#
# Discover the IPs living in the region where the proxy is deployed
#
def load_cidr_defs(free_region):
cidr_defs = []
if free_region != "NONE":
req = requests.request("GET", CLOUD_IP_URL)
cloud_prefixes = json.loads(req.content)
for prefix in cloud_prefixes['prefixes']:
if prefix['scope'] == 'us-central1':
cidr_defs.append(ipaddress.IPv4Network(prefix['ipv4Prefix']))
return cidr_defs
#
# Load in lists of CIDR defs for IPs we will allow or deny:
#
def load_cidr_list(list_string):
cidr_defs = []
if list_string == "NONE":
return cidr_defs
cidr_chunks = list_string.split(';')
for cidr_chunk in cidr_chunks:
cidr_defs.append(ipaddress.IPv4Network(cidr_chunk))
return cidr_defs
#
# Answer if the IP address is in the given CIDR list:
#
def is_in_cidr_list(ip_addr, CIDR_defs):
ip_addr_obj = ipaddress.IPv4Address(ip_addr)
for cidr in CIDR_defs:
if ip_addr_obj in cidr:
return True
return False
@app.route('/_ah/warmup')
def warmup():
# We are configured with warmup requests. If we need to do something, this is the place.
return '', 200, {}
#
# Send this back even if they just hit the server w/o a valid endpoint:
#
@app.route("/")
def return_404():
headers = {"Strict-Transport-Security": hsts_header}
return Response("Not Found", status=404, headers=headers)
#
# Let callers know where they stand, out of band:
#
@app.route('/quota_usage', methods=["GET", "OPTIONS"])
def quota_usage():
client_ip = request.remote_addr
#
# We need to force access via our own load balancer:
#
hostname = urlparse(request.base_url).hostname
if hostname != ALLOWED_HOST:
logger.info("request from {} has been dropped: invalid hostname".format(hostname))
abort(400)
if DISABLE:
logger.info("request from {} has been dropped: proxy disabled".format(client_ip))
abort(404)
now_time = datetime.date.today()
todays_date = str(now_time)
# Get bytes for this IP and for global usage:
curr_use_per_ip_str = redis_retry_wrapper(client_ip)
curr_use_global_str = redis_retry_wrapper(GLOBAL_IP_ADDRESS)
curr_use_per_ip = json.loads(curr_use_per_ip_str) if curr_use_per_ip_str is not None else None
curr_use_global = json.loads(curr_use_global_str) if curr_use_global_str is not None else None
#logger.info("Have data for {}: {}, global: {}".format(client_ip, str(curr_use_per_ip), str(curr_use_global)))
#
# Always provide the cors headers to keep OHIF happy:
#
cors_headers = {}
if 'origin' in request.headers:
cors_headers = {
"Access-Control-Allow-Origin": request.headers['origin'],
"Access-Control-Allow-Methods": "GET",
"Access-Control-Max-Age": "3600"
}
if 'access-control-request-headers' in request.headers:
cors_headers["Access-Control-Allow-Headers"] = request.headers['access-control-request-headers']
#logger.info("REQUEST METHOD {}".format(request.method))
#logger.info("Request headers: {}".format(str(request.headers)))
# Always add this:
cors_headers["Strict-Transport-Security"] = hsts_header
if request.method == "OPTIONS":
resp = Response('')
resp.headers = cors_headers
logger.info("returning OPTION headers {}".format(str(cors_headers)))
return resp
# Figure out if it is a new day, bag it if we are over the limit. Note that if we need to reset the byte_count
# to zero for a new day, we will not need to rewrite to DB yet, since the returns here will not be triggered
# with a zero count (with sane settings):
usage_return = {
"ip": client_ip,
"bytes_used": 0,
"fraction_used": 0.0,
"date": todays_date
}
if curr_use_per_ip is not None:
last_usage = curr_use_per_ip['day']
byte_count = curr_use_per_ip['bytes']
if last_usage != todays_date:
byte_count = 0
usage_return["bytes_used"] = byte_count
usage_return["fraction_used"] = float(byte_count)/float(MAX_PER_IP_PER_DAY)
# Suppress sending out global data!
#if curr_use_global is not None:
# last_global_usage = curr_use_global['day']
# last_global_byte_count = curr_use_global['bytes']
# if last_global_usage != todays_date:
# last_global_byte_count = 0
#
# usage_return["global_fraction_used"] = float(last_global_byte_count)/float(MAX_TOTAL_PER_DAY)
as_json = json.dumps(usage_return)
logger.info("[STATUS] Received usage request: {}".format(as_json))
return Response(as_json, mimetype='application/json', headers=cors_headers)
#
# During the transition to the new request URL approach, we support the old URL pending the upgrade to the viewers.
# Note this assumes we have used the USAGE_DECORATION
#
@app.route('{}{}<path:remainder>'.format(ALLOWED_LEGACY_PREFIX, USAGE_DECORATION), methods=["GET", "OPTIONS"])
def legacy_shim(remainder):
logger.warning("Using legacy shim for remainder: {} IP: {}".format(remainder, request.remote_addr))
return common_core(request, '{}{}'.format(USAGE_DECORATION, remainder))
#
# The new main handler, which uses an internally configured resource path:
#
@app.route('{}/current/<path:remainder>'.format("/{}".format(BULK_PATH_PREFIX) if IS_BULK else ''), methods=["GET", "OPTIONS"])
def root(remainder):
return common_core(request, remainder)
#
# Common core, used by both
#
def common_core(request, remainder):
client_ip = request.remote_addr
#
# Even the 429, 404, and 500 responses need to provide the cors headers to keep OHIF happy enough to process these
# errors cleanly. So we do this stuff here to make it available for all responses:
#
cors_headers = {}
if 'origin' in request.headers:
cors_headers = {
"Access-Control-Allow-Origin": request.headers['origin'],
"Access-Control-Allow-Methods": "GET",
"Access-Control-Max-Age": "3600"
}
if 'access-control-request-headers' in request.headers:
cors_headers["Access-Control-Allow-Headers"] = request.headers['access-control-request-headers']
# Always add this:
cors_headers["Strict-Transport-Security"] = hsts_header
#logger.info("REQUEST METHOD {}".format(request.method))
#logger.info("Request headers: {}".format(str(request.headers)))
#
# If an allowed hosts list exists, and the caller is not on it, we stop right here. Designed to restrict
# access to e.g. development team:
#
is_denied = (len(allow_cidr_defs) > 0) and not is_in_cidr_list(client_ip, allow_cidr_defs)
if is_denied:
logger.info("request from {} has been dropped: not an allowed IP".format(client_ip))
resp = Response(status=403)
resp.headers = cors_headers
return resp
#
# If a denied hosts list exists, and the caller is on it, we stop right here. Designed to block
# IP addresses that are abusing the proxy quota system:
#
is_denied = (len(deny_cidr_defs) > 0) and is_in_cidr_list(client_ip, deny_cidr_defs)
if is_denied:
logger.info("request from {} has been dropped: a blocked IP".format(client_ip))
resp = Response(status=403)
resp.headers = cors_headers
return resp
#
# If a restricted hosts list exists, and the caller is on it, we wre going to knock their quota down
# by the specified amount. Allows us to throttle certain IPs to a lower level than the general public
#
quota_multiplier = 1.0
is_restricted = (len(restrict_cidr_defs) > 0) and is_in_cidr_list(client_ip, restrict_cidr_defs)
if is_restricted:
logger.info("request from {} has restricted quota".format(client_ip))
quota_multiplier = RESTRICT_MULTIPLIER
#
# If user-agent secret exists, and the user agent string does not contain it, we stop right here.
# Another poor-man's method to restrict access to the e.g. development team:
#
if UA_SECRET != "NONE":
ua_string = request.headers.get('User-Agent')
if UA_SECRET not in ua_string:
logger.info("request from {} has been dropped: missing UA secret".format(client_ip))
resp = Response(status=403)
resp.headers = cors_headers
return resp
#
# We need to force access via our own load balancer:
#
hostname = urlparse(request.base_url).hostname
if hostname != ALLOWED_HOST:
logger.info("request from {} has been dropped: invalid hostname".format(hostname))
resp = Response(status=400)
resp.headers = cors_headers
return resp
if DISABLE:
logger.info("request from {} has been dropped: proxy disabled".format(client_ip))
resp = Response(status=404)
resp.headers = cors_headers
return resp
#
# We want to dress up the URL used by the viewers to include a usage restriction statement. If provided, this
# MUST be present in the URL. Strip it out of the provided path, and use the rest of the path
# to call the Healthcare API.
#
if USAGE_DECORATION is not None:
if remainder.find(USAGE_DECORATION) != -1:
remainder = remainder.replace(USAGE_DECORATION, '')
else:
logger.info("request from {} has been dropped: no required usage decoration in {}".format(client_ip, remainder))
resp = Response(status=404)
resp.headers = cors_headers
return resp
#
# Ditch the expected and required path tail from the remainder:
#
if remainder.find(PATH_TAIL) != -1:
remainder = remainder.replace(PATH_TAIL, '')
else:
logger.info("request from {} has been dropped: no required path tail in {}".format(client_ip, remainder))
resp = Response(status=404)
resp.headers = cors_headers
return resp
url = "{}/{}".format(CURRENT_STORE_PATH, remainder)
#
# Handle CORS:
#
if request.method == "OPTIONS":
resp = Response('')
resp.headers = cors_headers
logger.info("returning OPTION headers {}".format(str(cors_headers)))
return resp
#
# Wrap all processing so that we return CORS headers even if we fall over while processing the request:
#
try:
credentials, gcp_project = get_credentials()
scoped_credentials = credentials.with_scopes(["https://www.googleapis.com/auth/cloud-platform"])
auth_session = AuthorizedSession(scoped_credentials)
logger.info("[STATUS] {}Received proxy request: {}".format(BULK_LOG_TAG, url))
#logger.info("[STATUS] Received querystring: {}".format(request.query_string.decode("utf-8")))
#logger.info("Remote IP %s" % client_ip)
#logger.info("Header is {}".format(request.headers.getlist("X-Forwarded-For")[0]))
#
# Starting in v1beta1 as of 8/2024, the Google endpoint will return the actual full enpdoint URL as the "BulkDataURI"
# in a response for a metadata request. That's the URL that we are proxying. Thus, we need to do special handling
# of metadata requests to recast that value into the proxy's version of the URL. Check if we have a metadata request:
#
need_to_rewrite = url.endswith("/metadata")
#
# Check if we have a huge study that needs to suppress "transfer_encoding=*" yo get Google to send it compressed:
#
need_to_drop_trans = False
for study in HUGE_STUDIES_LIST:
if study in url:
need_to_drop_trans = True
break
#
# The idea here is that a client operating in our cloud region would not have a quota, since there
# would be no egress charge. But it turns out that bytes passing through the web app are going to get
# charged anyway, so the functionality is of limited use:
#
in_our_region = is_in_cidr_list(client_ip, local_cidr_defs)
#
# If IP is over the daily per-IP quota, we return a 429 Too Many Requests. If we are over the global quota,
# same thing. We are happy to just read the data at this point, and will atomically increment the whole count
# when we are done:
#
delay_time = 0.0
start_gb = 0
if not in_our_region:
byte_count = 0
now_time = datetime.date.today()
todays_date = str(now_time)
#logger.info("Time is now {}".format(now_time.ctime()))
#logger.info("Getting data for {}".format(client_ip))
# Get bytes for this IP and for global usage:
logger.info("[STATUS] Calling REDIS")
curr_use_per_ip_str = redis_retry_wrapper(client_ip)
curr_use_global_str = redis_retry_wrapper(GLOBAL_IP_ADDRESS)
logger.info("[STATUS] Return from REDIS")
curr_use_per_ip = json.loads(curr_use_per_ip_str) if curr_use_per_ip_str is not None else None
curr_use_global = json.loads(curr_use_global_str) if curr_use_global_str is not None else None
logger.info("{}Have data for {}: {}, global: {}".format(BULK_LOG_TAG, client_ip, str(curr_use_per_ip), str(curr_use_global)))
# Figure out if it is a new day, bag it if we are over the limit. Note that if we need to reset the byte_count
# to zero for a new day, we will not need to rewrite to DB yet, since the returns here will not be triggered
# with a zero count (with sane settings):
if curr_use_per_ip is not None:
last_usage = curr_use_per_ip['day']
byte_count = curr_use_per_ip['bytes']
if last_usage != todays_date:
byte_count = 0
if byte_count > (MAX_PER_IP_PER_DAY * quota_multiplier):
logger.info("{}Current byte count {} for IP {} exceeds daily threshold on {}".format(BULK_LOG_TAG, byte_count, client_ip, todays_date))
resp = Response(status=429)
resp.headers = cors_headers
return resp
start_gb = byte_count // 10737418240 # Integer divison by 10 GB
delay_time = calc_delay(byte_count)
if delay_time > 0.0:
time.sleep(delay_time)
if curr_use_global is not None:
last_global_usage = curr_use_global['day']
last_global_byte_count = curr_use_global['bytes']
if last_global_usage != todays_date:
last_global_byte_count = 0
# Delays are not supported for the global limit:
if last_global_byte_count > MAX_TOTAL_PER_DAY:
logger.info("{}Current byte count ALL IPS exceeds daily threshold IP: {} bytes: {} date: {}".format(BULK_LOG_TAG, client_ip,
last_global_byte_count,
todays_date))
resp = Response(status=429)
resp.headers = cors_headers
return resp
if delay_time > 0.0:
logger.info("Current byte count for IP is: {} so delay is starting at {}".format(byte_count, delay_time))
#
# Will need this for the teardown. Don't bother to update the delay during this request.
#
g.proxy_ip_addr = client_ip
g.proxy_date = todays_date
g.start_gb = start_gb
#
# Both free and quota use this:
#
g.proxy_byte_count = 0
#
# It is useful to test how well the OHIF viewer handles 500 return codes. 500 returns are not uncommon when
# App Engine needs to quickly spool up new instances when a big load appears out of the blue. So we can
# configure the proxy to return 500s at some specified random return rate
#
if RAND_500_RATE > 0.0:
rand_num = random()
if rand_num <= RAND_500_RATE:
logger.warning("Returning a test 500 (rate {}, val {}) to: {}".format(RAND_500_RATE, rand_num, client_ip))
resp = Response(status=500)
resp.headers = cors_headers
return resp
req_url = "{}/{}?{}".format(GOOGLE_HC_URL, url, request.query_string.decode("utf-8")) \
if request.query_string else "{}/{}".format(GOOGLE_HC_URL, url)
if request.query_string:
logger.info("Request URL with query: {}".format(req_url))
# For debug:
#for name, value in request.headers.items():
# logger.info("OHIF ASK: {}: {}".format(name, value))
#logger.info("Request headers: {}".format(str(request.headers)))
# per https://stackoverflow.com/questions/6656363/proxying-to-another-web-service-with-flask
req_headers = {key: value for (key, value) in request.headers if key != 'Host'}
if need_to_drop_trans:
for key in req_headers:
if key.lower() == "accept":
print("Looking at >>{}<< >>{}<<".format(key, req_headers[key]))
req_headers[key] = req_headers[key].replace("; transfer-syntax=*", "")
print("value now at", req_headers[key])
stream_val = not need_to_rewrite
req = auth_session.request(request.method, req_url, stream=stream_val,
headers=req_headers,
cookies=request.cookies,
allow_redirects=False)
#
# We have seen Google Healthcare API return 429s when the Healthcare API exceeds their per-minute throughput
# quota. This produces an ambiguous situation for the viewer, since it interprets 429s as *our* daily quota.
# We resolve this by mapping a Google Healthcare API 429 to a 500 (it is an internal error in this case,
# kinda...). Viewer should be designed to backoff and retry with a 500, since that also happens when
# App Engine spoolups are falling behind...
#
if req.status_code == 429:
logger.warning("{}Google returned a 429, mapping to 500, for IP: {}".format(BULK_LOG_TAG, client_ip))
resp = Response(status=500)
resp.headers = cors_headers
return resp
#
# If the Google backend has a problem and returns a 500, we want to know that *we* are not responsible for
# the problem:
#
if req.status_code >= 500:
logger.warning("ERROR: Google returned a 500 that we are passing through")
resp = Response(status=req.status_code)
resp.headers = cors_headers
return resp
#
# NO! excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection',
# 'access-control-allow-origin', "access-control-allow-methods" , "access-control-allow-headers"]
# In first iteration, included 'content-encoding' and 'content-length' in the excluded headers, since "Tried to drop
# content-encoding from this list, as it is returned by Google, but then the browser complains that the download failed".
# Proabably because Google said it was gzip encoded, but (see above comments on iter_content) we were unencoding the
# zipped content before sending it out. That is fixed, so sending the headers along:
#
#excluded_headers = ['transfer-encoding', 'connection',
if need_to_rewrite:
excluded_headers = ['content-encoding', 'transfer-encoding', 'connection', 'access-control-allow-origin', "access-control-allow-methods" , "access-control-allow-headers"]
else:
excluded_headers = ['connection', 'access-control-allow-origin', "access-control-allow-methods" , "access-control-allow-headers"]
# For debug
#logger.info("GOOGLE RETURNS STATUS: {}".format(req.status_code))
# For debug
#for name, value in req.raw.headers.items():
# logger.info("GOOGLE RETURNS: {}: {}".format(name, value))
headers = [(name, value) for (name, value) in req.raw.headers.items()
if name.lower() not in excluded_headers]
if cors_headers:
for item in cors_headers.items():
headers.append(item)
if need_to_rewrite:
try:
#
# We have two options for dealing with the BulkDataURI returned by Google Healthcare. We can either rewrite
# the URL to point to an AE Flex proxy that can handle > 32 MB requests, or we can strip the URI key/value
# entry out entirely. The latter will make the current v1beta1 (10/2024) enpoint behave like the current
# v1 endpoint, which does not provide a BulkDataURI
#
if not SUPPRESS_BULK:
backend_url_with_slash = '{}{}/'.format(GOOGLE_HC_URL, CURRENT_STORE_PATH)
proxy_url = "https://{}/{}/current/{}{}".format(ALLOWED_HOST, BULK_PATH_PREFIX, USAGE_DECORATION, PATH_TAIL)
if backend_url_with_slash in req.text:
patched_text = req.text.replace(backend_url_with_slash, proxy_url)
logger.info("Have performed a bulk data rewrite to: {}".format(proxy_url))
else:
patched_text = req.text
else:
backend_url = '{}{}'.format(GOOGLE_HC_URL, CURRENT_STORE_PATH)
if backend_url in req.text:
# logger.info(req.text[:200])
sub1 = r',"\w{8}":{"vr":"\w{2}","BulkDataURI":"'f'{backend_url}'r'/[\w/\.]*"}'
sub2 = r'{"\w{8}":{"vr":"\w{2}","BulkDataURI":"'f'{backend_url}'r'/[\w/\.]*"},'
logger.info(sub1)
logger.info(sub2)
patched_first_pass = re.sub(sub1, '', req.text)
if patched_first_pass == req.text:
logger.info("first pass unchanged")
results = re.findall(sub1, req.text)
for m in results:
logger.info(m)
patched_text = re.sub(sub2, "{", patched_first_pass)
if patched_first_pass == patched_text:
logger.info("second pass unchanged")
if "BulkDataURI" not in req.text:
logger.info("Have suppressed a bulk data key-value for: {}".format(backend_url))
else:
logger.info("Have NOT suppressed a bulk data key-value for: {}".format(backend_url))
else:
patched_text = req.text
json_metadata = json.loads(patched_text)
except requests.JSONDecodeError as e:
logging.error("Exception parsing JSON Metadata: {}".format(str(e)))
logging.exception(e)
resp = Response(status=500)
resp.headers = cors_headers
return resp
resp_as_json = json.dumps(json_metadata)
g.proxy_byte_count += len(resp_as_json)
res = make_response(resp_as_json, req.status_code)
res.headers = headers
return res
else:
#logger.info("Response headers: {}".format(str(headers)))
return Response(stream_with_context(counting_wrapper(req, delay_time)), headers=headers, status=req.status_code)
except Exception as e:
logging.error("Exception processing request: {}".format(str(e)))
logging.exception(e)
resp = Response(status=500)
resp.headers = cors_headers
return resp
root.provide_automatic_options = False
#
# Load in the info on what IP addresses are in a zone that we will allow unlimited access
#
local_cidr_defs = load_cidr_defs(FREE_CLOUD_REGION)
allow_cidr_defs = load_cidr_list(ALLOWED_LIST)
deny_cidr_defs = load_cidr_list(DENY_LIST)
restrict_cidr_defs = load_cidr_list(RESTRICT_LIST)
hsts_preload_directive = "; preload" if HSTS_PRELOAD else ""
hsts_header = 'max-age={}; includeSubDomains{}'.format(HSTS_AGE, hsts_preload_directive)
if __name__ == '__main__':
app.run()