Skip to content

Commit 6de9667

Browse files
committed
CR1 updates
1 parent 0ce4800 commit 6de9667

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.bettercloud.vault.EnvironmentLoader;
44
import com.bettercloud.vault.VaultConfig;
5-
import com.bettercloud.vault.VaultException;
65
import com.bettercloud.vault.response.LogicalResponse;
76
import io.scalecube.config.ConfigProperty;
87
import io.scalecube.config.ConfigSourceNotAvailableException;
@@ -45,16 +44,13 @@ private VaultConfigSource(VaultInvoker vault, String secretsPath) {
4544
public Map<String, ConfigProperty> loadConfig() {
4645
try {
4746
LogicalResponse response = vault.invoke(vault -> vault.logical().read(secretsPath));
48-
if (response.getRestResponse().getStatus() == 404) {
49-
throw new ConfigSourceNotAvailableException("unable to load config properties");
50-
}
5147
return response.getData().entrySet().stream()
5248
.map(LoadedConfigProperty::withNameAndValue)
5349
.map(LoadedConfigProperty.Builder::build)
5450
.collect(Collectors.toMap(LoadedConfigProperty::name, Function.identity()));
55-
} catch (VaultException vaultException) {
56-
LOGGER.warn("unable to load config properties", vaultException);
57-
throw new ConfigSourceNotAvailableException(vaultException);
51+
} catch (Exception ex) {
52+
LOGGER.warn("unable to load config properties", ex);
53+
throw new ConfigSourceNotAvailableException(ex);
5854
}
5955
}
6056

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.bettercloud.vault.response.LookupResponse;
99
import com.bettercloud.vault.response.VaultResponse;
1010
import com.bettercloud.vault.rest.RestResponse;
11-
import io.scalecube.config.ConfigSourceNotAvailableException;
11+
import io.scalecube.config.utils.ThrowableUtil;
1212
import java.nio.charset.StandardCharsets;
1313
import java.util.Objects;
1414
import java.util.Timer;
@@ -23,6 +23,11 @@ public class VaultInvoker {
2323

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

26+
private static final int STATUS_CODE_FORBIDDEN = 403;
27+
public static final int STATUS_CODE_HELTH_OK = 200;
28+
public static final int STATUS_CODE_RESPONSE_OK = 200;
29+
public static final int STATUS_CODE_RESPONSE_NO_DATA = 204;
30+
2631
private static final long MIN_REFRESH_MARGIN = TimeUnit.MINUTES.toSeconds(10);
2732

2833
private final Builder config;
@@ -55,7 +60,7 @@ public <T extends VaultResponse> T invoke(VaultCall<T> call) throws VaultExcepti
5560
return response;
5661
} catch (VaultException e) {
5762
// try recreate Vault according to https://www.vaultproject.io/api/overview#http-status-codes
58-
if (e.getHttpStatusCode() == 403) {
63+
if (e.getHttpStatusCode() == STATUS_CODE_FORBIDDEN) {
5964
LOGGER.warn("Authentication details are incorrect, occurred during invoking Vault", e);
6065
vault = recreateVault(vault);
6166
return call.apply(vault);
@@ -64,7 +69,7 @@ public <T extends VaultResponse> T invoke(VaultCall<T> call) throws VaultExcepti
6469
}
6570
}
6671

67-
private synchronized Vault recreateVault(Vault prev) {
72+
private synchronized Vault recreateVault(Vault prev) throws VaultException {
6873
try {
6974
if (!Objects.equals(prev, vault) && vault != null) {
7075
return vault;
@@ -101,12 +106,12 @@ private synchronized Vault recreateVault(Vault prev) {
101106
this.vault = vault;
102107
} catch (VaultException e) {
103108
LOGGER.error("Could not initialize and validate the vault", e);
104-
throw new ConfigSourceNotAvailableException(e);
109+
throw e;
105110
}
106111
return vault;
107112
}
108113

109-
private void renewToken() {
114+
private void renewToken() throws VaultException {
110115
Vault vault = this.vault;
111116
if (vault == null) {
112117
return;
@@ -133,7 +138,7 @@ private void renewToken() {
133138
}
134139
} catch (VaultException e) {
135140
// try recreate Vault according to https://www.vaultproject.io/api/overview#http-status-codes
136-
if (e.getHttpStatusCode() == 403) {
141+
if (e.getHttpStatusCode() == STATUS_CODE_FORBIDDEN) {
137142
LOGGER.warn("Could not renew the Vault token", e);
138143
//noinspection UnusedAssignment
139144
vault = recreateVault(vault);
@@ -149,7 +154,7 @@ private void renewToken() {
149154
*/
150155
private void checkVault(Vault vault) throws VaultException {
151156
RestResponse restResponse = vault.debug().health().getRestResponse();
152-
if (restResponse.getStatus() == 200) {
157+
if (restResponse.getStatus() == STATUS_CODE_HELTH_OK) {
153158
return;
154159
}
155160
throw new VaultException(bodyAsString(restResponse), restResponse.getStatus());
@@ -166,13 +171,8 @@ private void checkResponse(RestResponse restResponse) throws VaultException {
166171
}
167172
int status = restResponse.getStatus();
168173
switch (status) {
169-
case 200:
170-
case 204:
171-
return;
172-
case 404:
173-
LOGGER.warn(
174-
"Invalid path (non-existent or have no permissions to view), message: {}",
175-
bodyAsString(restResponse));
174+
case STATUS_CODE_RESPONSE_OK:
175+
case STATUS_CODE_RESPONSE_NO_DATA:
176176
return;
177177
default:
178178
String body = bodyAsString(restResponse);
@@ -202,7 +202,11 @@ private class RenewTokenTask extends TimerTask {
202202

203203
@Override
204204
public void run() {
205-
renewToken();
205+
try {
206+
renewToken();
207+
} catch (Exception e) {
208+
throw ThrowableUtil.propagate(e);
209+
}
206210
}
207211
}
208212

0 commit comments

Comments
 (0)