Skip to content

Commit 450bf52

Browse files
authored
Merge pull request #403 from IABTechLab/gdm-UID2-4997-verbose-logging
Added verbose logging for S3 download
2 parents bf71b07 + 4752cbd commit 450bf52

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

src/main/java/com/uid2/shared/Const.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static class Config {
4444
public static final String S3EndpointProp = "aws_s3_endpoint";
4545
public static final String CoreS3BucketProp = "core_s3_bucket";
4646
public static final String OptOutS3BucketProp = "optout_s3_bucket";
47+
public static final String S3VerboseLoggingProp = "aws_s3_verbose_logging";
4748

4849
// Config stores
4950
public static final String CloudRefreshIntervalProp = "cloud_refresh_interval";

src/main/java/com/uid2/shared/cloud/CloudStorageS3.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ public class CloudStorageS3 implements TaggableCloudStorage {
2525

2626
private final AmazonS3 s3;
2727
private final String bucket;
28+
private final boolean verbose;
2829
private long preSignedUrlExpiryInSeconds = 3600;
2930

30-
public CloudStorageS3(String accessKeyId, String secretAccessKey, String region, String bucket, String s3Endpoint) {
31+
public CloudStorageS3(String accessKeyId, String secretAccessKey, String region, String bucket, String s3Endpoint, boolean verbose) {
3132
// Reading https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html
3233
AWSCredentials creds = new BasicAWSCredentials(
3334
accessKeyId,
@@ -45,11 +46,14 @@ public CloudStorageS3(String accessKeyId, String secretAccessKey, String region,
4546
.build();
4647
}
4748
this.bucket = bucket;
49+
this.verbose = verbose;
4850
}
4951

50-
public CloudStorageS3(String region, String bucket, String s3Endpoint) {
51-
this.bucket = bucket;
52+
public CloudStorageS3(String accessKeyId, String secretAccessKey, String region, String bucket, String s3Endpoint) {
53+
this(accessKeyId, secretAccessKey, region, bucket, s3Endpoint, false);
54+
}
5255

56+
public CloudStorageS3(String region, String bucket, String s3Endpoint) {
5357
// In theory `new InstanceProfileCredentialsProvider()` or even omitting credentials provider should work,
5458
// but for some unknown reason it doesn't. The credential it provides look realistic, but are not valid.
5559
// After a lot of experimentation and help of Abu Abraham and Isaac Wilson the only working solution we've
@@ -71,8 +75,9 @@ public CloudStorageS3(String region, String bucket, String s3Endpoint) {
7175
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(s3Endpoint, region))
7276
.enablePathStyleAccess()
7377
.build();
74-
7578
}
79+
this.bucket = bucket;
80+
this.verbose = false;
7681
}
7782

7883
@Override
@@ -134,7 +139,7 @@ public InputStream download(String cloudPath) throws CloudStorageException {
134139
if (e.getErrorCode().equals("NoSuchKey")) {
135140
throw new CloudStorageException("The specified key does not exist: " + e.getClass().getSimpleName() + ": " + bucket);
136141
} else {
137-
throw new CloudStorageException("s3 get error: " + e.getClass().getSimpleName() + ": " + bucket);
142+
throw new CloudStorageException("s3 get error: " + e.getClass().getSimpleName() + ": " + bucket + (verbose ? " - " + e.getMessage() : ""));
138143
}
139144
} catch (Throwable t) {
140145
// Do not log the message or the original exception as that may contain the pre-signed url

src/main/java/com/uid2/shared/cloud/CloudUtils.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,11 @@
77
import io.vertx.core.json.JsonObject;
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
10-
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
11-
import software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider;
12-
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
13-
import software.amazon.awssdk.regions.Region;
14-
import software.amazon.awssdk.services.kms.KmsClient;
15-
import software.amazon.awssdk.services.kms.KmsClientBuilder;
1610

1711
import java.io.ByteArrayInputStream;
18-
import java.io.IOException;
1912
import java.net.*;
2013
import java.nio.file.Path;
21-
import java.util.ArrayList;
2214
import java.util.Collections;
23-
import java.util.List;
2415

2516
public class CloudUtils {
2617
private static final Logger LOGGER = LoggerFactory.getLogger(CloudUtils.class);
@@ -31,15 +22,15 @@ public static TaggableCloudStorage createStorage(String cloudBucket, JsonObject
3122
var accessKeyId = jsonConfig.getString(Const.Config.AccessKeyIdProp);
3223
var secretAccessKey = jsonConfig.getString(Const.Config.SecretAccessKeyProp);
3324
var s3Endpoint = jsonConfig.getString(Const.Config.S3EndpointProp, "");
25+
var verboseLogging = jsonConfig.getBoolean(Const.Config.S3VerboseLoggingProp, false);
3426

3527
if (accessKeyId == null || secretAccessKey == null) {
3628
// IAM authentication
3729
return new CloudStorageS3(region, cloudBucket, s3Endpoint);
3830
}
3931

4032
// User access key authentication
41-
return new CloudStorageS3(accessKeyId, secretAccessKey, region, cloudBucket, s3Endpoint);
42-
33+
return new CloudStorageS3(accessKeyId, secretAccessKey, region, cloudBucket, s3Endpoint, verboseLogging);
4334
}
4435

4536
// I think this is not used, Aleksandrs Ulme 26/07/2023
@@ -50,7 +41,8 @@ public static TaggableCloudStorage createStorage(String cloudBucket) {
5041
System.getProperty(Const.Config.SecretAccessKeyProp),
5142
System.getProperty(Const.Config.AwsRegionProp),
5243
cloudBucket,
53-
System.getProperty(Const.Config.S3EndpointProp, "")
44+
System.getProperty(Const.Config.S3EndpointProp, ""),
45+
Boolean.parseBoolean(System.getProperty(Const.Config.S3VerboseLoggingProp, "false"))
5446
);
5547
}
5648

0 commit comments

Comments
 (0)