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

Can't use self-signed SSL cert/key with emmett (serve|develop) #522

Closed
brunobord opened this issue Nov 14, 2024 · 2 comments · Fixed by #523
Closed

Can't use self-signed SSL cert/key with emmett (serve|develop) #522

brunobord opened this issue Nov 14, 2024 · 2 comments · Fixed by #523
Labels
Milestone

Comments

@brunobord
Copy link

Hello there.

I'm giving a try to emmett and I had trouble using the SSL options with emmett.
It's a fresh install, as this pip freeze result shows:

click==8.1.7
emmett==2.6.1
emmett-core==1.0.3
emmett-pydal==17.3.1
granian==1.6.2
pendulum==3.0.0
python-dateutil==2.9.0.post0
python-rapidjson==1.20
PyYAML==6.0.2
renoir==1.8.0
severus==1.4.0
six==1.16.0
time-machine==2.16.0
tzdata==2024.2
uvloop==0.21.0

app.py file:

from emmett import App, abort

app = App(__name__)

@app.route("/")
async def hello():
    return "Hello"

When I launch this app using emmett serve or emmett develop, there's no issue, the root route returns "Hello".

But then I've generated a SSL cert+key using the command listed in the docs:

openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes

Now I've got my key.pem and cert.pem files.

But the next commands fail:

emmett develop --ssl-certfile cert.pem --ssl-keyfile key.pem 
> Starting Emmett development server on app app
> Emmett application app running on http://127.0.0.1:8000 (press CTRL+C to quit)
> Restarting (stat mode)
Process Process-1:
Traceback (most recent call last):
  File "/usr/lib/python3.12/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/lib/python3.12/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/emmett_core/server.py", line 28, in run
    server = granian.Granian(
             ^^^^^^^^^^^^^^^^
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/granian/server.py", line 153, in __init__
    self.build_ssl_context(ssl_cert, ssl_key, ssl_key_password)
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/granian/server.py", line 175, in build_ssl_context
    self.ssl_ctx = (True, str(cert.resolve()), str(key.resolve()), password)
                              ^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'resolve'

almost the same error when using the serve command:

emmett serve --ssl-certfile cert.pem --ssl-keyfile key.pem 
Traceback (most recent call last):
  File "/home/bbord/Projects/Various/Emmett/venv/bin/emmett", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/emmett/cli.py", line 473, in main
    cli.main(prog_name="python -m emmett" if as_module else None)
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/emmett/cli.py", line 225, in main
    return super().main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/click/decorators.py", line 92, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/emmett/cli.py", line 322, in serve_command
    sgi_run(
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/emmett_core/server.py", line 28, in run
    server = granian.Granian(
             ^^^^^^^^^^^^^^^^
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/granian/server.py", line 153, in __init__
    self.build_ssl_context(ssl_cert, ssl_key, ssl_key_password)
  File "/home/bbord/Projects/Various/Emmett/venv/lib/python3.12/site-packages/granian/server.py", line 175, in build_ssl_context
    self.ssl_ctx = (True, str(cert.resolve()), str(key.resolve()), password)
                              ^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'resolve'

I first thought it was an error with granian, but surprisingly, the following command worked as expected:

granian --ssl-keyfile key.pem --ssl-certificate cert.pem app.py

Is there a bug in emmett? Is there something else to do on my side to enable ssl in dev/serve mode?

@gi0baro
Copy link
Member

gi0baro commented Nov 14, 2024

@brunobord thank you for reporting this, it's definitely an Emmett bug. Will fix it asap.

@gi0baro gi0baro added the bug label Nov 14, 2024
@gi0baro gi0baro added this to the 2.6 milestone Nov 14, 2024
gi0baro added a commit that referenced this issue Nov 15, 2024
@brunobord
Copy link
Author

Fantastic! I've tested on my machine and it worked! Thank you for the fast fix!

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

Successfully merging a pull request may close this issue.

2 participants