Skip to content

Resolve "Routes are not being generated on creation of internal bot users (support-bot, alert-bot etc)

Manoj M J requested to merge fix-routes-for-internal-users into master

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:

  1. bot users are always created using validate: false. Method
  2. 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.
  3. Creation of namespace, should ideally trigger building of a new route for the namespace. Method
  4. 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 using validate: false. This trickles down to even nested associations too, and hence the route for this user is never created. (There's no need to run before_validation if validate is false)

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 during before_save too, so that the route is created, even if the creation chain is triggered with validate: 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
Screenshot_2020-07-01_at_12.09.05_PM Screenshot_2020-07-01_at_12.06.39_PM

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

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
Edited by Pavel Shutsin

Merge request reports

Loading