88import com .bettercloud .vault .response .LookupResponse ;
99import com .bettercloud .vault .response .VaultResponse ;
1010import com .bettercloud .vault .rest .RestResponse ;
11- import io .scalecube .config .ConfigSourceNotAvailableException ;
11+ import io .scalecube .config .utils . ThrowableUtil ;
1212import java .nio .charset .StandardCharsets ;
1313import java .util .Objects ;
1414import 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