Skip to content

Commit 9def265

Browse files
authored
Merge pull request #1193 from cmu-delphi/proxy_depth_default_update_and_diags
update/fix PROXY_DEPTH default value, add 'diagnostics' endpoint
2 parents 3b1f12d + 24c0c6a commit 9def265

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/server/_config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@
5555
setting REVERSE_PROXY_DEPTH to "0" essentially indicates there are no proxies between this server and the outside
5656
world. in this case, the "X-Forwarded-For" header is ignored.
5757
"""
58-
REVERSE_PROXY_DEPTH = int(os.environ.get("PROXY_DEPTH", 2))
58+
REVERSE_PROXY_DEPTH = int(os.environ.get("PROXY_DEPTH", 4))
59+
# TODO: ^ this value should be "4" for the prod CC API server processes, and is currently unclear
60+
# for prod AWS API server processes (but should be the same or lower)... when thats properly
61+
# determined, set the default to the minimum of the two environments and special case the
62+
# other in conf file(s).
5963

6064

6165
REGION_TO_STATE = {

src/server/endpoints/admin.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from werkzeug.exceptions import NotFound, Unauthorized
66
from werkzeug.utils import redirect
77

8+
from .._common import log_info_with_request
89
from .._config import ADMIN_PASSWORD, API_KEY_REGISTRATION_FORM_LINK, API_KEY_REMOVAL_REQUEST_LINK, REGISTER_WEBHOOK_TOKEN
910
from .._security import resolve_auth_token
1011
from ..admin.models import User, UserRole
@@ -44,6 +45,24 @@ def user_exists(user_email: str = None, api_key: str = None):
4445
return True if user else False
4546

4647

48+
# ~~~~ PUBLIC ROUTES ~~~~
49+
50+
51+
@bp.route("/registration_form", methods=["GET"])
52+
def registration_form_redirect():
53+
# TODO: replace this with our own hosted registration form instead of external
54+
return redirect(API_KEY_REGISTRATION_FORM_LINK, code=302)
55+
56+
57+
@bp.route("/removal_request", methods=["GET"])
58+
def removal_request_redirect():
59+
# TODO: replace this with our own hosted form instead of external
60+
return redirect(API_KEY_REMOVAL_REQUEST_LINK, code=302)
61+
62+
63+
# ~~~~ PRIVLEGED ROUTES ~~~~
64+
65+
4766
@bp.route("/", methods=["GET", "POST"])
4867
def _index():
4968
token = _require_admin()
@@ -88,21 +107,6 @@ def _detail(user_id: int):
88107
return _render("detail", token, flags, user=user.as_dict)
89108

90109

91-
def register_new_key(api_key: str, email: str) -> str:
92-
User.create_user(api_key=api_key, email=email)
93-
return api_key
94-
95-
96-
@bp.route("/registration_form", methods=["GET"])
97-
def registration_form_redirect():
98-
# TODO: replace this with our own hosted registration form instead of external
99-
return redirect(API_KEY_REGISTRATION_FORM_LINK, code=302)
100-
101-
@bp.route("/removal_request", methods=["GET"])
102-
def removal_request_redirect():
103-
# TODO: replace this with our own hosted form instead of external
104-
return redirect(API_KEY_REMOVAL_REQUEST_LINK, code=302)
105-
106110
@bp.route("/register", methods=["POST"])
107111
def _register():
108112
body = request.get_json()
@@ -117,5 +121,16 @@ def _register():
117121
"User with email and/or API Key already exists, use different parameters or contact us for help",
118122
409,
119123
)
120-
api_key = register_new_key(user_api_key, user_email)
121-
return make_response(f"Successfully registered API key '{api_key}'", 200)
124+
User.create_user(api_key=user_api_key, email=user_email)
125+
return make_response(f"Successfully registered API key '{user_api_key}'", 200)
126+
127+
128+
@bp.route("/diagnostics", methods=["GET", "PUT", "POST", "DELETE"])
129+
def diags():
130+
# allows us to get useful diagnostic information written into server logs,
131+
# such as a full current "X-Forwarded-For" path as inserted into headers by intermediate proxies...
132+
# (but only when initiated purposefully by us to keep junk out of the logs)
133+
_require_admin()
134+
log_info_with_request("diagnostics", headers=request.headers)
135+
response_text = f"request path: {request.headers.get('X-Forwarded-For', 'idk')}"
136+
return make_response(response_text, 200, {'content-type': 'text/plain'})

0 commit comments

Comments
 (0)