Skip to content
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

Add support for client certificates #85

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

quality-leftovers
Copy link

Support authenticating using client certificates (#84).

@@ -119,6 +120,7 @@ def __init__(self, file_path: Optional[str] = None, file_stream: Optional[IO] =
self.file_stream = file_stream
self.stop_at = self.get_file_size()
self.client = client
self.client_cert = client_cert
Copy link
Author

Choose a reason for hiding this comment

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

Set to client.client_cert if client_cert is None? Probably makes sense. Not sure

Copy link
Member

Choose a reason for hiding this comment

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

Could you explain what you mean? I don't fully understand the problem.

Copy link
Author

Choose a reason for hiding this comment

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

I was just wondering about what should be done if this constructor is called with client_cert=None but the client already was constructed with a client certificate. But I think this should be considered incorrect use.

It's a bit complicated right now because baseuploader, uploader and TusRequest all do "HTTP" stuff and all need to use the client cert. But changing that would be more than I dare to touch atm. See: #77.

Copy link
Member

Choose a reason for hiding this comment

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

Why does the BaseUploader class constructor needs its own client_cert argument at all? Couldn't it access it via client.client_cert? Then we don't have to deal with cases where client_cert on BaseUploader differs from client_cert on Client.

Copy link
Member

Choose a reason for hiding this comment

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

Looking more at it, the reason seems to be that this way one can also set client_cert without providing a TusClient at all. However, I don't think that this is a use case we need to support. Just like headers can also be set through TusClient, we can also provide client_cert through a TusClient. Then we avoid having duplicate ways to achieve the same thing. Does that make sense?

Copy link
Author

Choose a reason for hiding this comment

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

I've changed it so certificate is only set via TusClient. Makes the code cleaner.

tusclient/client.py Outdated Show resolved Hide resolved

class TusClientTestWithClientCertificate(unittest.TestCase):
def setUp(self):
self.client_cert=("/tmp/client.crt.pem")
Copy link
Member

Choose a reason for hiding this comment

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

Does this require the file to be present on the machine? Is there some code, which generates this file?

Copy link
Author

Choose a reason for hiding this comment

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

The test only checks if the property is passed along to the uploader. The requests "mock" does not care about it.
Not sure if doing further tests for client certificates in unit tests makes a lot of sense.

Copy link
Member

Choose a reason for hiding this comment

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

I see now that this just tests the properties on our classes. Would it be possible to add a test ensuring that the client certificate is properly passed to requests? We have similar tests for verify_tls_cert and maybe that can serve as an inspiration:

def test_verify_tls_cert(self):

def test_upload_verify_tls_cert(self):

Copy link
Author

@quality-leftovers quality-leftovers Mar 7, 2024

Choose a reason for hiding this comment

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

I've had a look at the test and also noticed that I forgot to add the certificate for the async upload because there the asyncio library is used for the patches.

Here in uploader:

ssl_ctx = ssl.create_default_context()
if (self.client_cert is not None):
if self.client_cert is str:
ssl_ctx.load_cert_chain(certfile=self.client_cert)
else:
ssl_ctx.load_cert_chain(certfile=self.client_cert[0], keyfile=self.client_cert[1])
conn = aiohttp.TCPConnector(ssl=ssl_ctx)

And here in request:

ssl_ctx = ssl.create_default_context()
if (self.client_cert is not None):
if self.client_cert is str:
ssl_ctx.load_cert_chain(certfile=self.client_cert)
else:
ssl_ctx.load_cert_chain(certfile=self.client_cert[0], keyfile=self.client_cert[1])
conn = aiohttp.TCPConnector(ssl=ssl_ctx)
async with aiohttp.ClientSession(loop=self.io_loop, connector=conn) as session:

Need to test if that works and think about how that can be tested. It's getting more complicated than anticipated :/

Copy link
Member

Choose a reason for hiding this comment

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

I am sorry to hear that is complicated. Where you able to get any progress on the tests? :)

@@ -119,6 +120,7 @@ def __init__(self, file_path: Optional[str] = None, file_stream: Optional[IO] =
self.file_stream = file_stream
self.stop_at = self.get_file_size()
self.client = client
self.client_cert = client_cert
Copy link
Member

Choose a reason for hiding this comment

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

Could you explain what you mean? I don't fully understand the problem.

tusclient/client.py Outdated Show resolved Hide resolved
@quality-leftovers
Copy link
Author

Wondering if you could have a look again. Still not sure what to do related to the tests since TLS cannot really be tested in the unit tests.

@Acconut
Copy link
Member

Acconut commented Mar 4, 2024

Sure, I responded to your latest comments :)

@Acconut
Copy link
Member

Acconut commented Mar 25, 2024

One additional thing: Please avoid force pushing to the branch. This makes it harder for us to review changes as we don't know what changes between commits. In the end, the entire branch will be squashed anyways, so the Git commits don't matter much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants