Search in sources :

Example 1 with ControlledResource

use of bio.terra.workspace.service.resource.controlled.model.ControlledResource in project terra-workspace-manager by DataBiosphere.

the class CreateAndDeleteAzureControlledResourceFlightTest method createAzureNetworkControlledResource.

@Test
public void createAzureNetworkControlledResource() throws InterruptedException {
    UUID workspaceId = azureTestUtils.createWorkspace(workspaceService);
    AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
    // Cloud context needs to be created first
    FlightState createAzureContextFlightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateAzureContextFlight.class, azureTestUtils.createAzureContextInputParameters(workspaceId, userRequest), STAIRWAY_FLIGHT_TIMEOUT, null);
    assertEquals(FlightStatus.SUCCESS, createAzureContextFlightState.getFlightStatus());
    assertTrue(workspaceService.getAuthorizedAzureCloudContext(workspaceId, userRequest).isPresent());
    final ApiAzureNetworkCreationParameters creationParams = ControlledResourceFixtures.getAzureNetworkCreationParameters();
    // TODO: make this application-private resource once the POC supports it
    final UUID resourceId = UUID.randomUUID();
    ControlledAzureNetworkResource resource = ControlledAzureNetworkResource.builder().common(ControlledResourceFields.builder().workspaceId(workspaceId).resourceId(resourceId).name("testNetwork").description("testDesc").cloningInstructions(CloningInstructions.COPY_RESOURCE).accessScope(AccessScopeType.fromApi(ApiAccessScope.SHARED_ACCESS)).managedBy(ManagedByType.fromApi(ApiManagedBy.USER)).build()).networkName(creationParams.getName()).region(creationParams.getRegion()).subnetName(creationParams.getSubnetName()).addressSpaceCidr(creationParams.getAddressSpaceCidr()).subnetAddressCidr(creationParams.getSubnetAddressCidr()).build();
    // Submit a Network creation flight.
    FlightState flightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateControlledResourceFlight.class, azureTestUtils.createControlledResourceInputParameters(workspaceId, userRequest, resource), STAIRWAY_FLIGHT_TIMEOUT, null);
    assertEquals(FlightStatus.SUCCESS, flightState.getFlightStatus());
    // Verify controlled resource exists in the workspace.
    ControlledResource res = controlledResourceService.getControlledResource(workspaceId, resourceId, userRequest);
    try {
        ControlledAzureNetworkResource azureNetworkResource = res.castByEnum(WsmResourceType.CONTROLLED_AZURE_NETWORK);
        assertEquals(resource, azureNetworkResource);
    } catch (Exception e) {
        fail("Failed to cast resource to ControlledAzureNetworkResource", e);
    }
}
Also used : FlightState(bio.terra.stairway.FlightState) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) UUID(java.util.UUID) ApiAzureNetworkCreationParameters(bio.terra.workspace.generated.model.ApiAzureNetworkCreationParameters) ControlledAzureNetworkResource(bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource) ManagementException(com.azure.core.management.exception.ManagementException) Test(org.junit.jupiter.api.Test) BaseAzureTest(bio.terra.workspace.common.BaseAzureTest)

Example 2 with ControlledResource

use of bio.terra.workspace.service.resource.controlled.model.ControlledResource in project terra-workspace-manager by DataBiosphere.

the class CreateAndDeleteAzureControlledResourceFlightTest method createAndDeleteAzureVmControlledResource.

@Test
public void createAndDeleteAzureVmControlledResource() throws InterruptedException {
    // Setup workspace and cloud context
    UUID workspaceId = azureTestUtils.createWorkspace(workspaceService);
    AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
    // Cloud context needs to be created first
    FlightState createAzureContextFlightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateAzureContextFlight.class, azureTestUtils.createAzureContextInputParameters(workspaceId, userRequest), STAIRWAY_FLIGHT_TIMEOUT, null);
    assertEquals(FlightStatus.SUCCESS, createAzureContextFlightState.getFlightStatus());
    assertTrue(workspaceService.getAuthorizedAzureCloudContext(workspaceId, userRequest).isPresent());
    // Create ip
    ControlledAzureIpResource ipResource = createIp(workspaceId, userRequest);
    // Create disk
    ControlledAzureDiskResource diskResource = createDisk(workspaceId, userRequest);
    // Create network
    ControlledAzureNetworkResource networkResource = createNetwork(workspaceId, userRequest);
    final ApiAzureVmCreationParameters creationParameters = ControlledResourceFixtures.getAzureVmCreationParameters();
    // TODO: make this application-private resource once the POC supports it
    final UUID resourceId = UUID.randomUUID();
    ControlledAzureVmResource resource = ControlledAzureVmResource.builder().common(ControlledResourceFields.builder().workspaceId(workspaceId).resourceId(resourceId).name(getAzureName("vm")).description(getAzureName("vm-desc")).cloningInstructions(CloningInstructions.COPY_RESOURCE).accessScope(AccessScopeType.fromApi(ApiAccessScope.SHARED_ACCESS)).managedBy(ManagedByType.fromApi(ApiManagedBy.USER)).build()).vmName(creationParameters.getName()).vmSize(creationParameters.getVmSize()).vmImageUri(creationParameters.getVmImageUri()).region(creationParameters.getRegion()).ipId(ipResource.getResourceId()).diskId(diskResource.getResourceId()).networkId(networkResource.getResourceId()).build();
    // Submit a VM creation flight.
    FlightState flightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateControlledResourceFlight.class, azureTestUtils.createControlledResourceInputParameters(workspaceId, userRequest, resource), STAIRWAY_FLIGHT_TIMEOUT, null);
    assertEquals(FlightStatus.SUCCESS, flightState.getFlightStatus());
    // Verify controlled resource exists in the workspace.
    ControlledResource res = controlledResourceService.getControlledResource(workspaceId, resourceId, userRequest);
    try {
        ControlledAzureVmResource azureVmResource = res.castByEnum(WsmResourceType.CONTROLLED_AZURE_VM);
        assertEquals(resource, azureVmResource);
    } catch (Exception e) {
        fail("Failed to cast resource to ControlledAzureVmResource", e);
    }
    // Exercise resource enumeration for the underlying resources.
    // Verify that the resources we created are in the enumeration.
    List<WsmResource> resourceList = wsmResourceService.enumerateResources(workspaceId, null, null, 0, 100, userRequest);
    checkForResource(resourceList, ipResource);
    checkForResource(resourceList, diskResource);
    checkForResource(resourceList, networkResource);
    checkForResource(resourceList, resource);
    ComputeManager computeManager = azureTestUtils.getComputeManager();
    VirtualMachine vmTemp = null;
    var retries = 20;
    while (vmTemp == null) {
        try {
            retries = retries - 1;
            if (retries >= 0) {
                vmTemp = computeManager.virtualMachines().getByResourceGroup(azureTestUtils.getAzureCloudContext().getAzureResourceGroupId(), creationParameters.getName());
            } else
                throw new RuntimeException(String.format("%s is not created in time in Azure", creationParameters.getName()));
        } catch (com.azure.core.exception.HttpResponseException ex) {
            if (ex.getResponse().getStatusCode() == 404)
                Thread.sleep(10000);
            else
                throw ex;
        }
    }
    final VirtualMachine resolvedVm = vmTemp;
    // Submit a VM deletion flight.
    FlightState deleteFlightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), DeleteControlledResourceFlight.class, azureTestUtils.deleteControlledResourceInputParameters(workspaceId, resourceId, userRequest, resource), STAIRWAY_FLIGHT_TIMEOUT, null);
    assertEquals(FlightStatus.SUCCESS, deleteFlightState.getFlightStatus());
    Thread.sleep(10000);
    resolvedVm.networkInterfaceIds().forEach(nic -> assertThrows(com.azure.core.exception.HttpResponseException.class, () -> computeManager.networkManager().networks().getById(nic)));
    assertThrows(com.azure.core.exception.HttpResponseException.class, () -> computeManager.disks().getById(resolvedVm.osDiskId()));
}
Also used : ControlledAzureIpResource(bio.terra.workspace.service.resource.controlled.cloud.azure.ip.ControlledAzureIpResource) WsmResource(bio.terra.workspace.service.resource.model.WsmResource) ControlledAzureVmResource(bio.terra.workspace.service.resource.controlled.cloud.azure.vm.ControlledAzureVmResource) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) ApiAzureVmCreationParameters(bio.terra.workspace.generated.model.ApiAzureVmCreationParameters) ManagementException(com.azure.core.management.exception.ManagementException) ComputeManager(com.azure.resourcemanager.compute.ComputeManager) FlightState(bio.terra.stairway.FlightState) ControlledAzureDiskResource(bio.terra.workspace.service.resource.controlled.cloud.azure.disk.ControlledAzureDiskResource) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) UUID(java.util.UUID) ControlledAzureNetworkResource(bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.jupiter.api.Test) BaseAzureTest(bio.terra.workspace.common.BaseAzureTest)

Example 3 with ControlledResource

use of bio.terra.workspace.service.resource.controlled.model.ControlledResource in project terra-workspace-manager by DataBiosphere.

the class SamServiceTest method workspaceReaderIsSharedResourceReader.

@Test
void workspaceReaderIsSharedResourceReader() throws Exception {
    // Default user is workspace owner, secondary user is workspace reader
    samService.grantWorkspaceRole(workspaceId, defaultUserRequest(), WsmIamRole.READER, userAccessUtils.getSecondUserEmail());
    ControlledResource bucketResource = ControlledResourceFixtures.makeDefaultControlledGcsBucketBuilder(workspaceId).build();
    samService.createControlledResource(bucketResource, null, null, defaultUserRequest());
    // Workspace reader should have read access on a user-shared resource via inheritance
    assertTrue(samService.isAuthorized(secondaryUserRequest(), ControlledResourceCategory.USER_SHARED.getSamResourceName(), bucketResource.getResourceId().toString(), SamWorkspaceAction.READ));
    samService.deleteControlledResource(bucketResource, defaultUserRequest());
}
Also used : ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) BaseConnectedTest(bio.terra.workspace.common.BaseConnectedTest) Test(org.junit.jupiter.api.Test)

Example 4 with ControlledResource

use of bio.terra.workspace.service.resource.controlled.model.ControlledResource in project terra-workspace-manager by DataBiosphere.

the class SamServiceTest method workspaceReaderIsNotPrivateResourceReader.

@Test
void workspaceReaderIsNotPrivateResourceReader() throws Exception {
    // Default user is workspace owner, secondary user is workspace reader
    samService.grantWorkspaceRole(workspaceId, defaultUserRequest(), WsmIamRole.READER, userAccessUtils.getSecondUserEmail());
    // Create private resource assigned to the default user.
    ControlledResourceFields commonFields = ControlledResourceFixtures.makeDefaultControlledResourceFieldsBuilder().workspaceId(workspaceId).accessScope(AccessScopeType.ACCESS_SCOPE_PRIVATE).assignedUser(userAccessUtils.getDefaultUserEmail()).privateResourceState(PrivateResourceState.ACTIVE).build();
    ControlledResource bucketResource = ControlledGcsBucketResource.builder().bucketName(ControlledResourceFixtures.uniqueBucketName()).common(commonFields).build();
    samService.createControlledResource(bucketResource, ControlledResourceIamRole.EDITOR, userAccessUtils.getDefaultUserEmail(), defaultUserRequest());
    // Workspace reader should not have read access on a private resource.
    assertFalse(samService.isAuthorized(secondaryUserRequest(), ControlledResourceCategory.USER_PRIVATE.getSamResourceName(), bucketResource.getResourceId().toString(), SamConstants.SamWorkspaceAction.READ));
    // However, the assigned user should have read access.
    assertTrue(samService.isAuthorized(defaultUserRequest(), ControlledResourceCategory.USER_PRIVATE.getSamResourceName(), bucketResource.getResourceId().toString(), SamConstants.SamWorkspaceAction.READ));
    samService.deleteControlledResource(bucketResource, defaultUserRequest());
}
Also used : ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) ControlledResourceFields(bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields) BaseConnectedTest(bio.terra.workspace.common.BaseConnectedTest) Test(org.junit.jupiter.api.Test)

Example 5 with ControlledResource

use of bio.terra.workspace.service.resource.controlled.model.ControlledResource in project terra-workspace-manager by DataBiosphere.

the class ResourceDaoTest method listAndDeleteControlledResourceInContext.

@Test
public void listAndDeleteControlledResourceInContext() {
    UUID workspaceId = createGcpWorkspace();
    ControlledGcsBucketResource bucket = ControlledResourceFixtures.makeDefaultControlledGcsBucketBuilder(workspaceId).build();
    ControlledBigQueryDatasetResource dataset = ControlledResourceFixtures.makeDefaultControlledBigQueryBuilder(workspaceId).build();
    resourceDao.createControlledResource(bucket);
    resourceDao.createControlledResource(dataset);
    List<ControlledResource> gcpList = resourceDao.listControlledResources(workspaceId, CloudPlatform.GCP);
    List<ControlledResource> azureList = resourceDao.listControlledResources(workspaceId, CloudPlatform.AZURE);
    List<ControlledResource> allCloudList = resourceDao.listControlledResources(workspaceId, null);
    assertTrue(azureList.isEmpty());
    assertThat(gcpList, containsInAnyOrder(bucket, dataset));
    assertThat(allCloudList, containsInAnyOrder(bucket, dataset));
    assertTrue(resourceDao.deleteAllControlledResources(workspaceId, CloudPlatform.GCP));
    assertFalse(resourceDao.deleteAllControlledResources(workspaceId, CloudPlatform.AZURE));
    List<ControlledResource> listAfterDeletion = resourceDao.listControlledResources(workspaceId, CloudPlatform.GCP);
    assertTrue(listAfterDeletion.isEmpty());
}
Also used : ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) UUID(java.util.UUID) ControlledBigQueryDatasetResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.ControlledBigQueryDatasetResource) ControlledGcsBucketResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource) Test(org.junit.jupiter.api.Test) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest)

Aggregations

ControlledResource (bio.terra.workspace.service.resource.controlled.model.ControlledResource)27 Test (org.junit.jupiter.api.Test)13 UUID (java.util.UUID)9 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)8 BaseConnectedTest (bio.terra.workspace.common.BaseConnectedTest)6 FlightMap (bio.terra.stairway.FlightMap)5 FlightState (bio.terra.stairway.FlightState)5 BaseAzureTest (bio.terra.workspace.common.BaseAzureTest)5 ManagementException (com.azure.core.management.exception.ManagementException)5 ControlledGcsBucketResource (bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource)4 ControlledResourceFields (bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields)4 WsmResource (bio.terra.workspace.service.resource.model.WsmResource)3 BaseUnitTest (bio.terra.workspace.common.BaseUnitTest)2 ApiGcpGcsBucketCreationParameters (bio.terra.workspace.generated.model.ApiGcpGcsBucketCreationParameters)2 ControlledResourceIamRole (bio.terra.workspace.service.iam.model.ControlledResourceIamRole)2 JobBuilder (bio.terra.workspace.service.job.JobBuilder)2 ControlledAzureDiskResource (bio.terra.workspace.service.resource.controlled.cloud.azure.disk.ControlledAzureDiskResource)2 ControlledAzureIpResource (bio.terra.workspace.service.resource.controlled.cloud.azure.ip.ControlledAzureIpResource)2 ControlledAzureNetworkResource (bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource)2 ControlledBigQueryDatasetResource (bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.ControlledBigQueryDatasetResource)2