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

Refactor the admin architecture and configuration management #670

Merged
merged 16 commits into from Dec 6, 2018

Conversation

kaiyou
Copy link
Member

@kaiyou kaiyou commented Oct 18, 2018

Until now, the application was entirely generated by the init script, and configuration was only provided as environment variables.

This PR suggests generating the application in a function (namely create_app) and writing everything else as Flask extensions. This clarifies a lot of code inside the admin container, and makes some room for further improvement.

Among those improvements, the configuration is replaced in this version with a simple class that mimics the behavior of previous Flask configuration updated from env variables. It should evolve to the point where configuration can be modified from the Web interface, at least for items that do not impact the other containers.

Currently this builds and run but I still have an issue with the way Flask login interacts with Flask SQLAlchemy, as it does not seem to be using the application context properly, and thus fails when requesting SQLite which is not thread-safe.

@kaiyou kaiyou added the status/wip Work in progress. This will block mergify until the label is removed. label Oct 18, 2018
@muhlemmer
Copy link
Member

Just a suggestion. At this moment we are using manage.py to create the first user account. The first admin user should be equal to $POSTMASTER.

We could use the setup utility to generate a password, store it encrypted in mailu.env as $TEMP_PASS and inform the user on the final page. Then, when admin is started and doesn't find the first user, insert $POSTMASTER with $TEMP_PASS. User can now login using the password and change it.

This way, there can be no mismatch in $POSTMASTER and the chosen username and the over-all complexity of installing Mailu is less.

@kaiyou
Copy link
Member Author

kaiyou commented Nov 9, 2018

Sorry it took me so long to finish this up, I was struggling with sqlalchemy contexts when mixed with flask login. This is now ready for merge I believe.

@kaiyou kaiyou removed the status/wip Work in progress. This will block mergify until the label is removed. label Nov 9, 2018
Fixes the following error:
```
admin_1      | [2018-11-09 09:44:10,533] ERROR in app: Exception on /internal/auth/email [GET]
admin_1      | Traceback (most recent call last):
admin_1      |   File "/usr/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
admin_1      |     response = self.full_dispatch_request()
admin_1      |   File "/usr/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
admin_1      |     rv = self.handle_user_exception(e)
admin_1      |   File "/usr/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
admin_1      |     reraise(exc_type, exc_value, tb)
admin_1      |   File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
admin_1      |     raise value
admin_1      |   File "/usr/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
admin_1      |     rv = self.dispatch_request()
admin_1      |   File "/usr/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
admin_1      |     return self.view_functions[rule.endpoint](**req.view_args)
admin_1      |   File "/usr/lib/python3.6/site-packages/flask_limiter/extension.py", line 544, in __inner
admin_1      |     return obj(*a, **k)
admin_1      |   File "/app/mailu/internal/views/auth.py", line 18, in nginx_authentication
admin_1      |     headers = nginx.handle_authentication(flask.request.headers)
admin_1      |   File "/app/mailu/internal/nginx.py", line 48, in handle_authentication
admin_1      |     if user.check_password(password):
admin_1      |   File "/app/mailu/models.py", line 333, in check_password
admin_1      |     context = User.pw_context
admin_1      | AttributeError: type object 'User' has no attribute 'pw_context'
```
Copy link
Member

@muhlemmer muhlemmer left a comment

Choose a reason for hiding this comment

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

During our development of Postgresql, we found that login to the admin interface does not always work. We've downgraded to this branch and managed to reproduce the behavior. On Master everything seems fine.

Issue
Sometimes, when trying to log-in, the log-in page gets displayed again. No error is reported in the UI or container logs, pages seem to get served as intented (from the app point of view). When intentionally entering a wrong password, we do get an "wrong password" error served in UI.

Steps to reproduce

  1. Build and run this branch
  2. Log in to admin (this should work)
  3. Log out and try to log in again

You might need to repeat step 3 a couple of times to expose this. It seems when waiting a longer period between log-out and log-in, this does not expose. Try to keep the time as short as possible.
My gut tells me something (cookie, session) remains sticky on log-out and therefore the log-in page gets server over and over.

This happens on this branch and our usrpro/feat-psql-support, using both the SQlite and Postgresql database flavor. It might be very interesting to know, that looking at the postresql logs, no log-in queries are executed in cases where it didn't work. The only query that does get executed, is the one that lists the sign-up domains.

@muhlemmer
Copy link
Member

This seems to be chrom(ium) specific... When sending the login from chromium no redirect:

admin_1      | 79.115.207.234 - - [05/Dec/2018:20:56:28 +0000] "POST /ui/login?next=ui.index HTTP/1.0" 200 4395 "https://mail.usrpro.io/admin/ui/login?next=ui.index" "Mozilla/5.0 (X11; Fedora; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"

From firefox:

admin_1      | 79.115.207.234 - - [05/Dec/2018:20:56:51 +0000] "POST /ui/login?next=ui.index HTTP/1.0" 302 227 "https://mail.usrpro.io/admin/ui/login?next=ui.index" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0"
admin_1      | 79.115.207.234 - - [05/Dec/2018:20:56:51 +0000] "GET /ui/ HTTP/1.0" 302 253 "https://mail.usrpro.io/admin/ui/login?next=ui.index" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0"
admin_1      | 79.115.207.234 - - [05/Dec/2018:20:56:51 +0000] "GET /ui/user/settings HTTP/1.0" 200 7533 "https://mail.usrpro.io/admin/ui/login?next=ui.index" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0"

To be continued...

@muhlemmer
Copy link
Member

muhlemmer commented Dec 5, 2018

After doing some docker-compose down, database deletion and docker-compose up -d the problem seems to have disappeared. I think/hope that the issue was caused because of all the migrations between branches and probably a confusion in session management caused this. My test server does get confused from time to time 😉

I will drop my blocking review and go back to approving it.

@muhlemmer muhlemmer requested a review from a team December 5, 2018 21:17
@kaiyou
Copy link
Member Author

kaiyou commented Dec 6, 2018

Some more checking on my side and I think we can safely merge this.

@kaiyou kaiyou merged commit 3d98124 into Mailu:master Dec 6, 2018
@TheLegend875 TheLegend875 mentioned this pull request Dec 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants