-
Notifications
You must be signed in to change notification settings - Fork 15
feat: support initial model and tuple loading #171
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
Open
Adrastopoulos
wants to merge
2
commits into
openfga:main
Choose a base branch
from
Adrastopoulos:feat/openfga-initialization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/main/java/dev/openfga/autoconfigure/OpenFgaInitializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package dev.openfga.autoconfigure; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import dev.openfga.sdk.api.client.OpenFgaClient; | ||
| import dev.openfga.sdk.api.client.model.ClientWriteRequest; | ||
| import dev.openfga.sdk.api.configuration.ClientWriteOptions; | ||
| import dev.openfga.sdk.api.model.WriteAuthorizationModelRequest; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.boot.ApplicationArguments; | ||
| import org.springframework.boot.ApplicationRunner; | ||
| import org.springframework.core.io.Resource; | ||
| import org.springframework.core.io.ResourceLoader; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| /** | ||
| * Writes an initial authorization model, and optionally a set of initial tuples, into OpenFGA at | ||
| * application startup. This is the OpenFGA analogue of Spring Boot's {@code schema.sql}/{@code data.sql} | ||
| * database initialization. | ||
| * | ||
| * <p>The initializer is idempotent: if the configured store already has an authorization model, it | ||
| * does nothing. Otherwise it writes the model from {@code openfga.initialization.model-location} and, | ||
| * if configured, the tuples from {@code openfga.initialization.tuples-location}. Any failure is | ||
| * propagated so that the application fails fast. | ||
| */ | ||
| public class OpenFgaInitializer implements ApplicationRunner { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(OpenFgaInitializer.class); | ||
|
|
||
| private final OpenFgaClient fgaClient; | ||
| private final OpenFgaProperties.Initialization initialization; | ||
| private final ResourceLoader resourceLoader; | ||
| private final ObjectMapper objectMapper; | ||
|
|
||
| /** | ||
| * Create a new initializer. | ||
| * | ||
| * @param fgaClient the {@link OpenFgaClient} to write the model and tuples with | ||
| * @param initialization the initialization properties | ||
| * @param resourceLoader the {@link ResourceLoader} used to resolve the configured locations | ||
| * @param objectMapper the {@link ObjectMapper} used to deserialize the model and tuples | ||
| */ | ||
| public OpenFgaInitializer( | ||
| OpenFgaClient fgaClient, | ||
| OpenFgaProperties.Initialization initialization, | ||
| ResourceLoader resourceLoader, | ||
| ObjectMapper objectMapper) { | ||
| this.fgaClient = fgaClient; | ||
| this.initialization = initialization; | ||
| this.resourceLoader = resourceLoader; | ||
| this.objectMapper = objectMapper; | ||
| } | ||
|
|
||
| @Override | ||
| public void run(ApplicationArguments args) throws Exception { | ||
| if (authorizationModelExists()) { | ||
| logger.info("OpenFGA store already has an authorization model; skipping initialization"); | ||
| return; | ||
| } | ||
| String authorizationModelId = writeModel(); | ||
| writeTuples(authorizationModelId); | ||
| } | ||
|
|
||
| private boolean authorizationModelExists() throws Exception { | ||
| return fgaClient.readLatestAuthorizationModel().get().getAuthorizationModel() != null; | ||
| } | ||
|
Comment on lines
+64
to
+66
|
||
|
|
||
| private String writeModel() throws Exception { | ||
| Resource resource = resourceLoader.getResource(initialization.getModelLocation()); | ||
| if (!resource.exists()) { | ||
| throw new IllegalStateException( | ||
| "OpenFGA authorization model location does not exist: " + initialization.getModelLocation()); | ||
| } | ||
| var request = objectMapper.readValue(resource.getContentAsByteArray(), WriteAuthorizationModelRequest.class); | ||
| String authorizationModelId = | ||
| fgaClient.writeAuthorizationModel(request).get().getAuthorizationModelId(); | ||
|
Comment on lines
+75
to
+76
|
||
| logger.info( | ||
| "Wrote OpenFGA authorization model {} from {}", | ||
| authorizationModelId, | ||
| initialization.getModelLocation()); | ||
| return authorizationModelId; | ||
| } | ||
|
|
||
| private void writeTuples(String authorizationModelId) throws Exception { | ||
| if (!StringUtils.hasText(initialization.getTuplesLocation())) { | ||
| return; | ||
| } | ||
| Resource resource = resourceLoader.getResource(initialization.getTuplesLocation()); | ||
| if (!resource.exists()) { | ||
| throw new IllegalStateException( | ||
| "OpenFGA initial tuples location does not exist: " + initialization.getTuplesLocation()); | ||
| } | ||
| var request = objectMapper.readValue(resource.getContentAsByteArray(), ClientWriteRequest.class); | ||
| fgaClient | ||
| .write( | ||
| request, | ||
| new ClientWriteOptions() | ||
| .authorizationModelId(authorizationModelId) | ||
| .disableTransactions(true)) | ||
| .get(); | ||
|
Comment on lines
+94
to
+100
|
||
| logger.info("Wrote initial OpenFGA tuples from {}", initialization.getTuplesLocation()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.