use of com.talend.shaded.com.amazonaws.services.s3.AmazonS3Client in project herd by FINRAOS.
the class S3DaoTest method testGetAWSCredentialsProviderAssertAdditionalProviderIsSet.
/**
* A case where additional credentials provider is given in the request params. The credentials returned should be an AWS session credential where the
* values are from the provided custom credentials provider.
*/
@Test
public void testGetAWSCredentialsProviderAssertAdditionalProviderIsSet() throws Exception {
S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
S3Operations mockS3Operations = mock(S3Operations.class);
ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);
try {
String s3BucketName = "s3BucketName";
String s3KeyPrefix = "s3KeyPrefix";
String awsAccessKey = "awsAccessKey";
String awsSecretKey = "awsSecretKey";
String awsSessionToken = "awsSessionToken";
S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
s3FileTransferRequestParamsDto.setS3BucketName(s3BucketName);
s3FileTransferRequestParamsDto.setS3KeyPrefix(s3KeyPrefix);
s3FileTransferRequestParamsDto.setAdditionalAwsCredentialsProviders(Arrays.asList(new HerdAWSCredentialsProvider() {
@Override
public AwsCredential getAwsCredential() {
return new AwsCredential(awsAccessKey, awsSecretKey, awsSessionToken, null);
}
}));
when(mockS3Operations.putObject(any(), any())).then(new Answer<PutObjectResult>() {
@SuppressWarnings("unchecked")
@Override
public PutObjectResult answer(InvocationOnMock invocation) throws Throwable {
AmazonS3Client amazonS3Client = invocation.getArgument(1);
AWSCredentialsProviderChain awsCredentialsProviderChain = (AWSCredentialsProviderChain) ReflectionTestUtils.getField(amazonS3Client, "awsCredentialsProvider");
List<AWSCredentialsProvider> credentialsProviders = (List<AWSCredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProviderChain, "credentialsProviders");
assertEquals(2, credentialsProviders.size());
// refresh() does nothing, but gives code coverage
credentialsProviders.get(0).refresh();
/*
* We can't inspect the field directly since the class definition is private.
* Instead we call the getCredentials() and verify that it returns the credentials staged as part of this test.
*/
AWSCredentials credentials = awsCredentialsProviderChain.getCredentials();
assertEquals(BasicSessionCredentials.class, credentials.getClass());
BasicSessionCredentials basicSessionCredentials = (BasicSessionCredentials) credentials;
assertEquals(awsAccessKey, basicSessionCredentials.getAWSAccessKeyId());
assertEquals(awsSecretKey, basicSessionCredentials.getAWSSecretKey());
assertEquals(awsSessionToken, basicSessionCredentials.getSessionToken());
return new PutObjectResult();
}
});
s3Dao.createDirectory(s3FileTransferRequestParamsDto);
} finally {
ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
}
}
use of com.talend.shaded.com.amazonaws.services.s3.AmazonS3Client in project herd by FINRAOS.
the class S3DaoTest method testGetAmazonS3AssertS3EndpointIsSet.
@Test
public void testGetAmazonS3AssertS3EndpointIsSet() {
S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
S3Operations mockS3Operations = mock(S3Operations.class);
ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);
try {
String s3BucketName = "s3BucketName";
String s3KeyPrefix = "s3KeyPrefix";
String s3Endpoint = "s3Endpoint";
S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
s3FileTransferRequestParamsDto.setS3BucketName(s3BucketName);
s3FileTransferRequestParamsDto.setS3KeyPrefix(s3KeyPrefix);
s3FileTransferRequestParamsDto.setS3Endpoint(s3Endpoint);
when(mockS3Operations.putObject(any(), any())).then(new Answer<PutObjectResult>() {
@Override
public PutObjectResult answer(InvocationOnMock invocation) throws Throwable {
AmazonS3Client amazonS3Client = invocation.getArgument(1);
assertEquals(new URI("https://" + s3Endpoint), ReflectionTestUtils.getField(amazonS3Client, "endpoint"));
return new PutObjectResult();
}
});
s3Dao.createDirectory(s3FileTransferRequestParamsDto);
} finally {
ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
}
}
use of com.talend.shaded.com.amazonaws.services.s3.AmazonS3Client in project herd by FINRAOS.
the class S3DaoTest method testGetAWSCredentialsProviderAssertStaticCredentialsIsNotSetWhenSecretKeyIsNull.
@Test
public void testGetAWSCredentialsProviderAssertStaticCredentialsIsNotSetWhenSecretKeyIsNull() {
S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
S3Operations mockS3Operations = mock(S3Operations.class);
ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);
try {
String s3BucketName = "s3BucketName";
String s3KeyPrefix = "s3KeyPrefix";
String s3AccessKey = "s3AccessKey";
String s3SecretKey = null;
S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
s3FileTransferRequestParamsDto.setS3BucketName(s3BucketName);
s3FileTransferRequestParamsDto.setS3KeyPrefix(s3KeyPrefix);
s3FileTransferRequestParamsDto.setAwsAccessKeyId(s3AccessKey);
s3FileTransferRequestParamsDto.setAwsSecretKey(s3SecretKey);
when(mockS3Operations.putObject(any(), any())).then(new Answer<PutObjectResult>() {
@SuppressWarnings("unchecked")
@Override
public PutObjectResult answer(InvocationOnMock invocation) throws Throwable {
AmazonS3Client amazonS3Client = invocation.getArgument(1);
AWSCredentialsProviderChain awsCredentialsProviderChain = (AWSCredentialsProviderChain) ReflectionTestUtils.getField(amazonS3Client, "awsCredentialsProvider");
List<AWSCredentialsProvider> credentialsProviders = (List<AWSCredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProviderChain, "credentialsProviders");
assertEquals(1, credentialsProviders.size());
assertEquals(DefaultAWSCredentialsProviderChain.class, credentialsProviders.get(0).getClass());
return new PutObjectResult();
}
});
s3Dao.createDirectory(s3FileTransferRequestParamsDto);
} finally {
ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
}
}
use of com.talend.shaded.com.amazonaws.services.s3.AmazonS3Client in project herd by FINRAOS.
the class S3DaoImpl method createDirectory.
@Override
public void createDirectory(final S3FileTransferRequestParamsDto params) {
// Create metadata for the directory marker and set content-length to 0 bytes.
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
prepareMetadata(params, metadata);
// Create empty content.
InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
// Create a PutObjectRequest passing the folder name suffixed by '/'.
String directoryName = StringUtils.appendIfMissing(params.getS3KeyPrefix(), "/");
PutObjectRequest putObjectRequest = new PutObjectRequest(params.getS3BucketName(), directoryName, emptyContent, metadata);
// KMS key ID is being set through prepareMetadata()
AmazonS3Client s3Client = getAmazonS3(params);
try {
s3Operations.putObject(putObjectRequest, s3Client);
} catch (AmazonServiceException e) {
throw new IllegalStateException(String.format("Failed to create 0 byte S3 object with \"%s\" key in bucket \"%s\". Reason: %s", directoryName, params.getS3BucketName(), e.getMessage()), e);
} finally {
// Shutdown the AmazonS3Client instance to release resources.
s3Client.shutdown();
}
}
use of com.talend.shaded.com.amazonaws.services.s3.AmazonS3Client in project herd by FINRAOS.
the class S3DaoImpl method restoreObjects.
@Override
public void restoreObjects(final S3FileTransferRequestParamsDto params, int expirationInDays) {
LOGGER.info("Restoring a list of objects in S3... s3KeyPrefix=\"{}\" s3BucketName=\"{}\" s3KeyCount={}", params.getS3KeyPrefix(), params.getS3BucketName(), params.getFiles().size());
if (!CollectionUtils.isEmpty(params.getFiles())) {
// Initialize a key value pair for the error message in the catch block.
String key = params.getFiles().get(0).getPath().replaceAll("\\\\", "/");
try {
// Create an S3 client.
AmazonS3Client s3Client = getAmazonS3(params);
// Create a restore object request.
RestoreObjectRequest requestRestore = new RestoreObjectRequest(params.getS3BucketName(), null, expirationInDays);
// Make Bulk as default glacier retrieval option
requestRestore.setGlacierJobParameters(new GlacierJobParameters().withTier(GLACIER_RETRIEVAL_OPTION));
try {
for (File file : params.getFiles()) {
key = file.getPath().replaceAll("\\\\", "/");
ObjectMetadata objectMetadata = s3Operations.getObjectMetadata(params.getS3BucketName(), key, s3Client);
// Request a restore for objects that are not already being restored.
if (BooleanUtils.isNotTrue(objectMetadata.getOngoingRestore())) {
requestRestore.setKey(key);
s3Operations.restoreObject(requestRestore, s3Client);
}
}
} finally {
s3Client.shutdown();
}
} catch (Exception e) {
throw new IllegalStateException(String.format("Failed to initiate a restore request for \"%s\" key in \"%s\" bucket. Reason: %s", key, params.getS3BucketName(), e.getMessage()), e);
}
}
}
Aggregations