jimmyjames / fga-spring-examples

simple example demonstrating using FGA in a spring boot application
15 stars 4 forks source link

Add InitializeOpenFgaAutoconfiguration #5

Closed rwinch closed 8 months ago

rwinch commented 8 months ago

Similar to Spring Boot's support for initializing a database it would be nice to add support for initializing OpenFGA using convention based resources for both the authorization model(s) and initial relations.

This is very similar to LoadFgaData except:

rwinch commented 8 months ago

@jimmyjames I'm happy to submit a PR if you agree with this

Crain-32 commented 8 months ago

I got a version working locally (for the most part), just waiting on the Testcontainer PR to really test it, then I can make another PR.

It also adds in a new property fgaStoreName, since we should assume if we're populating the auth model, we need to create a Store. I've included the code for reference since I can't make a PR yet.

    private void doInitialization() throws Exception {
        if (openFgaProperties.getStoreId() != null) {
            validateStore(openFgaProperties.getFgaStoreId()); // Makes sure our Store ID is correct if provided
        } else {
            openFgaProperties.setFgaStoreId(makeStore()); // Creates a new Store using our fgaStoreName
        }

        if (openFgaProperties.getInitScript() != null) {
            var script = resourceLoader.getResource(openFgaProperties.getInitScript());
            var mapper = new ObjectMapper(); // Wrong thing to do for a proper init, but this works for now.
            var authWriteResponse = openFgaClient.writeAuthorizationModel(mapper.readValue(script.getContentAsByteArray(), WriteAuthorizationModelRequest.class)).get();
            openFgaProperties.setFgaAuthorizationModelId(authWriteResponse.getAuthorizationModelId());
        }
Crain-32 commented 8 months ago

@rwinch I see you doing some PR stuff. Are you adding this to your PR? I was just about to push my implementation before checking up on it.

Looking at the PR right now, I'll base my changes off that, and just keep an eye on any changes you make.

I also broke the gradlew in gh-6, that's on me

rwinch commented 8 months ago

@Crain-32

Looking at the PR right now, I'll base my changes off that, and just keep an eye on any changes you make.

Sorry about that. I hope that it didn't make your changes too difficult. I've finished with a single PR with multiple commits to make it easier to merge.

I also broke the gradlew in https://github.com/jimmyjames/fga-spring-examples/issues/6, that's on me

No worries. It should be fixed now.

Crain-32 commented 8 months ago

None of your changes impacted me, since I'm just targeting the LoadFgaData for my stuff.

Will make rebasing on your changes a lot easier, should that happen.