-
Notifications
You must be signed in to change notification settings - Fork 1
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
pyatls: urllib3 and requests support with cleanup #10
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d92f7b9
pyatls: shorten class names
HernanGatta dcb0e25
pyatls: support atls in urllib3
HernanGatta ffe9fd7
pyatls: support atls in requests
HernanGatta dc71720
pyatls: restructure validators into hierarchy
HernanGatta 0ce8bdd
samples: update ACI sample
HernanGatta 58c7d96
pyatls: update pyproject
HernanGatta 4130e15
pyatls: update README
HernanGatta c354134
address comments
HernanGatta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#!/usr/bin/python3 | ||
|
||
# Sample usage of the atls package | ||
# | ||
# Suppose a simple HTTP server with a single GET endpoint /index is running | ||
|
@@ -17,10 +19,13 @@ | |
# --url /index | ||
|
||
import argparse | ||
from typing import List, Optional | ||
import ast | ||
from typing import List, Mapping, Optional | ||
|
||
from atls import AttestedHTTPSConnection, AttestedTLSContext | ||
from atls.validators import AzAasAciValidator | ||
import requests | ||
from atls import ATLSContext, HTTPAConnection | ||
from atls.utils.requests import HTTPAAdapter | ||
from atls.validators.azure.aas import AciValidator | ||
|
||
# Parse arguments | ||
parser = argparse.ArgumentParser() | ||
|
@@ -36,29 +41,51 @@ | |
parser.add_argument( | ||
"--method", | ||
default="GET", | ||
help="HTTP method to use in the request " "(default: GET)", | ||
help="HTTP method to use in the request (default: GET)", | ||
) | ||
|
||
parser.add_argument( | ||
"--url", | ||
default="/index", | ||
help="URL to perform the HTTP request against " "(default: /index)", | ||
help="URL to perform the HTTP request against (default: /index)", | ||
) | ||
|
||
parser.add_argument( | ||
"--policy", | ||
nargs="*", | ||
help="path to a CCE policy in Rego format, may be " | ||
"specified multiple times, once for each allowed policy " | ||
"(default: ignore)", | ||
help="path to a CCE policy in Rego format, may be specified multiple " | ||
"times, once for each allowed policy (default: ignore)", | ||
) | ||
|
||
parser.add_argument( | ||
"--jku", | ||
nargs="*", | ||
help="allowed JWKS URL to verify the JKU claim in the AAS " | ||
"JWT token against, may be specified multiple times, one " | ||
"for each allowed value (default: ignore)", | ||
action="extend", | ||
help="allowed JWKS URL to verify the JKU claim in the AAS JWT token " | ||
"against, may be specified multiple times, one for each allowed value " | ||
"(default: ignore)", | ||
) | ||
|
||
parser.add_argument( | ||
"--body", | ||
type=argparse.FileType("r"), | ||
help="path to a file containing the content to include in the request " | ||
"(default: nothing)", | ||
) | ||
|
||
parser.add_argument( | ||
"--headers", | ||
type=argparse.FileType("r"), | ||
help="path to a file containing the string representation of a Python " | ||
"dictionary containing the headers to be sent along with the request " | ||
"(default: none)", | ||
) | ||
|
||
parser.add_argument( | ||
"--use-requests", | ||
action="store_true", | ||
help="use the requests library with the HTTPS/aTLS adapater (default: " | ||
"false)", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
@@ -81,17 +108,69 @@ | |
# - The JKUs array carries all allowed JWKS URLs, or none if the JKU claim in | ||
# the AAS JWT token sent by the server during the aTLS handshake should not | ||
# be checked. | ||
validator = AzAasAciValidator(policies=policies, jkus=jkus) | ||
validator = AciValidator(policies=policies, jkus=jkus) | ||
|
||
# Parse provided headers, if any. | ||
headers: Mapping[str, str] = {} | ||
if args.headers is not None: | ||
raw = args.headers.read() | ||
headers = ast.literal_eval(raw) | ||
|
||
# Read in the provided body, if any. | ||
body: Optional[str] = None | ||
if args.body is not None: | ||
body = args.body.read() | ||
|
||
|
||
def use_direct() -> None: | ||
# Set up the aTLS context, including at least one attestation document | ||
# validator (only one need succeed). | ||
ctx = ATLSContext([validator]) | ||
|
||
# Set up the HTTP request machinery using the aTLS context. | ||
conn = HTTPAConnection(args.server, ctx, args.port) | ||
|
||
# Send the HTTP request, and read and print the response in the usual way. | ||
conn.request( | ||
args.method, | ||
args.url, | ||
body, | ||
headers, | ||
) | ||
|
||
response = conn.getresponse() | ||
code = response.getcode() | ||
|
||
print(f"Status: {code}") | ||
print(f"Response: {response.read().decode()}") | ||
|
||
conn.close() | ||
|
||
|
||
def use_requests() -> None: | ||
session = requests.Session() | ||
|
||
# Mount the HTTP/aTLS adapter such that any URL whose scheme is httpa:// | ||
# results in an HTTPAConnection object that in turn establishes an aTLS | ||
# connection with the server. | ||
session.mount("httpa://", HTTPAAdapter([validator])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How to set timeout for the connection in this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. session.request(..., timeout=) |
||
|
||
# Set up the aTLS context, including at least one attestation document | ||
# validator (only one need succeed). | ||
ctx = AttestedTLSContext([validator]) | ||
# The rest of the usage of the requests library is as usual. Do remember to | ||
# use session.request from the session object that has the mounted adapter, | ||
# not requests.request, since that's the global request function and has | ||
# therefore no knowledge of the adapter. | ||
response = session.request( | ||
args.method, | ||
f"httpa://{args.server}:{args.port}{args.url}", | ||
data=body, | ||
headers=headers, | ||
) | ||
|
||
# Set up the HTTP request machinery using the aTLS context. | ||
conn = AttestedHTTPSConnection(args.server, ctx, args.port) | ||
print(f"Status: {response.status_code}") | ||
print(f"Response: {response.text}") | ||
|
||
# Send the HTTP request, and read and print the response in the usual way. | ||
conn.request(args.method, args.url) | ||
print(conn.getresponse().read().decode()) | ||
|
||
conn.close() | ||
if args.use_requests: | ||
use_requests() | ||
else: | ||
use_direct() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
from atls.attested_https_connection import AttestedHTTPSConnection | ||
from atls.attested_tls_context import AttestedTLSContext | ||
from atls.atls_context import ATLSContext | ||
from atls.httpa_connection import HTTPAConnection | ||
|
||
__all__ = ["AttestedHTTPSConnection", "AttestedTLSContext"] | ||
__all__ = [ | ||
"HTTPAConnection", | ||
"ATLSContext", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from atls.utils.requests.adapter import HTTPAAdapter | ||
|
||
__all__ = ["HTTPAAdapter"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: possibly low priority but better to add this as pytest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to add unit tests that run automatically but we need an ACI-capable server instance. I do want to add tests for things that can be tested without a server-side component, though; that's on my to-do list.