use of bio.terra.workspace.model.CreatedControlledGcpGcsBucket in project terra-workspace-manager by DataBiosphere.
the class ControlledApplicationSharedGcsBucketLifecycle method doUserJourney.
@Override
public void doUserJourney(TestUserSpecification unused, WorkspaceApi workspaceApi) throws Exception {
ApiClient ownerApiClient = ClientTestUtils.getClientForTestUser(owner, server);
ApiClient wsmappApiClient = ClientTestUtils.getClientForTestUser(wsmapp, server);
WorkspaceApplicationApi ownerWsmAppApi = new WorkspaceApplicationApi(ownerApiClient);
ControlledGcpResourceApi wsmappResourceApi = new ControlledGcpResourceApi(wsmappApiClient);
// Owner adds a reader and a writer to the workspace
workspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(reader.userEmail), getWorkspaceId(), IamRole.READER);
logger.info("Added {} as a reader to workspace {}", reader.userEmail, getWorkspaceId());
workspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(writer.userEmail), getWorkspaceId(), IamRole.WRITER);
logger.info("Added {} as a writer to workspace {}", writer.userEmail, getWorkspaceId());
// Create the cloud context
String projectId = CloudContextMaker.createGcpCloudContext(getWorkspaceId(), workspaceApi);
assertNotNull(projectId);
logger.info("Created project {}", projectId);
// Create the bucket - should fail because application is not enabled
String bucketResourceName = RandomStringUtils.random(6, true, false);
ApiException createBucketFails = assertThrows(ApiException.class, () -> GcsBucketUtils.makeControlledGcsBucketAppShared(wsmappResourceApi, getWorkspaceId(), bucketResourceName, CloningInstructionsEnum.NOTHING));
// TODO: [PF-1208] this should be FORBIDDEN (403), but we are throwing the wrong thing
assertEquals(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED, createBucketFails.getCode());
logger.info("Failed to create bucket, as expected");
// Enable the application in the workspace
WorkspaceApplicationDescription applicationDescription = ownerWsmAppApi.enableWorkspaceApplication(getWorkspaceId(), TEST_WSM_APP);
assertThat(applicationDescription.getApplicationState(), equalTo(ApplicationState.OPERATING));
logger.info("Enabled application in the workspace");
// Validate that it is enabled
WorkspaceApplicationDescription retrievedDescription = ownerWsmAppApi.getWorkspaceApplication(getWorkspaceId(), TEST_WSM_APP);
assertThat(applicationDescription, equalTo(retrievedDescription));
assertThat(applicationDescription.getWorkspaceApplicationState(), equalTo(WorkspaceApplicationState.ENABLED));
// Create the bucket - should work this time
CreatedControlledGcpGcsBucket createdBucket = GcsBucketUtils.makeControlledGcsBucketAppShared(wsmappResourceApi, getWorkspaceId(), bucketResourceName, CloningInstructionsEnum.NOTHING);
bucketName = createdBucket.getGcpBucket().getAttributes().getBucketName();
assertNotNull(bucketName);
logger.info("Created bucket {}", bucketName);
// Try to disable; should error because you cannot disable an app if it owns resources
// in the workspace.
ApiException disableAppFails = assertThrows(ApiException.class, () -> ownerWsmAppApi.disableWorkspaceApplication(getWorkspaceId(), TEST_WSM_APP));
assertEquals(HttpStatusCodes.STATUS_CODE_BAD_REQUEST, disableAppFails.getCode());
logger.info("Failed to disable app, as expected");
try (GcsBucketAccessTester tester = new GcsBucketAccessTester(wsmapp, bucketName, projectId)) {
tester.checkAccess(wsmapp, ControlledResourceIamRole.EDITOR);
tester.checkAccess(owner, ControlledResourceIamRole.WRITER);
tester.checkAccess(writer, ControlledResourceIamRole.WRITER);
tester.checkAccess(reader, ControlledResourceIamRole.READER);
}
// The reader should be able to enumerate the bucket.
ResourceApi readerResourceApi = ClientTestUtils.getResourceClient(reader, server);
ResourceList bucketList = readerResourceApi.enumerateResources(getWorkspaceId(), 0, 5, ResourceType.GCS_BUCKET, StewardshipType.CONTROLLED);
assertEquals(1, bucketList.getResources().size());
MultiResourcesUtils.assertResourceType(ResourceType.GCS_BUCKET, bucketList);
// Owner cannot delete the bucket through WSM
ControlledGcpResourceApi ownerResourceApi = new ControlledGcpResourceApi(ownerApiClient);
ApiException cannotDelete = assertThrows(ApiException.class, () -> GcsBucketUtils.deleteControlledGcsBucket(createdBucket.getResourceId(), getWorkspaceId(), ownerResourceApi));
// TODO: [PF-1208] this should be FORBIDDEN (403), but we are throwing the wrong thing
assertEquals(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED, cannotDelete.getCode());
logger.info("Owner delete failed as expected");
// Application can delete the bucket through WSM
GcsBucketUtils.deleteControlledGcsBucket(createdBucket.getResourceId(), getWorkspaceId(), wsmappResourceApi);
logger.info("Application delete succeeded");
}
use of bio.terra.workspace.model.CreatedControlledGcpGcsBucket in project terra-workspace-manager by DataBiosphere.
the class ControlledGcsBucketLifecycle method testCloneBucket.
private void testCloneBucket(GcpGcsBucketResource sourceBucket, TestUserSpecification cloningUser, ControlledGcpResourceApi resourceApi) throws Exception {
final String destinationBucketName = "clone-" + UUID.randomUUID().toString();
// clone the bucket
final String clonedBucketDescription = "A cloned bucket";
final CloneControlledGcpGcsBucketRequest cloneRequest = new CloneControlledGcpGcsBucketRequest().bucketName(destinationBucketName).destinationWorkspaceId(getDestinationWorkspaceId()).name(sourceBucket.getMetadata().getName()).description(clonedBucketDescription).location(// use same as src
null).cloningInstructions(CloningInstructionsEnum.RESOURCE).jobControl(new JobControl().id(UUID.randomUUID().toString()));
logger.info("Cloning bucket\n\tname: {}\n\tresource ID: {}\n\tworkspace: {}\n\t" + "projectID: {}\ninto destination bucket\n\tname: {}\n\tworkspace: {}\n\tprojectID: {}", sourceBucket.getMetadata().getName(), sourceBucket.getMetadata().getResourceId(), sourceBucket.getMetadata().getWorkspaceId(), getSourceProjectId(), destinationBucketName, getDestinationWorkspaceId(), getDestinationProjectId());
CloneControlledGcpGcsBucketResult cloneResult = resourceApi.cloneGcsBucket(cloneRequest, sourceBucket.getMetadata().getWorkspaceId(), sourceBucket.getMetadata().getResourceId());
cloneResult = ClientTestUtils.pollWhileRunning(cloneResult, () -> resourceApi.getCloneGcsBucketResult(cloneRequest.getDestinationWorkspaceId(), cloneRequest.getJobControl().getId()), CloneControlledGcpGcsBucketResult::getJobReport, Duration.ofSeconds(5));
ClientTestUtils.assertJobSuccess("cloned bucket", cloneResult.getJobReport(), cloneResult.getErrorReport());
final ClonedControlledGcpGcsBucket clonedBucket = cloneResult.getBucket();
assertEquals(getWorkspaceId(), clonedBucket.getSourceWorkspaceId());
assertEquals(sourceBucket.getMetadata().getResourceId(), clonedBucket.getSourceResourceId());
final CreatedControlledGcpGcsBucket createdBucket = clonedBucket.getBucket();
final GcpGcsBucketResource clonedResource = createdBucket.getGcpBucket();
assertEquals(destinationBucketName, clonedResource.getAttributes().getBucketName());
final ResourceMetadata clonedResourceMetadata = clonedResource.getMetadata();
assertEquals(getDestinationWorkspaceId(), clonedResourceMetadata.getWorkspaceId());
assertEquals(sourceBucket.getMetadata().getName(), clonedResourceMetadata.getName());
assertEquals(clonedBucketDescription, clonedResourceMetadata.getDescription());
final ResourceMetadata sourceMetadata = sourceBucket.getMetadata();
assertEquals(CloningInstructionsEnum.NOTHING, clonedResourceMetadata.getCloningInstructions());
assertEquals(sourceMetadata.getCloudPlatform(), clonedResourceMetadata.getCloudPlatform());
assertEquals(ResourceType.GCS_BUCKET, clonedResourceMetadata.getResourceType());
assertEquals(StewardshipType.CONTROLLED, clonedResourceMetadata.getStewardshipType());
assertEquals(sourceMetadata.getControlledResourceMetadata().getAccessScope(), clonedResourceMetadata.getControlledResourceMetadata().getAccessScope());
assertEquals(sourceMetadata.getControlledResourceMetadata().getManagedBy(), clonedResourceMetadata.getControlledResourceMetadata().getManagedBy());
assertEquals(sourceMetadata.getControlledResourceMetadata().getPrivateResourceUser(), clonedResourceMetadata.getControlledResourceMetadata().getPrivateResourceUser());
assertEquals(CloudPlatform.GCP, clonedResourceMetadata.getCloudPlatform());
final Storage destinationProjectStorageClient = ClientTestUtils.getGcpStorageClient(cloningUser, getDestinationProjectId());
final Bucket destinationGcsBucket = destinationProjectStorageClient.get(destinationBucketName);
// Location, storage class, and lifecycle rules should match values from createBucketAttempt
assertEquals(StorageClass.STANDARD, destinationGcsBucket.getStorageClass());
assertEquals(BUCKET_LOCATION, // default since not specified
destinationGcsBucket.getLocation());
assertEquals(2, destinationGcsBucket.getLifecycleRules().size());
verifyClonedLifecycleRules(destinationGcsBucket);
assertEquals(CloningInstructionsEnum.RESOURCE, clonedBucket.getEffectiveCloningInstructions());
// test retrieving file from destination bucket
Storage cloningUserStorageClient = ClientTestUtils.getGcpStorageClient(cloningUser, getDestinationProjectId());
BlobId blobId = BlobId.of(destinationBucketName, GCS_BLOB_NAME);
assertNotNull(blobId);
final Blob retrievedFile = cloningUserStorageClient.get(blobId);
assertNotNull(retrievedFile);
assertEquals(blobId.getName(), retrievedFile.getBlobId().getName());
}
use of bio.terra.workspace.model.CreatedControlledGcpGcsBucket in project terra-workspace-manager by DataBiosphere.
the class PrivateControlledGcsBucketLifecycle method doUserJourney.
@Override
public void doUserJourney(TestUserSpecification testUser, WorkspaceApi workspaceApi) throws Exception {
String projectId = CloudContextMaker.createGcpCloudContext(getWorkspaceId(), workspaceApi);
ControlledGcpResourceApi workspaceOwnerResourceApi = ClientTestUtils.getControlledGcpResourceClient(testUser, server);
ControlledGcpResourceApi privateUserResourceApi = ClientTestUtils.getControlledGcpResourceClient(privateResourceUser, server);
workspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(workspaceReader.userEmail), getWorkspaceId(), IamRole.READER);
logger.info("Added {} as a reader to workspace {}", workspaceReader.userEmail, getWorkspaceId());
workspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(privateResourceUser.userEmail), getWorkspaceId(), IamRole.WRITER);
logger.info("Added {} as a writer to workspace {}", privateResourceUser.userEmail, getWorkspaceId());
// Create a private bucket, which privateResourceUser assigns to themselves.
// Cloud IAM permissions may take several minutes to sync, so we retry this operation until
// it succeeds.
CreatedControlledGcpGcsBucket bucket = ClientTestUtils.getWithRetryOnException(() -> createPrivateBucket(privateUserResourceApi));
UUID resourceId = bucket.getResourceId();
// Retrieve the bucket resource from WSM
logger.info("Retrieving bucket resource id {}", resourceId.toString());
GcpGcsBucketResource gotBucket = privateUserResourceApi.getBucket(getWorkspaceId(), resourceId);
String bucketName = gotBucket.getAttributes().getBucketName();
assertEquals(bucket.getGcpBucket().getAttributes().getBucketName(), bucketName);
// Assert the bucket is assigned to privateResourceUser, even though resource user was
// not specified
assertEquals(privateResourceUser.userEmail, gotBucket.getMetadata().getControlledResourceMetadata().getPrivateResourceUser().getUserName());
try (GcsBucketAccessTester tester = new GcsBucketAccessTester(privateResourceUser, bucketName, projectId)) {
tester.checkAccessWait(privateResourceUser, ControlledResourceIamRole.EDITOR);
// workspace owner can do nothing
tester.checkAccess(testUser, null);
tester.checkAccess(workspaceReader, null);
}
// Any workspace user should be able to enumerate all buckets, even though they can't access
// their contents.
ResourceApi readerApi = ClientTestUtils.getResourceClient(workspaceReader, server);
ResourceList bucketList = readerApi.enumerateResources(getWorkspaceId(), 0, 5, ResourceType.GCS_BUCKET, StewardshipType.CONTROLLED);
assertEquals(1, bucketList.getResources().size());
MultiResourcesUtils.assertResourceType(ResourceType.GCS_BUCKET, bucketList);
// Workspace owner has DELETER role and can delete the bucket through WSM
var ownerDeleteResult = deleteBucket(workspaceOwnerResourceApi, resourceId);
ClientTestUtils.assertJobSuccess("owner delete bucket", ownerDeleteResult.getJobReport(), ownerDeleteResult.getErrorReport());
// verify the bucket was deleted from WSM metadata
ApiException bucketIsMissing = assertThrows(ApiException.class, () -> workspaceOwnerResourceApi.getBucket(getWorkspaceId(), resourceId), "Incorrectly found a deleted bucket!");
assertEquals(HttpStatusCodes.STATUS_CODE_NOT_FOUND, bucketIsMissing.getCode());
// also verify it was deleted from GCP
Storage ownerStorageClient = ClientTestUtils.getGcpStorageClient(testUser, projectId);
Bucket maybeBucket = ownerStorageClient.get(bucketName);
assertNull(maybeBucket);
// TODO: PF-1218 - change these to negative tests - should error - when
// the ticket is complete. These exercise two create cases with currently
// valid combinations of private user.
PrivateResourceIamRoles roles = new PrivateResourceIamRoles();
roles.add(ControlledResourceIamRole.READER);
// Supply all private user parameters
PrivateResourceUser privateUserFull = new PrivateResourceUser().userName(privateResourceUser.userEmail).privateResourceIamRoles(roles);
CreatedControlledGcpGcsBucket userFullBucket = GcsBucketUtils.makeControlledGcsBucket(privateUserResourceApi, getWorkspaceId(), RESOURCE_PREFIX + UUID.randomUUID().toString(), /*bucketName=*/
null, AccessScope.PRIVATE_ACCESS, ManagedBy.USER, CloningInstructionsEnum.NOTHING, privateUserFull);
assertNotNull(userFullBucket.getGcpBucket().getAttributes().getBucketName());
deleteBucket(workspaceOwnerResourceApi, userFullBucket.getResourceId());
// Supply just the roles, but no email
PrivateResourceUser privateUserNoEmail = new PrivateResourceUser().userName(null).privateResourceIamRoles(roles);
CreatedControlledGcpGcsBucket userNoEmailBucket = GcsBucketUtils.makeControlledGcsBucket(privateUserResourceApi, getWorkspaceId(), RESOURCE_PREFIX + UUID.randomUUID().toString(), /*bucketName=*/
null, AccessScope.PRIVATE_ACCESS, ManagedBy.USER, CloningInstructionsEnum.NOTHING, privateUserNoEmail);
assertNotNull(userNoEmailBucket.getGcpBucket().getAttributes().getBucketName());
deleteBucket(workspaceOwnerResourceApi, userNoEmailBucket.getResourceId());
String uniqueBucketName = String.format("terra_%s_bucket", UUID.randomUUID().toString().replace("-", "_"));
CreatedControlledGcpGcsBucket bucketWithBucketNameSpecified = GcsBucketUtils.makeControlledGcsBucket(privateUserResourceApi, getWorkspaceId(), RESOURCE_PREFIX + UUID.randomUUID().toString(), /*bucketName=*/
uniqueBucketName, AccessScope.PRIVATE_ACCESS, ManagedBy.USER, CloningInstructionsEnum.NOTHING, privateUserFull);
assertEquals(uniqueBucketName, bucketWithBucketNameSpecified.getGcpBucket().getAttributes().getBucketName());
deleteBucket(workspaceOwnerResourceApi, bucketWithBucketNameSpecified.getResourceId());
}
use of bio.terra.workspace.model.CreatedControlledGcpGcsBucket in project terra-workspace-manager by DataBiosphere.
the class ControlledApplicationPrivateGcsBucketLifecycle method testAssignedReader.
private void testAssignedReader(ControlledGcpResourceApi resourceApi, String projectId) throws Exception {
PrivateResourceIamRoles iamRoles = new PrivateResourceIamRoles();
iamRoles.add(ControlledResourceIamRole.READER);
PrivateResourceUser privateUser = new PrivateResourceUser().privateResourceIamRoles(iamRoles).userName(writer.userEmail);
String bucketResourceName = RandomStringUtils.random(6, true, false);
CreatedControlledGcpGcsBucket createdBucket = GcsBucketUtils.makeControlledGcsBucketAppPrivate(resourceApi, getWorkspaceId(), bucketResourceName, CloningInstructionsEnum.NOTHING, privateUser);
bucketName = createdBucket.getGcpBucket().getAttributes().getBucketName();
assertNotNull(bucketName);
logger.info("Created assigned-reader bucket {}", bucketName);
try (GcsBucketAccessTester tester = new GcsBucketAccessTester(wsmapp, bucketName, projectId)) {
tester.checkAccess(wsmapp, ControlledResourceIamRole.EDITOR);
tester.checkAccess(reader, null);
tester.checkAccess(writer, ControlledResourceIamRole.READER);
}
deleteBucket(resourceApi, createdBucket);
}
use of bio.terra.workspace.model.CreatedControlledGcpGcsBucket in project terra-workspace-manager by DataBiosphere.
the class ControlledApplicationPrivateGcsBucketLifecycle method testNoAssignedUser.
private void testNoAssignedUser(ControlledGcpResourceApi resourceApi, String projectId) throws Exception {
String bucketResourceName = RandomStringUtils.random(6, true, false);
CreatedControlledGcpGcsBucket createdBucket = GcsBucketUtils.makeControlledGcsBucketAppPrivate(resourceApi, getWorkspaceId(), bucketResourceName, CloningInstructionsEnum.NOTHING, null);
bucketName = createdBucket.getGcpBucket().getAttributes().getBucketName();
assertNotNull(bucketName);
logger.info("Created no-assigned-user bucket {}", bucketName);
try (GcsBucketAccessTester tester = new GcsBucketAccessTester(wsmapp, bucketName, projectId)) {
tester.checkAccess(wsmapp, ControlledResourceIamRole.EDITOR);
tester.checkAccess(owner, null);
// Don't bother testing reader and writer here.
}
deleteBucket(resourceApi, createdBucket);
}
Aggregations