Search in sources :

Example 1 with UploadSingleCredentialExtensionResponse

use of org.finra.herd.model.api.xml.UploadSingleCredentialExtensionResponse in project herd by FINRAOS.

the class UploadDownloadServiceImpl method extendUploadSingleCredentials.

@NamespacePermission(fields = "#namespace", permissions = NamespacePermissionEnum.WRITE)
@Override
public UploadSingleCredentialExtensionResponse extendUploadSingleCredentials(String namespace, String businessObjectDefinitionName, String businessObjectFormatUsage, String businessObjectFormatFileType, Integer businessObjectFormatVersion, String partitionValue, Integer businessObjectDataVersion) {
    // Create the business object data key.
    BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey(namespace, businessObjectDefinitionName, businessObjectFormatUsage, businessObjectFormatFileType, businessObjectFormatVersion, partitionValue, null, businessObjectDataVersion);
    // Validate and trim the business object data key.
    businessObjectDataHelper.validateBusinessObjectDataKey(businessObjectDataKey, true, true);
    // Get the business object data for the key.
    BusinessObjectDataEntity businessObjectDataEntity = businessObjectDataDaoHelper.getBusinessObjectDataEntity(businessObjectDataKey);
    // Ensure the status of the business object data is "uploading" in order to extend credentials.
    if (!(businessObjectDataEntity.getStatus().getCode().equals(BusinessObjectDataStatusEntity.UPLOADING))) {
        throw new IllegalArgumentException(String.format(String.format("Business object data {%s} has a status of \"%s\" and must be \"%s\" to extend " + "credentials.", businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey), businessObjectDataEntity.getStatus().getCode(), BusinessObjectDataStatusEntity.UPLOADING)));
    }
    // Get the S3 managed "loading dock" storage entity and make sure it exists.
    StorageEntity storageEntity = storageDaoHelper.getStorageEntity(StorageEntity.MANAGED_LOADING_DOCK_STORAGE);
    String s3BucketName = storageHelper.getStorageBucketName(storageEntity);
    // Get the storage unit entity for this business object data in the S3 managed "loading dock" storage and make sure it exists.
    StorageUnitEntity storageUnitEntity = storageUnitDaoHelper.getStorageUnitEntity(StorageEntity.MANAGED_LOADING_DOCK_STORAGE, businessObjectDataEntity);
    // Validate that the storage unit contains exactly one storage file.
    assertHasOneStorageFile(storageUnitEntity);
    // Get the storage file entity.
    StorageFileEntity storageFileEntity = IterableUtils.get(storageUnitEntity.getStorageFiles(), 0);
    // Get the storage file path.
    String storageFilePath = storageFileEntity.getPath();
    String awsRoleArn = getStorageUploadRoleArn(storageEntity);
    Integer awsRoleDurationSeconds = getStorageUploadSessionDuration(storageEntity);
    String awsKmsKeyId = storageHelper.getStorageKmsKeyId(storageEntity);
    // Get the temporary security credentials to access S3_MANAGED_STORAGE.
    Credentials assumedSessionCredentials = stsDao.getTemporarySecurityCredentials(awsHelper.getAwsParamsDto(), String.valueOf(businessObjectDataEntity.getId()), awsRoleArn, awsRoleDurationSeconds, createUploaderPolicy(s3BucketName, storageFilePath, awsKmsKeyId));
    // Create the response.
    UploadSingleCredentialExtensionResponse response = new UploadSingleCredentialExtensionResponse();
    response.setAwsAccessKey(assumedSessionCredentials.getAccessKeyId());
    response.setAwsSecretKey(assumedSessionCredentials.getSecretAccessKey());
    response.setAwsSessionToken(assumedSessionCredentials.getSessionToken());
    response.setAwsSessionExpirationTime(HerdDateUtils.getXMLGregorianCalendarValue(assumedSessionCredentials.getExpiration()));
    return response;
}
Also used : StorageFileEntity(org.finra.herd.model.jpa.StorageFileEntity) StorageUnitEntity(org.finra.herd.model.jpa.StorageUnitEntity) UploadSingleCredentialExtensionResponse(org.finra.herd.model.api.xml.UploadSingleCredentialExtensionResponse) StorageEntity(org.finra.herd.model.jpa.StorageEntity) BusinessObjectDataEntity(org.finra.herd.model.jpa.BusinessObjectDataEntity) BusinessObjectDataKey(org.finra.herd.model.api.xml.BusinessObjectDataKey) Credentials(com.amazonaws.services.securitytoken.model.Credentials) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Example 2 with UploadSingleCredentialExtensionResponse

use of org.finra.herd.model.api.xml.UploadSingleCredentialExtensionResponse in project herd by FINRAOS.

the class UploadDownloadRestControllerTest method testExtendUploadSingleCredentials.

@Test
public void testExtendUploadSingleCredentials() {
    // Create a response.
    UploadSingleCredentialExtensionResponse response = new UploadSingleCredentialExtensionResponse(AWS_ASSUMED_ROLE_ACCESS_KEY, AWS_ASSUMED_ROLE_SECRET_KEY, AWS_ASSUMED_ROLE_SESSION_TOKEN, AWS_ASSUMED_ROLE_SESSION_EXPIRATION_TIME);
    // Mock the external calls.
    when(uploadDownloadService.extendUploadSingleCredentials(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, DATA_VERSION)).thenReturn(response);
    // Call the method under test.
    UploadSingleCredentialExtensionResponse result = uploadDownloadRestController.extendUploadSingleCredentials(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, DATA_VERSION);
    // Verify the external calls.
    verify(uploadDownloadService).extendUploadSingleCredentials(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, DATA_VERSION);
    verifyNoMoreInteractionsHelper();
    // Validate the results.
    assertEquals(response, result);
}
Also used : UploadSingleCredentialExtensionResponse(org.finra.herd.model.api.xml.UploadSingleCredentialExtensionResponse) Test(org.junit.Test)

Example 3 with UploadSingleCredentialExtensionResponse

use of org.finra.herd.model.api.xml.UploadSingleCredentialExtensionResponse in project herd by FINRAOS.

the class UploadDownloadServiceTest method testExtendUploadSingleCredentials.

@Test
public void testExtendUploadSingleCredentials() throws InterruptedException {
    // Create source and target business object formats database entities which are required to initiate an upload.
    uploadDownloadServiceTestHelper.createDatabaseEntitiesForUploadDownloadTesting();
    // Initiate a file upload.
    UploadSingleInitiationResponse uploadSingleInitiationResponse = uploadDownloadService.initiateUploadSingle(uploadDownloadServiceTestHelper.createUploadSingleInitiationRequest());
    // Sleep a short amount of time to ensure the extended credentials don't return the same expiration as the initial credentials.
    Thread.sleep(10);
    // Initiate the download against the uploaded data (i.e. the target business object data).
    UploadSingleCredentialExtensionResponse uploadSingleCredentialExtensionResponse = extendUploadSingleCredentials(uploadSingleInitiationResponse.getSourceBusinessObjectData());
    // Validate the returned object.
    assertNotNull(uploadSingleCredentialExtensionResponse.getAwsAccessKey());
    assertNotNull(uploadSingleCredentialExtensionResponse.getAwsSecretKey());
    assertNotNull(uploadSingleCredentialExtensionResponse.getAwsSessionToken());
    assertNotNull(uploadSingleCredentialExtensionResponse.getAwsSessionExpirationTime());
    assertNotNull(uploadSingleInitiationResponse.getAwsSessionExpirationTime());
    // We are displaying the values in case there is a problem because this test was acting flaky.
    if (uploadSingleCredentialExtensionResponse.getAwsSessionExpirationTime().toGregorianCalendar().getTimeInMillis() <= uploadSingleInitiationResponse.getAwsSessionExpirationTime().toGregorianCalendar().getTimeInMillis()) {
        fail("Initial expiration time \"" + uploadSingleInitiationResponse.getAwsSessionExpirationTime().toGregorianCalendar().getTimeInMillis() + "\" is not > extended expiration time \"" + uploadSingleCredentialExtensionResponse.getAwsSessionExpirationTime().toGregorianCalendar().getTimeInMillis() + "\".");
    }
}
Also used : UploadSingleCredentialExtensionResponse(org.finra.herd.model.api.xml.UploadSingleCredentialExtensionResponse) UploadSingleInitiationResponse(org.finra.herd.model.api.xml.UploadSingleInitiationResponse) Test(org.junit.Test)

Aggregations

UploadSingleCredentialExtensionResponse (org.finra.herd.model.api.xml.UploadSingleCredentialExtensionResponse)3 Test (org.junit.Test)2 Credentials (com.amazonaws.services.securitytoken.model.Credentials)1 NamespacePermission (org.finra.herd.model.annotation.NamespacePermission)1 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)1 UploadSingleInitiationResponse (org.finra.herd.model.api.xml.UploadSingleInitiationResponse)1 BusinessObjectDataEntity (org.finra.herd.model.jpa.BusinessObjectDataEntity)1 StorageEntity (org.finra.herd.model.jpa.StorageEntity)1 StorageFileEntity (org.finra.herd.model.jpa.StorageFileEntity)1 StorageUnitEntity (org.finra.herd.model.jpa.StorageUnitEntity)1