Skip to content

Commit

Permalink
feat(authentication): renamed config-directory to token-directory and…
Browse files Browse the repository at this point in the history
… removed somewhat redundant createDefaultFileIfNotExists (inspectIT#18)
  • Loading branch information
Heiko Holz committed Nov 24, 2022
1 parent 8afd1fb commit 528a43b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -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"
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() + ">");
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 1 addition & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,7 +30,7 @@ static class Initializer implements ApplicationContextInitializer<ConfigurableAp
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
String tokenDir = getClass().getClassLoader().getResource("security/simple-auth-provider").getFile();
TestPropertyValues.of("inspectit-eum-server.security.enabled=true", "inspectit-eum-server.security.auth-provider.simple.enabled=true", "inspectit-eum-server.security.auth-provider.simple.config-directory=" + tokenDir, "inspectit-eum-server.security.auth-provider.simple.create-default-file-if-not-exists=false")
TestPropertyValues.of("inspectit-eum-server.security.enabled=true", "inspectit-eum-server.security.auth-provider.simple.enabled=true", "inspectit-eum-server.security.auth-provider.simple.token-directory=" + tokenDir, "inspectit-eum-server.security.auth-provider.simple.default-file-name=\"\"")
.applyTo(applicationContext);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ private HashMap<String, Object> 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
Expand Down Expand Up @@ -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());
Expand All @@ -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();

Expand Down

0 comments on commit 528a43b

Please sign in to comment.