From 543cd2adbee864d6866a13d35b18599b12578454 Mon Sep 17 00:00:00 2001 From: reugn Date: Mon, 13 Jan 2020 00:11:04 +0200 Subject: [PATCH] use logger; write logs on parse exceptions --- src/main/java/com/bettercloud/vault/Vault.java | 4 ++-- src/main/java/com/bettercloud/vault/VaultConfig.java | 11 +++++++---- .../com/bettercloud/vault/response/AuthResponse.java | 4 ++++ .../bettercloud/vault/response/LogicalResponse.java | 9 +++++++-- .../bettercloud/vault/response/LookupResponse.java | 6 +++++- .../com/bettercloud/vault/response/SealResponse.java | 7 ++++++- 6 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/bettercloud/vault/Vault.java b/src/main/java/com/bettercloud/vault/Vault.java index 479bb09a..d7dac8ca 100644 --- a/src/main/java/com/bettercloud/vault/Vault.java +++ b/src/main/java/com/bettercloud/vault/Vault.java @@ -57,7 +57,7 @@ public class Vault { private final VaultConfig vaultConfig; - private Logger logger = Logger.getLogger(Vault.class.getCanonicalName()); + private static final Logger logger = Logger.getLogger(Vault.class.getCanonicalName()); /** * Construct a Vault driver instance with the provided config settings. @@ -282,7 +282,7 @@ private Map collectSecretEngineVersions() { } return data; } catch (RestException e) { - System.err.print(String.format("Unable to retrieve the KV Engine secrets, due to exception: %s", e.getMessage())); + logger.severe(String.format("Unable to retrieve the KV Engine secrets, due to exception: %s", e.getMessage())); return null; } } diff --git a/src/main/java/com/bettercloud/vault/VaultConfig.java b/src/main/java/com/bettercloud/vault/VaultConfig.java index 7d4545fd..47bd0f41 100644 --- a/src/main/java/com/bettercloud/vault/VaultConfig.java +++ b/src/main/java/com/bettercloud/vault/VaultConfig.java @@ -3,6 +3,7 @@ import java.io.Serializable; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Logger; /** *

A container for the configuration settings needed to initialize a Vault driver instance.

@@ -25,6 +26,8 @@ */ public class VaultConfig implements Serializable { + private static final Logger logger = Logger.getLogger(VaultConfig.class.getCanonicalName()); + protected static final String VAULT_TOKEN = "VAULT_TOKEN"; private static final String VAULT_ADDR = "VAULT_ADDR"; private static final String VAULT_OPEN_TIMEOUT = "VAULT_OPEN_TIMEOUT"; @@ -327,16 +330,16 @@ public VaultConfig build() throws VaultException { try { this.openTimeout = Integer.valueOf(environmentLoader.loadVariable(VAULT_OPEN_TIMEOUT)); } catch (NumberFormatException e) { - System.err.printf("The " + VAULT_OPEN_TIMEOUT + " environment variable contains value \"%s\", which cannot be parsed as an integer timeout period.%n", - environmentLoader.loadVariable(VAULT_OPEN_TIMEOUT)); + logger.severe(String.format("The " + VAULT_OPEN_TIMEOUT + " environment variable contains value \"%s\", which cannot be parsed as an integer timeout period.%n", + environmentLoader.loadVariable(VAULT_OPEN_TIMEOUT))); } } if (this.readTimeout == null && environmentLoader.loadVariable(VAULT_READ_TIMEOUT) != null) { try { this.readTimeout = Integer.valueOf(environmentLoader.loadVariable(VAULT_READ_TIMEOUT)); } catch (NumberFormatException e) { - System.err.printf("The " + VAULT_READ_TIMEOUT + " environment variable contains value \"%s\", which cannot be parsed as an integer timeout period.%n", - environmentLoader.loadVariable(VAULT_READ_TIMEOUT)); + logger.severe(String.format("The " + VAULT_READ_TIMEOUT + " environment variable contains value \"%s\", which cannot be parsed as an integer timeout period.%n", + environmentLoader.loadVariable(VAULT_READ_TIMEOUT))); } } if (this.sslConfig == null) { diff --git a/src/main/java/com/bettercloud/vault/response/AuthResponse.java b/src/main/java/com/bettercloud/vault/response/AuthResponse.java index 09f1e00f..6e269813 100644 --- a/src/main/java/com/bettercloud/vault/response/AuthResponse.java +++ b/src/main/java/com/bettercloud/vault/response/AuthResponse.java @@ -9,12 +9,15 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.logging.Logger; /** * This class is a container for the information returned by Vault in auth backend operations. */ public class AuthResponse extends VaultResponse { + private static final Logger logger = Logger.getLogger(AuthResponse.class.getCanonicalName()); + private Boolean renewable; private String authClientToken; private String tokenAccessor; @@ -58,6 +61,7 @@ public AuthResponse(final RestResponse restResponse, final int retries) { authPolicies.add(authPolicy.asString()); } } catch (ParseException e) { + logger.warning("RestResponse parse exception:" + e.getMessage()); } } diff --git a/src/main/java/com/bettercloud/vault/response/LogicalResponse.java b/src/main/java/com/bettercloud/vault/response/LogicalResponse.java index 93f3fb8d..25ee17b8 100644 --- a/src/main/java/com/bettercloud/vault/response/LogicalResponse.java +++ b/src/main/java/com/bettercloud/vault/response/LogicalResponse.java @@ -11,6 +11,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.logging.Logger; /** * This class is a container for the information returned by Vault in logical API @@ -18,6 +19,8 @@ */ public class LogicalResponse extends VaultResponse { + private static final Logger logger = Logger.getLogger(LogicalResponse.class.getCanonicalName()); + private Map data = new HashMap<>(); private List listData = new ArrayList<>(); private JsonObject dataObject = null; @@ -68,7 +71,8 @@ private void parseMetadataFields() { this.leaseId = jsonObject.get("lease_id").asString(); this.renewable = jsonObject.get("renewable").asBoolean(); this.leaseDuration = jsonObject.get("lease_duration").asLong(); - } catch (Exception ignored) { + } catch (Exception e) { + logger.warning("RestResponse parse exception:" + e.getMessage()); } } @@ -105,7 +109,8 @@ private void parseResponseData(final Logical.logicalOperations operation) { } } - } catch (Exception ignored) { + } catch (Exception e) { + logger.warning("RestResponse parse exception:" + e.getMessage()); } } } diff --git a/src/main/java/com/bettercloud/vault/response/LookupResponse.java b/src/main/java/com/bettercloud/vault/response/LookupResponse.java index a8fc8eda..1de29a66 100644 --- a/src/main/java/com/bettercloud/vault/response/LookupResponse.java +++ b/src/main/java/com/bettercloud/vault/response/LookupResponse.java @@ -9,12 +9,15 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.logging.Logger; /** * This class is a container for the information returned by Vault in lookup operations on auth backends. */ public class LookupResponse extends VaultResponse { + private static final Logger logger = Logger.getLogger(LookupResponse.class.getCanonicalName()); + private String accessor; private long creationTime; private long creationTTL; @@ -69,7 +72,8 @@ public LookupResponse(final RestResponse restResponse, final int retries) { renewable = dataJsonObject.getBoolean("renewable", false); ttl = dataJsonObject.getLong("ttl", 0); - } catch (ParseException ignored) { + } catch (ParseException e) { + logger.warning("RestResponse parse exception:" + e.getMessage()); } } diff --git a/src/main/java/com/bettercloud/vault/response/SealResponse.java b/src/main/java/com/bettercloud/vault/response/SealResponse.java index 3322b073..cb28443b 100644 --- a/src/main/java/com/bettercloud/vault/response/SealResponse.java +++ b/src/main/java/com/bettercloud/vault/response/SealResponse.java @@ -5,12 +5,16 @@ import com.bettercloud.vault.json.ParseException; import com.bettercloud.vault.rest.RestResponse; import java.nio.charset.StandardCharsets; +import java.util.logging.Logger; /** * This class is a container for the information returned by Vault in v1/sys/*seal* * operations. */ public class SealResponse extends VaultResponse { + + private static final Logger logger = Logger.getLogger(SealResponse.class.getCanonicalName()); + private Boolean sealed; private Long threshold; private Long numberOfShares; @@ -34,7 +38,8 @@ public SealResponse(final RestResponse restResponse, final int retries) { numberOfShares = jsonObject.getLong("n", 0); progress = jsonObject.getLong("progress", 0); - } catch (ParseException ignored) { + } catch (ParseException e) { + logger.warning("RestResponse parse exception:" + e.getMessage()); } }