Refactor; failing tests for some reason.

This commit is contained in:
Itamar Turner-Trauring
2023-03-28 13:06:43 -04:00
parent e8c72e6753
commit d36adf33a4

View File

@ -106,28 +106,31 @@ def _authorization_decorator(required_secrets):
def decorator(f): def decorator(f):
@wraps(f) @wraps(f)
def route(self, request, *args, **kwargs): def route(self, request, *args, **kwargs):
with start_action(
action_type="allmydata:storage:http-server:handle_request",
method=request.method,
path=request.path,
) as ctx:
try:
# Check Authorization header:
if not timing_safe_compare( if not timing_safe_compare(
request.requestHeaders.getRawHeaders("Authorization", [""])[0].encode( request.requestHeaders.getRawHeaders("Authorization", [""])[0].encode(
"utf-8" "utf-8"
), ),
swissnum_auth_header(self._swissnum), swissnum_auth_header(self._swissnum),
): ):
request.setResponseCode(http.UNAUTHORIZED) raise _HTTPError(http.UNAUTHORIZED)
return b""
# Check secrets:
authorization = request.requestHeaders.getRawHeaders( authorization = request.requestHeaders.getRawHeaders(
"X-Tahoe-Authorization", [] "X-Tahoe-Authorization", []
) )
try: try:
secrets = _extract_secrets(authorization, required_secrets) secrets = _extract_secrets(authorization, required_secrets)
except ClientSecretsException: except ClientSecretsException:
request.setResponseCode(http.BAD_REQUEST) raise _HTTPError(http.BAD_REQUEST)
return b"Missing required secrets"
with start_action( # Run the business logic:
action_type="allmydata:storage:http-server:request",
method=request.method,
path=request.path,
) as ctx:
try:
result = f(self, request, secrets, *args, **kwargs) result = f(self, request, secrets, *args, **kwargs)
except _HTTPError as e: except _HTTPError as e:
# This isn't an error necessarily for logging purposes, # This isn't an error necessarily for logging purposes,
@ -136,6 +139,7 @@ def _authorization_decorator(required_secrets):
ctx.add_success_fields(response_code=e.code) ctx.add_success_fields(response_code=e.code)
ctx.finish() ctx.finish()
raise raise
else:
ctx.add_success_fields(response_code=request.code) ctx.add_success_fields(response_code=request.code)
return result return result