Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
import io.scalecube.config.source.ConfigSource;
import io.scalecube.config.source.LoadedConfigProperty;
import io.scalecube.config.utils.ThrowableUtil;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.slf4j.Logger;
Expand All @@ -30,6 +34,7 @@ public class VaultConfigSource implements ConfigSource {

private final Vault vault;
private final String secretsPath;
private final Duration renewEvery;

/**
* Create a new {@link VaultConfigSource} with the given {@link Builder}.
Expand All @@ -38,7 +43,34 @@ public class VaultConfigSource implements ConfigSource {
*/
private VaultConfigSource(Builder builder) {
this.secretsPath = builder.secretsPath();
this.renewEvery = builder.renewEvery;
vault = new Vault(builder.config);

if (renewEvery != null) {
long initialDelay = renewEvery.toMillis();
long period = renewEvery.toMillis();
TimeUnit unit = TimeUnit.MILLISECONDS;
ThreadFactory factory =
r -> {
Thread thread = new Thread(r);
thread.setDaemon(true);
thread.setName(VaultConfigSource.class.getSimpleName() + " token renewer");
return thread;
};
Executors.newScheduledThreadPool(1, factory)
.scheduleAtFixedRate(
() -> {
try {
vault.auth().renewSelf();
LOGGER.info("renew token success");
} catch (VaultException vaultException) {
LOGGER.error("failed to renew token", vaultException);
}
},
initialDelay,
period,
unit);
}
}

private void checkVaultStatus() throws VaultException {
Expand All @@ -56,10 +88,7 @@ public Map<String, ConfigProperty> loadConfig() {
try {
checkVaultStatus();
LogicalResponse response = vault.logical().read(this.secretsPath);
return response
.getData()
.entrySet()
.stream()
return response.getData().entrySet().stream()
.map(LoadedConfigProperty::withNameAndValue)
.map(LoadedConfigProperty.Builder::build)
.collect(Collectors.toMap(LoadedConfigProperty::name, Function.identity()));
Expand Down Expand Up @@ -104,6 +133,7 @@ public static final class Builder {

final VaultConfig config = new VaultConfig();
private final String secretsPath;
private Duration renewEvery = null;

Builder(String address, String token, String secretsPath) {
config
Expand All @@ -123,6 +153,11 @@ public Builder readTimeout(int readTimeout) {
return this;
}

public Builder renewEvery(Duration duration) {
renewEvery = duration;
return this;
}

/**
* Builds vault config source.
*
Expand Down