From b6e6795b60e9f70e08d0d8e3a91c796596219d1c Mon Sep 17 00:00:00 2001 From: Sriram Date: Sun, 27 Jan 2019 15:41:54 +0530 Subject: [PATCH] Added logs in material poller + s3 artifact store We were running into AWS auth issues and we couldn't find out which codepath was being exercised. Added logs to get clarity on the same --- .../s3material/plugin/S3PackageMaterialPoller.java | 9 ++++++--- .../main/java/com/indix/gocd/models/Artifact.java | 10 ++++++++++ .../java/com/indix/gocd/utils/GoEnvironment.java | 13 +++++++++++++ .../com/indix/gocd/utils/store/S3ArtifactStore.java | 10 ++++++++-- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/material/src/main/java/com/indix/gocd/s3material/plugin/S3PackageMaterialPoller.java b/material/src/main/java/com/indix/gocd/s3material/plugin/S3PackageMaterialPoller.java index 19bc6e7..3b8e88b 100644 --- a/material/src/main/java/com/indix/gocd/s3material/plugin/S3PackageMaterialPoller.java +++ b/material/src/main/java/com/indix/gocd/s3material/plugin/S3PackageMaterialPoller.java @@ -74,15 +74,16 @@ private GoPluginApiResponse handleLatestRevisionSince(GoPluginApiRequest goPlugi String s3Bucket = repositoryKeyValuePairs.get(S3_BUCKET); S3ArtifactStore artifactStore = s3ArtifactStore(s3Bucket); + Artifact artifact = artifact(packageKeyValuePairs); try { - RevisionStatus revision = artifactStore.getLatest(artifact(packageKeyValuePairs)); + RevisionStatus revision = artifactStore.getLatest(artifact); if(new Revision(revision.revision.getRevision()).compareTo(new Revision(previousRevision)) > 0) { return createResponse(DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE, revision.toMap()); } return createResponse(DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE, null); } catch (Exception e) { - logger.error(e.getMessage(), e); + logger.error("Error during handleLatestRevisionSince for "+artifact.toString()+", with msg: "+e.getMessage(), e); return createResponse(DefaultGoPluginApiResponse.INTERNAL_ERROR, null); } } @@ -92,11 +93,13 @@ private GoPluginApiResponse handleGetLatestRevision(GoPluginApiRequest goPluginA final Map packageKeyValuePairs = keyValuePairs(goPluginApiRequest, REQUEST_PACKAGE_CONFIGURATION); String s3Bucket = repositoryKeyValuePairs.get(S3_BUCKET); S3ArtifactStore artifactStore = s3ArtifactStore(s3Bucket); + Artifact artifact = artifact(packageKeyValuePairs); try { - RevisionStatus revision = artifactStore.getLatest(artifact(packageKeyValuePairs)); + RevisionStatus revision = artifactStore.getLatest(artifact); return createResponse(DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE, revision.toMap()); } catch (Exception e) { logger.error(e.getMessage(), e); + logger.error("Error during getLatestRevision for "+artifact.toString()+", with msg: "+e.getMessage(), e); return createResponse(DefaultGoPluginApiResponse.INTERNAL_ERROR, null); } } diff --git a/utils/src/main/java/com/indix/gocd/models/Artifact.java b/utils/src/main/java/com/indix/gocd/models/Artifact.java index dcd83d5..a5db92e 100644 --- a/utils/src/main/java/com/indix/gocd/models/Artifact.java +++ b/utils/src/main/java/com/indix/gocd/models/Artifact.java @@ -34,4 +34,14 @@ public String prefixWithRevision(){ else return prefix(); } + + @Override + public String toString() { + return "Artifact{" + + "pipelineName='" + pipelineName + '\'' + + ", stageName='" + stageName + '\'' + + ", jobName='" + jobName + '\'' + + ", revision=" + revision.getRevision() + + '}'; + } } diff --git a/utils/src/main/java/com/indix/gocd/utils/GoEnvironment.java b/utils/src/main/java/com/indix/gocd/utils/GoEnvironment.java index f4680f5..69b5a78 100644 --- a/utils/src/main/java/com/indix/gocd/utils/GoEnvironment.java +++ b/utils/src/main/java/com/indix/gocd/utils/GoEnvironment.java @@ -121,4 +121,17 @@ private String getEnvInvalidFormatMessage(String environmentVariable, String val "Unexpected value in %s environment variable; was %s, but expected one of the following %s", environmentVariable, value, expected); } + + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(); + for(String key : environment.keySet()) { + buffer.append(key+":"+environment.get(key)); + buffer.append(";"); + } + + return "GoEnvironment{" + + "environment=" + buffer.toString() + + '}'; + } } diff --git a/utils/src/main/java/com/indix/gocd/utils/store/S3ArtifactStore.java b/utils/src/main/java/com/indix/gocd/utils/store/S3ArtifactStore.java index ac8bbd7..3a51a83 100644 --- a/utils/src/main/java/com/indix/gocd/utils/store/S3ArtifactStore.java +++ b/utils/src/main/java/com/indix/gocd/utils/store/S3ArtifactStore.java @@ -17,6 +17,7 @@ import com.indix.gocd.utils.utils.Lists; import com.indix.gocd.utils.utils.Maps; import org.apache.commons.lang3.StringUtils; +import com.thoughtworks.go.plugin.api.logging.Logger; import java.io.File; import java.util.Collections; @@ -26,7 +27,7 @@ import static com.indix.gocd.utils.Constants.*; public class S3ArtifactStore { - + private static Logger logger = Logger.getLoggerFor(S3ArtifactStore.class); private static Map STORAGE_CLASSES = Maps.builder() .with(STORAGE_CLASS_STANDARD, StorageClass.Standard) .with(STORAGE_CLASS_STANDARD_IA, StorageClass.StandardInfrequentAccess) @@ -229,15 +230,20 @@ public String getLatestPrefix(String pipeline, String stage, String job, String public static AmazonS3 getS3client(GoEnvironment env) { AmazonS3ClientBuilder amazonS3ClientBuilder = AmazonS3ClientBuilder.standard(); - + logger.debug("Instantiating S3 client with following env variables: "); + logger.debug(env.toString()); if (env.has(AWS_REGION)) { amazonS3ClientBuilder.withRegion(env.get(AWS_REGION)); } if (env.hasAWSUseIamRole()) { + logger.info("S3Artifact's getS3client uses AWS IAM Role"); amazonS3ClientBuilder.withCredentials(new InstanceProfileCredentialsProvider(false)); } else if (env.has(AWS_ACCESS_KEY_ID) && env.has(AWS_SECRET_ACCESS_KEY)) { + logger.info("S3Artifact's getS3client uses AWS credentials from ENV"); BasicAWSCredentials basicCreds = new BasicAWSCredentials(env.get(AWS_ACCESS_KEY_ID), env.get(AWS_SECRET_ACCESS_KEY)); amazonS3ClientBuilder.withCredentials(new AWSStaticCredentialsProvider(basicCreds)); + } else { + logger.warn("S3Artifact's getS3client fallback to default credentials chain"); } return amazonS3ClientBuilder.build();