-
Notifications
You must be signed in to change notification settings - Fork 3k
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 custom SSLContext when using FastHttpUser #2113
Merged
cyberw
merged 7 commits into
locustio:master
from
renato-farias:feat/fasthttpuser_sslcontext
Jun 17, 2022
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
80dd68a
add support for custom sslcontext when using FastHttpUser
renato-farias c86a68b
missing self
renato-farias 506cdb5
fix format and remove unecessary method
renato-farias 4fbf504
fix format and test
renato-farias 878a08f
make custom ssl context a callable option
renato-farias f8b0d6c
remove unnecessary test
renato-farias 95e6ffd
writing the right tests fro the sslcontext factories
renato-farias 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,13 +75,16 @@ def __init__( | |
user: Optional[User], | ||
insecure=True, | ||
client_pool: Optional[HTTPClientPool] = None, | ||
ssl_context: Optional[gevent.ssl.SSLContext] = None, | ||
**kwargs, | ||
): | ||
self.environment = environment | ||
self.base_url = base_url | ||
self.cookiejar = CookieJar() | ||
self.user = user | ||
if insecure: | ||
if ssl_context and self._check_ssl_context(ssl_context): | ||
ssl_context_factory = lambda: ssl_context | ||
elif insecure: | ||
ssl_context_factory = insecure_ssl_context_factory | ||
else: | ||
ssl_context_factory = gevent.ssl.create_default_context | ||
|
@@ -107,6 +110,13 @@ def __init__( | |
# store authentication header (we construct this by using _basic_auth_str() function from requests.auth) | ||
self.auth_header = _construct_basic_auth_str(parsed_url.username, parsed_url.password) | ||
|
||
def _check_ssl_context(ssl_context): | ||
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. Not sure this deserves its own method :) |
||
if not isinstance(ssl_context, gevent.ssl.SSLContext): | ||
raise TypeError( | ||
'You must provide a valid SSLContext. Expected type is gevent.ssl.SSLContext ' | ||
f'but you provided a ${type(ssl_context)}') | ||
return True | ||
|
||
def _build_url(self, path): | ||
"""prepend url with hostname unless it's already an absolute URL""" | ||
if absolute_http_url_regexp.match(path): | ||
|
@@ -318,6 +328,9 @@ class by using the :py:func:`@task decorator <locust.task>` on the methods, or b | |
client_pool: Optional[HTTPClientPool] = None | ||
"""HTTP client pool to use. If not given, a new pool is created per single user.""" | ||
|
||
ssl_context: Optional[gevent.ssl.SSLContext] = None | ||
"""A pre-configured SSL context for overriding the default context context created by the FastHttpSession.""" | ||
|
||
abstract = True | ||
"""Dont register this as a User class that can be run by itself""" | ||
|
||
|
@@ -341,6 +354,7 @@ def __init__(self, environment): | |
concurrency=self.concurrency, | ||
user=self, | ||
client_pool=self.client_pool, | ||
ssl_context=self.ssl_context | ||
) | ||
""" | ||
Instance of HttpSession that is created upon instantiation of User. | ||
|
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
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.
Doesnt it make more sense to just pass a function instead of an instantiated SSLContext that we make a fake function for? (I made a comment and then deleted it, but after some more thought I think my initial comment was correct :)
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.
Surely it does. I was trying to make use of
ssl_context
as an actual context then the caller could set it accordingly, but making it a factory keeps the way it does in the background. I will rename it tossl_context_factory
and type-hint it as Callable. Thanks for reviewing it.