AwsParamsDto awsParamsDto = awsHelper.getAwsParamsDto();
ClientConfiguration clientConfiguration = awsHelper.getClientConfiguration(awsHelper.getAwsParamsDto());
/** * Logs overall transfer rate for an S3 file transfer operation. * * @param s3FileTransferResultsDto the DTO for the S3 file transfer operation results */ private void logOverallTransferRate(S3FileTransferResultsDto s3FileTransferResultsDto) { if (LOGGER.isInfoEnabled()) { NumberFormat formatter = new DecimalFormat("#0.00"); LOGGER.info("overallTransferRateKiloBytesPerSecond={} overallTransferRateMegaBitsPerSecond={}", formatter.format(awsHelper .getTransferRateInKilobytesPerSecond(s3FileTransferResultsDto.getTotalBytesTransferred(), s3FileTransferResultsDto.getDurationMillis())), formatter.format(awsHelper .getTransferRateInMegabitsPerSecond(s3FileTransferResultsDto.getTotalBytesTransferred(), s3FileTransferResultsDto.getDurationMillis()))); } }
@Override public String decrypt(AwsParamsDto awsParamsDto, String base64ciphertextBlob) { // Construct a new AWS KMS service client using the specified client configuration. // A credentials provider chain will be used that searches for credentials in this order: // - Environment Variables - AWS_ACCESS_KEY_ID and AWS_SECRET_KEY // - Java System Properties - aws.accessKeyId and aws.secretKey // - Instance Profile Credentials - delivered through the Amazon EC2 metadata service AWSKMSClient awsKmsClient = new AWSKMSClient(awsHelper.getClientConfiguration(awsParamsDto)); // Decode the base64 encoded ciphertext. ByteBuffer ciphertextBlob = ByteBuffer.wrap(Base64.decodeBase64(base64ciphertextBlob)); // Create the decrypt request. DecryptRequest decryptRequest = new DecryptRequest().withCiphertextBlob(ciphertextBlob); // Call AWS KMS decrypt service method. DecryptResult decryptResult = kmsOperations.decrypt(awsKmsClient, decryptRequest); // Get decrypted plaintext data. ByteBuffer plainText = decryptResult.getPlaintext(); // Return the plain text as a string. return new String(plainText.array(), StandardCharsets.UTF_8); } }
@Test public void testGetTransferRateInKilobytesPerSecond() { assertEquals(Double.valueOf(0.1), awsHelper.getTransferRateInKilobytesPerSecond(10L, 100L)); }
@Test public void testGetTransferRateInMegabitsPerSecond() { assertEquals(Double.valueOf(0.8), awsHelper.getTransferRateInMegabitsPerSecond(1000L, 10L)); } }
@Bean public AwsHelper awsHelper() { return new AwsHelper(); }
@Override public String decrypt(AwsParamsDto awsParamsDto, String base64ciphertextBlob) { // Construct a new AWS KMS service client using the specified client configuration. // A credentials provider chain will be used that searches for credentials in this order: // - Environment Variables - AWS_ACCESS_KEY_ID and AWS_SECRET_KEY // - Java System Properties - aws.accessKeyId and aws.secretKey // - Instance Profile Credentials - delivered through the Amazon EC2 metadata service AWSKMSClient awsKmsClient = new AWSKMSClient(awsHelper.getClientConfiguration(awsParamsDto)); // Decode the base64 encoded ciphertext. ByteBuffer ciphertextBlob = ByteBuffer.wrap(Base64.decodeBase64(base64ciphertextBlob)); // Create the decrypt request. DecryptRequest decryptRequest = new DecryptRequest().withCiphertextBlob(ciphertextBlob); // Call AWS KMS decrypt service method. DecryptResult decryptResult = kmsOperations.decrypt(awsKmsClient, decryptRequest); // Get decrypted plaintext data. ByteBuffer plainText = decryptResult.getPlaintext(); // Return the plain text as a string. return new String(plainText.array(), StandardCharsets.UTF_8); } }
@Bean public AwsHelper awsHelper() { return new AwsHelper(); }
AwsParamsDto awsParamsDto = awsHelper.getAwsParamsDto();
ClientConfiguration clientConfiguration = awsHelper.getClientConfiguration(awsHelper.getAwsParamsDto());
/** * Creates a client for accessing Amazon SNS. * * @param awsParamsDto the AWS related parameters DTO that includes optional proxy information * * @return the Amazon SNS client */ @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) public AmazonSNS getAmazonSNSClient(AwsParamsDto awsParamsDto) { // Construct and return a new client to invoke service methods on Amazon SNS using default credentials provider chain. return AmazonSNSClientBuilder.standard().withClientConfiguration(awsHelper.getClientConfiguration(awsParamsDto)) .withRegion(awsParamsDto.getAwsRegionName()).build(); }
/** * Logs overall transfer rate for an S3 file transfer operation. * * @param s3FileTransferResultsDto the DTO for the S3 file transfer operation results */ private void logOverallTransferRate(S3FileTransferResultsDto s3FileTransferResultsDto) { if (LOGGER.isInfoEnabled()) { NumberFormat formatter = new DecimalFormat("#0.00"); LOGGER.info("overallTransferRateKiloBytesPerSecond={} overallTransferRateMegaBitsPerSecond={}", formatter.format(awsHelper .getTransferRateInKilobytesPerSecond(s3FileTransferResultsDto.getTotalBytesTransferred(), s3FileTransferResultsDto.getDurationMillis())), formatter.format(awsHelper .getTransferRateInMegabitsPerSecond(s3FileTransferResultsDto.getTotalBytesTransferred(), s3FileTransferResultsDto.getDurationMillis()))); } }
/** * Sends out an SQS message that contains the specified storage policy selection to the SQS queue name. * * @param sqsQueueName the SQS queue name * @param storagePolicySelection the storage policy selection */ protected void sendStoragePolicySelectionSqsMessage(String sqsQueueName, StoragePolicySelection storagePolicySelection) { // Send the storage policy selection to the relative AWS SQS queue. AwsParamsDto awsParamsDto = awsHelper.getAwsParamsDto(); String messageText = null; try { messageText = jsonHelper.objectToJson(storagePolicySelection); sqsDao.sendMessage(awsParamsDto, sqsQueueName, messageText, null); } catch (RuntimeException e) { LOGGER.error("Failed to publish message to the JMS queue. jmsQueueName=\"{}\" jmsMessagePayload={}", sqsQueueName, messageText); // Rethrow the original exception. throw e; } }
when(awsHelper.getAwsParamsDto()).thenReturn(awsParamsDto); when(awsHelper.getClientConfiguration(awsParamsDto)).thenReturn(clientConfiguration); when(credStashFactory.getCredStash(AWS_REGION_NAME, TABLE_NAME, clientConfiguration)).thenReturn(credStash); when(jsonHelper.unmarshallJsonToObject(Map.class, CREDSTASH_ENCRYPTION_CONTEXT)).thenReturn(credStashEncryptionContextMap); verify(awsHelper).getAwsParamsDto(); verify(awsHelper).getClientConfiguration(awsParamsDto); verify(credStashFactory).getCredStash(AWS_REGION_NAME, TABLE_NAME, clientConfiguration); verify(jsonHelper).unmarshallJsonToObject(Map.class, CREDSTASH_ENCRYPTION_CONTEXT);
/** * Creates a client for accessing Amazon SQS. * * @param awsParamsDto the AWS related parameters DTO that includes optional proxy information * * @return the Amazon SQS client */ @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) public AmazonSQS getAmazonSQSClient(AwsParamsDto awsParamsDto) { // Construct and return a new client to invoke service methods on Amazon SQS using default credentials provider chain. return AmazonSQSClientBuilder.standard().withClientConfiguration(awsHelper.getClientConfiguration(awsParamsDto)) .withRegion(awsParamsDto.getAwsRegionName()).build(); }
/** * Sends out an SQS message that contains the specified storage policy selection to the SQS queue name. * * @param sqsQueueName the SQS queue name * @param storagePolicySelection the storage policy selection */ protected void sendStoragePolicySelectionSqsMessage(String sqsQueueName, StoragePolicySelection storagePolicySelection) { // Send the storage policy selection to the relative AWS SQS queue. AwsParamsDto awsParamsDto = awsHelper.getAwsParamsDto(); String messageText = null; try { messageText = jsonHelper.objectToJson(storagePolicySelection); sqsDao.sendMessage(awsParamsDto, sqsQueueName, messageText, null); } catch (RuntimeException e) { LOGGER.error("Failed to publish message to the JMS queue. jmsQueueName=\"{}\" jmsMessagePayload={}", sqsQueueName, messageText); // Rethrow the original exception. throw e; } }
when(awsHelper.getAwsParamsDto()).thenReturn(awsParamsDto); when(awsHelper.getClientConfiguration(awsParamsDto)).thenReturn(clientConfiguration); when(credStashFactory.getCredStash(AWS_REGION_NAME, TABLE_NAME, clientConfiguration)).thenReturn(credStash); when(jsonHelper.unmarshallJsonToObject(Map.class, CREDSTASH_ENCRYPTION_CONTEXT)).thenReturn(credStashEncryptionContextMap); verify(awsHelper).getAwsParamsDto(); verify(awsHelper).getClientConfiguration(awsParamsDto); verify(credStashFactory).getCredStash(AWS_REGION_NAME, TABLE_NAME, clientConfiguration); verify(jsonHelper).unmarshallJsonToObject(Map.class, CREDSTASH_ENCRYPTION_CONTEXT);
/** * Creates a client for accessing Amazon SNS. * * @param awsParamsDto the AWS related parameters DTO that includes optional proxy information * * @return the Amazon SNS client */ @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) public AmazonSNS getAmazonSNSClient(AwsParamsDto awsParamsDto) { // Construct and return a new client to invoke service methods on Amazon SNS using default credentials provider chain. return AmazonSNSClientBuilder.standard().withClientConfiguration(awsHelper.getClientConfiguration(awsParamsDto)) .withRegion(awsParamsDto.getAwsRegionName()).build(); }
/** * Gets the decrypted value for the given configuration option. * * @param configurationValue - The configuration which contains the encrypted value. * * @return decrypted value */ public String getDecryptedConfigurationValue(ConfigurationValue configurationValue) { String encryptedValue = configurationHelper.getProperty(configurationValue); if (StringUtils.isBlank(encryptedValue)) { throw new IllegalStateException("Unable to decrypt configuration value \"" + configurationValue.getKey() + "\" since it is not configured."); } try { return kmsDao.decrypt(awsHelper.getAwsParamsDto(), encryptedValue); } catch (Exception e) { throw new IllegalStateException("Error decrypting configuration value \"" + configurationValue.getKey() + "\".", e); } } }