Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a defaultCredentialsProvider for AWS S3 #56

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 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,35 +33,41 @@ 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
public ThreadState<T> run(ThreadState<T> in) {

switch (in.tail().payload()) {
case Payload.Image i -> {
this.sendRequest(i.value(), in.userId().toString(), i.extension());
this.sendRequest(i.value(), in.userId().toString(), i.extension(), this.credentials);
}
case Payload.Document i -> {
this.sendRequest(i.value(), in.userId().toString(), i.extension());
this.sendRequest(i.value(), in.userId().toString(), i.extension(), this.credentials);
}
default -> {
return in;
Expand All @@ -73,13 +83,12 @@ public ThreadState<T> run(ThreadState<T> in) {
Identifier.random())); // TODO: remove last message
}

public void sendRequest(byte[] media, String senderID, String extension) {
public void sendRequest(byte[] media, String senderID, String extension, AwsCredentialsProvider credentials) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't need to pass the credentials? this should have access to this.credentials

Copy link
Contributor Author

@sachinkarve sachinkarve Jul 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it. I was debating making it a local variable and overlooked, but this makes more sense.

String key = senderID + '_' + Instant.now().toEpochMilli() + '.' + extension;

try (S3Client s3Client =
S3Client.builder()
.region(Region.of(this.region))
.credentialsProvider(this.credentialsProvider)
.credentialsProvider(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
Loading