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

add draft OpenFGA client #10504

Draft
wants to merge 62 commits into
base: dev
Choose a base branch
from
Draft

add draft OpenFGA client #10504

wants to merge 62 commits into from

Conversation

leovalais
Copy link
Contributor

@leovalais leovalais commented Jan 23, 2025

Note

This is a lengthy PR which also requires some amount of OpenFGA knowledge to be understood. It's probably better to peer review it.

Draft OpenFGA client.

  • Will be moved into editoast's workspace when ready (it's currently outside for dev comfort)
  • Only a few operations have been implemented
  • Meant to remain a new sub-crate which will be included in editoast_authz (in which we'll map the authorization model, define our objects, their conversion, relations, etc.)
  • The fga binary is necessary to run unit tests for now: https://github.com/openfga/cli

TODO:

  • Implement all the operations we'll need
    • Operations needed for the roles reimplementation
    • POST /stores/{}/list-users
    • POST /stores/{}/stream-list-objects
    • POST /stores/{}/expand
    • POST /stores/{}/read
    • POST /stores/{}/batch-check
    • Other operations currently not needed, but since there's so few of them, we may want to implement them for completeness?
  • Keep an authorization_model_id in the client to forward it to requests—as advised by OpenFGA
  • Improve tests utils & debugging tools
    • Client check assertions (make them public + async variant?)
    • Forward error body from OpenFGA response
    • Expose compile_model?
  • Documentation (for real this time 👀)
    • mod client
    • mod model
    • crate (ongoing)
  • INSTRUMENT EVERYTHING
  • Setup OpenFGA in docker compose
  • Add check for User.id ≠ '*'
  • Add early verification of User.id and Object.id? Not really required for us.
  • Implement list-users as this will change the trait User API
  • Macros
    • fga!
    • relations!
    • derive(User)
    • derive(Object)

About macros

This crates introduces a few macro_rules, which are only used for tests currently.

  • relations! allows defining typed relations concisely to allow quick comparisons with the OpenFGA schema. I think we should keep this one, that'll help the review process and reduce straightforward boilerplate.
  • fga_type! declares the newtype structs representing the types of OpenFGA and generates a few implementations. I think this one should remain a #[cfg(test)] definition. However I think derive macros derive(fga::User, fga::Object) would help us and cost nothing as they are trivial to implement.
  • fga! allows manipulating and instantiating types and relations with the same syntax as OpenFGA, but in a type-safe way. This is particularly useful for the conciseness of unit tests. I think it should be exported.

@codecov-commenter
Copy link

codecov-commenter commented Jan 23, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 0% with 271 lines in your changes missing coverage. Please review.

Project coverage is 82.20%. Comparing base (34159e3) to head (e5416a8).
Report is 19 commits behind head on dev.

Files with missing lines Patch % Lines
fga/src/client.rs 0.00% 271 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #10504      +/-   ##
==========================================
- Coverage   82.56%   82.20%   -0.37%     
==========================================
  Files        1084     1085       +1     
  Lines      107257   107811     +554     
  Branches      729      729              
==========================================
+ Hits        88559    88622      +63     
- Misses      18656    19147     +491     
  Partials       42       42              
Flag Coverage Δ
editoast 74.39% <ø> (-0.01%) ⬇️
front 90.51% <ø> (+0.04%) ⬆️
gateway 2.18% <ø> (ø)
osrdyne 2.98% <0.00%> (-0.31%) ⬇️
railjson_generator 87.58% <ø> (ø)
tests 87.91% <ø> (ø)

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@leovalais leovalais force-pushed the lva/openfga-client branch 7 times, most recently from 131388a to ac0adfd Compare February 6, 2025 12:27
@leovalais leovalais self-assigned this Feb 6, 2025
Copy link
Contributor

@woshilapin woshilapin left a comment

Choose a reason for hiding this comment

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

Only a first pass on src/client.rs. I'll take a look at the rest later.

fga/Cargo.lock Outdated Show resolved Hide resolved
fga/src/client.rs Show resolved Hide resolved
fga/src/client.rs Outdated Show resolved Hide resolved
fga/src/client.rs Outdated Show resolved Hide resolved
fga/src/client.rs Outdated Show resolved Hide resolved
fga/src/client.rs Outdated Show resolved Hide resolved
fga/src/client.rs Outdated Show resolved Hide resolved
fga/src/client.rs Outdated Show resolved Hide resolved
fga/src/client.rs Show resolved Hide resolved
fga/src/client.rs Outdated Show resolved Hide resolved
fga/src/client.rs Outdated Show resolved Hide resolved
fga/Cargo.toml Outdated Show resolved Hide resolved
@github-actions github-actions bot added the area:editoast Work on Editoast Service label Feb 17, 2025
Copy link
Contributor

@flomonster flomonster left a comment

Choose a reason for hiding this comment

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

Small review of the docker-compose

docker-compose.yml Outdated Show resolved Hide resolved
docker-compose.yml Outdated Show resolved Hide resolved
docker-compose.yml Outdated Show resolved Hide resolved
docker-compose.yml Show resolved Hide resolved
docker-compose.yml Outdated Show resolved Hide resolved
docker-compose.yml Show resolved Hide resolved
@github-actions github-actions bot removed the area:editoast Work on Editoast Service label Feb 18, 2025
fga/src/model.rs Outdated
format!("{}:{}", Self::NAMESPACE, self.id())
}

fn tbpa() -> Tbpa<Self> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe need to be renamed all? Or wildcard?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wildcard, as discussed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fga/Cargo.toml Outdated Show resolved Hide resolved
fga/Cargo.toml Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

We're missing an additionnal step in the CI to launch the tests? (or do we wait for this crate to be merged into editoast's workspace?

fga/src/model.rs Outdated Show resolved Hide resolved
Comment on lines 36 to 69

use fga::model::Relation as _;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be move at the top.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fga/src/model.rs Outdated
Comment on lines 81 to 110
const NAMESPACE: &'static str;

fn id(&self) -> &str;

fn fga_ident(&self) -> String {
format!("{}:{}", Self::NAMESPACE, self.id())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This piece of code is the same between User and Object. How about making an trait Id that both Object and User would be a subset of?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, by yourself

Copy link
Contributor Author

Choose a reason for hiding this comment

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

trait Type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fga/src/model.rs Outdated
Comment on lines 111 to 149
pub trait AsUser {
type User: User;

fn id(&self) -> &str;

fn fga_ident(&self) -> String {
let id = self.id();
if id == "*" {
panic!(
"Refusing to generate an identifier for a type-bound public access\n\
without using the `fga::model::Tpba` type.\n\
This is a security measure to avoid having user identifiers coerced to\n\
public accesses maliciously."
);
}
format!("{}:{}", Self::User::NAMESPACE, id)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand why we need this trait. It's exactly the same than User (except User has a bit more). It feels that everywhere a AsUser would be expected, a User bound would be enough.

leovalais and others added 21 commits February 18, 2025 16:22
Signed-off-by: Leo Valais <[email protected]>
Signed-off-by: Leo Valais <[email protected]>
Signed-off-by: Leo Valais <[email protected]>
Signed-off-by: Leo Valais <[email protected]>
Signed-off-by: Leo Valais <[email protected]>
Signed-off-by: Leo Valais <[email protected]>
Signed-off-by: Leo Valais <[email protected]>
Co-authored-by: Florian Amsallem  <[email protected]>
Signed-off-by: Léo Valais <[email protected]>
Signed-off-by: Leo Valais <[email protected]>
Signed-off-by: Leo Valais <[email protected]>
Signed-off-by: Leo Valais <[email protected]>
@leovalais leovalais changed the base branch from lva/roles-openfga-edition to dev February 18, 2025 15:23
Signed-off-by: Leo Valais <[email protected]>
Signed-off-by: Leo Valais <[email protected]>
Signed-off-by: Leo Valais <[email protected]>
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.

4 participants