"Run as" broken + "Could not find default user admin" is logged
Closed this issue · 6 comments
Describe the bug
I have a very strange situation with one of my Lovelace instances. The instance in question is "Run as" the Guest user since it is used by guests. They should not be able to control every entity. The setup has been working with 3.x before.
![2024-05-16_18-29-52](https://private-user-images.githubusercontent.com/16245/331295049-96464e0c-1a6e-4693-8c92-57911c73fc9f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUwODgwMzksIm5iZiI6MTcyNTA4NzczOSwicGF0aCI6Ii8xNjI0NS8zMzEyOTUwNDktOTY0NjRlMGMtMWE2ZS00NjkzLThjOTItNTc5MTFjNzNmYzlmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MzElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODMxVDA3MDIxOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWVlNmM4NTE5ZWNiNzk0NDNjYmY2YTE2MjMzY2JkNzVlOWIxNzBlNDAyYWFkOGY1ZmQyOGI2ODgzM2Q5MDk5MmQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.CvIz0-qjJ97V14Pve99GUbAlNwddOCbE9xyRp9A40lI)
First off, when the adapter starts it logs
Could not find default user Administrator - Using admin - Please update your configuration.
How should I update my configuration? It also seems like "admin" exists:
![2024-05-16_18-28-57](https://private-user-images.githubusercontent.com/16245/331294391-085ede52-a4bd-4c86-8ae2-2ed0e380a238.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUwODgwMzksIm5iZiI6MTcyNTA4NzczOSwicGF0aCI6Ii8xNjI0NS8zMzEyOTQzOTEtMDg1ZWRlNTItYTRiZC00Yzg2LThhZTItMmVkMGUzODBhMjM4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MzElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODMxVDA3MDIxOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY5N2JlZGEzZmY4OWVhNzQxM2RhZjNlOTA2MGZjNzZjZDA4YTM2MDk1OGJiMjJiMGFhY2E3ZjYyMjZlNTNjYjEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.kE5w2axAnUM-HCjqeb8h-Pc3XyD9uSNUGnwgayRv2vg)
Despite the instance is running as the "Guest" user users can control entities they should not be able to control.
E.g. the entity in question is owned by me and the Administrators group and "others" cannot write the state:
![2024-05-16_18-31-40](https://private-user-images.githubusercontent.com/16245/331296529-c9b18f6d-d48c-4c47-91d7-04a902a4d2ea.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUwODgwMzksIm5iZiI6MTcyNTA4NzczOSwicGF0aCI6Ii8xNjI0NS8zMzEyOTY1MjktYzliMThmNmQtZDQ4Yy00YzQ3LTkxZDctMDRhOTAyYTRkMmVhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MzElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODMxVDA3MDIxOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTY3ZmE4NDU2YmI5NjZjN2JkOTcwNjcyY2YzMzIwNDllOWNmMzcyM2ExNmY0MzI1MDBlNTViZDZhZDJiZWUwMmUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.FZBd2k_yURZ5Vgt41CB30tyo3diBaaHo2jxhnzKFM_I)
Yet when I switch the toggle associated with the entity, the state changes.
It's probably a good idea to
- Let the admin select the "Run as" user by name
- ...but write the user ID to the adapter config
- ...and later check
defaultUserObject
based on the ID instead of the name
The issue in my case is that the guest
user ID has the Guest
user name. When selecting Guest
on the UI, defaultUser: 'guest'
is written to the adapter config but later when checking for the defaultUserObject
the user name Guest
is compared to the saved user ID guest
.
Once I patched the config to defaultUser: "Guest"
permissions are applied correctly.
Another important aspect if the configured default user cannot be found: instead of moving on with the adapter initialization and essentially removing security (quote: "all users will be able to read everything in this case.") I'd prefer if the adapter instance would stop. I was running with no security for a couple of weeks before noticing the logged warning yesterday.
The issue in my case is that the
guest
user ID has theGuest
user name. When selectingGuest
on the UI,defaultUser: 'guest'
So this is wrong then. Hm. It seems what ever is stored as user comes from a undocumented admin function... hmpf... :-/
Your PR definitively won't work for cases where username and userid are different and a user enables authorization. The translation from name to id needs to stay as it was...
Probably the adapter should augment the defaultUser to full ID and use it as ID. I honestly did not see that before.
Sorry for me being a bit grumpy last night. Foremost, thanks for your report and the work you put into fixing it.
You are perfectly right, the adapter should not switch to admin if he does not find the configured user.
The misconception in the adapter was, that the defaultUser
was supplied to the function getUserIDFromName
. Not 100% sure why it was that way. Probably because I did not get that the frontend stores the last part of the user ID instead of the name. (I did not write that part and don't know about the fillUsers
function it uses to fill the select options).
So I did change the adapter, so that config.defaultUser
is augmented to be the full ID once (that was not necessary in older js-controller versions, another reason for the misconception emerging) and uses that if auth is disabled (or in the cases where no username is available).
That being said, the getUserIDFromName
function is required in case authentication is enabled. In that case, we don't already have the ID, but the user supplies the username during login, and we need to figure out the ID from that. So the function needs to stay basically as it was.
You are welcome to test my last commits. I'll try to get a testing release ready, but for some reason I do not yet understand, two tests are currently failing (so the release process won't launch).