Skip to content

http_sig_examples: generate the content-digest dynamically for test messages #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions http_sig_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,28 +149,37 @@
-----END EC PRIVATE KEY-----
"""

exampleRequestMessage = b"""POST /foo?param=Value&Pet=dog HTTP/1.1
def addContentDigest(message, body):
b64Digest = base64.b64encode(SHA512.new(body).digest()).decode('utf-8')
contentDigest = "sha-512=:%s:" % b64Digest
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with the rest of the script, this should use the http_sfv library primitives to create the value instead of string substitution.

contentLength = len(body)
return message.format(
contentDigest=contentDigest,
contentLength=contentLength,
body=body.decode('utf-8'),
).encode('utf-8')

exampleRequestMessage = addContentDigest("""POST /foo?param=Value&Pet=dog HTTP/1.1
Host: example.com
Date: Tue, 20 Apr 2021 02:07:55 GMT
Content-Type: application/json
Content-Digest: sha-512=:WZDPaVn/7XgHaAy8pmojAkGWoRx2UFChF41A2svX+TaPm+AbwAgBWnrIiYllu7BNNyealdVLvRwEmTHWXvJwew==:
Content-Length: 18
Content-Digest: {contentDigest}
Content-Length: {contentLength}

{"hello": "world"}"""
{body}""", b"""{"hello": "world"}""")

exampleReverseProxyMessage = b"""
exampleReverseProxyMessage = addContentDigest("""
POST /foo?param=Value&Pet=dog HTTP/1.1
Host: example.com
Date: Tue, 20 Apr 2021 02:07:55 GMT
Content-Type: application/json
Content-Digest: sha-512=:WZDPaVn/7XgHaAy8pmojAkGWoRx2UFChF41A2svX+TaPm+AbwAgBWnrIiYllu7BNNyealdVLvRwEmTHWXvJwew==:
Content-Length: 18
Content-Digest: {contentDigest}
Content-Length: {contentLength}
Forwarded: for=192.0.2.123
Signature-Input: sig1=("@method" "@authority" "@path" "content-digest" "content-length" "content-type");created=1618884475;keyid="test-key-rsa-pss"
Signature: sig1=:LAH8BjcfcOcLojiuOBFWn0P5keD3xAOuJRGziCLuD8r5MW9S0RoXXLzLSRfGY/3SF8kVIkHjE13SEFdTo4Af/fJ/Pu9wheqoLVdwXyY/UkBIS1M8Brc8IODsn5DFIrG0IrburbLi0uCc+E2ZIIb6HbUJ+o+jP58JelMTe0QE3IpWINTEzpxjqDf5/Df+InHCAkQCTuKsamjWXUpyOT1Wkxi7YPVNOjW4MfNuTZ9HdbD2Tr65+BXeTG9ZS/9SWuXAc+BZ8WyPz0QRz//ec3uWXd7bYYODSjRAxHqX+S1ag3LZElYyUKaAIjZ8MGOt4gXEwCSLDv/zqxZeWLj/PDkn6w==:

{"hello": "world"}
"""
{body}""", b"""{"hello": "world"}""")

exampleClientCertMessage = b"""POST /foo?param=Value&Pet=dog HTTP/1.1
Host: service.internal.example
Expand All @@ -182,13 +191,13 @@
{"hello": "world"}
"""

exampleResponseMessage = b"""HTTP/1.1 200 OK
exampleResponseMessage = addContentDigest("""HTTP/1.1 200 OK
Date: Tue, 20 Apr 2021 02:07:56 GMT
Content-Type: application/json
Content-Digest: sha-512=:JlEy2bfUz7WrWIjc1qV6KVLpdr/7L5/L4h7Sxvh6sNHpDQWDCL+GauFQWcZBvVDhiyOnAQsxzZFYwi0wDH+1pw==:
Content-Length: 23
Content-Digest: {contentDigest}
Content-Length: {contentLength}

{"message": "good dog"}"""
{body}""", b"""{"message": "good dog"}""")

exampleRequestResponseMessage = b"""HTTP/1.1 503 Service Unavailable
Date: Tue, 20 Apr 2021 02:07:56 GMT
Expand Down