From 6f2d18c159f8d58e3f950e6327fd1601d3888525 Mon Sep 17 00:00:00 2001 From: Shaun Ford Date: Thu, 7 Jun 2018 11:20:46 -0700 Subject: [PATCH] Only log credentials provider failures when all providers have failed (#38) * Only log credentials provider failures when all providers fail * Update gradle version to 5.3.1 --- gradle.properties | 2 +- .../CerberusCredentialsProviderChain.java | 28 +++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/gradle.properties b/gradle.properties index 5bd19d0..3ae4d09 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,6 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -version=5.3.0 +version=5.3.1 groupId=com.nike artifactId=cerberus-client diff --git a/src/main/java/com/nike/cerberus/client/auth/CerberusCredentialsProviderChain.java b/src/main/java/com/nike/cerberus/client/auth/CerberusCredentialsProviderChain.java index 42ccbfc..d06ebc2 100644 --- a/src/main/java/com/nike/cerberus/client/auth/CerberusCredentialsProviderChain.java +++ b/src/main/java/com/nike/cerberus/client/auth/CerberusCredentialsProviderChain.java @@ -21,6 +21,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -74,6 +75,7 @@ public CerberusCredentials getCredentials() { return lastUsedProvider.getCredentials(); } + List logMessages = new ArrayList<>(); for (final CerberusCredentialsProvider credentialsProvider : credentialsProviderList) { try { final CerberusCredentials credentials = credentialsProvider.getCredentials(); @@ -82,26 +84,30 @@ public CerberusCredentials getCredentials() { lastUsedProvider = credentialsProvider; return credentials; } - } catch (CerberusClientException sce) { - if(LOGGER.isDebugEnabled()) { - LOGGER.debug("Failed to resolve Cerberus credentials with credential provider: {}. Moving " + - "on to next provider", credentialsProvider.getClass().toString(), sce); - } else { - LOGGER.info("Failed to resolve Cerberus credentials with credential provider: {} for reason: {} moving " + - "on to next provider", credentialsProvider.getClass().toString(), sce.getMessage()); - } + } catch (CerberusClientException cce) { + String message = String.format("Failed to resolve Cerberus credentials with credential provider: %s for" + + "reason: %s moving on to next provider", + credentialsProvider.getClass().toString(), + cce.getMessage()); + logMessages.add(message); } catch (Exception e) { // The catch all is so that we don't break the chain of providers. // If we do get an unexpected exception, we should at least log it for review. - LOGGER.warn("Unexpected error attempting to get credentials with provider: " - + credentialsProvider.getClass().getName(), e); + String message = String.format("Unexpected error attempting to get credentials with provider: %s. Reason: %s", + credentialsProvider.getClass().getName(), + e.getMessage()); + LOGGER.debug(message, e); + + logMessages.add(message); } } + for (String message : logMessages) { + LOGGER.info(message); + } throw new CerberusClientException("Unable to find credentials from any provider in the specified chain!"); } - /** * Returns the reuse last provider flag. *