Verified Commit c666f955 authored by Mike Barkmin's avatar Mike Barkmin 🕶
Browse files

refactor: run black

parent 05f2ab83
# This file contains two schemas, against which all requests and responses are validated.
request_schema = {
"$schema": "http://json-schema.org/draft-04/schema",
"type": "object",
"properties": {
"image": {"type": "string"},
"payload": {"type": "object"},
"user": {"type": "string"},
"timeout": {"type": "integer"}
},
"required": ["image", "payload", "user", "timeout"]
}
"$schema": "http://json-schema.org/draft-04/schema",
"type": "object",
"properties": {
"image": {"type": "string"},
"payload": {"type": "object"},
"user": {"type": "string"},
"timeout": {"type": "integer"},
},
"required": ["image", "payload", "user", "timeout"],
}
......@@ -27,12 +27,15 @@ def sign_request(request: requests.Request) -> requests.PreparedRequest:
get_config().LOGGER.warning(
"OPENPATCH_HMAC_SECRET environment variable is not set.\n"
"HMAC is disabled.\n"
"Your API-endpoint is open to all requests.")
"Your API-endpoint is open to all requests."
)
return prep_req
else:
prep_req.headers['HMAC'] = hmac.new(
bytes(get_config().OPENPATCH_HMAC_SECRET, 'utf-8'),
bytes(str(prep_req.body), 'utf-8'), hashlib.sha512).hexdigest()
prep_req.headers["HMAC"] = hmac.new(
bytes(get_config().OPENPATCH_HMAC_SECRET, "utf-8"),
bytes(str(prep_req.body), "utf-8"),
hashlib.sha512,
).hexdigest()
return prep_req
......@@ -57,22 +60,24 @@ def verify_request(request: requests.Request) -> None:
get_config().LOGGER.warning(
"OPENPATCH_HMAC_SECRET environment variable is not set.\n"
"HMAC is disabled.\n"
"Your API-endpoint is open to all requests.")
"Your API-endpoint is open to all requests."
)
return
digest_maker = hmac.new(
bytes(get_config().OPENPATCH_HMAC_SECRET, 'utf-8'),
bytes(str(request.data), 'utf-8'), hashlib.sha512)
if 'HMAC' not in request.headers:
bytes(get_config().OPENPATCH_HMAC_SECRET, "utf-8"),
bytes(str(request.data), "utf-8"),
hashlib.sha512,
)
if "HMAC" not in request.headers:
raise RuntimeError("Request did not contain a HMAC-header.")
elif not hmac.compare_digest(request.headers['HMAC'],
digest_maker.hexdigest()):
elif not hmac.compare_digest(request.headers["HMAC"], digest_maker.hexdigest()):
raise RuntimeError("HMAC-header did not match.")
def sign_and_send(request: requests.Request,
timeout=get_config().RUNNER_DEFAULT_TIMEOUT
) -> requests.Response:
def sign_and_send(
request: requests.Request, timeout=get_config().RUNNER_DEFAULT_TIMEOUT
) -> requests.Response:
"""Signs and sends a prepared requests.request-object.
This is a wrapper to sign_request() in combination with opening a session and sending the message in one line.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment