Search in sources :

Example 1 with HerdAWSCredentialsProvider

use of org.finra.herd.model.dto.HerdAWSCredentialsProvider 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);
    }
}
Also used : BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) AWSCredentials(com.amazonaws.auth.AWSCredentials) DefaultAWSCredentialsProviderChain(com.amazonaws.auth.DefaultAWSCredentialsProviderChain) AWSCredentialsProviderChain(com.amazonaws.auth.AWSCredentialsProviderChain) HerdAWSCredentialsProvider(org.finra.herd.model.dto.HerdAWSCredentialsProvider) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) InvocationOnMock(org.mockito.invocation.InvocationOnMock) List(java.util.List) ArrayList(java.util.ArrayList) AwsCredential(org.finra.herd.model.api.xml.AwsCredential) HerdAWSCredentialsProvider(org.finra.herd.model.dto.HerdAWSCredentialsProvider) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) Test(org.junit.Test)

Example 2 with HerdAWSCredentialsProvider

use of org.finra.herd.model.dto.HerdAWSCredentialsProvider in project herd by FINRAOS.

the class S3DaoImpl method getAWSCredentialsProvider.

/**
 * <p> Gets the {@link AWSCredentialsProvider} based on the credentials in the given parameters. </p> <p> Returns {@link DefaultAWSCredentialsProviderChain}
 * if either access or secret key is {@code null}. Otherwise returns a {@link StaticCredentialsProvider} with the credentials. </p>
 *
 * @param params - Access parameters
 *
 * @return AWS credentials provider implementation
 */
private AWSCredentialsProvider getAWSCredentialsProvider(S3FileTransferRequestParamsDto params) {
    List<AWSCredentialsProvider> providers = new ArrayList<>();
    String accessKey = params.getAwsAccessKeyId();
    String secretKey = params.getAwsSecretKey();
    if (accessKey != null && secretKey != null) {
        providers.add(new StaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)));
    }
    for (HerdAWSCredentialsProvider herdAWSCredentialsProvider : params.getAdditionalAwsCredentialsProviders()) {
        providers.add(new HerdAwsCredentialsProviderWrapper(herdAWSCredentialsProvider));
    }
    providers.add(new DefaultAWSCredentialsProviderChain());
    return new AWSCredentialsProviderChain(providers.toArray(new AWSCredentialsProvider[providers.size()]));
}
Also used : DefaultAWSCredentialsProviderChain(com.amazonaws.auth.DefaultAWSCredentialsProviderChain) HerdAWSCredentialsProvider(org.finra.herd.model.dto.HerdAWSCredentialsProvider) StaticCredentialsProvider(com.amazonaws.internal.StaticCredentialsProvider) ArrayList(java.util.ArrayList) HerdAWSCredentialsProvider(org.finra.herd.model.dto.HerdAWSCredentialsProvider) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) DefaultAWSCredentialsProviderChain(com.amazonaws.auth.DefaultAWSCredentialsProviderChain) AWSCredentialsProviderChain(com.amazonaws.auth.AWSCredentialsProviderChain)

Example 3 with HerdAWSCredentialsProvider

use of org.finra.herd.model.dto.HerdAWSCredentialsProvider in project herd by FINRAOS.

the class DownloaderControllerTest method testPerformDownloadAssertCredentialsRetrieved.

/**
 * Asserts that the controller is sending the proper implementation of credentials provider when calling S3.
 */
@Test
public void testPerformDownloadAssertCredentialsRetrieved() throws Exception {
    /*
         * Create and inject mock objects
         */
    DownloaderWebClient mockDownloaderWebClient = mock(DownloaderWebClient.class);
    DownloaderWebClient originalDownloaderWebClient = (DownloaderWebClient) ReflectionTestUtils.getField(downloaderController, "downloaderWebClient");
    ReflectionTestUtils.setField(downloaderController, "downloaderWebClient", mockDownloaderWebClient);
    DownloaderManifestReader mockDownloaderManifestReader = mock(DownloaderManifestReader.class);
    DownloaderManifestReader originalDownloaderManifestReader = (DownloaderManifestReader) ReflectionTestUtils.getField(downloaderController, "manifestReader");
    ReflectionTestUtils.setField(downloaderController, "manifestReader", mockDownloaderManifestReader);
    BusinessObjectDataHelper mockBusinessObjectDataHelper = mock(BusinessObjectDataHelper.class);
    BusinessObjectDataHelper originalBusinessObjectDataHelper = (BusinessObjectDataHelper) ReflectionTestUtils.getField(downloaderController, "businessObjectDataHelper");
    ReflectionTestUtils.setField(downloaderController, "businessObjectDataHelper", mockBusinessObjectDataHelper);
    S3Service mockS3Service = mock(S3Service.class);
    S3Service originalS3Service = (S3Service) ReflectionTestUtils.getField(downloaderController, "s3Service");
    ReflectionTestUtils.setField(downloaderController, "s3Service", mockS3Service);
    StorageFileHelper mockStorageFileHelper = mock(StorageFileHelper.class);
    StorageFileHelper originalStorageFileHelper = (StorageFileHelper) ReflectionTestUtils.getField(downloaderController, "storageFileHelper");
    ReflectionTestUtils.setField(downloaderController, "storageFileHelper", mockStorageFileHelper);
    StorageHelper mockStorageHelper = mock(StorageHelper.class);
    StorageHelper originalStorageHelper = (StorageHelper) ReflectionTestUtils.getField(downloaderController, "storageHelper");
    ReflectionTestUtils.setField(downloaderController, "storageHelper", mockStorageHelper);
    /*
         * Start test
         */
    Path localPath = Files.createTempDirectory(null);
    try {
        String s3KeyPrefix = "s3KeyPrefix";
        String storageName = "storageName";
        DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto();
        downloaderInputManifestDto.setStorageName(storageName);
        BusinessObjectData businessObjectData = new BusinessObjectData();
        StorageUnit storageUnit = new StorageUnit(new Storage(storageName, null, null), null, null, StorageUnitStatusEntity.ENABLED, null, null, null);
        S3KeyPrefixInformation s3KeyPrefixInformation = new S3KeyPrefixInformation();
        s3KeyPrefixInformation.setS3KeyPrefix(s3KeyPrefix);
        /*
             * Mock operations on mocked dependencies
             */
        when(mockDownloaderManifestReader.readJsonManifest(any())).thenReturn(downloaderInputManifestDto);
        when(mockDownloaderWebClient.getBusinessObjectData(any())).thenReturn(businessObjectData);
        when(mockBusinessObjectDataHelper.getStorageUnitByStorageName(any(), any())).thenReturn(storageUnit);
        when(mockDownloaderWebClient.getS3KeyPrefix(any())).thenReturn(s3KeyPrefixInformation);
        when(mockDownloaderWebClient.getStorageUnitDownloadCredential(any(), any())).thenReturn(new StorageUnitDownloadCredential(new AwsCredential("awsAccessKey", "awsSecretKey", "awsSessionToken", DatatypeFactory.newInstance().newXMLGregorianCalendar())));
        when(mockS3Service.downloadDirectory(any())).then(new Answer<S3FileTransferResultsDto>() {

            @Override
            public S3FileTransferResultsDto answer(InvocationOnMock invocation) throws Throwable {
                /*
                     * Call the providers' getAwsCredentials(), just like real implementation would.
                     */
                S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = invocation.getArgument(0);
                List<HerdAWSCredentialsProvider> additionalAwsCredentialsProviders = s3FileTransferRequestParamsDto.getAdditionalAwsCredentialsProviders();
                for (HerdAWSCredentialsProvider herdAWSCredentialsProvider : additionalAwsCredentialsProviders) {
                    herdAWSCredentialsProvider.getAwsCredential();
                }
                return null;
            }
        });
        /*
             * Make the call to the method under test
             */
        RegServerAccessParamsDto regServerAccessParamsDto = null;
        File manifestPath = null;
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
        s3FileTransferRequestParamsDto.setLocalPath(localPath.toString());
        s3FileTransferRequestParamsDto.setMaxThreads(1);
        downloaderController.performDownload(regServerAccessParamsDto, manifestPath, s3FileTransferRequestParamsDto);
        // Assert that the proper delegate method is called with the expected params to retrieve credentials
        verify(mockDownloaderWebClient).getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName);
    } finally {
        /*
             * Restore mocked dependencies to their original implementation
             */
        ReflectionTestUtils.setField(downloaderController, "downloaderWebClient", originalDownloaderWebClient);
        ReflectionTestUtils.setField(downloaderController, "manifestReader", originalDownloaderManifestReader);
        ReflectionTestUtils.setField(downloaderController, "businessObjectDataHelper", originalBusinessObjectDataHelper);
        ReflectionTestUtils.setField(downloaderController, "s3Service", originalS3Service);
        ReflectionTestUtils.setField(downloaderController, "storageFileHelper", originalStorageFileHelper);
        ReflectionTestUtils.setField(downloaderController, "storageHelper", originalStorageHelper);
        // Clean up any temporary files
        FileUtils.deleteDirectory(localPath.toFile());
    }
}
Also used : StorageFileHelper(org.finra.herd.service.helper.StorageFileHelper) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) StorageUnitDownloadCredential(org.finra.herd.model.api.xml.StorageUnitDownloadCredential) RegServerAccessParamsDto(org.finra.herd.model.dto.RegServerAccessParamsDto) HerdAWSCredentialsProvider(org.finra.herd.model.dto.HerdAWSCredentialsProvider) ArrayList(java.util.ArrayList) List(java.util.List) S3KeyPrefixInformation(org.finra.herd.model.api.xml.S3KeyPrefixInformation) S3Service(org.finra.herd.service.S3Service) S3FileTransferResultsDto(org.finra.herd.model.dto.S3FileTransferResultsDto) Path(java.nio.file.Path) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) StorageUnit(org.finra.herd.model.api.xml.StorageUnit) Storage(org.finra.herd.model.api.xml.Storage) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StorageHelper(org.finra.herd.service.helper.StorageHelper) BusinessObjectDataHelper(org.finra.herd.service.helper.BusinessObjectDataHelper) File(java.io.File) AwsCredential(org.finra.herd.model.api.xml.AwsCredential) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 HerdAWSCredentialsProvider (org.finra.herd.model.dto.HerdAWSCredentialsProvider)3 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)2 AWSCredentialsProviderChain (com.amazonaws.auth.AWSCredentialsProviderChain)2 DefaultAWSCredentialsProviderChain (com.amazonaws.auth.DefaultAWSCredentialsProviderChain)2 List (java.util.List)2 AwsCredential (org.finra.herd.model.api.xml.AwsCredential)2 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)2 Test (org.junit.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)1 StaticCredentialsProvider (com.amazonaws.internal.StaticCredentialsProvider)1 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)1 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)1 File (java.io.File)1 Path (java.nio.file.Path)1 BusinessObjectData (org.finra.herd.model.api.xml.BusinessObjectData)1 S3KeyPrefixInformation (org.finra.herd.model.api.xml.S3KeyPrefixInformation)1