Resolve "Routes are not being generated on creation of internal bot users (support-bot, alert-bot etc)
What does this MR do?
Background
While working on #210523, I noticed that it has been described that the gitlab-alert bot that is supposed to reside at https://gitlab.com/alert-bot, is returning a 404, while ideally this should have just shown the profile of the bot, like any normal user.
Investigation.
Investigating this on my local dev environment, I was able to reproduce this behaviour in Rails console, while creating a support bot user.
# Remove any already existing support-bot
User.support_bot.destroy
# create a new one
User.support_bot
If we notice the INSERT
SQL commands created by the above command, we can notice that no new Route
records are created for this user, which ideally should have been created.
It is the absence of the route record for this user, that is causing the 404, because it cannot find this user by its path - which should have been present in the routes
table. Method
Assumptions.
My assumptions, that lead me towards a fix are:
- bot users are always created using
validate: false
. Method - Creation of a user, triggers building a new namespace. Method - notice that this method runs even if validation is skipped, since it is a
before_save
validation. - Creation of namespace, should ideally trigger building of a new route for the namespace. Method
- The above validation is a
before_validation
hook, which, during my investigation I figured does not run at all, since the whole chain of record creation from step (1), skips validations usingvalidate: false
. This trickles down to even nested associations too, and hence the route for this user is never created. (There's no need to runbefore_validation
ifvalidate
isfalse
)
Fix.
I figured there are 2 ways to fix this.
- Create new bot users with
validate: true
. But this would be hard to do, as multiple validations can fail during creation, like if a domain allow list is configured, and the email of the bot user does not fall under the specified domain. - Run the validation in
Route
duringbefore_save
too, so that the route is created, even if the creation chain is triggered withvalidate: false
. We have precedent for this kind of validations, too.
Further
I think we will need to run migrations to fix the routes of already existing bot users, without routes. We can use a migration similar to gitlab-foss@b63218e2, to do that in a follow up MR.
Screenshots
Before | After |
---|---|
Does this MR meet the acceptance criteria?
Conformity
-
Changelog entry -
Documentation (if required) -
Code review guidelines -
Merge request performance guidelines -
Style guides -
Database guides -
Separation of EE specific content
Availability and Testing
-
Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process. -
Tested in all supported browsers -
Informed Infrastructure department of a default or new setting change, if applicable per definition of done
Security
If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:
-
Label as security and @ mention @gitlab-com/gl-security/appsec
-
The MR includes necessary changes to maintain consistency between UI, API, email, or other methods -
Security reports checked/validated by a reviewer from the AppSec team