Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth: Fix orgrole picker disabled if isSynced user #64033

Merged
merged 33 commits into from
Mar 22, 2023

Conversation

eleijonmarck
Copy link
Contributor

@eleijonmarck eleijonmarck commented Mar 2, 2023

What is this feature?

  • disables the orgrole picker if the user is synced from external provider
  • disables the basicRole picker for role picker if the users is synced
  • adds header to column with "Synced from"
  • backend checks for externalUser info
  • add tests on backend for IsExternallySynced
  • implemented feature toggle for this breaking change, onlyExternalOrgRoleSync

TODO:

  • implement featuretoggle for this
  • Currently resetting the particular User's entry/entries in user_auth table is not supported. Thus, I think the best we can do on the short term to implement what @Jguer suggested: when a provider is disabled should be to always return not ExternallySync.
  • update doc for Grafana 10 we remove the feature toggle
  • reach out to support
  • runbook available in case supports needs help - https://github.com/grafana/grafana-authnz-team/wiki/Orgrole-picker-disabled-in-users-page

Fixes #
#63835

Special notes for your reviewer:

Goes together w. enterprise - https://github.com/grafana/grafana-enterprise/pull/4774

oss test
image

enterprise test
image

@eleijonmarck eleijonmarck requested review from a team as code owners March 2, 2023 15:15
@eleijonmarck eleijonmarck requested review from joshhunt, ashharrison90, Jguer, IevaVasiljeva, sakjur, papagian and zserge and removed request for a team March 2, 2023 15:15
@eleijonmarck eleijonmarck added no-backport Skip backport of PR no-changelog Skip including change in changelog/release notes and removed no-backport Skip backport of PR labels Mar 2, 2023
@eleijonmarck eleijonmarck added this to the 9.4.3 milestone Mar 2, 2023
@eleijonmarck eleijonmarck added the backport v9.4.x Mark PR for automatic backport to v9.4.x label Mar 2, 2023
@eleijonmarck eleijonmarck added type/bug product-approved Pull requests that are approved by product/managers and are allowed to be backported labels Mar 2, 2023
@eleijonmarck eleijonmarck added product-approved Pull requests that are approved by product/managers and are allowed to be backported and removed product-approved Pull requests that are approved by product/managers and are allowed to be backported labels Mar 2, 2023
@eleijonmarck eleijonmarck dismissed chri2547’s stale review March 22, 2023 14:33

docs will be separate

Comment on lines 396 to 404
if err != nil {
if errors.Is(err, user.ErrUserNotFound) {
hs.log.Warn("Failed to get user auth info for basic auth user", cmd.UserID, nil)
} else {
hs.log.Error("Failed to get user auth info for external sync check", cmd.UserID, err)
return response.Error(http.StatusInternalServerError, "Failed to get user auth info", nil)
}
}
if qAuth.Result != nil && qAuth.Result.AuthModule != "" && login.IsExternallySynced(hs.Cfg, qAuth.Result.AuthModule) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we need to check for Auth.Results == nil for a basic Auth user. here we look at the error if the userNotFound for a basic Auth user.

Comment on lines +236 to +256
desc: "should not be able to change basicRole with a different provider",
SkipOrgRoleSync: false,
AuthEnabled: true,
AuthModule: login.GenericOAuthModule,
expectedCode: http.StatusForbidden,
},
{
desc: "should be able to change basicRole with a basic Auth",
SkipOrgRoleSync: false,
AuthEnabled: false,
AuthModule: "",
expectedCode: http.StatusOK,
},
{
desc: "should be able to change basicRole with a basic Auth",
SkipOrgRoleSync: true,
AuthEnabled: true,
AuthModule: "",
expectedCode: http.StatusOK,
},
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

added tests for basic Auth as well

@@ -67,7 +67,7 @@ func (hs *HTTPServer) getUserUserProfile(c *contextmodel.ReqContext, userID int6
authLabel := login.GetAuthProviderLabel(getAuthQuery.Result.AuthModule)
userProfile.AuthLabels = append(userProfile.AuthLabels, authLabel)
userProfile.IsExternal = true
userProfile.IsExternallySynced = login.IsExternallySynced(hs.Cfg, authLabel)
userProfile.IsExternallySynced = login.IsExternallySynced(hs.Cfg, getAuthQuery.Result.AuthModule)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

using AuthModule instead of Labels

@@ -150,7 +150,7 @@ const UserListAdminPageUnConnected = ({
<Icon name="question-circle" />
</Tooltip>
</th>
<th style={{ width: '1%' }}>Synced from</th>
<th style={{ width: '1%' }}>Origin</th>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed to Origin instead, wdyt?

Copy link
Contributor

@IevaVasiljeva IevaVasiljeva left a comment

Choose a reason for hiding this comment

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

I've just tested it again, and everything works great!

Please lower the level of the log before merging (and maybe fix the way the error is used). But apart from that this looks good 👌

err := hs.authInfoService.GetAuthInfo(c.Req.Context(), &qAuth)
if err != nil {
if errors.Is(err, user.ErrUserNotFound) {
hs.log.Warn("Failed to get user auth info for basic auth user", cmd.UserID, nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would put it at debug level - it is currently expected that a basic auth user won't have an auth info entry.

}
}
if qAuth.Result != nil && qAuth.Result.AuthModule != "" && login.IsExternallySynced(hs.Cfg, qAuth.Result.AuthModule) {
return response.ErrOrFallback(http.StatusForbidden, "Cannot change role for externally synced user", org.ErrCannotChangeRoleForExternallySyncedUser)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the intended use of errutil errors is that they are propagated down from other methods that are called from handlers. There's not much benefit of returning it directly from the handler I think. But if you want to do it, you'll need something like this:

Suggested change
return response.ErrOrFallback(http.StatusForbidden, "Cannot change role for externally synced user", org.ErrCannotChangeRoleForExternallySyncedUser)
return response.Err(org.ErrCannotChangeRoleForExternallySyncedUser.Errorf("Cannot change role for externally synced user"))

@eleijonmarck eleijonmarck merged commit 3cd952b into main Mar 22, 2023
@eleijonmarck eleijonmarck deleted the eleijonmarck/auth/skip-org-role-sync-org-page branch March 22, 2023 17:42
@eleijonmarck eleijonmarck added backport v9.4.x Mark PR for automatic backport to v9.4.x and removed no-backport Skip backport of PR labels Mar 29, 2023
@grafanabot
Copy link
Contributor

The backport to v9.4.x failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new branch
git switch --create backport-64033-to-v9.4.x origin/v9.4.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x 3cd952b8bad8d23daee10fcc303a6d19c8b6d0a0
# Push it to GitHub
git push --set-upstream origin backport-64033-to-v9.4.x
git switch main
# Remove the local backport branch
git branch -D backport-64033-to-v9.4.x

Then, create a pull request where the base branch is v9.4.x and the compare/head branch is backport-64033-to-v9.4.x.

@grafanabot grafanabot added the backport-failed Failed to generate backport PR. Please resolve conflicts and create one manually. label Mar 29, 2023
eleijonmarck added a commit that referenced this pull request Mar 29, 2023
* fix: disable orgrolepicker if externaluser is synced

* add disable to role picker

* just took me 2 hours to center the icon

* wip

* fix: check externallySyncedUser for API call

* remove check from store

* add: tests

* refactor authproxy and made tests run

* add: feature toggle

* set feature toggle for tests

* add: IsProviderEnabled

* refactor: featuretoggle name

* IsProviderEnabled tests

* add specific tests for isProviderEnabled

* fix: org_user tests

* add: owner to featuretoggle

* add missing authlabels

* remove fmt

* feature toggle

* change config

* add test for a different authmodule

* test refactor

* gen feature toggle again

* fix basic auth user able to change the org role

* test for basic auth role

* make err.base to error

* lowered lvl of log and input mesg

(cherry picked from commit 3cd952b)
eleijonmarck added a commit that referenced this pull request Mar 30, 2023
* fix: disable orgrolepicker if externaluser is synced

* add disable to role picker

* just took me 2 hours to center the icon

* wip

* fix: check externallySyncedUser for API call

* remove check from store

* add: tests

* refactor authproxy and made tests run

* add: feature toggle

* set feature toggle for tests

* add: IsProviderEnabled

* refactor: featuretoggle name

* IsProviderEnabled tests

* add specific tests for isProviderEnabled

* fix: org_user tests

* add: owner to featuretoggle

* add missing authlabels

* remove fmt

* feature toggle

* change config

* add test for a different authmodule

* test refactor

* gen feature toggle again

* fix basic auth user able to change the org role

* test for basic auth role

* make err.base to error

* lowered lvl of log and input mesg

(cherry picked from commit 3cd952b)
eleijonmarck added a commit that referenced this pull request Mar 31, 2023
* fix: disable orgrolepicker if externaluser is synced

* add disable to role picker

* just took me 2 hours to center the icon

* wip

* fix: check externallySyncedUser for API call

* remove check from store

* add: tests

* refactor authproxy and made tests run

* add: feature toggle

* set feature toggle for tests

* add: IsProviderEnabled

* refactor: featuretoggle name

* IsProviderEnabled tests

* add specific tests for isProviderEnabled

* fix: org_user tests

* add: owner to featuretoggle

* add missing authlabels

* remove fmt

* feature toggle

* change config

* add test for a different authmodule

* test refactor

* gen feature toggle again

* fix basic auth user able to change the org role

* test for basic auth role

* make err.base to error

* lowered lvl of log and input mesg

(cherry picked from commit 3cd952b)
eleijonmarck added a commit that referenced this pull request Apr 5, 2023
Auth: Fix orgrole picker disabled if isSynced user (#64033)

* fix: disable orgrolepicker if externaluser is synced

* add disable to role picker

* just took me 2 hours to center the icon

* wip

* fix: check externallySyncedUser for API call

* remove check from store

* add: tests

* refactor authproxy and made tests run

* add: feature toggle

* set feature toggle for tests

* add: IsProviderEnabled

* refactor: featuretoggle name

* IsProviderEnabled tests

* add specific tests for isProviderEnabled

* fix: org_user tests

* add: owner to featuretoggle

* add missing authlabels

* remove fmt

* feature toggle

* change config

* add test for a different authmodule

* test refactor

* gen feature toggle again

* fix basic auth user able to change the org role

* test for basic auth role

* make err.base to error

* lowered lvl of log and input mesg

(cherry picked from commit 3cd952b)
@jdbaldry
Copy link
Member

jdbaldry commented Apr 12, 2023

@eleijonmarck Should this be backported to v9.5.x? It has the 9.5.0 milestone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add to changelog area/backend area/frontend backport v9.4.x Mark PR for automatic backport to v9.4.x backport-failed Failed to generate backport PR. Please resolve conflicts and create one manually. type/bug type/docs Flags the technical writing team for documentation support; auto adds to org-wide docs project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants