Skip to content

Commit b8e7b5f

Browse files
authored
Merge pull request #211 from scalecube/enh1
Cosmetic changes. Key changes - VaultClientTokenSupplier.getToken()
2 parents ceea40e + 36fe859 commit b8e7b5f

File tree

6 files changed

+74
-65
lines changed

6 files changed

+74
-65
lines changed

config-vault/src/main/java/io/scalecube/config/vault/EnvironmentVaultTokenSupplier.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
public class EnvironmentVaultTokenSupplier implements VaultTokenSupplier {
77

88
public String getToken(VaultConfig config) {
9-
return Objects.requireNonNull(config.getToken(), "vault token");
9+
return Objects.requireNonNull(config.getToken(), "VaultConfig.token is missing");
1010
}
1111
}

config-vault/src/main/java/io/scalecube/config/vault/KubernetesVaultTokenSupplier.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ private KubernetesVaultTokenSupplier(Builder builder) {
3030
public String getToken(VaultConfig config) {
3131
try (Stream<String> stream = Files.lines(Paths.get(serviceAccountTokenPath))) {
3232
String jwt = stream.collect(Collectors.joining());
33-
return Objects.requireNonNull(
34-
new Vault(config)
35-
.auth()
36-
.loginByJwt(vaultJwtProvider, vaultRole, jwt)
37-
.getAuthClientToken(),
38-
"vault token");
33+
return new Vault(config)
34+
.auth()
35+
.loginByJwt(vaultJwtProvider, vaultRole, jwt)
36+
.getAuthClientToken();
3937
} catch (Exception e) {
4038
throw ThrowableUtil.propagate(e);
4139
}

config-vault/src/main/java/io/scalecube/config/vault/VaultClientTokenSupplier.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.bettercloud.vault.VaultException;
55
import java.util.Objects;
66
import java.util.concurrent.CompletableFuture;
7-
import java.util.concurrent.Future;
87
import org.slf4j.Logger;
98
import org.slf4j.LoggerFactory;
109

@@ -20,8 +19,8 @@ public class VaultClientTokenSupplier {
2019
* Constructor.
2120
*
2221
* @param vaultAddress vaultAddress
23-
* @param vaultToken vaultToken (must not set be together with vaultRole)
24-
* @param vaultRole vaultRole (must not set be together with vaultToken)
22+
* @param vaultToken vaultToken (must not set be together with {@code vaultRole})
23+
* @param vaultRole vaultRole (must not set be together with {@code vaultToken})
2524
*/
2625
public VaultClientTokenSupplier(String vaultAddress, String vaultToken, String vaultRole) {
2726
this.vaultAddress = vaultAddress;
@@ -63,11 +62,7 @@ public static VaultClientTokenSupplier supplierByRole(String vaultAddress, Strin
6362
*
6463
* @return future result
6564
*/
66-
public Future<String> getToken() {
67-
return CompletableFuture.supplyAsync(this::getToken0);
68-
}
69-
70-
private String getToken0() {
65+
public CompletableFuture<String> getToken() {
7166
try {
7267
VaultTokenSupplier vaultTokenSupplier;
7368
VaultConfig vaultConfig;
@@ -87,7 +82,7 @@ private String getToken0() {
8782
vaultConfig = new VaultConfig().address(vaultAddress).token(vaultToken).build();
8883
}
8984

90-
return vaultTokenSupplier.getToken(vaultConfig);
85+
return CompletableFuture.supplyAsync(() -> vaultTokenSupplier.getToken(vaultConfig));
9186
} catch (VaultException e) {
9287
throw new RuntimeException(e);
9388
}

config-vault/src/main/java/io/scalecube/config/vault/VaultConfigSource.java

+44-42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.scalecube.config.vault;
22

3+
import static io.scalecube.config.vault.VaultInvoker.STATUS_CODE_NOT_FOUND;
4+
35
import com.bettercloud.vault.EnvironmentLoader;
46
import com.bettercloud.vault.VaultConfig;
57
import com.bettercloud.vault.VaultException;
@@ -11,7 +13,6 @@
1113
import java.util.ArrayList;
1214
import java.util.Arrays;
1315
import java.util.Collection;
14-
import java.util.Collections;
1516
import java.util.HashMap;
1617
import java.util.HashSet;
1718
import java.util.Map;
@@ -33,7 +34,6 @@ public class VaultConfigSource implements ConfigSource {
3334
private static final Logger LOGGER = LoggerFactory.getLogger(VaultConfigSource.class);
3435

3536
private static final EnvironmentLoader ENVIRONMENT_LOADER = new EnvironmentLoader();
36-
3737
private static final String PATHS_SEPARATOR = ":";
3838

3939
private final VaultInvoker vault;
@@ -46,7 +46,7 @@ private VaultConfigSource(VaultInvoker vault, Collection<String> secretsPaths) {
4646

4747
@Override
4848
public Map<String, ConfigProperty> loadConfig() {
49-
Map<String, ConfigProperty> result = new HashMap<>();
49+
Map<String, ConfigProperty> propertyMap = new HashMap<>();
5050
for (String path : secretsPaths) {
5151
try {
5252
LogicalResponse response = vault.invoke(vault -> vault.logical().read(path));
@@ -55,9 +55,9 @@ public Map<String, ConfigProperty> loadConfig() {
5555
.map(LoadedConfigProperty::withNameAndValue)
5656
.map(LoadedConfigProperty.Builder::build)
5757
.collect(Collectors.toMap(LoadedConfigProperty::name, Function.identity()));
58-
result.putAll(pathProps);
58+
propertyMap.putAll(pathProps);
5959
} catch (VaultException ex) {
60-
if (ex.getHttpStatusCode() == 404) {
60+
if (ex.getHttpStatusCode() == STATUS_CODE_NOT_FOUND) {
6161
LOGGER.error("Unable to load config properties from: {}", path);
6262
} else {
6363
throw new ConfigSourceNotAvailableException(ex);
@@ -67,13 +67,12 @@ public Map<String, ConfigProperty> loadConfig() {
6767
throw new ConfigSourceNotAvailableException(ex);
6868
}
6969
}
70-
return result;
70+
return propertyMap;
7171
}
7272

7373
public static final class Builder {
7474

75-
private Function<VaultInvoker.Builder, VaultInvoker.Builder> builderFunction =
76-
Function.identity();
75+
private Function<VaultInvoker.Builder, VaultInvoker.Builder> builderFunction = b -> b;
7776

7877
private VaultInvoker invoker;
7978

@@ -89,37 +88,21 @@ public static final class Builder {
8988
public Builder() {}
9089

9190
/**
92-
* Appends {@code secretsPath} to {@code secretsPaths}.
93-
*
94-
* @param secretsPath secretsPath (may contain value with paths separated by {@code :})
95-
* @return this builder
96-
* @deprecated will be removed in future releases without notice, use {@link
97-
* #addSecretsPath(String...)} or {@link #secretsPaths(Collection)}.
98-
*/
99-
@Deprecated
100-
public Builder secretsPath(String secretsPath) {
101-
this.secretsPaths.addAll(toSecretsPaths(Collections.singletonList(secretsPath)));
102-
return this;
103-
}
104-
105-
/**
106-
* Appends one or several secretsPath\es to {@code secretsPaths}.
91+
* Appends secrets paths (each path value may contain values separated by colons).
10792
*
108-
* @param secretsPath one or several secretsPath\es (each value may contain paths separated by
109-
* {@code :})
110-
* @return this builder
93+
* @param secretsPath secretsPath
94+
* @return this
11195
*/
11296
public Builder addSecretsPath(String... secretsPath) {
113-
this.secretsPaths.addAll(toSecretsPaths(Arrays.asList(secretsPath)));
97+
secretsPaths.addAll(toSecretsPaths(Arrays.asList(secretsPath)));
11498
return this;
11599
}
116100

117101
/**
118-
* Setter for {@code secretsPaths}.
102+
* Setter for secrets paths (each path value may contain values separated by colons).
119103
*
120-
* @param secretsPaths collection of secretsPath\es (each value may contain paths separated by
121-
* colon)
122-
* @return this builder
104+
* @param secretsPaths secretsPaths
105+
* @return this
123106
*/
124107
public Builder secretsPaths(Collection<String> secretsPaths) {
125108
this.secretsPaths = toSecretsPaths(secretsPaths);
@@ -132,31 +115,50 @@ private static Set<String> toSecretsPaths(Collection<String> secretsPaths) {
132115
.collect(Collectors.toSet());
133116
}
134117

135-
public Builder invoker(VaultInvoker invoker) {
136-
this.invoker = invoker;
118+
/**
119+
* Setter for {@link VaultInvoker}.
120+
*
121+
* @param vaultInvoker vaultInvoker
122+
* @return this
123+
*/
124+
public Builder invoker(VaultInvoker vaultInvoker) {
125+
this.invoker = vaultInvoker;
137126
return this;
138127
}
139128

140-
public Builder vault(UnaryOperator<VaultInvoker.Builder> opts) {
141-
this.builderFunction = this.builderFunction.andThen(opts);
129+
/**
130+
* Setter for {@link VaultInvoker.Builder} operator.
131+
*
132+
* @param operator operator for {@link VaultInvoker.Builder}
133+
* @return this
134+
*/
135+
public Builder vault(UnaryOperator<VaultInvoker.Builder> operator) {
136+
this.builderFunction = this.builderFunction.andThen(operator);
142137
return this;
143138
}
144139

140+
/**
141+
* Setter for {@link VaultConfig}.
142+
*
143+
* @param vaultConfig vaultConfig
144+
* @return this
145+
*/
145146
public Builder config(UnaryOperator<VaultConfig> vaultConfig) {
146147
this.builderFunction = this.builderFunction.andThen(b -> b.options(vaultConfig));
147148
return this;
148149
}
149150

150-
public Builder tokenSupplier(VaultTokenSupplier supplier) {
151-
this.builderFunction = this.builderFunction.andThen(b -> b.tokenSupplier(supplier));
152-
return this;
153-
}
154-
155151
/**
156-
* Builds vault config source.
152+
* Setter for {@link VaultTokenSupplier}.
157153
*
158-
* @return instance of {@link VaultConfigSource}
154+
* @param tokenSupplier tokenSupplier
155+
* @return this
159156
*/
157+
public Builder tokenSupplier(VaultTokenSupplier tokenSupplier) {
158+
this.builderFunction = this.builderFunction.andThen(b -> b.tokenSupplier(tokenSupplier));
159+
return this;
160+
}
161+
160162
public VaultConfigSource build() {
161163
return new VaultConfigSource(
162164
invoker != null ? invoker : builderFunction.apply(new VaultInvoker.Builder()).build(),

config-vault/src/main/java/io/scalecube/config/vault/VaultInvoker.java

+20-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public class VaultInvoker {
2424

2525
private static final Logger LOGGER = LoggerFactory.getLogger(VaultInvoker.class);
2626

27-
private static final int STATUS_CODE_FORBIDDEN = 403;
27+
public static final int STATUS_CODE_FORBIDDEN = 403;
28+
public static final int STATUS_CODE_NOT_FOUND = 404;
2829
public static final int STATUS_CODE_HEALTH_OK = 200;
2930
public static final int STATUS_CODE_RESPONSE_OK = 200;
3031
public static final int STATUS_CODE_RESPONSE_NO_DATA = 204;
@@ -173,16 +174,17 @@ private void checkResponse(RestResponse restResponse) throws VaultException {
173174
* We should refresh tokens from Vault before they expire, so we add a MIN_REFRESH_MARGIN margin.
174175
* If the token is valid for less than MIN_REFRESH_MARGIN * 2, we use duration / 2 instead.
175176
*/
176-
private long suggestedRefreshInterval(long duration) {
177+
private static long suggestedRefreshInterval(long duration) {
177178
return duration < MIN_REFRESH_MARGIN * 2 ? duration / 2 : duration - MIN_REFRESH_MARGIN;
178179
}
179180

180-
private String bodyAsString(RestResponse response) {
181+
private static String bodyAsString(RestResponse response) {
181182
return new String(response.getBody(), StandardCharsets.UTF_8);
182183
}
183184

184185
@FunctionalInterface
185186
public interface VaultCall<T extends VaultResponse> {
187+
186188
T apply(Vault vault) throws VaultException;
187189
}
188190

@@ -213,13 +215,25 @@ public static class Builder {
213215

214216
public Builder() {}
215217

216-
public Builder options(UnaryOperator<VaultConfig> config) {
217-
this.options = this.options.andThen(config);
218+
/**
219+
* Setter for {@link VaultConfig} operator.
220+
*
221+
* @param operator operator for {@link VaultConfig}
222+
* @return this
223+
*/
224+
public Builder options(UnaryOperator<VaultConfig> operator) {
225+
options = options.andThen(operator);
218226
return this;
219227
}
220228

229+
/**
230+
* Setter for {@link VaultTokenSupplier}.
231+
*
232+
* @param supplier vault token supplier
233+
* @return this
234+
*/
221235
public Builder tokenSupplier(VaultTokenSupplier supplier) {
222-
this.tokenSupplier = supplier;
236+
tokenSupplier = supplier;
223237
return this;
224238
}
225239

config-vault/src/test/java/io/scalecube/config/vault/VaultConfigSourceTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ void shouldWorkWhenRegistryIsReloadedAndVaultIsUnSealed() throws InterruptedExce
273273
"vault",
274274
new VaultConfigSource.Builder()
275275
.config(vaultConfig -> vaultConfig.address(address).token(rootToken))
276-
.secretsPath(VAULT_SECRETS_PATH1)
276+
.addSecretsPath(VAULT_SECRETS_PATH1)
277277
.build())
278278
.jmxEnabled(false)
279279
.reloadIntervalSec(1)

0 commit comments

Comments
 (0)