-
-
Notifications
You must be signed in to change notification settings - Fork 754
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
Implement SSLContext factory #1815
Conversation
uvicorn/config.py
Outdated
@@ -446,6 +448,9 @@ def load(self) -> None: | |||
ca_certs=self.ssl_ca_certs, | |||
ciphers=self.ssl_ciphers, | |||
) | |||
|
|||
elif self.ssl_context: | |||
self.ssl = self.ssl_context.custom_ssl_context_factory() # type: ignore |
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.
self.ssl = self.ssl_context.custom_ssl_context_factory() # type: ignore | |
self.ssl = self.ssl_context() |
this should do it no ? I dont get it otherwise
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.
it wont work with out calling the callable, here is my usage
class SSL_Context(object):
@classmethod
def __call__(self):
context = ssl.SSLContext(int(ssl.PROTOCOL_TLS))
context.load_cert_chain(certfile=tls_cert, keyfile=tls_key)
if allowed_ciphers:
context.set_ciphers(allowed_ciphers)
if list_options:
for each_option in list_options:
context.options |= each_option
return context
ssl_context = SSL_Context
uvicorn.run(
"web:app",
host="0.0.0.0",
port=int(port),
reload=True,
ssl_context=ssl_context,
)
and inside config.py
elif self.ssl_context:
self.ssl = self.ssl_context.__call__() # type: ignore
The way I see this feature is something like: from ssl import SSLContext
import uvicorn
def ssl_context_factory(context: SSLContext) -> SSLContext:
return context
if __name__ == "__main__":
uvicorn.run("main:app", ssl_context_factory=ssl_context_factory) All the SSL parameters we have can be used together with the If using this feature via python code, then the above is what I imagine. If we also want to use it via CLI, I guess we'd need to use the |
got it updated with your suggestions as follows.
|
uvicorn/main.py
Outdated
@@ -319,6 +319,13 @@ def print_version(ctx: click.Context, param: click.Parameter, value: bool) -> No | |||
help="Ciphers to use (see stdlib ssl module's)", | |||
show_default=True, | |||
) | |||
@click.option( | |||
"--ssl-context-factory", | |||
type=typing.Callable, |
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.
This can't be a callable. What I said was to load the function object from the path... Which I'm not sure if we really should make this available via CLI.
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.
There are messages I've written here that you ignored. I'm not reviewing this again without them addressed.
The code changes here don't fully take in consideration my previous comments.
uvicorn/config.py
Outdated
@@ -446,6 +448,9 @@ def load(self) -> None: | |||
ca_certs=self.ssl_ca_certs, | |||
ciphers=self.ssl_ciphers, | |||
) | |||
|
|||
elif self.ssl_context_factory: | |||
self.ssl = self.ssl_context_factory() |
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.
Hmm... Maybe we shouldn't ignore the previous parameters passed? 🤔
What I propose was for the factory function to receive a SSLContext
, which would be the one that uvicorn
creates on the lines above.
@aswanidutt87 Are you still interested on this? |
yes @Kludex , we really need this, |
The factory is meant to be called only when the |
I have modified the code to incorporate your suggestion of consuming the passed parameters to the custom ssl_context supplied. |
Would you mind fixing the linter, and removing the parameter from the CLI parameters? |
@Kludex , Linter is happy when I ran in my local Reg: the removal of parameter from the CLI, then the test_cli is breaking. |
@aswanidutt87 You probably need to update your dependencies. Please also run |
and regarding the test_cli fail, since we removed the @click.option( |
@Kludex , please let me know if we really need to remove the entry for CLI parameter, and if so how to fix the test failure due to the removal of CLI parameter. |
I've commented already: #1815 (comment) |
@Kludex the latest comment is #1815 (comment) , if I remove the CLI parameter for --ssl-context, the test_cli is failing, I need help here. |
uvicorn/main.py
Outdated
@@ -319,6 +319,13 @@ def print_version(ctx: click.Context, param: click.Parameter, value: bool) -> No | |||
help="Ciphers to use (see stdlib ssl module's)", | |||
show_default=True, | |||
) | |||
@click.option( | |||
"--ssl-context-factory", | |||
type=typing.Callable, |
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.
There are messages I've written here that you ignored. I'm not reviewing this again without them addressed.
The code changes here don't fully take in consideration my previous comments.
Also, the pipeline is not passing... ??? |
This comment was marked as spam.
This comment was marked as spam.
Closing as stale. |
No description provided.