Search in sources :

Example 1 with RetryPolicy

use of com.amazonaws.retry.RetryPolicy in project elasticsearch by elastic.

the class AwsEc2ServiceImpl method buildConfiguration.

protected static ClientConfiguration buildConfiguration(Logger logger, Settings settings) {
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(CLOUD_EC2.PROTOCOL_SETTING.get(settings));
    if (PROXY_HOST_SETTING.exists(settings) || CLOUD_EC2.PROXY_HOST_SETTING.exists(settings)) {
        String proxyHost = CLOUD_EC2.PROXY_HOST_SETTING.get(settings);
        Integer proxyPort = CLOUD_EC2.PROXY_PORT_SETTING.get(settings);
        String proxyUsername = CLOUD_EC2.PROXY_USERNAME_SETTING.get(settings);
        String proxyPassword = CLOUD_EC2.PROXY_PASSWORD_SETTING.get(settings);
        clientConfiguration.withProxyHost(proxyHost).withProxyPort(proxyPort).withProxyUsername(proxyUsername).withProxyPassword(proxyPassword);
    }
    // #155: we might have 3rd party users using older EC2 API version
    String awsSigner = CLOUD_EC2.SIGNER_SETTING.get(settings);
    if (Strings.hasText(awsSigner)) {
        logger.debug("using AWS API signer [{}]", awsSigner);
        AwsSigner.configureSigner(awsSigner, clientConfiguration);
    }
    // Increase the number of retries in case of 5xx API responses
    final Random rand = Randomness.get();
    RetryPolicy retryPolicy = new RetryPolicy(RetryPolicy.RetryCondition.NO_RETRY_CONDITION, new RetryPolicy.BackoffStrategy() {

        @Override
        public long delayBeforeNextRetry(AmazonWebServiceRequest originalRequest, AmazonClientException exception, int retriesAttempted) {
            // with 10 retries the max delay time is 320s/320000ms (10 * 2^5 * 1 * 1000)
            logger.warn("EC2 API request failed, retry again. Reason was:", exception);
            return 1000L * (long) (10d * Math.pow(2, retriesAttempted / 2.0d) * (1.0d + rand.nextDouble()));
        }
    }, 10, false);
    clientConfiguration.setRetryPolicy(retryPolicy);
    clientConfiguration.setSocketTimeout((int) CLOUD_EC2.READ_TIMEOUT.get(settings).millis());
    return clientConfiguration;
}
Also used : Random(java.util.Random) AmazonClientException(com.amazonaws.AmazonClientException) RetryPolicy(com.amazonaws.retry.RetryPolicy) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 2 with RetryPolicy

use of com.amazonaws.retry.RetryPolicy in project herd by FINRAOS.

the class StsDaoTest method testGetTemporarySecurityCredentialsMissingOptionalParameters.

@Test
public void testGetTemporarySecurityCredentialsMissingOptionalParameters() {
    // Create an AWS parameters DTO without proxy settings.
    AwsParamsDto awsParamsDto = new AwsParamsDto();
    // Specify the duration, in seconds, of the role session.
    int awsRoleDurationSeconds = INTEGER_VALUE;
    // Create a retry policy.
    RetryPolicy retryPolicy = new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true);
    // Create the expected assume role request.
    AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest().withRoleArn(AWS_ROLE_ARN).withRoleSessionName(SESSION_NAME).withDurationSeconds(awsRoleDurationSeconds);
    // Create AWS credentials for API authentication.
    Credentials credentials = new Credentials();
    credentials.setAccessKeyId(AWS_ASSUMED_ROLE_ACCESS_KEY);
    credentials.setSecretAccessKey(AWS_ASSUMED_ROLE_SECRET_KEY);
    credentials.setSessionToken(AWS_ASSUMED_ROLE_SESSION_TOKEN);
    // Create an assume role result.
    AssumeRoleResult assumeRoleResult = new AssumeRoleResult();
    assumeRoleResult.setCredentials(credentials);
    // Mock the external calls.
    when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy);
    when(stsOperations.assumeRole(any(AWSSecurityTokenServiceClient.class), eq(assumeRoleRequest))).thenReturn(assumeRoleResult);
    // Call the method under test. Please note that we do not specify an IAM policy.
    Credentials result = stsDaoImpl.getTemporarySecurityCredentials(awsParamsDto, SESSION_NAME, AWS_ROLE_ARN, awsRoleDurationSeconds, null);
    // Verify the external calls.
    verify(retryPolicyFactory).getRetryPolicy();
    verify(stsOperations).assumeRole(any(AWSSecurityTokenServiceClient.class), eq(assumeRoleRequest));
    verifyNoMoreInteractionsHelper();
    // Validate the returned object.
    assertEquals(credentials, result);
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) AwsParamsDto(org.finra.herd.model.dto.AwsParamsDto) AWSSecurityTokenServiceClient(com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClient) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) RetryPolicy(com.amazonaws.retry.RetryPolicy) Credentials(com.amazonaws.services.securitytoken.model.Credentials) Test(org.junit.Test)

Example 3 with RetryPolicy

use of com.amazonaws.retry.RetryPolicy in project herd by FINRAOS.

the class S3DaoImplTest method testDeleteDirectoryNoS3VersionsExist.

@Test
public void testDeleteDirectoryNoS3VersionsExist() {
    // Create an S3 file transfer request parameters DTO to access S3 objects.
    S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
    s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME);
    s3FileTransferRequestParamsDto.setS3KeyPrefix(S3_KEY_PREFIX);
    // Create a retry policy.
    RetryPolicy retryPolicy = new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true);
    // Create an empty version listing.
    VersionListing versionListing = new VersionListing();
    // Mock the external calls.
    when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy);
    when(s3Operations.listVersions(any(ListVersionsRequest.class), any(AmazonS3Client.class))).thenReturn(versionListing);
    // Call the method under test.
    s3DaoImpl.deleteDirectory(s3FileTransferRequestParamsDto);
    // Verify the external calls.
    verify(retryPolicyFactory).getRetryPolicy();
    verify(s3Operations).listVersions(any(ListVersionsRequest.class), any(AmazonS3Client.class));
    verifyNoMoreInteractionsHelper();
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) VersionListing(com.amazonaws.services.s3.model.VersionListing) ListVersionsRequest(com.amazonaws.services.s3.model.ListVersionsRequest) RetryPolicy(com.amazonaws.retry.RetryPolicy) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 4 with RetryPolicy

use of com.amazonaws.retry.RetryPolicy in project herd by FINRAOS.

the class S3DaoImplTest method testTagObjects.

@Test
public void testTagObjects() {
    // Create an S3 file transfer request parameters DTO to access S3 objects.
    S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
    s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME);
    s3FileTransferRequestParamsDto.setFiles(Arrays.asList(new File(S3_KEY_PREFIX + "/" + LOCAL_FILE)));
    // Create an S3 file transfer request parameters DTO to tag S3 objects.
    S3FileTransferRequestParamsDto s3ObjectTaggerParamsDto = new S3FileTransferRequestParamsDto();
    s3ObjectTaggerParamsDto.setAwsAccessKeyId(AWS_ASSUMED_ROLE_ACCESS_KEY);
    s3ObjectTaggerParamsDto.setAwsSecretKey(AWS_ASSUMED_ROLE_SECRET_KEY);
    s3ObjectTaggerParamsDto.setSessionToken(AWS_ASSUMED_ROLE_SESSION_TOKEN);
    // Create an S3 object tag.
    Tag tag = new Tag(S3_OBJECT_TAG_KEY, S3_OBJECT_TAG_VALUE);
    // Create a retry policy.
    RetryPolicy retryPolicy = new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true);
    // Create a get object tagging result.
    GetObjectTaggingResult getObjectTaggingResult = new GetObjectTaggingResult(null);
    // Create a set object tagging result.
    SetObjectTaggingResult setObjectTaggingResult = new SetObjectTaggingResult();
    // Mock the external calls.
    when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy);
    when(s3Operations.getObjectTagging(any(GetObjectTaggingRequest.class), any(AmazonS3Client.class))).thenReturn(getObjectTaggingResult);
    when(s3Operations.setObjectTagging(any(SetObjectTaggingRequest.class), any(AmazonS3Client.class))).thenReturn(setObjectTaggingResult);
    // Call the method under test.
    s3DaoImpl.tagObjects(s3FileTransferRequestParamsDto, s3ObjectTaggerParamsDto, tag);
    // Verify the external calls.
    verify(retryPolicyFactory, times(2)).getRetryPolicy();
    verify(s3Operations).getObjectTagging(any(GetObjectTaggingRequest.class), any(AmazonS3Client.class));
    verify(s3Operations).setObjectTagging(any(SetObjectTaggingRequest.class), any(AmazonS3Client.class));
    verifyNoMoreInteractionsHelper();
}
Also used : GetObjectTaggingRequest(com.amazonaws.services.s3.model.GetObjectTaggingRequest) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) SetObjectTaggingResult(com.amazonaws.services.s3.model.SetObjectTaggingResult) SetObjectTaggingRequest(com.amazonaws.services.s3.model.SetObjectTaggingRequest) Tag(com.amazonaws.services.s3.model.Tag) File(java.io.File) RetryPolicy(com.amazonaws.retry.RetryPolicy) GetObjectTaggingResult(com.amazonaws.services.s3.model.GetObjectTaggingResult) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 5 with RetryPolicy

use of com.amazonaws.retry.RetryPolicy in project herd by FINRAOS.

the class S3DaoImplTest method testDeleteDirectoryMultiObjectDeleteException.

@Test
public void testDeleteDirectoryMultiObjectDeleteException() {
    // Create an S3 file transfer request parameters DTO to access S3 objects.
    S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
    s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME);
    s3FileTransferRequestParamsDto.setS3KeyPrefix(S3_KEY_PREFIX);
    // Create a retry policy.
    RetryPolicy retryPolicy = new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true);
    // Create an S3 version summary.
    S3VersionSummary s3VersionSummary = new S3VersionSummary();
    s3VersionSummary.setKey(S3_KEY);
    s3VersionSummary.setVersionId(S3_VERSION_ID);
    // Create a version listing.
    VersionListing versionListing = new VersionListing();
    versionListing.setVersionSummaries(Arrays.asList(s3VersionSummary));
    // Create a delete error.
    MultiObjectDeleteException.DeleteError deleteError = new MultiObjectDeleteException.DeleteError();
    deleteError.setKey(S3_KEY);
    deleteError.setVersionId(S3_VERSION_ID);
    deleteError.setCode(ERROR_CODE);
    deleteError.setMessage(ERROR_MESSAGE);
    // Create a multi object delete exception.
    MultiObjectDeleteException multiObjectDeleteException = new MultiObjectDeleteException(Arrays.asList(deleteError), new ArrayList<>());
    // Mock the external calls.
    when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy);
    when(s3Operations.listVersions(any(ListVersionsRequest.class), any(AmazonS3Client.class))).thenReturn(versionListing);
    when(s3Operations.deleteObjects(any(DeleteObjectsRequest.class), any(AmazonS3Client.class))).thenThrow(multiObjectDeleteException);
    // Try to call the method under test.
    try {
        s3DaoImpl.deleteDirectory(s3FileTransferRequestParamsDto);
    } catch (IllegalStateException e) {
        assertEquals(String.format("Failed to delete keys/key versions with prefix \"%s\" from bucket \"%s\". " + "Reason: One or more objects could not be deleted (Service: null; Status Code: 0; Error Code: null; Request ID: null; S3 Extended Request ID: null)", S3_KEY_PREFIX, S3_BUCKET_NAME), e.getMessage());
    }
    // Verify the external calls.
    verify(retryPolicyFactory, times(2)).getRetryPolicy();
    verify(s3Operations).listVersions(any(ListVersionsRequest.class), any(AmazonS3Client.class));
    verify(s3Operations).deleteObjects(any(DeleteObjectsRequest.class), any(AmazonS3Client.class));
    verifyNoMoreInteractionsHelper();
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) VersionListing(com.amazonaws.services.s3.model.VersionListing) S3VersionSummary(com.amazonaws.services.s3.model.S3VersionSummary) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) ListVersionsRequest(com.amazonaws.services.s3.model.ListVersionsRequest) RetryPolicy(com.amazonaws.retry.RetryPolicy) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Aggregations

RetryPolicy (com.amazonaws.retry.RetryPolicy)8 Test (org.junit.Test)6 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)3 AbstractDaoTest (org.finra.herd.dao.AbstractDaoTest)3 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)3 ClientConfiguration (com.amazonaws.ClientConfiguration)2 ListVersionsRequest (com.amazonaws.services.s3.model.ListVersionsRequest)2 VersionListing (com.amazonaws.services.s3.model.VersionListing)2 AWSSecurityTokenServiceClient (com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClient)2 AssumeRoleRequest (com.amazonaws.services.securitytoken.model.AssumeRoleRequest)2 AssumeRoleResult (com.amazonaws.services.securitytoken.model.AssumeRoleResult)2 Credentials (com.amazonaws.services.securitytoken.model.Credentials)2 Random (java.util.Random)2 AwsParamsDto (org.finra.herd.model.dto.AwsParamsDto)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)1 Policy (com.amazonaws.auth.policy.Policy)1 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)1 GetObjectTaggingRequest (com.amazonaws.services.s3.model.GetObjectTaggingRequest)1 GetObjectTaggingResult (com.amazonaws.services.s3.model.GetObjectTaggingResult)1