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

feat: Ability to relate private documents to actors #2907

Merged
merged 12 commits into from
Oct 2, 2024

Conversation

shahzadlone
Copy link
Member

@shahzadlone shahzadlone commented Aug 12, 2024

Relevant issue(s)

Resolves #2762

Description

This PR introduces the ability to make use of the relations defined within a policy to create relationships between an actor and a document within a collection. For users sake, I have made the clients (http, and cli) not consume the policyID and resource name but instead a docID and collection name, since the collection will have the policy and resource information available we can fetch that and make lives easier for the users.

This PR also makes use of the manages feature we have had in our policy. The manages essentially defines who can make the relationship manipulation requests.

There are a lot of tests in this PR due to a lot of edge cases I wanted to have tested specific to manger, and ensuring write and read permissions don't leak (i.e. are accidently granted).

CLI Demo

The following lets the target actor be able to now read the private document:

defradb client acp relationship add \
--collection Users \
--docID bae-ff3ceb1c-b5c0-5e86-a024-dd1b16a4261c \
--relation reader \
--actor did:key:z7r8os2G88XXBNBTLj3kFR5rzUJ4VAesbX7PgsA68ak9B5RYcXF5EZEmjRzzinZndPSSwujXb4XKHG6vmKEFG6ZfsfcQn \
--identity e3b722906ee4e56368f581cd8b18ab0f48af1ea53e635e3f7b8acd076676f6ac

Result:

{
  "ExistedAlready": false // <-------------- Indicates a new relationship was formed
}

Future (out-of-scope of this PR):

How has this been tested?

  • Integration tests

Specify the platform(s) on which this was tested:

  • Manjaro WSL2

@shahzadlone shahzadlone added feature New feature or request area/acp Related to the acp (access control) system labels Aug 12, 2024
@shahzadlone shahzadlone self-assigned this Aug 12, 2024
Copy link

codecov bot commented Aug 12, 2024

Codecov Report

Attention: Patch coverage is 93.13725% with 21 lines in your changes missing coverage. Please review.

Project coverage is 79.70%. Comparing base (3101c61) to head (5c05eeb).
Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
acp/acp_source_hub.go 74.19% 4 Missing and 4 partials ⚠️
http/handler_acp.go 82.35% 4 Missing and 2 partials ⚠️
http/client_acp.go 83.33% 2 Missing and 2 partials ⚠️
internal/db/db.go 86.96% 2 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #2907      +/-   ##
===========================================
+ Coverage    79.42%   79.70%   +0.28%     
===========================================
  Files          346      348       +2     
  Lines        26715    27014     +299     
===========================================
+ Hits         21217    21530     +313     
+ Misses        3964     3957       -7     
+ Partials      1534     1527       -7     
Flag Coverage Δ
all-tests 79.70% <93.14%> (+0.28%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
acp/acp_local.go 97.56% <100.00%> (+0.29%) ⬆️
acp/errors.go 88.07% <100.00%> (+3.02%) ⬆️
acp/source_hub_client.go 91.15% <100.00%> (+2.95%) ⬆️
cli/acp_relationship.go 100.00% <100.00%> (ø)
cli/acp_relationship_add.go 100.00% <100.00%> (ø)
cli/cli.go 100.00% <100.00%> (ø)
client/db.go 91.30% <ø> (ø)
client/errors.go 59.72% <ø> (ø)
internal/db/permission/check.go 85.19% <100.00%> (ø)
internal/db/permission/permission.go 100.00% <100.00%> (ø)
... and 5 more

... and 21 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3101c61...5c05eeb. Read the comment docs.

}

var docID string
if action.DocID == -1 {
Copy link
Member Author

@shahzadlone shahzadlone Aug 26, 2024

Choose a reason for hiding this comment

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

discussion: I understand if people prefer having DocIDIndex and DocID to test these edge cases rather than -1 to test empty/invalid DocID input. I don't have any strong preferences here, happy to follow consensus.

Same for other -1 inputs as well uses as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

it sort of breaks consistency with immutable.Option, but I see how it simplifies test cases. I also don't have strong preference.

Copy link
Member Author

Choose a reason for hiding this comment

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

immutable.Option is for optional fields. This is not an optional field, it's more to test invalid input of that field. Is why I was wondering if others prefer non-index input as well (i.e. DocIDIndex for normal cases and DocID for actual invalid testing).

users:
permissions:
read:
expr: owner + reader
Copy link
Member Author

Choose a reason for hiding this comment

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

discussion: Since in these tests writer won't get read permission, even if a relation is made to make an actor a writer, they won't be able to write because the policy doesn't model ability to read. This behavior we can try manipulate on defradb level to change (however not in this PR), but opening this here to start a discussion.

Copy link
Contributor

@islamaliev islamaliev left a comment

Choose a reason for hiding this comment

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

Good work so far. I'm just not sure why it's still in Draft state.
There are a lot of tests here, but I'm not sure if all them are necessary.
A lot of repetitive code in tests, which makes it hard to read every detail.

defradb client ... --identity e3b722906ee4e56368f581cd8b18ab0f48af1ea53e635e3f7b8acd076676f6ac


Example: Let another actor read my private document:
Copy link
Contributor

Choose a reason for hiding this comment

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

praise: thanks for examples

tests/integration/acp.go Outdated Show resolved Hide resolved
tests/integration/acp.go Outdated Show resolved Hide resolved
}

var docID string
if action.DocID == -1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

it sort of breaks consistency with immutable.Option, but I see how it simplifies test cases. I also don't have strong preference.

@shahzadlone shahzadlone added this to the DefraDB v0.14 milestone Aug 29, 2024
@shahzadlone shahzadlone marked this pull request as ready for review August 29, 2024 17:26
@shahzadlone shahzadlone requested a review from a team August 29, 2024 17:38
@shahzadlone shahzadlone force-pushed the lone/doc-share-acp branch 2 times, most recently from f3edce2 to f5efbd0 Compare August 29, 2024 18:23
Copy link
Contributor

@islamaliev islamaliev left a comment

Choose a reason for hiding this comment

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

Approving assuming tests will be moved as discussed

internal/db/db.go Outdated Show resolved Hide resolved
client/db.go Outdated Show resolved Hide resolved
@shahzadlone shahzadlone force-pushed the lone/doc-share-acp branch 6 times, most recently from 14480ed to f227499 Compare September 30, 2024 18:56
Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

LGTM, thanks Shahzad!

@shahzadlone shahzadlone force-pushed the lone/doc-share-acp branch 4 times, most recently from 782e70c to a42ca82 Compare October 1, 2024 23:04
@shahzadlone shahzadlone merged commit c986312 into sourcenetwork:develop Oct 2, 2024
42 of 43 checks passed
@shahzadlone shahzadlone deleted the lone/doc-share-acp branch October 2, 2024 00:22
shahzadlone added a commit that referenced this pull request Oct 4, 2024
## Relevant issue(s)
Resolves #2906 

## Description
Follow-up to #2907, the
ability to delete a relationship (in order to revoke access from an
identity).

## CLI Demo
The following revokes the target actors 'relational' access to the
private doc (can't read anymore):

```bash
defradb client acp relationship delete \
--collection Users \
--docID bae-ff3ceb1c-b5c0-5e86-a024-dd1b16a4261c \
--relation reader \
--actor did:key:z7r8os2G88XXBNBTLj3kFR5rzUJ4VAesbX7PgsA68ak9B5RYcXF5EZEmjRzzinZndPSSwujXb4XKHG6vmKEFG6ZfsfcQn \
--identity e3b722906ee4e56368f581cd8b18ab0f48af1ea53e635e3f7b8acd076676f6ac
```

Result:
```json
{
  "RecordFound": true // <-------------- Indicates a relationship was found and deleted
}
```


## How has this been tested?
 CI + Integration Tests + Unit Tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/acp Related to the acp (access control) system feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to share a resource of DAC (document access control)
3 participants