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

Allow setting Store Id per request for multi tenant implementations #150

Open
joarobles opened this issue Feb 17, 2025 · 4 comments
Open

Comments

@joarobles
Copy link

Describe the problem you'd like to have solved

In multitenant solutions it would be nice to have a single client instance and the possibility to set the store id for each request, this way the connection pool can be reused without the need to have multiple client instances.

Describe the ideal solution

The store id should available as a client option:

var request = new ClientCheckRequest()
    .user("user:81684243-9356-4421-8fbf-a4f8d36aa31b")
    .relation("writer")
    ._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a");
var options = new ClientCheckOptions()
    .additionalHeaders(Map.of("Some-Http-Header", "Some value"))
    // You can rely on the model id set in the configuration or override it for this specific request
    .authorizationModelId("01GXSA8YR785C4FYS3C0RTG7B1")
    // SET THE STORE ID PER REQUEST
    .storeId("01ARZ3NDEKTSV4RRFFQ69G5FAV");

var response = fgaClient.check(request, options).get();
// response.getAllowed() = true

Alternatives and current workarounds

There are two alternatives:

  1. Having one client for each store id
  2. Explicitly calling fgaClient.setStoreId(store.getId()); on each request, but my guess is there could be race condition issues

Additional context

N/A

@jimmyjames
Copy link
Contributor

Thanks @joarobles for raising! Yes, the Java SDK should support per-request storeId override, as the Go, .NET, and JS SDKs do.

For any method that includes a storeId in the path, we should allow for an per-request override, and if set use that. We should be able to do this just as we handle for authorizationModelId from the options: https://github.com/openfga/java-sdk/blob/main/src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java#L561

@joarobles
Copy link
Author

Excellent @jimmyjames! If you give me a couple of hints I can try to submit a PR

@jimmyjames
Copy link
Contributor

❤ that would be great @joarobles!

I pushed a temporary branch with an example of how it could look for check. Note the TODOs, there's improvments needed, but in general the pattern will be:

  • for requests that use the storeId in the path, update the request options object to include a storeId
  • In OpenFgaClient, if the options are not null and storeId is non-null and non-empty, use that to build the path. Otherwise, use the storeId configured on the client
  • Update the tests to verify that the storeId can be overridden per request, and that the value on the client is used if not overridden.

In general, it should be pretty similar to how the authorizationModelId is allowed to be overridden per request.

Hope that helps!

@joarobles
Copy link
Author

Excellent, I'm on it!

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

No branches or pull requests

2 participants