Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

improve TLS handling with SNI support and cert caching #432

Merged
merged 4 commits into from
Dec 20, 2024

Conversation

lukehinds
Copy link

Implements improved TLS handling system with Server Name Indication (SNI) support and certificate caching. Changes include:

- Implement SNI support to provide better hostname verification
- Add certificate expiration checks and automatic cleanup
- Domain cert caching, with validation
- Improved and refactored code layout

The new implementation improves performance by caching certificates and contexts
while providing proper hostname verification through SNI. This allows the proxy
to securely handle multiple GitHub domains with domain-specific certificates which in turn fixes: #371

Luke Hinds added 4 commits December 19, 2024 22:53
Implements improved TLS handling system with Server Name Indication (SNI) support
and certificate caching. Changes include:

- Implement SNI support to provide better hostname verification
- Add certificate expiration checks and automatic cleanup
- Cache SSL contexts to reduce overhead on repeated connections
- Domain cert caching, with validation
- Improved and refactored code layout

The new implementation improves performance by caching certificates and contexts
while providing proper hostname verification through SNI. This allows the proxy
to securely handle multiple GitHub domains with domain-specific certificates.
self._ca = ca_provider
# Use strong references for caching
self._cert_cache: Dict[str, CachedCertificate] = {}
self._context_cache: Dict[str, ssl.SSLContext] = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not for this PR, but would we ever want to do some cache cleanup like least-recently used or are the certificates small enough that we don't care?

Copy link
Author

Choose a reason for hiding this comment

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

I think so, I will follow up, just want to think it over a bit more

return

# Get current time for expiry checks
current_time = datetime.now(timezone.utc)
Copy link
Contributor

Choose a reason for hiding this comment

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

I had to look up in the docs if datetime.now is equivalent to datetime.utcnow and based on my understanding it is - just wanted to double check during review if you think it's OK to use both in the code (not a blocker)

# remove and recreate certs directory
try:
logger.debug(f"Removing certs directory: {self.certs_dir}")
os.rmdir(self.certs_dir)
Copy link
Contributor

Choose a reason for hiding this comment

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

This will only work if the certs_dir is empty, right? Or did you mean to use rmtree here?

Copy link
Contributor

@jhrozek jhrozek left a comment

Choose a reason for hiding this comment

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

This seems to work great and is an improvement. I left some small comments inline - two are questions and one might be a bug (the confusion between rmdir and rmtree).

But since this is quite a nice improvement, I say let's merge and if there are things to follow up on, let's make a follow-up patch.

@lukehinds lukehinds merged commit fd1b3e5 into main Dec 20, 2024
3 checks passed
@lukehinds lukehinds deleted the tls-sni-working branch December 20, 2024 19:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VSCode needs a restart to re-auth the user account via the proxy
2 participants