From 16cf573a45f1b10f729508274f192e373a6f8174 Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Wed, 6 Mar 2024 13:14:33 -0600 Subject: [PATCH 1/3] chore: add spotless plugin --- build.gradle | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 4f2b7b9..257a3ce 100644 --- a/build.gradle +++ b/build.gradle @@ -3,14 +3,18 @@ plugins { id 'org.springframework.boot' version '3.2.2' apply false id 'io.spring.dependency-management' version '1.1.4' + id 'com.diffplug.spotless' version '6.25.0' + id 'maven-publish' } group = "dev.openfga" version = "0.0.1-SNAPSHOT" -sourceCompatibility = 17 -targetCompatibility = 17 +java { + sourceCompatibility = 17 + targetCompatibility = 17 +} repositories { mavenCentral() @@ -47,3 +51,29 @@ publishing { } } } + +spotless { + // comment out below to run spotless as part of the `check` task + enforceCheck false + format 'misc', { + // define the files (e.g. '*.gradle', '*.md') to apply `misc` to + target '.gitignore', '*.gradle' + // define the steps to apply to those files + trimTrailingWhitespace() + indentWithSpaces() // Takes an integer argument if you don't like 4 + endWithNewline() + } + java { + palantirJavaFormat() + removeUnusedImports() + importOrder() + } +} + +tasks.register('fmt') { + dependsOn 'spotlessApply' +} + +tasks.named('check').configure { + dependsOn 'spotlessCheck' +} \ No newline at end of file From d2a9ebac4c0a66537a1db0b62fece83c453f2416 Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Wed, 6 Mar 2024 13:17:49 -0600 Subject: [PATCH 2/3] chore: apply spotless formatting --- build.gradle | 2 +- .../ConditionalOnFgaProperties.java | 7 +- .../OpenFgaAutoConfiguration.java | 6 +- .../autoconfigure/OpenFgaProperties.java | 20 +++-- .../FgaAutoConfigurationTests.java | 75 ++++++++++--------- 5 files changed, 61 insertions(+), 49 deletions(-) diff --git a/build.gradle b/build.gradle index 257a3ce..c0f81fe 100644 --- a/build.gradle +++ b/build.gradle @@ -76,4 +76,4 @@ tasks.register('fmt') { tasks.named('check').configure { dependsOn 'spotlessCheck' -} \ No newline at end of file +} diff --git a/src/main/java/dev/openfga/autoconfigure/ConditionalOnFgaProperties.java b/src/main/java/dev/openfga/autoconfigure/ConditionalOnFgaProperties.java index b828375..662cdea 100644 --- a/src/main/java/dev/openfga/autoconfigure/ConditionalOnFgaProperties.java +++ b/src/main/java/dev/openfga/autoconfigure/ConditionalOnFgaProperties.java @@ -1,16 +1,15 @@ package dev.openfga.autoconfigure; import dev.openfga.sdk.api.client.OpenFgaClient; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @Retention(RetentionPolicy.RUNTIME) -@Target({ ElementType.TYPE, ElementType.METHOD }) +@Target({ElementType.TYPE, ElementType.METHOD}) @ConditionalOnProperty(name = {"openfga.api-url"}) @ConditionalOnClass(OpenFgaClient.class) public @interface ConditionalOnFgaProperties {} diff --git a/src/main/java/dev/openfga/autoconfigure/OpenFgaAutoConfiguration.java b/src/main/java/dev/openfga/autoconfigure/OpenFgaAutoConfiguration.java index 6c2cab1..ad32012 100644 --- a/src/main/java/dev/openfga/autoconfigure/OpenFgaAutoConfiguration.java +++ b/src/main/java/dev/openfga/autoconfigure/OpenFgaAutoConfiguration.java @@ -35,8 +35,10 @@ public ClientConfiguration openFgaConfig() { if (credentialsProperties != null) { if (OpenFgaProperties.CredentialsMethod.API_TOKEN.equals(credentialsProperties.getMethod())) { credentials.setCredentialsMethod(CredentialsMethod.API_TOKEN); - credentials.setApiToken(new ApiToken(credentialsProperties.getConfig().getApiToken())); - } else if (OpenFgaProperties.CredentialsMethod.CLIENT_CREDENTIALS.equals(credentialsProperties.getMethod())) { + credentials.setApiToken( + new ApiToken(credentialsProperties.getConfig().getApiToken())); + } else if (OpenFgaProperties.CredentialsMethod.CLIENT_CREDENTIALS.equals( + credentialsProperties.getMethod())) { ClientCredentials clientCredentials = new ClientCredentials() .clientId(credentialsProperties.getConfig().getClientId()) .clientSecret(credentialsProperties.getConfig().getClientSecret()) diff --git a/src/main/java/dev/openfga/autoconfigure/OpenFgaProperties.java b/src/main/java/dev/openfga/autoconfigure/OpenFgaProperties.java index b417ad3..aa26a51 100644 --- a/src/main/java/dev/openfga/autoconfigure/OpenFgaProperties.java +++ b/src/main/java/dev/openfga/autoconfigure/OpenFgaProperties.java @@ -3,12 +3,10 @@ import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.context.properties.ConfigurationProperties; -import java.util.Set; - /** * Properties used to configure an {@link dev.openfga.sdk.api.client.OpenFgaClient} */ -@ConfigurationProperties(prefix="openfga") +@ConfigurationProperties(prefix = "openfga") public class OpenFgaProperties implements InitializingBean { private String apiUrl; @@ -63,18 +61,24 @@ public void validate() { } CredentialsConfiguration credentialsConfig = credentialsProperty.getConfig(); switch (credentialsMethod) { - case NONE -> {} + case NONE -> {} case API_TOKEN -> { if (credentialsConfig == null || credentialsConfig.getApiToken() == null) { - throw new IllegalStateException("'API_TOKEN' credentials method specified, but no token specified"); + throw new IllegalStateException( + "'API_TOKEN' credentials method specified, but no token specified"); } } case CLIENT_CREDENTIALS -> { - if (credentialsConfig == null || credentialsConfig.getApiTokenIssuer() == null || credentialsConfig.getClientId() == null || credentialsConfig.getClientSecret() == null) { - throw new IllegalStateException("'CLIENT_CREDENTIALS' configuration must contain 'client-id', 'client-secret', and 'api-token-issuer'"); + if (credentialsConfig == null + || credentialsConfig.getApiTokenIssuer() == null + || credentialsConfig.getClientId() == null + || credentialsConfig.getClientSecret() == null) { + throw new IllegalStateException( + "'CLIENT_CREDENTIALS' configuration must contain 'client-id', 'client-secret', and 'api-token-issuer'"); } } - default -> throw new IllegalStateException("credentials method must be either 'NONE', 'API_TOKEN', or 'CLIENT_CREDENTIALS'"); + default -> throw new IllegalStateException( + "credentials method must be either 'NONE', 'API_TOKEN', or 'CLIENT_CREDENTIALS'"); } } } diff --git a/src/test/java/dev/openfga/autoconfigure/FgaAutoConfigurationTests.java b/src/test/java/dev/openfga/autoconfigure/FgaAutoConfigurationTests.java index efeb12d..671fb8a 100644 --- a/src/test/java/dev/openfga/autoconfigure/FgaAutoConfigurationTests.java +++ b/src/test/java/dev/openfga/autoconfigure/FgaAutoConfigurationTests.java @@ -1,17 +1,17 @@ package dev.openfga.autoconfigure; -import dev.openfga.sdk.api.configuration.ClientConfiguration; -import dev.openfga.sdk.api.configuration.CredentialsMethod; -import org.junit.jupiter.api.Test; -import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.runner.ApplicationContextRunner; - import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.Is.is; import static org.junit.jupiter.api.Assertions.assertThrows; +import dev.openfga.sdk.api.configuration.ClientConfiguration; +import dev.openfga.sdk.api.configuration.CredentialsMethod; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + public class FgaAutoConfigurationTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner(); @@ -38,12 +38,12 @@ public void beanConfiguredIfPropertiesPresent() { @Test public void beanConfiguredForNoAuthorization() { this.contextRunner - .withPropertyValues("openfga.api-url=https://api.fga.example", + .withPropertyValues( + "openfga.api-url=https://api.fga.example", "openfga.authorization-model-id=authorization model ID", "openfga.store-id=store ID", "openfga.credentials.method=NONE", - "openfga.credentials.config.api-token=XYZ" - ) + "openfga.credentials.config.api-token=XYZ") .withConfiguration(AutoConfigurations.of(OpenFgaAutoConfiguration.class)) .run((context) -> { ClientConfiguration config = (ClientConfiguration) context.getBean("openFgaConfig"); @@ -57,10 +57,10 @@ public void beanConfiguredForNoAuthorization() { @Test public void beanConfiguredForNoAuthorizationIfCredentialsNotSet() { this.contextRunner - .withPropertyValues("openfga.api-url=https://api.fga.example", + .withPropertyValues( + "openfga.api-url=https://api.fga.example", "openfga.authorization-model-id=authorization model ID", - "openfga.store-id=store ID" - ) + "openfga.store-id=store ID") .withConfiguration(AutoConfigurations.of(OpenFgaAutoConfiguration.class)) .run((context) -> { ClientConfiguration config = (ClientConfiguration) context.getBean("openFgaConfig"); @@ -74,12 +74,12 @@ public void beanConfiguredForNoAuthorizationIfCredentialsNotSet() { @Test public void beanConfiguredForApiToken() { this.contextRunner - .withPropertyValues("openfga.api-url=https://api.fga.example", + .withPropertyValues( + "openfga.api-url=https://api.fga.example", "openfga.authorization-model-id=authorization model ID", "openfga.store-id=store ID", "openfga.credentials.method=API_TOKEN", - "openfga.credentials.config.api-token=XYZ" - ) + "openfga.credentials.config.api-token=XYZ") .withConfiguration(AutoConfigurations.of(OpenFgaAutoConfiguration.class)) .run((context) -> { ClientConfiguration config = (ClientConfiguration) context.getBean("openFgaConfig"); @@ -94,7 +94,8 @@ public void beanConfiguredForApiToken() { @Test public void beanConfiguredForOauth2() { this.contextRunner - .withPropertyValues("openfga.api-url=https://api.fga.example", + .withPropertyValues( + "openfga.api-url=https://api.fga.example", "openfga.authorization-model-id=authorization model ID", "openfga.store-id=store ID", "openfga.credentials.method=CLIENT_CREDENTIALS", @@ -103,19 +104,20 @@ public void beanConfiguredForOauth2() { "openfga.credentials.config.client-secret=CLIENT_SECRET", "openfga.credentials.config.api-token-issuer=API_TOKEN_ISSUER", "openfga.credentials.config.api-audience=API_AUDIENCE", - "openfga.credentials.config.scopes=SCOPE1 SCOPE2" - ) + "openfga.credentials.config.scopes=SCOPE1 SCOPE2") .withConfiguration(AutoConfigurations.of(OpenFgaAutoConfiguration.class)) .run((context) -> { ClientConfiguration config = (ClientConfiguration) context.getBean("openFgaConfig"); assertThat(config.getApiUrl(), is("https://api.fga.example")); assertThat(config.getAuthorizationModelId(), is("authorization model ID")); assertThat(config.getStoreId(), is("store ID")); - assertThat(config.getCredentials().getCredentialsMethod(), is(CredentialsMethod.CLIENT_CREDENTIALS)); + assertThat( + config.getCredentials().getCredentialsMethod(), is(CredentialsMethod.CLIENT_CREDENTIALS)); assertThat(config.getCredentials().getApiToken(), is(nullValue())); assertThat(config.getCredentials().getClientCredentials().getClientId(), is("CLIENT_ID")); assertThat(config.getCredentials().getClientCredentials().getClientSecret(), is("CLIENT_SECRET")); - assertThat(config.getCredentials().getClientCredentials().getApiTokenIssuer(), is("API_TOKEN_ISSUER")); + assertThat( + config.getCredentials().getClientCredentials().getApiTokenIssuer(), is("API_TOKEN_ISSUER")); assertThat(config.getCredentials().getClientCredentials().getApiAudience(), is("API_AUDIENCE")); assertThat(config.getCredentials().getClientCredentials().getScopes(), is("SCOPE1 SCOPE2")); }); @@ -125,43 +127,48 @@ public void beanConfiguredForOauth2() { public void failsIfApiTokenMethodSetButNoToken() { IllegalStateException exception = assertThrows(IllegalStateException.class, () -> { this.contextRunner - .withPropertyValues("openfga.api-url=https://api.fga.example", + .withPropertyValues( + "openfga.api-url=https://api.fga.example", "openfga.authorization-model-id=authorization model ID", "openfga.store-id=store ID", - "openfga.credentials.method=API_TOKEN" - ) + "openfga.credentials.method=API_TOKEN") .withConfiguration(AutoConfigurations.of(OpenFgaAutoConfiguration.class)) .run((context) -> context.getBean("openFgaConfig")); }); - assertThat(exception.getCause().getMessage(), containsString("'API_TOKEN' credentials method specified, but no token specified")); + assertThat( + exception.getCause().getMessage(), + containsString("'API_TOKEN' credentials method specified, but no token specified")); } @Test public void failsIfClientCredentialsMethodSetButNotConfigured() { IllegalStateException exception = assertThrows(IllegalStateException.class, () -> { this.contextRunner - .withPropertyValues("openfga.api-url=https://api.fga.example", + .withPropertyValues( + "openfga.api-url=https://api.fga.example", "openfga.authorization-model-id=authorization model ID", "openfga.store-id=store ID", - "openfga.credentials.method=CLIENT_CREDENTIALS" - ) + "openfga.credentials.method=CLIENT_CREDENTIALS") .withConfiguration(AutoConfigurations.of(OpenFgaAutoConfiguration.class)) .run((context) -> context.getBean("openFgaConfig")); }); - assertThat(exception.getCause().getMessage(), containsString("'CLIENT_CREDENTIALS' configuration must contain 'client-id', 'client-secret', and 'api-token-issuer'")); + assertThat( + exception.getCause().getMessage(), + containsString( + "'CLIENT_CREDENTIALS' configuration must contain 'client-id', 'client-secret', and 'api-token-issuer'")); } @Test public void failsIfCredentialsWithNoMethod() { IllegalStateException exception = assertThrows(IllegalStateException.class, () -> { this.contextRunner - .withPropertyValues("openfga.api-url=https://api.fga.example", + .withPropertyValues( + "openfga.api-url=https://api.fga.example", "openfga.authorization-model-id=authorization model ID", "openfga.store-id=store ID", - "openfga.credentials.config.api-token=API_TOKEN" - ) + "openfga.credentials.config.api-token=API_TOKEN") .withConfiguration(AutoConfigurations.of(OpenFgaAutoConfiguration.class)) .run((context) -> context.getBean("openFgaConfig")); }); @@ -173,12 +180,12 @@ public void failsIfCredentialsWithNoMethod() { public void failsIfCredentialsWithInvalidMethod() { assertThrows(IllegalStateException.class, () -> { this.contextRunner - .withPropertyValues("openfga.api-url=https://api.fga.example", + .withPropertyValues( + "openfga.api-url=https://api.fga.example", "openfga.authorization-model-id=authorization model ID", "openfga.store-id=store ID", "openfga.credentials.method=INVALID", - "openfga.credentials.config.api-token=API_TOKEN" - ) + "openfga.credentials.config.api-token=API_TOKEN") .withConfiguration(AutoConfigurations.of(OpenFgaAutoConfiguration.class)) .run((context) -> context.getBean("openFgaConfig")); }); From 78ffa010408008b0be912d7f7074aea4c5623d5c Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Wed, 6 Mar 2024 13:23:24 -0600 Subject: [PATCH 3/3] cleanup build file, spotlessCheck runs on check by default --- build.gradle | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index c0f81fe..ef56ddf 100644 --- a/build.gradle +++ b/build.gradle @@ -53,14 +53,12 @@ publishing { } spotless { - // comment out below to run spotless as part of the `check` task - enforceCheck false format 'misc', { // define the files (e.g. '*.gradle', '*.md') to apply `misc` to target '.gitignore', '*.gradle' // define the steps to apply to those files trimTrailingWhitespace() - indentWithSpaces() // Takes an integer argument if you don't like 4 + indentWithSpaces() endWithNewline() } java { @@ -73,7 +71,3 @@ spotless { tasks.register('fmt') { dependsOn 'spotlessApply' } - -tasks.named('check').configure { - dependsOn 'spotlessCheck' -}