Search in sources :

Example 6 with ApiException

use of org.broadinstitute.dsde.workbench.client.sam.ApiException in project terra-workspace-manager by DataBiosphere.

the class SamService method listWorkspaceIds.

/**
 * List all workspace IDs in Sam this user has access to. Note that in environments shared with
 * Rawls, some of these workspaces will be Rawls managed and WSM will not know about them.
 */
@Traced
public List<UUID> listWorkspaceIds(AuthenticatedUserRequest userRequest) throws InterruptedException {
    ResourcesApi resourceApi = samResourcesApi(userRequest.getRequiredToken());
    List<UUID> workspaceIds = new ArrayList<>();
    try {
        List<ResourceAndAccessPolicy> resourceAndPolicies = SamRetry.retry(() -> resourceApi.listResourcesAndPolicies(SamConstants.SamResource.WORKSPACE));
        for (var resourceAndPolicy : resourceAndPolicies) {
            try {
                workspaceIds.add(UUID.fromString(resourceAndPolicy.getResourceId()));
            } catch (IllegalArgumentException e) {
                // ignored here.
                continue;
            }
        }
    } catch (ApiException apiException) {
        throw SamExceptionFactory.create("Error listing Workspace Ids in Sam", apiException);
    }
    return workspaceIds;
}
Also used : ArrayList(java.util.ArrayList) ResourcesApi(org.broadinstitute.dsde.workbench.client.sam.api.ResourcesApi) ResourceAndAccessPolicy(org.broadinstitute.dsde.workbench.client.sam.model.ResourceAndAccessPolicy) UUID(java.util.UUID) ApiException(org.broadinstitute.dsde.workbench.client.sam.ApiException) Traced(io.opencensus.contrib.spring.aop.Traced)

Example 7 with ApiException

use of org.broadinstitute.dsde.workbench.client.sam.ApiException in project terra-workspace-manager by DataBiosphere.

the class SamService method createControlledResource.

/**
 * Create a controlled resource in Sam.
 *
 * @param resource The WSM representation of the resource to create.
 * @param privateIamRole The IAM role to grant on a private resource. It is required for
 *     user-private resources and optional for application-private resources.
 * @param assignedUserEmail Email identifier of the assigned user of this resource. Same
 *     constraints as privateIamRoles.
 * @param userRequest Credentials to use for talking to Sam.
 */
@Traced
public void createControlledResource(ControlledResource resource, @Nullable ControlledResourceIamRole privateIamRole, @Nullable String assignedUserEmail, AuthenticatedUserRequest userRequest) throws InterruptedException {
    // We need the WSM SA for setting controlled resource policies
    initializeWsmServiceAccount();
    FullyQualifiedResourceId workspaceParentFqId = new FullyQualifiedResourceId().resourceId(resource.getWorkspaceId().toString()).resourceTypeName(SamConstants.SamResource.WORKSPACE);
    CreateResourceRequestV2 resourceRequest = new CreateResourceRequestV2().resourceId(resource.getResourceId().toString()).parent(workspaceParentFqId);
    var builder = new ControlledResourceSamPolicyBuilder(this, privateIamRole, assignedUserEmail, userRequest, ControlledResourceCategory.get(resource.getAccessScope(), resource.getManagedBy()));
    builder.addPolicies(resourceRequest);
    try {
        // We use the user request for the create, but could equally well use the WSM SA.
        // The creating token has no effect on the resource policies.
        ResourcesApi resourceApi = samResourcesApi(userRequest.getRequiredToken());
        SamRetry.retry(() -> resourceApi.createResourceV2(resource.getCategory().getSamResourceName(), resourceRequest));
        logger.info("Created Sam controlled resource {}", resource.getResourceId());
        dumpRoleBindings(resource.getCategory().getSamResourceName(), resource.getResourceId().toString(), getWsmServiceAccountToken());
    } catch (ApiException apiException) {
        // Do nothing if the resource to create already exists, this may not be the first time do is
        // called. Other exceptions still need to be surfaced.
        // Resource IDs are randomly generated, so we trust that the caller must have created
        // an existing Sam resource.
        logger.info("Sam API error while creating a controlled resource, code is " + apiException.getCode());
        if (apiException.getCode() == HttpStatus.CONFLICT.value()) {
            logger.info("Sam error was CONFLICT on creation request. This means the resource already " + "exists but is not an error so no exception thrown.");
            return;
        }
        throw SamExceptionFactory.create("Error creating controlled resource in Sam", apiException);
    }
}
Also used : FullyQualifiedResourceId(org.broadinstitute.dsde.workbench.client.sam.model.FullyQualifiedResourceId) ResourcesApi(org.broadinstitute.dsde.workbench.client.sam.api.ResourcesApi) CreateResourceRequestV2(org.broadinstitute.dsde.workbench.client.sam.model.CreateResourceRequestV2) ApiException(org.broadinstitute.dsde.workbench.client.sam.ApiException) Traced(io.opencensus.contrib.spring.aop.Traced)

Example 8 with ApiException

use of org.broadinstitute.dsde.workbench.client.sam.ApiException in project terra-workspace-manager by DataBiosphere.

the class SamService method dumpRoleBindings.

// Add code to retrieve and dump the role assignments for WSM controlled resources
// for debugging. No permission check outside of Sam.
public void dumpRoleBindings(String samResourceType, String resourceId, String token) {
    logger.debug("DUMP ROLE BINDING - resourceType {} resourceId {}", samResourceType, resourceId);
    ResourcesApi resourceApi = samResourcesApi(token);
    try {
        List<AccessPolicyResponseEntryV2> samResult = SamRetry.retry(() -> resourceApi.listResourcePoliciesV2(samResourceType, resourceId));
        for (AccessPolicyResponseEntryV2 entry : samResult) {
            logger.debug("  samPolicy: {}", entry);
        }
    } catch (ApiException apiException) {
        throw SamExceptionFactory.create("Error listing role bindings in Sam", apiException);
    } catch (InterruptedException e) {
        logger.warn("dump role binding was interrupted");
    }
}
Also used : ResourcesApi(org.broadinstitute.dsde.workbench.client.sam.api.ResourcesApi) AccessPolicyResponseEntryV2(org.broadinstitute.dsde.workbench.client.sam.model.AccessPolicyResponseEntryV2) ApiException(org.broadinstitute.dsde.workbench.client.sam.ApiException)

Example 9 with ApiException

use of org.broadinstitute.dsde.workbench.client.sam.ApiException in project terra-workspace-manager by DataBiosphere.

the class SamService method deleteWorkspace.

@Traced
public void deleteWorkspace(AuthenticatedUserRequest userRequest, UUID id) throws InterruptedException {
    String authToken = userRequest.getRequiredToken();
    ResourcesApi resourceApi = samResourcesApi(authToken);
    try {
        SamRetry.retry(() -> resourceApi.deleteResource(SamConstants.SamResource.WORKSPACE, id.toString()));
        logger.info("Deleted Sam resource for workspace {}", id);
    } catch (ApiException apiException) {
        logger.info("Sam API error while deleting workspace, code is " + apiException.getCode());
        // called. Other exceptions still need to be surfaced.
        if (apiException.getCode() == HttpStatus.NOT_FOUND.value()) {
            logger.info("Sam error was NOT_FOUND on a deletion call. " + "This just means the deletion was tried twice so no error thrown.");
            return;
        }
        throw SamExceptionFactory.create("Error deleting a workspace in Sam", apiException);
    }
}
Also used : ResourcesApi(org.broadinstitute.dsde.workbench.client.sam.api.ResourcesApi) ApiException(org.broadinstitute.dsde.workbench.client.sam.ApiException) Traced(io.opencensus.contrib.spring.aop.Traced)

Example 10 with ApiException

use of org.broadinstitute.dsde.workbench.client.sam.ApiException in project terra-workspace-manager by DataBiosphere.

the class SamService method isAuthorized.

@Traced
public boolean isAuthorized(AuthenticatedUserRequest userRequest, String iamResourceType, String resourceId, String action) throws InterruptedException {
    String accessToken = userRequest.getRequiredToken();
    ResourcesApi resourceApi = samResourcesApi(accessToken);
    try {
        return SamRetry.retry(() -> resourceApi.resourcePermissionV2(iamResourceType, resourceId, action));
    } catch (ApiException apiException) {
        throw SamExceptionFactory.create("Error checking resource permission in Sam", apiException);
    }
}
Also used : ResourcesApi(org.broadinstitute.dsde.workbench.client.sam.api.ResourcesApi) ApiException(org.broadinstitute.dsde.workbench.client.sam.ApiException) Traced(io.opencensus.contrib.spring.aop.Traced)

Aggregations

ApiException (org.broadinstitute.dsde.workbench.client.sam.ApiException)20 ResourcesApi (org.broadinstitute.dsde.workbench.client.sam.api.ResourcesApi)15 Traced (io.opencensus.contrib.spring.aop.Traced)12 ArrayList (java.util.ArrayList)5 UUID (java.util.UUID)5 GoogleApi (org.broadinstitute.dsde.workbench.client.sam.api.GoogleApi)5 UsersApi (org.broadinstitute.dsde.workbench.client.sam.api.UsersApi)5 ResourceAndAccessPolicy (org.broadinstitute.dsde.workbench.client.sam.model.ResourceAndAccessPolicy)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 ApiClient (org.broadinstitute.dsde.workbench.client.sam.ApiClient)4 AccessPolicyResponseEntry (org.broadinstitute.dsde.workbench.client.sam.model.AccessPolicyResponseEntry)4 CreateResourceRequestV2 (org.broadinstitute.dsde.workbench.client.sam.model.CreateResourceRequestV2)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Component (org.springframework.stereotype.Component)4 InternalServerErrorException (bio.terra.common.exception.InternalServerErrorException)3