Skip to content

Commit

Permalink
Update AWS SDK for Java to v2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Elwizzy12 committed Jul 25, 2024
1 parent 26674d8 commit b04a6b1
Show file tree
Hide file tree
Showing 34 changed files with 690 additions and 506 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package de.adorsys.datasafe.business.impl.e2e;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import dagger.Lazy;
import de.adorsys.datasafe.business.impl.service.DaggerDefaultDatasafeServices;
import de.adorsys.datasafe.business.impl.service.DefaultDatasafeServices;
Expand Down Expand Up @@ -47,9 +45,17 @@
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
import software.amazon.awssdk.services.s3.model.S3Object;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.security.UnrecoverableKeyException;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -112,12 +118,13 @@ static void initDistributedMinios() {
log.info("ENDPOINT IS {}", endpoint);
endpointsByHostNoBucket.put(it, endpoint);

AmazonS3 client = S3ClientFactory.getClient(
endpoint,
REGION,
accessKey(it),
secretKey(it)
);
S3Client client = S3Client.builder()
.endpointOverride(URI.create(endpoint))
.region(Region.of(REGION))
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(accessKey(it), secretKey(it))
))
.build();

AwsClientRetry.createBucketWithRetry(client, it);
});
Expand Down Expand Up @@ -290,19 +297,26 @@ private void registerUser(UserIDAuth auth) {
}

private List<String> listInBucket(String bucket) {
return S3ClientFactory.getClient(
endpointsByHostNoBucket.get(bucket),
REGION,
accessKey(bucket),
secretKey(bucket)
)
.listObjects(bucket, "")
.getObjectSummaries()
.stream()
.map(S3ObjectSummary::getKey)
.collect(Collectors.toList());
S3Client client = S3Client.builder()
.endpointOverride(URI.create(endpointsByHostNoBucket.get(bucket)))
.region(Region.of(REGION))
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(accessKey(bucket), secretKey(bucket))
))
.build();

ListObjectsV2Request request = ListObjectsV2Request.builder()
.bucket(bucket)
.build();

ListObjectsV2Response response = client.listObjectsV2(request);
return response.contents()
.stream()
.map(S3Object::key)
.collect(Collectors.toList());
}


@SneakyThrows
private void writeToPrivate(UserIDAuth user, StorageIdentifier id, String path, String data) {
try (OutputStream os = datasafeServices.privateService().write(WriteRequest.forPrivate(user, id, path))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.adorsys.datasafe.business.impl.e2e;

import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.google.common.io.ByteStreams;
import de.adorsys.datasafe.business.impl.service.DefaultDatasafeServices;
import de.adorsys.datasafe.encrypiton.api.types.UserIDAuth;
Expand All @@ -14,6 +13,7 @@
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import software.amazon.awssdk.services.s3.model.S3Exception;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
Expand Down Expand Up @@ -73,7 +73,7 @@ void testVersionedRemoveManually() {
writeAndGetVersion(jane, FILE, "Hello 3");

removeByVersion(jane, FILE, new StorageVersion(oldVersion));
assertThrows(AmazonS3Exception.class, () -> readByVersion(jane, FILE, new StorageVersion(oldVersion)));
assertThrows(S3Exception.class, () -> readByVersion(jane, FILE, new StorageVersion(oldVersion)));
assertThat(readPrivateUsingPrivateKey(jane, BasePrivateResource.forPrivate(FILE))).isEqualTo("Hello 3");
}

Expand Down
9 changes: 9 additions & 0 deletions datasafe-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>auth</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>protocol-core</artifactId>
<version>2.26.23</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package de.adorsys.datasafe.cli.config;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.google.common.collect.ImmutableMap;
import dagger.Lazy;
import de.adorsys.datasafe.business.impl.service.DaggerDefaultDatasafeServices;
Expand All @@ -29,7 +22,12 @@
import lombok.experimental.Delegate;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

import java.net.URI;
import java.nio.file.Path;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -80,15 +78,12 @@ private static StorageService httpS3() {

private static StorageService amazonS3() {
return new UriBasedAuthStorageService(
acc -> new S3StorageService(
S3ClientFactory.getAmazonClient(
acc.getRegion(),
acc.getAccessKey(),
acc.getSecretKey()
),
// Bucket name is encoded in first path segment
acc.getBucketName(),
ExecutorServiceUtil.submitterExecutesOnStarvationExecutingService()
acc -> getStorageService(
acc.getAccessKey(),
acc.getSecretKey(),
acc.getEndpoint(),
acc.getRegion(),
acc.getBucketName()
),
uri -> (uri.getHost() + "/" + uri.getPath().replaceFirst("^/", "")).split("/")
);
Expand All @@ -103,44 +98,26 @@ private WithCredentialProvider(Lazy<StorageKeyStoreOperations> storageKeyStoreOp
super(null);
this.delegate = new RegexAccessServiceWithStorageCredentialsImpl(storageKeyStoreOperations);
}

}

private static S3StorageService getStorageService(String accessKey, String secretKey, String url, String region,
String bucket) {
AmazonS3ClientBuilder amazonS3ClientBuilder = AmazonS3ClientBuilder.standard()
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials(
accessKey,
secretKey))
)
.enablePathStyleAccess();

AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration(
url,
region
);
amazonS3ClientBuilder.withEndpointConfiguration(endpoint);

if (! url.toLowerCase().startsWith("https")) {
log.info("Creating S3 client without https");
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setProtocol(Protocol.HTTP);
clientConfig.disableSocketProxy();
amazonS3ClientBuilder.withClientConfiguration(clientConfig);
}

AmazonS3 amazons3 = amazonS3ClientBuilder.build();
AwsBasicCredentials creds = AwsBasicCredentials.create(accessKey, secretKey);
S3Client s3 = S3Client.builder()
.endpointOverride(URI.create(url))
.region(Region.of(region))
.credentialsProvider(StaticCredentialsProvider.create(creds))
.build();

return new S3StorageService(
amazons3,
s3,
bucket,
ExecutorServiceUtil
.submitterExecutesOnStarvationExecutingService(
5,
5
)
);
}
}
}}

6 changes: 6 additions & 0 deletions datasafe-examples/datasafe-examples-multidfs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<!-- Disable maven-dependency-plugin - that's ok to have undeclared transitive dependencies here-->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.adorsys.datasafe.examples.business.s3;

import com.amazonaws.services.s3.AmazonS3;
import dagger.Lazy;
import de.adorsys.datasafe.business.impl.service.DaggerDefaultDatasafeServices;
import de.adorsys.datasafe.business.impl.service.DefaultDatasafeServices;
Expand Down Expand Up @@ -35,6 +34,10 @@
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

import java.io.OutputStream;
import java.net.URI;
Expand Down Expand Up @@ -64,7 +67,7 @@ class MultiDfsWithCredentialsExampleIT{
private static final ExecutorService EXECUTOR = ExecutorServiceUtil.submitterExecutesOnStarvationExecutingService(4, 4);

private static Map<MinioContainerId, GenericContainer> minios = new EnumMap<>(MinioContainerId.class);
private static AmazonS3 directoryClient = null;
private static S3Client directoryClient = null;
private static Map<MinioContainerId, String> endpointsByHost = new HashMap<>();

@BeforeAll
Expand All @@ -79,13 +82,12 @@ static void startup() {
log.info("MINIO for {} is available at: {} with access: '{}'/'{}'", it, endpoint, it.getAccessKey(),
it.getSecretKey());

AmazonS3 client = S3ClientFactory.getClient(
endpoint,
REGION,
it.getAccessKey(),
it.getSecretKey()
);

S3Client client = S3Client.builder()
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(it.getAccessKey(), it.getSecretKey())))
.region(Region.of(REGION))
.endpointOverride(URI.create(endpoint))
.build();
AwsClientRetry.createBucketWithRetry(client, it.getBucketName());

if (it.equals(DIRECTORY_BUCKET)) {
Expand Down
Loading

0 comments on commit b04a6b1

Please sign in to comment.