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

MG-2201 - Add Policy to Fail Role Update if User Not on Platform #2234

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

JeffMboya
Copy link
Contributor

@JeffMboya JeffMboya commented May 14, 2024

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

@JeffMboya JeffMboya force-pushed the MG-2201 branch 11 times, most recently from 1768f0a to 3bf4593 Compare May 15, 2024 09:47
@JeffMboya JeffMboya marked this pull request as ready for review May 15, 2024 10:57
Copy link
Member

@rodneyosodo rodneyosodo left a 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?

auth/api/grpc/server.go Outdated Show resolved Hide resolved
Copy link
Contributor

@arvindh123 arvindh123 left a 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.

@@ -1075,7 +1075,7 @@ func TestUpdateClientRole(t *testing.T) {
updateRoleResponse: mgclients.Client{},
token: validToken,
deletePolicyErr: svcerr.ErrMalformedEntity,
err: svcerr.ErrDeletePolicies,
Copy link
Contributor

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

@JeffMboya JeffMboya force-pushed the MG-2201 branch 3 times, most recently from 6c58f06 to d519516 Compare May 21, 2024 08:28
@JeffMboya JeffMboya self-assigned this May 21, 2024
arvindh123
arvindh123 previously approved these changes May 21, 2024
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{
Copy link
Member

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
}

Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Update accordingly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay

@JeffMboya JeffMboya changed the title MG-2201 - In update user role , Add policy for role should fail if user id is not member of Magistrala platform MG-2201 - Add Policy to Fail Role Update if User Not on Platform May 22, 2024
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]>
@@ -1101,6 +1103,14 @@ func TestUpdateClientRole(t *testing.T) {
updateRoleErr: svcerr.ErrAuthentication,
err: svcerr.ErrAuthentication,
},
{
Copy link
Member

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

Copy link
Contributor

@WashingtonKK WashingtonKK left a comment

Choose a reason for hiding this comment

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

Please update tests.

Comment on lines 1107 to 1121
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,
},
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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",
Copy link
Contributor

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.

Comment on lines 1118 to 1119
identifyResponse: &magistrala.IdentityRes{},
identifyErr: svcerr.ErrAuthorization,
Copy link
Contributor

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]>

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]>
Copy link
Contributor

@WashingtonKK WashingtonKK left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants