cmiller96 opened a new issue, #22704:
URL: https://github.com/apache/superset/issues/22704

   When trying to create a dashboard through the public API I get a response 
with a 500 status and the body is
   ```
   {
       "message": "Fatal error"
   }
   ```
   
   The logs show
   ```
   2023-01-12 06:10:50,034:ERROR:root:'AnonymousUserMixin' object has no 
attribute '_sa_instance_state'
   Traceback (most recent call last):
     File 
"/usr/local/lib/python3.8/site-packages/flask_appbuilder/api/__init__.py", line 
86, in wraps
       return f(self, *args, **kwargs)
     File "/app/superset/views/base_api.py", line 113, in wraps
       raise ex
     File "/app/superset/views/base_api.py", line 110, in wraps
       duration, response = time_function(f, self, *args, **kwargs)
     File "/app/superset/utils/core.py", line 1524, in time_function
       response = func(*args, **kwargs)
     File "/app/superset/utils/log.py", line 245, in wrapper
       value = f(*args, **kwargs)
     File "/app/superset/views/base_api.py", line 83, in wraps
       return f(self, *args, **kwargs)
     File "/app/superset/dashboards/api.py", line 507, in post
       new_model = CreateDashboardCommand(g.user, item).run()
     File "/app/superset/dashboards/commands/create.py", line 45, in run
       dashboard = DashboardDAO.create(self._properties, commit=False)
     File "/app/superset/dao/base.py", line 126, in create
       db.session.add(model)
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/scoping.py", 
line 163, in do
       return getattr(self.registry(), name)(*args, **kwargs)
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", 
line 2023, in add
       self._save_or_update_state(state)
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", 
line 2039, in _save_or_update_state
       for o, m, st_, dct_ in mapper.cascade_iterator(
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/mapper.py", 
line 3098, in cascade_iterator
       queue = deque(
     File 
"/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/relationships.py", line 
1938, in cascade_iterator
       tuples = state.manager[self.key].impl.get_all_pending(state, dict_)
     File 
"/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/attributes.py", line 
1153, in get_all_pending
       current_states = [
     File 
"/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/attributes.py", line 
1154, in <listcomp>
       ((c is not None) and instance_state(c) or None, c)
   AttributeError: 'AnonymousUserMixin' object has no attribute 
'_sa_instance_state'
   ```
   
   #### How to reproduce the bug
   
   docker-compose.yml
   ```
   version: '3'
   services:
     redis:
       image: redis
       restart: always
       volumes:
         - redis:/data
   
     superset:
       image: apache/superset:2.0.1
       restart: always
       depends_on:
         - redis
       ports:
         - "8088"
       volumes:
         - ${PWD}/superset_config.py:/app/pythonpath/superset_config.py
   
   volumes:
     redis:
   ```
   
   superset_config.py
   ```
   ENABLE_PROXY_FIX = True
   
   FEATURE_FLAGS = {
       "EMBEDDED_SUPERSET": True
   }
   
   SQLALCHEMY_DATABASE_URI = # My external postgres database uri. I get the 
same error using the internal db though.
   #SECRET_KEY = # My secret key
   
   SESSION_COOKIE_SAMESITE = None
   ENABLE_PROXY_FIX = True
   PUBLIC_ROLE_LIKE = "Gamma"
   
   CORS_OPTIONS = {
     'supports_credentials': True,
     'allow_headers': ['*'],
     'resources':['*'],
     'origins': ['*']
   }
   
   WTF_CSRF_ENABLED = False
   ```
   
   test.js
   ```
   const axios = require('axios')
   const SUPERSET_HOST = 'http://localhost:8088'
   
   async function createDashboard() {
       try {
           const result = await axios({
               url: `${SUPERSET_HOST}/api/v1/dashboard/`,
               data: {
                   dashboard_title: 'Some Title'
               },
               headers: {
                   Authorization: 'Bearer ' + await getAuthToken()
               },
               method: 'post'
           });
   
           console.log(result);
       } catch (err) {
           console.log(err.response.status); // 500
           console.log(err.response.data); // { message: 'Fatal error' }
       }
   }
   
   async function getAuthToken() {
       const result = await axios({
           url: `${SUPERSET_HOST}/api/v1/security/login`,
           data: {
               "username": 'admin',
               "password": 'admin',
               "provider": "db",
               "refresh": true
           },
           method: 'post'
       });
   
       return result?.data?.access_token;
   }
   
   createDashboard()
   ```
   
   ### Expected results
   
   Expect dashboard to get created.
   
   ### Actual results
   
   Get status code 500. 'Fatal error'
   
   ### Environment
   
   - superset version: 2.0.1
   - node.js version: v14.20.0
   - any feature flags active: Embedded Dashboards 
   
   ### Checklist
   
   - [ X] I have checked the superset logs for python stacktraces and included 
it here as text if there are any.
   - [ X] I have reproduced the issue with at least the latest released version 
of superset.
   - [ X] I have checked the issue tracker for the same issue and I haven't 
found one similar.
   
   ### Additional context
   
   The following code produces a 401. Could the user not be getting set from 
the token? I'm not performing these requests anonymously, so I don't know why I 
am getting the error `'AnonymousUserMixin' object has no attribute 
'_sa_instance_state'`.
   Also it works when I use the swagger ui, but that request sends a cookie not 
a bearer token.
   
   ```
   async function whoAmI() {
       try {
           const result = await axios( {
               url: `${SUPERSET_HOST}/api/v1/me/`,
               headers: {
                   Authorization: 'Bearer ' + await getAuthToken()
               },
               method: 'get'
           } );
   
           console.log(result);
       } catch (err) {
           console.log(err.response.status); // 401
           console.log(err.response.data); // { message: 'Not authorized' }
       }
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to