Skip to content

Commit

Permalink
Merge pull request #56 from facebookincubator/sach/feat-s3-creds
Browse files Browse the repository at this point in the history
Adding a defaultCredentialsProvider for AWS S3
  • Loading branch information
sachinkarve authored Jul 11, 2024
2 parents be2ea48 + 0e486ab commit 6d25555
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
31 changes: 20 additions & 11 deletions src/main/java/com/meta/cp4m/S3PreProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
import com.meta.cp4m.message.Payload;
import com.meta.cp4m.message.ThreadState;
import java.time.Instant;
import java.util.Objects;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
Expand All @@ -29,24 +33,30 @@ public class S3PreProcessor<T extends Message> implements PreProcessor<T> {
private final String region;
private final String bucket;
private final @Nullable String textMessageAddition;
private final StaticCredentialsProvider credentialsProvider;
private final AwsCredentialsProvider credentials;

public S3PreProcessor(
String awsAccessKeyID,
String awsSecretAccessKey,
String region,
String bucket,
@Nullable String textMessageAddition) {
String awsAccessKeyID,
String awsSecretAccessKey,
String region,
String bucket,
@Nullable String textMessageAddition) {
this.awsAccessKeyID = awsAccessKeyID;
this.awsSecretAccessKey = awsSecretAccessKey;
this.region = region;
this.bucket = bucket;
this.textMessageAddition = textMessageAddition;

AwsSessionCredentials sessionCredentials =
AwsSessionCredentials.create(this.awsAccessKeyID, this.awsSecretAccessKey, "");
@Nullable StaticCredentialsProvider staticCredentials;
if (!this.awsAccessKeyID.isEmpty() && !this.awsSecretAccessKey.isEmpty()) {
AwsSessionCredentials sessionCredentials =
AwsSessionCredentials.create(this.awsAccessKeyID, this.awsSecretAccessKey, "");
staticCredentials = StaticCredentialsProvider.create(sessionCredentials);
} else {
staticCredentials = null;
}

this.credentialsProvider = StaticCredentialsProvider.create(sessionCredentials);
this.credentials = Objects.requireNonNullElse(staticCredentials, DefaultCredentialsProvider.create());
}

@Override
Expand Down Expand Up @@ -75,11 +85,10 @@ public ThreadState<T> run(ThreadState<T> in) {

public void sendRequest(byte[] media, String senderID, String extension) {
String key = senderID + '_' + Instant.now().toEpochMilli() + '.' + extension;

try (S3Client s3Client =
S3Client.builder()
.region(Region.of(this.region))
.credentialsProvider(this.credentialsProvider)
.credentialsProvider(this.credentials)
.build()) {

PutObjectRequest request =
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/com/meta/cp4m/S3PreProcessorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

public record S3PreProcessorConfig(
String name,
String awsAccessKeyId,
String awsSecretAccessKey,
@Nullable String awsAccessKeyId,
@Nullable String awsSecretAccessKey,
String region,
String bucket,
@Nullable String textMessageAddition)
Expand All @@ -30,8 +30,8 @@ public record S3PreProcessorConfig(
@JsonCreator
public S3PreProcessorConfig(
@JsonProperty("name") String name,
@JsonProperty("aws_access_key_id") String awsAccessKeyId,
@JsonProperty("aws_secret_access_key") String awsSecretAccessKey,
@JsonProperty("aws_access_key_id") @Nullable String awsAccessKeyId,
@JsonProperty("aws_secret_access_key") @Nullable String awsSecretAccessKey,
@JsonProperty("region") String region,
@JsonProperty("bucket") String bucket,
@JsonProperty("text_message_addition") @Nullable String textMessageAddition) {
Expand All @@ -41,10 +41,8 @@ public S3PreProcessorConfig(
"bucket does not match the aws region format(kebab case) or is empty");

this.name = Objects.requireNonNull(name, "name is a required parameter");
this.awsAccessKeyId =
Objects.requireNonNull(awsAccessKeyId, "aws access key is a required parameter");
this.awsSecretAccessKey =
Objects.requireNonNull(awsSecretAccessKey, "aws secret access key is a required parameter");
this.awsAccessKeyId = Objects.requireNonNullElse(awsAccessKeyId, "");
this.awsSecretAccessKey = Objects.requireNonNullElse(awsSecretAccessKey, "");
this.region = Objects.requireNonNull(region, "region is a required parameter");
this.bucket = Objects.requireNonNull(bucket, "bucket is a required parameter");
this.textMessageAddition = textMessageAddition;
Expand Down

0 comments on commit 6d25555

Please sign in to comment.