uvicorn icon indicating copy to clipboard operation
uvicorn copied to clipboard

Implement SSLContext factory

Open aswanidutt87 opened this issue 3 years ago • 17 comments

aswanidutt87 avatar Dec 21 '22 17:12 aswanidutt87

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 ssl_context_factory parameter.

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 import_string that we have - but the CLI can wait for the python code part to be ready.

Kludex avatar Dec 25 '22 17:12 Kludex

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 ssl_context_factory parameter.

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 import_string that we have - but the CLI can wait for the python code part to be ready.

got it updated with your suggestions as follows.

def ssl_context() -> ssl.SSLContext:
    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

    uvicorn.run(
        "web:app",
        host="0.0.0.0",
        port=int(port),
        reload=True,
        ssl_context_factory=ssl_context,
    )

in config.py
        elif self.ssl_context_factory:
            self.ssl = self.ssl_context_factory()

aswanidutt87 avatar Dec 27 '22 15:12 aswanidutt87

@Kludex / @euri10 please review, I have modified per your suggestions, i think this should work

aswanidutt87 avatar Jan 04 '23 17:01 aswanidutt87

@aswanidutt87 Are you still interested on this?

Kludex avatar Feb 16 '23 10:02 Kludex

@aswanidutt87 Are you still interested on this?

yes @Kludex , we really need this, regarding your question "Hmm... Maybe we shouldn't ignore the previous parameters passed? " its either we pass the parameters to build the ssl_context in the uvicorn code or pass the custom created ssl_context directly to uvicorn, it cant be both at the same time. and for the second question "What I propose was for the factory function to receive a SSLContext, which would be the one that uvicorn creates on the lines above. ", thats the whole problem that we cant pass the SSLContext or any other object for that matter, so passing a ssl_context_factory and return SSLContext wherever we need it. please advise

aswanidutt87 avatar Feb 16 '23 14:02 aswanidutt87

@Kludex - also, for our case or in general adding ssl_options will suffice all the use cases as the uvicorn code already have all the options except the ssl_options, so kindly consider this pr also-https://github.com/encode/uvicorn/pull/1692

aswanidutt87 avatar Feb 16 '23 14:02 aswanidutt87

ontext wherever we need it.

The factory is meant to be called only when the SSLContext is needed, not in the Config, that's why it's the same problem as before... That's not the idea. I've pasted a link on how it's intended to be done on Gunicorn, and here would be analogous.

Kludex avatar Feb 17 '23 08:02 Kludex

@Kludex

I have modified the code to incorporate your suggestion of consuming the passed parameters to the custom ssl_context supplied. please review

aswanidutt87 avatar Feb 17 '23 23:02 aswanidutt87

Would you mind fixing the linter, and removing the parameter from the CLI parameters?

Kludex avatar Mar 09 '23 21:03 Kludex

Would you mind fixing the linter, and removing the parameter from the CLI parameters?

@Kludex , Linter is happy when I ran in my local image

Reg: the removal of parameter from the CLI, then the test_cli is breaking. image

aswanidutt87 avatar Mar 10 '23 15:03 aswanidutt87

@aswanidutt87 You probably need to update your dependencies. Please also run ./scripts/lint.

Kludex avatar Mar 12 '23 12:03 Kludex

@aswanidutt87 You probably need to update your dependencies. Please also run ./scripts/lint. @Kludex lint is clean I guess image

and regarding the test_cli fail, since we removed the @click.option( "--ssl-context", from main.py but still it has ssl_context: typing.Callable, as one of the parameter, the test_cli is failing with below error . not sure what dependencies I need to update so that the test_cli is happy. image

aswanidutt87 avatar Mar 13 '23 14:03 aswanidutt87

@aswanidutt87 You probably need to update your dependencies. Please also run ./scripts/lint. @Kludex lint is clean I guess

image

and regarding the test_cli fail, since we removed the @click.option( "--ssl-context", from main.py but still it has ssl_context: typing.Callable, as one of the parameter, the test_cli is failing with below error . not sure what dependencies I need to update so that the test_cli is happy. image

@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.

aswanidutt87 avatar May 18 '23 14:05 aswanidutt87

@Kludex / @euri10 please review the comment above and suggest

aswanidutt87 avatar May 25 '23 14:05 aswanidutt87

@Kludex / @euri10 please review the comment above and suggest

hello @Kludex / @euri10 , could you please review this change and approve/comment. we need this custom ssl_context for our use case.

aswanidutt87 avatar Jul 04 '23 14:07 aswanidutt87

I've commented already: https://github.com/encode/uvicorn/pull/1815#issuecomment-1364716832

Kludex avatar Jul 04 '23 14:07 Kludex

I've commented already: #1815 (comment)

@Kludex the latest comment is https://github.com/encode/uvicorn/pull/1815#issuecomment-1553167615 , if I remove the CLI parameter for --ssl-context, the test_cli is failing, I need help here.

aswanidutt87 avatar Jul 04 '23 14:07 aswanidutt87

Also, the pipeline is not passing... ???

Kludex avatar Feb 19 '24 19:02 Kludex

It would be helpful if this feature is added. Please merge.

UnshiftedEarth avatar Feb 26 '24 18:02 UnshiftedEarth

Closing as stale.

Kludex avatar Apr 13 '24 08:04 Kludex