From 528a43b9255be8df15562cde39fd0b24b9347bec Mon Sep 17 00:00:00 2001 From: Heiko Holz Date: Thu, 24 Nov 2022 10:38:27 +0100 Subject: [PATCH] feat(authentication): renamed config-directory to token-directory and removed somewhat redundant createDefaultFileIfNotExists (#18) --- README.md | 6 ++---- ...eApiTokenAuthenticationProviderSettings.java | 13 +++++-------- .../SimpleApiTokenAuthenticationProvider.java | 7 ++++--- src/main/resources/application.yml | 4 +--- ...leApiTokenAuthenticationProviderIntTest.java | 4 +++- ...impleApiTokenAuthenticationProviderTest.java | 17 +++++++++++------ 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 37e0969..0f0d162 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ inspectit-eum-server: enabled: false watch: true frequency: 60s - config-directory: "" # Empty by default to force users to provide one + token-directory: "" # Empty by default to force users to provide one default-file-name: "default-token-file.yaml" create-default-file-if-not-exists: true ``` @@ -178,9 +178,7 @@ inspectit-eum-server: # How often directory should be watched for changes frequency: 60s # The directory where token files are stored - config-directory: "" # Empty by default to force users to provide one - # Flag indicates if a default token file should be created with an initial token - create-default-file-if-not-exists: true + token-directory: "" # Empty by default to force users to provide one # The name of the initial token file default-file-name: "default-token-file.yaml" ``` diff --git a/src/main/java/rocks/inspectit/oce/eum/server/configuration/model/security/authProvider/SimpleApiTokenAuthenticationProviderSettings.java b/src/main/java/rocks/inspectit/oce/eum/server/configuration/model/security/authProvider/SimpleApiTokenAuthenticationProviderSettings.java index 61888a0..f083f04 100644 --- a/src/main/java/rocks/inspectit/oce/eum/server/configuration/model/security/authProvider/SimpleApiTokenAuthenticationProviderSettings.java +++ b/src/main/java/rocks/inspectit/oce/eum/server/configuration/model/security/authProvider/SimpleApiTokenAuthenticationProviderSettings.java @@ -6,7 +6,6 @@ import org.springframework.validation.annotation.Validated; import javax.validation.constraints.AssertTrue; -import javax.validation.constraints.NotEmpty; import java.time.Duration; @Data @@ -21,29 +20,27 @@ public class SimpleApiTokenAuthenticationProviderSettings { /** * Path to directory where token provider files can be loaded from. */ - private String configDirectory; + private String tokenDirectory; /** - * Duration how often {@link #configDirectory} should be checked for changes. + * Duration how often {@link #tokenDirectory} should be checked for changes. */ @DurationMin(millis = 1000) private Duration frequency; /** - * Flag indicates if {@link #configDirectory} should be watched for changes. + * Flag indicates if {@link #tokenDirectory} should be watched for changes. */ private boolean watch; /** - * Name of the default token provider file if it does not exist and {@link #createDefaultFileIfNotExists} is true. + * Name of the default token provider file. If the file does not already exists in the tokenDirectory, it will be created. */ - @NotEmpty private String defaultFileName; - private boolean createDefaultFileIfNotExists = true; @AssertTrue(message = "configDirectory can not be null or empty if SimpleApiTokenAuthentication is enabled") public boolean isConfigDirectoryNotNullIfEnabled() { - return !isEnabled() || (isEnabled() && StringUtils.hasText(configDirectory)); + return !isEnabled() || (isEnabled() && StringUtils.hasText(tokenDirectory)); } } diff --git a/src/main/java/rocks/inspectit/oce/eum/server/security/authprovider/SimpleApiTokenAuthenticationProvider.java b/src/main/java/rocks/inspectit/oce/eum/server/security/authprovider/SimpleApiTokenAuthenticationProvider.java index 1fd1e99..5212b37 100644 --- a/src/main/java/rocks/inspectit/oce/eum/server/security/authprovider/SimpleApiTokenAuthenticationProvider.java +++ b/src/main/java/rocks/inspectit/oce/eum/server/security/authprovider/SimpleApiTokenAuthenticationProvider.java @@ -16,6 +16,7 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; import rocks.inspectit.oce.eum.server.configuration.model.EumServerConfiguration; import rocks.inspectit.oce.eum.server.security.ApiTokenAuthentication; import rocks.inspectit.oce.eum.server.utils.DirectoryPoller; @@ -49,7 +50,7 @@ * # How often directory should be watched for changes * frequency: 60s * # The directory where token files are stored - * config-directory: "" # Empty by default to force users to provide one + * token-directory: "" # Empty by default to force users to provide one * # Flag indicates if a default token file should be created with an initial token * create-default-file-if-not-exists: true * # The name of the initial token file @@ -93,7 +94,7 @@ public boolean supports(Class authentication) { @VisibleForTesting @PostConstruct void init() { - tokenDirectory = new File(configuration.getSecurity().getAuthProvider().getSimple().getConfigDirectory()); + tokenDirectory = new File(configuration.getSecurity().getAuthProvider().getSimple().getTokenDirectory()); if (tokenDirectory.exists() && !tokenDirectory.isDirectory()) { throw new IllegalStateException("Not a directory <" + tokenDirectory.getAbsolutePath() + ">"); @@ -132,7 +133,7 @@ private void startWatchingTokenDirectory() { } private void createDefaultTokenProviderFile() { - if (configuration.getSecurity().getAuthProvider().getSimple().isCreateDefaultFileIfNotExists()) { + if (StringUtils.hasText(configuration.getSecurity().getAuthProvider().getSimple().getDefaultFileName())) { File file = new File(tokenDirectory.getAbsolutePath() + File.separator + configuration.getSecurity() .getAuthProvider() .getSimple() diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 7127c32..d28fcb9 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -321,9 +321,7 @@ inspectit-eum-server: # How often directory should be watched for changes frequency: 60s # The directory where token files are stored - config-directory: "" # Empty by default to force users to provide one - # Flag indicates if a default token file should be created with an initial token - create-default-file-if-not-exists: true + token-directory: "" # Empty by default to force users to provide one # The name of the initial token file default-file-name: "default-token-file.yaml" diff --git a/src/test/java/rocks/inspectit/oce/eum/server/security/authprovider/SimpleApiTokenAuthenticationProviderIntTest.java b/src/test/java/rocks/inspectit/oce/eum/server/security/authprovider/SimpleApiTokenAuthenticationProviderIntTest.java index d541630..1c3c0e3 100644 --- a/src/test/java/rocks/inspectit/oce/eum/server/security/authprovider/SimpleApiTokenAuthenticationProviderIntTest.java +++ b/src/test/java/rocks/inspectit/oce/eum/server/security/authprovider/SimpleApiTokenAuthenticationProviderIntTest.java @@ -12,12 +12,14 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ContextConfiguration(initializers = SimpleApiTokenAuthenticationProviderIntTest.Initializer.class) +@DirtiesContext public class SimpleApiTokenAuthenticationProviderIntTest { @Autowired @@ -28,7 +30,7 @@ static class Initializer implements ApplicationContextInitializer defaultTokens() { private void configurationReturnsDefaultTokensDirectory() { File file = new File(getClass().getClassLoader().getResource("security/simple-auth-provider").getFile()); - when(configuration.getSecurity().getAuthProvider().getSimple().getConfigDirectory()).thenReturn(file.getAbsolutePath()); + when(configuration.getSecurity() + .getAuthProvider() + .getSimple() + .getTokenDirectory()).thenReturn(file.getAbsolutePath()); } @Nested @@ -78,8 +81,9 @@ public void authenticationFailure() { class LoadTokens { @Test - public void loadExistingTokens() throws Exception { + public void loadExistingTokens() { configurationReturnsDefaultTokensDirectory(); + authenticationProvider.init(); Object o = ReflectionTestUtils.getField(authenticationProvider, "knownTokens"); assertThat(o).isEqualTo(defaultTokens()); @@ -89,11 +93,12 @@ public void loadExistingTokens() throws Exception { @Test public void createTokenDirectoryAndCreateInitialToken(@TempDir File tempTokenDir) { String tokenDir = tempTokenDir.getAbsolutePath() + File.separator + "tokens"; - when(configuration.getSecurity().getAuthProvider().getSimple().getConfigDirectory()).thenReturn(tokenDir); - - when(configuration.getSecurity().getAuthProvider().getSimple().isCreateDefaultFileIfNotExists()).thenReturn(true); + when(configuration.getSecurity().getAuthProvider().getSimple().getTokenDirectory()).thenReturn(tokenDir); - when(configuration.getSecurity().getAuthProvider().getSimple().getDefaultFileName()).thenReturn("test-tokens.yaml"); + when(configuration.getSecurity() + .getAuthProvider() + .getSimple() + .getDefaultFileName()).thenReturn("test-tokens.yaml"); authenticationProvider.init();