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 1 commit
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
30 changes: 18 additions & 12 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,26 @@ 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 @Nullable StaticCredentialsProvider staticCredentialsProvider;
Copy link
Contributor

Choose a reason for hiding this comment

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

should be any reason to remove the final here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved the 2nd assignment to variable in else condition which allows on-time assignment, in turn allowing final


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;
this.staticCredentialsProvider = null;

AwsSessionCredentials sessionCredentials =
AwsSessionCredentials.create(this.awsAccessKeyID, this.awsSecretAccessKey, "");

this.credentialsProvider = StaticCredentialsProvider.create(sessionCredentials);
if (!this.awsAccessKeyID.isEmpty() && !this.awsSecretAccessKey.isEmpty()) {
AwsSessionCredentials sessionCredentials =
AwsSessionCredentials.create(this.awsAccessKeyID, this.awsSecretAccessKey, "");
staticCredentialsProvider = StaticCredentialsProvider.create(sessionCredentials);
}
}

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

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

AwsCredentialsProvider credentialsProvider = Objects.requireNonNullElse(this.staticCredentialsProvider, DefaultCredentialsProvider.create());
Copy link
Contributor

Choose a reason for hiding this comment

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

why not push this logic up into the constructor so that you don't have to do a null check here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved this to constructor

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