Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Open sidebar
OpenPatch
runner
Commits
c666f955
Verified
Commit
c666f955
authored
Jan 26, 2021
by
Mike Barkmin
🕶
Browse files
refactor: run black
parent
05f2ab83
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
23 deletions
+28
-23
openpatch_runner/api/v1/schemas/json_schemas.py
openpatch_runner/api/v1/schemas/json_schemas.py
+10
-10
openpatch_runner/api/v1/utils/HmacUtils.py
openpatch_runner/api/v1/utils/HmacUtils.py
+18
-13
No files found.
openpatch_runner/api/v1/schemas/json_schemas.py
View file @
c666f955
# 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"
]
,
}
openpatch_runner/api/v1/utils/HmacUtils.py
View file @
c666f955
...
...
@@ -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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment