-
Notifications
You must be signed in to change notification settings - Fork 662
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
MG-2201 - Add Policy to Fail Role Update if User Not on Platform #2234
base: main
Are you sure you want to change the base?
Conversation
1768f0a
to
3bf4593
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we check if the user is SuperAdmin first before updating the role, then they are certainly in the platform @arvindh123 what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we check if the user is SuperAdmin first before updating the role, then they are certainly in the platform @arvindh123 what do you think?
@rodneyosodo
In previous part of the code, we are checking the Request User is SuperAdmin or Normal User.
we are not checking the role change user ID .
It is like super admin can make other user as super admin or normal user
In role change API endpoint, SuperAdmin will be sending the user id of the other user to change the role.
So we should check the user id given by super admin in request , is exists in platform or not.
users/service_test.go
Outdated
@@ -1075,7 +1075,7 @@ func TestUpdateClientRole(t *testing.T) { | |||
updateRoleResponse: mgclients.Client{}, | |||
token: validToken, | |||
deletePolicyErr: svcerr.ErrMalformedEntity, | |||
err: svcerr.ErrDeletePolicies, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JeffMboya
Instead of this changing this test response, Please add new test case for invalid / non existance user
6c58f06
to
d519516
Compare
users/service.go
Outdated
@@ -694,6 +694,19 @@ func (svc service) addClientPolicyRollback(ctx context.Context, userID string, r | |||
} | |||
|
|||
func (svc service) updateClientPolicy(ctx context.Context, userID string, role mgclients.Role) error { | |||
res, err := svc.auth.Authorize(ctx, &magistrala.AuthorizeReq{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This authorization should happen once not during rolling back. This is because when we roll back we are sure the user is in the platform
client := mgclients.Client{
ID: cli.ID,
Role: cli.Role,
UpdatedAt: time.Now(),
UpdatedBy: tokenUserID,
}
if _, err := svc.authorize(ctx, auth.UserType, auth.UsersKind, tokenUserID, auth.MembershipPermission, auth.PlatformType, auth.MagistralaObject); err != nil {
return mgclients.Client{}, err
}
if err := svc.updateClientPolicy(ctx, cli.ID, cli.Role); err != nil {
return mgclients.Client{}, err
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rodneyosodo, since we want to verify if the user whose role the super admin wants to update is on the platform, I think the subject should be client.ID
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update accordingly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay
Signed-off-by: JeffMboya <[email protected]> refactor: add memberRelation policy Signed-off-by: JeffMboya <[email protected]> refactor: add memberRelation policy Signed-off-by: JeffMboya <[email protected]> refactor:Add memberRelation policy Signed-off-by: JeffMboya <[email protected]> refactor:Add memberRelation policy Signed-off-by: JeffMboya <[email protected]> refactor:Add memberRelation policy Signed-off-by: JeffMboya <[email protected]> refactor:Add memberRelation policy Signed-off-by: JeffMboya <[email protected]> refactor:Add memberRelation policy Signed-off-by: JeffMboya <[email protected]> refactor:Add memberRelation policy Signed-off-by: JeffMboya <[email protected]> refactor: Revert changes in grpcServer struct Signed-off-by: JeffMboya <[email protected]> refactor: Revert changes in grpcServer struct Signed-off-by: JeffMboya <[email protected]> refactor: Fix authorization error in updateClientPolicy Signed-off-by: JeffMboya <[email protected]> refactor: Fix authorization error in updateClientPolicy Signed-off-by: JeffMboya <[email protected]> refactor: Fix authorization error in updateClientPolicy Signed-off-by: JeffMboya <[email protected]>
Signed-off-by: JeffMboya <[email protected]>
Signed-off-by: JeffMboya <[email protected]>
@@ -1101,6 +1103,14 @@ func TestUpdateClientRole(t *testing.T) { | |||
updateRoleErr: svcerr.ErrAuthentication, | |||
err: svcerr.ErrAuthentication, | |||
}, | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add missing test case. You can run the following command and look at the HTML output for the missing case
cd users && go test -v --race -covermode=atomic -coverprofile=cover.out . && go tool cover -html cover.out -o cover.html
Signed-off-by: JeffMboya <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update tests.
users/service_test.go
Outdated
desc: "update client role with failed MembershipPermission authorization", | ||
client: client, | ||
identifyResponse: &magistrala.IdentityRes{UserId: client.ID}, | ||
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false}, | ||
token: validToken, | ||
err: svcerr.ErrAuthorization, | ||
}, | ||
{ | ||
desc: "Update client role for non-existent user", | ||
client: mgclients.Client{}, | ||
token: validToken, | ||
identifyResponse: &magistrala.IdentityRes{}, | ||
identifyErr: svcerr.ErrAuthorization, | ||
err: svcerr.ErrAuthorization, | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These test cases are not correct since the mock call for authorize returns tc.authorizeErr
yet you have only provided err
meaning that authorize will not return an error when it should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
desc: "update client role with failed MembershipPermission authorization", | |
client: client, | |
identifyResponse: &magistrala.IdentityRes{UserId: client.ID}, | |
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false}, | |
token: validToken, | |
err: svcerr.ErrAuthorization, | |
}, | |
{ | |
desc: "Update client role for non-existent user", | |
client: mgclients.Client{}, | |
token: validToken, | |
identifyResponse: &magistrala.IdentityRes{}, | |
identifyErr: svcerr.ErrAuthorization, | |
err: svcerr.ErrAuthorization, | |
}, | |
desc: "update client role with failed MembershipPermission authorization", | |
client: client, | |
identifyResponse: &magistrala.IdentityRes{UserId: client.ID}, | |
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false}, | |
token: validToken, | |
err: svcerr.ErrAuthorization, | |
authorizeErr: svcerr.ErrAuthorization, | |
}, | |
{ | |
desc: "Update client role for non-existent user", | |
client: mgclients.Client{}, | |
token: validToken, | |
identifyResponse: &magistrala.IdentityRes{}, | |
identifyErr: svcerr.ErrAuthorization, | |
err: svcerr.ErrAuthorization, | |
authorizeErr: svcerr.ErrAuthorization, | |
}, |
@@ -1101,6 +1103,22 @@ func TestUpdateClientRole(t *testing.T) { | |||
updateRoleErr: svcerr.ErrAuthentication, | |||
err: svcerr.ErrAuthentication, | |||
}, | |||
{ | |||
desc: "update client role with failed MembershipPermission authorization", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test does not cover your changes for a failed membership permission. It fails at svc.checkSuperAdmin
when checking for admin permissions at svc.auth.Authorize
.
Update the test and add a mock call which will pass in this case, and cover your changes.
users/service_test.go
Outdated
identifyResponse: &magistrala.IdentityRes{}, | ||
identifyErr: svcerr.ErrAuthorization, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If svc.Identify
fails, it means that the token provided is not valid, or the user who is trying to update the role does not exist and not that the client is non-existent.
Effectively this means this test will still also not cover your changes for this PR.
Signed-off-by: JeffMboya <[email protected]>
Signed-off-by: JeffMboya <[email protected]> fix: failing tests Signed-off-by: JeffMboya <[email protected]> fix: failing tests Signed-off-by: JeffMboya <[email protected]> fix: failing tests Signed-off-by: JeffMboya <[email protected]> fix: failing tests Signed-off-by: JeffMboya <[email protected]>
Signed-off-by: JeffMboya <[email protected]>
Signed-off-by: JeffMboya <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
What type of PR is this?
This is a feature because it adds the following functionality: client check check in policy layer when updating client role.
What does this do?
This PR modifies the update client role functionality to check for client existence in the policy layer (spiceDB) instead of the repo layer. This prevents the system from adding a policy for a non-existent user and then having to roll it back.
Which issue(s) does this PR fix/relate to?
Have you included tests for your changes?
No
Did you document any new/modified feature?
No