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

Should guard better for user metadata being malformed/missing? #1085

Closed
yarikoptic opened this issue May 5, 2022 · 8 comments · Fixed by #1113
Closed

Should guard better for user metadata being malformed/missing? #1085

yarikoptic opened this issue May 5, 2022 · 8 comments · Fixed by #1113
Assignees
Labels
bug Something isn't working DX Affects developer experience released This issue/pull request has been released.

Comments

@yarikoptic
Copy link
Member

Trying to get a local instance of API server + web UI for development.
I have DJANGO logged in in /swagger under my email/password. Tried to authorize and get a token but

image

I went to /admin interface and added first and last name for my record in Users.

and then now when I go to web UI at http://localhost:8085/ I get "Connection to server failed." and following traceback in console

$> [15:34:50] INFO     "GET /api/info/ HTTP/1.1" 200 344                                                                                   basehttp.py:187
           ERROR    Internal Server Error: /api/users/me/                                                                                    log.py:241
                    Traceback (most recent call last):                                                                                                 
                      File "/home/yoh/proj/dandi/dandi-archive/venvs/dev3/lib/python3.9/site-packages/django/core/handlers/exception.py",              
                    line 55, in inner                                                                                                                  
                        response = get_response(request)                                                                                               
                      File "/home/yoh/proj/dandi/dandi-archive/venvs/dev3/lib/python3.9/site-packages/django/core/handlers/base.py", line              
                    197, in _get_response                                                                                                              
                        response = wrapped_callback(request, *callback_args, **callback_kwargs)                                                        
                      File "/home/yoh/proj/dandi/dandi-archive/venvs/dev3/lib/python3.9/site-packages/django/views/decorators/csrf.py", line           
                    54, in wrapped_view                                                                                                                
                        return view_func(*args, **kwargs)                                                                                              
                      File "/home/yoh/proj/dandi/dandi-archive/venvs/dev3/lib/python3.9/site-packages/django/views/generic/base.py", line              
                    84, in view                                                                                                                        
                        return self.dispatch(request, *args, **kwargs)                                                                                 
                      File "/home/yoh/proj/dandi/dandi-archive/venvs/dev3/lib/python3.9/site-packages/rest_framework/views.py", line 509, in           
                    dispatch                                                                                                                           
                        response = self.handle_exception(exc)                                                                                          
                      File "/home/yoh/proj/dandi/dandi-archive/venvs/dev3/lib/python3.9/site-packages/rest_framework/views.py", line 469, in           
                    handle_exception                                                                                                                   
                        self.raise_uncaught_exception(exc)                                                                                             
                      File "/home/yoh/proj/dandi/dandi-archive/venvs/dev3/lib/python3.9/site-packages/rest_framework/views.py", line 480, in           
                    raise_uncaught_exception                                                                                                           
                        raise exc                                                                                                                      
                      File "/home/yoh/proj/dandi/dandi-archive/venvs/dev3/lib/python3.9/site-packages/rest_framework/views.py", line 506, in           
                    dispatch                                                                                                                           
                        response = handler(request, *args, **kwargs)                                                                                   
                      File "/home/yoh/proj/dandi/dandi-archive/venvs/dev3/lib/python3.9/site-packages/rest_framework/decorators.py", line              
                    50, in handler                                                                                                                     
                        return func(*args, **kwargs)                                                                                                   
                      File "/home/yoh/proj/dandi/dandi-archive/dandiapi/api/views/users.py", line 67, in users_me_view                                 
                        user_dict = user_to_dict(request.user)                                                                                         
                      File "/home/yoh/proj/dandi/dandi-archive/dandiapi/api/views/users.py", line 29, in user_to_dict                                  
                        'status': user.metadata.status,                                                                                                
                      File "/home/yoh/proj/dandi/dandi-archive/venvs/dev3/lib/python3.9/site-packages/django/db/models/fields/related_descri           
                    ptors.py", line 461, in __get__                                                                                                    
                        raise self.RelatedObjectDoesNotExist(                                                                                          
                    django.contrib.auth.models.User.metadata.RelatedObjectDoesNotExist: User has no metadata.                                          
           ERROR    "GET /api/users/me/ HTTP/1.1" 500 133940  

suggesting that something about my user freaks server out. Confirmed by going to http://localhost:8000/api/users/me/ in the browser directly.

I think regardless of the problem with the user, server must not 500

@yarikoptic yarikoptic added the DX Affects developer experience label May 5, 2022
yarikoptic added a commit that referenced this issue May 5, 2022
…OMPLETE

Underlying issue is somewhere in the logic/instructions so that locally created
user lacks .metadata somehow, although it seems to be available within /admin
interface among Users table for that user.  So must be some glue missing etc.
With this workaround it should still work ok, but would avoid crashing
and due to INCOMPLETE I would expect that user lacking any super powers
otherwise granted

Workaround for #1085
@yarikoptic
Copy link
Member Author

with @djarecka we also got into the same error while trying to login using local user login but whenever neither first or last name of user were yet known to DB. Apparently if name is set (and I think I did it manually in /admin interface) then attempt to login does not go to questionnaire server side pages and just crashes while trying to get .metadata. And if names aren't known -- it does go to questionnair but then after names are entered it kabooms with that .metadata fieasco.

I think that .metadata is likely populated by OAuth authentication but not whenever it is a local user. And that is what gives grief here and forbids proceeding local user through the questionnair. @djarecka will try to reproduce from tabula rasa state and report back if above observation generalizes.

@djarecka
Copy link
Member

djarecka commented May 6, 2022

Yes, if you do not set name and last name for your user in http://localhost:8000/admin/, you will be asked for name and last name when you try to login in http://localhost:8085 and you will get this crash:
Screen Shot 2022-05-06 at 2 12 34 PM

edit

actually, adding First Nam and Last Name doesn't solve the issue completely, this would give you new error on http://localhost:8085/:
Screen Shot 2022-05-06 at 2 31 44 PM

Looks like using @Yarik patch from #1086 AND adding First Name & Last Name gives me a way to finally log in.

Although, still not able to create a new dataset:
Screen Shot 2022-05-06 at 2 37 11 PM

(you have to change your status to approved in http://localhost:8000/admin/auth/user/ to b able to create new datasets)

@mvandenburgh
Copy link
Member

I'm able to reproduce this as well. It seems that the manage.py createsuperuser command doesn't create the UserMetadata for the new user.

@yarikoptic
Copy link
Member Author

and I don't think that superuser can login into the archive, can he/she?

@yarikoptic
Copy link
Member Author

@mvandenburgh could you please look into this issue again -- I decided to try local instance to develop but immediately ran into this issue

@mvandenburgh
Copy link
Member

@mvandenburgh could you please look into this issue again -- I decided to try local instance to develop but immediately ran into this issue

The changes in #1113 will fix this. In the meantime, this README line from another PR describes a workaround for this - https://github.com/dandi/dandi-archive/pull/1809/files#diff-fc2a310fcfedfefb0046def697f932def80cbb1e78c689bc0af3c8eab3e33eceR10

@mvandenburgh
Copy link
Member

Closed by #1113

@dandibot
Copy link
Member

🚀 Issue was released in v0.3.77 🚀

@dandibot dandibot added the released This issue/pull request has been released. label Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working DX Affects developer experience released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants