Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

rbac: oob migration fix for role assignment - #47946

Merged
BolajiOlajide merged 3 commits into
mainfrom
bo/oob-migration-fix
Feb 21, 2023
Merged

rbac: oob migration fix for role assignment#47946
BolajiOlajide merged 3 commits into
mainfrom
bo/oob-migration-fix

Conversation

@BolajiOlajide

Copy link
Copy Markdown
Contributor

Earlier today (or some days/ months ago depending on when you're reading this), I noticed that on S2 the OOB migration for assigning roles to existing users was at 99% for quite some time.

CleanShot 2023-02-21 at 12 44 21@2x

I did some digging to find out what happened, and I found out we had some site admins who weren't assigned the USER role. I confirmed they weren't newly created admins since the PR for assigning roles on creation merged pretty recently - they weren't. I suspect it's a case of the instance getting restarted when the OOB migration was in progress.

The existing logic for checking users that need to be assigned roles just checked the user_roles table to see if you had any record; we didn't check specifically for what roles were assigned.

SELECT
	id, site_admin
FROM users u
WHERE
	u.id NOT IN (SELECT user_id from user_roles)

This PR updates that query to check for the assigned roles and only sets what's needed. I also updated the test to test for this scenario.

Test plan

The test added validates that the Up method checks for specific role assignments on a USER and SITE_ADMINISTRATOR level.

@BolajiOlajide BolajiOlajide added the batch-changes Issues related to Batch Changes label Feb 21, 2023
@BolajiOlajide BolajiOlajide self-assigned this Feb 21, 2023
@cla-bot cla-bot Bot added the cla-signed label Feb 21, 2023
@BolajiOlajide
BolajiOlajide requested a review from a team February 21, 2023 11:52
@BolajiOlajide BolajiOlajide changed the title oob migration fix rbac: oob migration fix for role assignment Feb 21, 2023
@@ -65,18 +65,29 @@ WITH user_system_role AS MATERIALIZED (
site_admin_system_role AS MATERIALIZED (

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just saw above: if this OOB doesn't do anything in the down migration, does it correctly track back to 0% progress if reverted?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. If all or some of the roles are revoked, it'll be updated.

@BolajiOlajide
BolajiOlajide merged commit 72471c2 into main Feb 21, 2023
@BolajiOlajide
BolajiOlajide deleted the bo/oob-migration-fix branch February 21, 2023 16:25
coury-clark pushed a commit that referenced this pull request Feb 21, 2023
Earlier today (or some days/ months ago depending on when you're reading
this), I noticed that on S2 the OOB migration for assigning roles to
existing users was at 99% for quite some time.

<img width="918" alt="CleanShot 2023-02-21 at 12 44 21@2x"
src="https://user-images.githubusercontent.com/25608335/220336203-2fe406cb-e8bd-4c6b-9066-af14fd03d50a.png">

I did some digging to find out what happened, and I found out we had
some site admins who weren't assigned the `USER` role. I confirmed they
weren't newly created admins since the PR for assigning roles on
creation merged pretty recently - they weren't. I suspect it's a case of
the instance getting restarted when the OOB migration was in progress.

The existing logic for checking users that need to be assigned roles
just checked the `user_roles` table to see if you had any record; we
didn't check specifically for what roles were assigned.

```sql
SELECT
	id, site_admin
FROM users u
WHERE
	u.id NOT IN (SELECT user_id from user_roles)
```

This PR updates that query to check for the assigned roles and only sets
what's needed. I also updated the test to test for this scenario.

## Test plan

<!-- All pull requests REQUIRE a test plan:
https://docs.sourcegraph.com/dev/background-information/testing_principles
-->
The test added validates that the `Up` method checks for specific role
assignments on a `USER` and `SITE_ADMINISTRATOR` level.
coury-clark added a commit that referenced this pull request Feb 21, 2023
Backporting to 4.5

Original PR:

Earlier today (or some days/ months ago depending on when you're reading
this), I noticed that on S2 the OOB migration for assigning roles to
existing users was at 99% for quite some time.

<img width="918" alt="CleanShot 2023-02-21 at 12 44 21@2x"
src="https://user-images.githubusercontent.com/25608335/220336203-2fe406cb-e8bd-4c6b-9066-af14fd03d50a.png">

I did some digging to find out what happened, and I found out we had
some site admins who weren't assigned the `USER` role. I confirmed they
weren't newly created admins since the PR for assigning roles on
creation merged pretty recently - they weren't. I suspect it's a case of
the instance getting restarted when the OOB migration was in progress.

The existing logic for checking users that need to be assigned roles
just checked the `user_roles` table to see if you had any record; we
didn't check specifically for what roles were assigned.

```sql
SELECT
	id, site_admin
FROM users u
WHERE
	u.id NOT IN (SELECT user_id from user_roles)
```

This PR updates that query to check for the assigned roles and only sets
what's needed. I also updated the test to test for this scenario.

## Test plan

<!-- All pull requests REQUIRE a test plan:

https://docs.sourcegraph.com/dev/background-information/testing_principles
-->
The test added validates that the `Up` method checks for specific role
assignments on a `USER` and `SITE_ADMINISTRATOR` level.



## Test plan

<!-- All pull requests REQUIRE a test plan:
https://docs.sourcegraph.com/dev/background-information/testing_principles
-->

Co-authored-by: Bolaji Olajide <25608335+BolajiOlajide@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

batch-changes Issues related to Batch Changes cla-signed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants