Skip to content
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

Cleanup #208

Merged
merged 12 commits into from
Oct 7, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.scalecube.config.ConfigRegistry;
import io.scalecube.config.ConfigRegistrySettings;
import io.scalecube.config.StringConfigProperty;
import io.scalecube.config.audit.Slf4JConfigEventListener;
import io.scalecube.config.audit.LoggingConfigEventListener;
import io.scalecube.config.source.ClassPathConfigSource;
import io.scalecube.config.source.FileDirectoryConfigSource;
import java.nio.file.Path;
Expand All @@ -28,7 +28,7 @@ public static void main(String[] args) {
.addLastSource("classpath", new ClassPathConfigSource(propsPredicate))
.addLastSource(
"configDirectory", new FileDirectoryConfigSource(basePath, propsPredicate))
.addListener(new Slf4JConfigEventListener())
.addListener(new LoggingConfigEventListener())
.jmxEnabled(true)
.jmxMBeanName("config.exporter:name=ConfigRegistry")
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.scalecube.config.ConfigRegistry;
import io.scalecube.config.ConfigRegistrySettings;
import io.scalecube.config.StringConfigProperty;
import io.scalecube.config.audit.Slf4JConfigEventListener;
import io.scalecube.config.audit.LoggingConfigEventListener;
import io.scalecube.config.source.FileDirectoryConfigSource;
import java.nio.file.Path;
import java.util.function.Predicate;
Expand Down Expand Up @@ -33,7 +33,7 @@ public static void main(String[] args) {
ConfigRegistrySettings.builder()
.addLastSource(
"ConfigDirectory", new FileDirectoryConfigSource(basePath, propsPredicate))
.addListener(new Slf4JConfigEventListener())
.addListener(new LoggingConfigEventListener())
.keepRecentConfigEvents(10)
.reloadIntervalSec(3)
.jmxEnabled(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.scalecube.config.ListConfigProperty;
import io.scalecube.config.ObjectConfigProperty;
import io.scalecube.config.StringConfigProperty;
import io.scalecube.config.audit.Slf4JConfigEventListener;
import io.scalecube.config.audit.LoggingConfigEventListener;
import io.scalecube.config.source.FileDirectoryConfigSource;
import java.io.BufferedWriter;
import java.io.File;
Expand Down Expand Up @@ -45,7 +45,7 @@ public static void main(String[] args) throws Exception {
basePath,
Stream.of(reloadablePropsPredicate, propsPredicate)
.collect(Collectors.toList())))
.addListener(new Slf4JConfigEventListener())
.addListener(new LoggingConfigEventListener())
.reloadIntervalSec(1)
.build());

Expand Down
52 changes: 50 additions & 2 deletions config-vault/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -20,12 +22,58 @@
<groupId>com.bettercloud</groupId>
<artifactId>vault-java-driver</artifactId>
</dependency>
<!-- Tests -->
<!-- Test -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>vault</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito-junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,23 @@ public class KubernetesVaultTokenSupplier implements VaultTokenSupplier {

private static final EnvironmentLoader ENVIRONMENT_LOADER = new EnvironmentLoader();

private String vaultRole = ENVIRONMENT_LOADER.loadVariable("VAULT_ROLE");
private final String vaultRole;
private final String vaultJwtProvider;
private final String serviceAccountTokenPath;

private String vaultJwtProvider =
Optional.ofNullable(
Optional.ofNullable(ENVIRONMENT_LOADER.loadVariable("VAULT_JWT_PROVIDER"))
.orElse(ENVIRONMENT_LOADER.loadVariable("VAULT_MOUNT_POINT")))
.orElse("kubernetes");

private String serviceAccountTokenPath =
Optional.ofNullable(ENVIRONMENT_LOADER.loadVariable("SERVICE_ACCOUNT_TOKEN_PATH"))
.orElse("/var/run/secrets/kubernetes.io/serviceaccount/token");

public KubernetesVaultTokenSupplier vaultRole(String vaultRole) {
this.vaultRole = vaultRole;
return this;
private KubernetesVaultTokenSupplier(Builder builder) {
this.vaultRole = Objects.requireNonNull(builder.vaultRole, "vault role");
this.vaultJwtProvider = Objects.requireNonNull(builder.vaultJwtProvider, "jwt provider");
this.serviceAccountTokenPath =
Objects.requireNonNull(builder.serviceAccountTokenPath, "k8s service account token path");
}

public KubernetesVaultTokenSupplier vaultJwtProvider(String vaultJwtProvider) {
this.vaultJwtProvider = vaultJwtProvider;
return this;
}

public KubernetesVaultTokenSupplier serviceAccountTokenPath(String serviceAccountTokenPath) {
this.serviceAccountTokenPath = serviceAccountTokenPath;
return this;
public static Builder builder() {
return new Builder();
}

@Override
public String getToken(VaultConfig config) {
Objects.requireNonNull(vaultRole, "vault role");
Objects.requireNonNull(vaultJwtProvider, "jwt provider");
Objects.requireNonNull(serviceAccountTokenPath, "k8s service account token path");
try (Stream<String> stream = Files.lines(Paths.get(serviceAccountTokenPath))) {
String jwt = stream.collect(Collectors.joining());
return Objects.requireNonNull(
Expand All @@ -59,4 +44,40 @@ public String getToken(VaultConfig config) {
throw ThrowableUtil.propagate(e);
}
}

public static class Builder {

private String vaultRole = ENVIRONMENT_LOADER.loadVariable("VAULT_ROLE");

private String vaultJwtProvider =
Optional.ofNullable(
Optional.ofNullable(ENVIRONMENT_LOADER.loadVariable("VAULT_JWT_PROVIDER"))
.orElse(ENVIRONMENT_LOADER.loadVariable("VAULT_MOUNT_POINT")))
.orElse("kubernetes");

private String serviceAccountTokenPath =
Optional.ofNullable(ENVIRONMENT_LOADER.loadVariable("SERVICE_ACCOUNT_TOKEN_PATH"))
.orElse("/var/run/secrets/kubernetes.io/serviceaccount/token");

private Builder() {}

public Builder vaultRole(String vaultRole) {
this.vaultRole = vaultRole;
return this;
}

public Builder vaultJwtProvider(String vaultJwtProvider) {
this.vaultJwtProvider = vaultJwtProvider;
return this;
}

public Builder serviceAccountTokenPath(String serviceAccountTokenPath) {
this.serviceAccountTokenPath = serviceAccountTokenPath;
return this;
}

public KubernetesVaultTokenSupplier build() {
return new KubernetesVaultTokenSupplier(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.scalecube.config.vault;

import com.bettercloud.vault.VaultConfig;
import com.bettercloud.vault.VaultException;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.logging.Logger;

public class VaultClientTokenSupplier {

private static final Logger LOGGER = Logger.getLogger(VaultClientTokenSupplier.class.getName());

private final String vaultAddress;
private final String vaultToken;
private final String vaultRole;

/**
* Constructor.
*
* @param vaultAddress vaultAddress
* @param vaultToken vaultToken (must not set be together with vaultRole)
* @param vaultRole vaultRole (must not set be together with vaultToken)
*/
public VaultClientTokenSupplier(String vaultAddress, String vaultToken, String vaultRole) {
this.vaultAddress = vaultAddress;
this.vaultToken = vaultToken;
this.vaultRole = vaultRole;
if (isNullOrNoneOrEmpty(vaultAddress)) {
throw new IllegalArgumentException("Vault address is required");
}
if (isNullOrNoneOrEmpty(vaultToken) && isNullOrNoneOrEmpty(vaultRole)) {
throw new IllegalArgumentException(
"Vault auth scheme is required (specify either vaultToken or vaultRole)");
}
}

/**
* Returns new instance of {@link VaultClientTokenSupplier}.
*
* @param vaultAddress vaultAddress
* @param vaultToken vaultToken
* @return new instance of {@link VaultClientTokenSupplier}
*/
public static VaultClientTokenSupplier supplierByToken(String vaultAddress, String vaultToken) {
return new VaultClientTokenSupplier(vaultAddress, vaultToken, null);
}

/**
* Returns new instance of {@link VaultClientTokenSupplier}.
*
* @param vaultAddress vaultAddress
* @param vaultRole vaultRole
* @return new instance of {@link VaultClientTokenSupplier}
*/
public static VaultClientTokenSupplier supplierByRole(String vaultAddress, String vaultRole) {
return new VaultClientTokenSupplier(vaultAddress, null, vaultRole);
}

/**
* Obtains vault client token.
*
* @return future result
*/
public Future<String> getToken() {
return CompletableFuture.supplyAsync(this::getToken0);
}

private String getToken0() {
try {
VaultTokenSupplier vaultTokenSupplier;
VaultConfig vaultConfig;

if (!isNullOrNoneOrEmpty(vaultRole)) {
if (!isNullOrNoneOrEmpty(vaultToken)) {
LOGGER.warning(
"Taking KubernetesVaultTokenSupplier by precedence rule, "
+ "ignoring EnvironmentVaultTokenSupplier "
+ "(specify either vaultToken or vaultRole, not both)");
}
vaultTokenSupplier = KubernetesVaultTokenSupplier.builder().vaultRole(vaultRole).build();
vaultConfig = new VaultConfig().address(vaultAddress).build();
} else {
vaultTokenSupplier = new EnvironmentVaultTokenSupplier();
vaultConfig = new VaultConfig().address(vaultAddress).token(vaultToken).build();
}

return vaultTokenSupplier.getToken(vaultConfig);
} catch (VaultException e) {
throw new RuntimeException(e);
}
}

private static boolean isNullOrNoneOrEmpty(String value) {
return Objects.isNull(value)
|| "none".equalsIgnoreCase(value)
|| "null".equals(value)
|| value.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import java.util.Set;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class is an implementation of {@link ConfigSource} for Vault.
Expand All @@ -30,7 +30,7 @@
*/
public class VaultConfigSource implements ConfigSource {

private static final Logger LOGGER = LoggerFactory.getLogger(VaultConfigSource.class);
private static final Logger LOGGER = Logger.getLogger(VaultConfigSource.class.getName());

private static final EnvironmentLoader ENVIRONMENT_LOADER = new EnvironmentLoader();

Expand Down Expand Up @@ -58,12 +58,12 @@ public Map<String, ConfigProperty> loadConfig() {
result.putAll(pathProps);
} catch (VaultException ex) {
if (ex.getHttpStatusCode() == 404) {
LOGGER.warn("Unable to load config properties from: {}", path);
LOGGER.log(Level.SEVERE, "Unable to load config properties from: " + path);
} else {
throw new ConfigSourceNotAvailableException(ex);
}
} catch (Exception ex) {
LOGGER.error("Unable to load config properties from: {}, cause:", path, ex);
LOGGER.log(Level.SEVERE, "Unable to load config properties from: " + path, ex);
throw new ConfigSourceNotAvailableException(ex);
}
}
Expand Down
Loading
Loading