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

NOISSUE - Check For Subject During Policy Addition #1826

Merged
merged 19 commits into from
Aug 2, 2023

Conversation

rodneyosodo
Copy link
Member

@rodneyosodo rodneyosodo commented Jun 19, 2023

Signed-off-by: rodneyosodo blackd0t@protonmail.com

What does this do?

On things services check if the subject is a thing or a user.

  1. If the subject is a thing, check the following:
  • The user adding the policy is the owner of the thing
  • The user adding the policy has c_share action on the thing
  1. If the subject is a user, check the following:
  • The user adding the policy is the owner of the user
  • The user adding the policy has c_share action on the user

Which issue(s) does this PR fix/relate to?

No issue

List any changes that modify/break current functionality

  • Ensures that the object column in the policies table references the id column of the groups table.
  • Check for subject in addition to checking for object

Have you included tests for your changes?

Yes

Did you document any new/modified functionality?

No

Notes

To be merged after https://github.com/mainflux/mainflux/pull/1825

@codecov
Copy link

codecov bot commented Jun 20, 2023

Codecov Report

Merging #1826 (ceeaa85) into master (8c17841) will decrease coverage by 0.04%.
The diff coverage is 56.25%.

@@            Coverage Diff             @@
##           master    #1826      +/-   ##
==========================================
- Coverage   67.44%   67.40%   -0.04%     
==========================================
  Files         118      118              
  Lines        9470     9483      +13     
==========================================
+ Hits         6387     6392       +5     
- Misses       2404     2408       +4     
- Partials      679      683       +4     
Files Changed Coverage Δ
things/groups/service.go 74.57% <ø> (ø)
things/policies/policies.go 77.27% <ø> (ø)
users/groups/service.go 67.96% <ø> (ø)
things/clients/service.go 73.82% <50.00%> (ø)
users/clients/service.go 59.84% <50.00%> (ø)
things/policies/service.go 53.43% <55.55%> (-1.65%) ⬇️
users/policies/policies.go 88.63% <100.00%> (ø)
users/policies/service.go 65.06% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@rodneyosodo rodneyosodo marked this pull request as ready for review June 20, 2023 08:03
@rodneyosodo rodneyosodo requested a review from a team as a code owner June 20, 2023 08:03
Copy link
Collaborator

@dborovcanin dborovcanin left a comment

Choose a reason for hiding this comment

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

Please resolve conflicts and remove duplicate APIs (ShareClient = AddPolicy). Don't forget to update API docs accordingly.

@rodneyosodo
Copy link
Member Author

Please resolve conflicts and remove duplicate APIs (ShareClient = AddPolicy). Don't forget to update API docs accordingly.

That is done at https://github.com/mainflux/mainflux/pull/1825

@@ -101,7 +107,7 @@ func (svc service) Authorize(ctx context.Context, ar AccessRequest) (Policy, err
// 1. The client is admin
//
// 2. The client has `g_add` action on the object or is the owner of the object.
func (svc service) AddPolicy(ctx context.Context, token string, p Policy) (Policy, error) {
func (svc service) AddPolicy(ctx context.Context, token, client string, p Policy) (Policy, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's make the client param bool flag external. At the moment, external indicates anything that's not a Thing, and later we'll add verification if it's a user.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't forget to document this param, it's very important.

@rodneyosodo rodneyosodo force-pushed the NOISSUE-checksub branch 5 times, most recently from 473df09 to c02a4fd Compare June 26, 2023 15:37
things/policies/service.go Outdated Show resolved Hide resolved

return errors.ErrAuthorization
default:
return errors.New("invalid client")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Extract the error to a new exported var.

var PolicyTypes = []string{"g_add", "g_delete", "g_update", "g_list", "c_delete", "c_update", "c_list", "m_write", "m_read"}
//
// Sharing policies
// 11. c_share - share a client
Copy link
Collaborator

Choose a reason for hiding this comment

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

Elaborate: Allowed to add members of the group to the other groups.

@arvindh123
Copy link
Contributor

LGTM

@rodneyosodo rodneyosodo force-pushed the NOISSUE-checksub branch 4 times, most recently from 292e3b7 to 97363bc Compare July 10, 2023 12:30
Copy link
Contributor

@SammyOina SammyOina left a comment

Choose a reason for hiding this comment

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

Tested, LGTM

Comment on lines 16 to 23
ReadAction = "m_read"
WriteAction = "m_write"
addPolicyAction = "g_add"
sharePolicyAction = "c_share"
ClientEntityType = "client"
GroupEntityType = "group"
ThingEntityType = "thing"
thingsObjectKey = "things"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Separate logically.

pkg/sdk/go/things_test.go Show resolved Hide resolved
@@ -16,15 +16,21 @@ import (
)

const (
MyKey = "mine"
thingsObjectKey = "things"
MyKey = "mine"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason to export this (except for tests)? If not, please don't export.

)

var AdminRelationKey = []string{updateRelationKey, listRelationKey, deleteRelationKey}
var AdminRelationKey = []string{updateRelationKey, listRelationKey, deleteRelationKey, shareRelationKey}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Comment exported vars. Also, rename to AdminRelationKeys.

@rodneyosodo rodneyosodo force-pushed the NOISSUE-checksub branch 2 times, most recently from e90ae3e to ca5b8b5 Compare August 2, 2023 13:29
Comment on lines 169 to 181
switch external {
case false:
ar := AccessRequest{Subject: userID, Object: p.Subject, Action: sharePolicyAction}
if _, err := svc.policies.EvaluateThingAccess(ctx, ar); err != nil {
return err
}
case true:
if err := svc.usersAuthorize(ctx, userID, p.Subject, sharePolicyAction, ClientEntityType); err != nil {
return err
}
}

return nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
switch external {
case false:
ar := AccessRequest{Subject: userID, Object: p.Subject, Action: sharePolicyAction}
if _, err := svc.policies.EvaluateThingAccess(ctx, ar); err != nil {
return err
}
case true:
if err := svc.usersAuthorize(ctx, userID, p.Subject, sharePolicyAction, ClientEntityType); err != nil {
return err
}
}
return nil
switch {
case external:
ar := AccessRequest{Subject: userID, Object: p.Subject, Action: sharePolicyAction}
if _, err := svc.policies.EvaluateThingAccess(ctx, ar); err != nil {
return err
}
default:
if err := svc.usersAuthorize(ctx, userID, p.Subject, sharePolicyAction, ClientEntityType); err != nil {
return err
}
}
return nil

This looks a little bit more elegant. Or use simple if instead.

@@ -17,12 +17,15 @@ import (
)

const (
MyKey = "mine"
clientsObjectKey = "clients"
MyKey = "mine"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as previous Things remarks.

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Copy link
Contributor

@drasko drasko left a comment

Choose a reason for hiding this comment

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

LGTM

@drasko drasko merged commit 1d80301 into absmach:master Aug 2, 2023
1 of 3 checks passed
rodneyosodo added a commit to rodneyosodo/magistrala that referenced this pull request Aug 3, 2023
* Check For Subject During Adding Policies

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Make Object to be Group ID

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Tests

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Change  from string to  bool

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Update Tests

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* When it is Admin Don't Check Subject

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Minor Refractoring

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Group Constants

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Test if User Doesn't Have Policy

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Combine Share Things Cases

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Remove Unnecessary Case

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Check For Non NIL error

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Remove 3 Cases From Bool

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Add Listing Actions Incase of Sharing

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Capitalize comments

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Move AdminRelationKeys to Tests

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Tests After Rebase

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Rename myKey

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Simplify checkSubject

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

---------

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
WashingtonKK pushed a commit to WashingtonKK/magistrala that referenced this pull request Aug 4, 2023
* Check For Subject During Adding Policies

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Make Object to be Group ID

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Tests

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Change  from string to  bool

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Update Tests

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* When it is Admin Don't Check Subject

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Minor Refractoring

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Group Constants

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Test if User Doesn't Have Policy

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Combine Share Things Cases

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Remove Unnecessary Case

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Check For Non NIL error

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Remove 3 Cases From Bool

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Add Listing Actions Incase of Sharing

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Capitalize comments

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Move AdminRelationKeys to Tests

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Tests After Rebase

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Rename myKey

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Simplify checkSubject

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

---------

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
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.

5 participants