Search in sources :

Example 16 with Traced

use of io.opencensus.contrib.spring.aop.Traced in project terra-workspace-manager by DataBiosphere.

the class WorkspaceService method validateWorkspaceAndAction.

/**
 * Convenience function that checks existence of a workspace, followed by an authorization check
 * against that workspace.
 *
 * <p>Throws WorkspaceNotFoundException from getWorkspace if the workspace does not exist,
 * regardless of the user's permission.
 *
 * <p>Throws ForbiddenException if the user is not permitted to perform the specified action on
 * the workspace in question.
 *
 * <p>Returns the Workspace object if it exists and the user is permitted to perform the specified
 * action.
 *
 * @param userRequest the user's authenticated request
 * @param workspaceId id of the workspace in question
 * @param action the action to authorize against the workspace
 * @return the workspace, if it exists and the user is permitted to perform the specified action.
 */
@Traced
public Workspace validateWorkspaceAndAction(AuthenticatedUserRequest userRequest, UUID workspaceId, String action) {
    logger.info("validateWorkspaceAndAction - userRequest: {}\nworkspaceId: {}\naction: {}", userRequest, workspaceId, action);
    Workspace workspace = workspaceDao.getWorkspace(workspaceId);
    SamRethrow.onInterrupted(() -> samService.checkAuthz(userRequest, SamConstants.SamResource.WORKSPACE, workspaceId.toString(), action), "checkAuthz");
    return workspace;
}
Also used : Workspace(bio.terra.workspace.service.workspace.model.Workspace) Traced(io.opencensus.contrib.spring.aop.Traced)

Example 17 with Traced

use of io.opencensus.contrib.spring.aop.Traced in project terra-workspace-manager by DataBiosphere.

the class WorkspaceService method createWorkspace.

/**
 * Create a workspace with the specified parameters. Returns workspaceID of the new workspace.
 */
@Traced
public UUID createWorkspace(Workspace workspace, AuthenticatedUserRequest userRequest) {
    String workspaceName = workspace.getDisplayName().orElse("");
    String workspaceId = workspace.getWorkspaceId().toString();
    String jobDescription = String.format("Create workspace: name: '%s' id: '%s'  ", workspaceName, workspaceId);
    JobBuilder createJob = jobService.newJob().description(jobDescription).flightClass(WorkspaceCreateFlight.class).request(workspace).userRequest(userRequest).workspaceId(workspaceId).operationType(OperationType.CREATE).addParameter(WorkspaceFlightMapKeys.WORKSPACE_STAGE, workspace.getWorkspaceStage().name()).addParameter(WorkspaceFlightMapKeys.DISPLAY_NAME, workspaceName).addParameter(WorkspaceFlightMapKeys.DESCRIPTION, workspace.getDescription().orElse(""));
    if (workspace.getSpendProfileId().isPresent()) {
        createJob.addParameter(WorkspaceFlightMapKeys.SPEND_PROFILE_ID, workspace.getSpendProfileId().get().getId());
    }
    // Skip the access check, which would fail since this workspace doesn't exist yet.
    return createJob.submitAndWait(UUID.class, false);
}
Also used : JobBuilder(bio.terra.workspace.service.job.JobBuilder) WorkspaceCreateFlight(bio.terra.workspace.service.workspace.flight.WorkspaceCreateFlight) Traced(io.opencensus.contrib.spring.aop.Traced)

Example 18 with Traced

use of io.opencensus.contrib.spring.aop.Traced in project terra-workspace-manager by DataBiosphere.

the class SamService method createWorkspaceWithDefaults.

/**
 * Wrapper around the Sam client to create a workspace resource in Sam.
 *
 * <p>This creates a workspace with the provided ID and requesting user as the sole Owner. Empty
 * reader and writer policies are also created. Errors from the Sam client will be thrown as Sam
 * specific exception types.
 */
@Traced
public void createWorkspaceWithDefaults(AuthenticatedUserRequest userRequest, UUID id) throws InterruptedException {
    ResourcesApi resourceApi = samResourcesApi(userRequest.getRequiredToken());
    // Sam will throw an error if no owner is specified, so the caller's email is required. It can
    // be looked up using the auth token if that's all the caller provides.
    // If we called WSM as the pet SA and went through the proxy, this becomes the pet SA's email if
    // we use the request email. That caused an issue where the human user wasn't recognized on the
    // workspace.
    String humanUserEmail = getUserEmailFromSam(userRequest);
    CreateResourceRequestV2 workspaceRequest = new CreateResourceRequestV2().resourceId(id.toString()).policies(defaultWorkspacePolicies(humanUserEmail));
    try {
        SamRetry.retry(() -> resourceApi.createResourceV2(SamConstants.SamResource.WORKSPACE, workspaceRequest));
        logger.info("Created Sam resource for workspace {}", id);
    } catch (ApiException apiException) {
        throw SamExceptionFactory.create("Error creating a Workspace resource in Sam", apiException);
    }
    dumpRoleBindings(SamConstants.SamResource.WORKSPACE, id.toString(), userRequest.getRequiredToken());
}
Also used : 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 19 with Traced

use of io.opencensus.contrib.spring.aop.Traced in project terra-workspace-manager by DataBiosphere.

the class SamService method listRoleBindings.

/**
 * Wrapper around Sam client to retrieve the full current permissions model of a workspace.
 *
 * <p>This operation is only available to MC_WORKSPACE stage workspaces, as Rawls manages
 * permissions directly on other workspaces.
 */
@Traced
public List<RoleBinding> listRoleBindings(UUID workspaceId, AuthenticatedUserRequest userRequest) throws InterruptedException {
    stageService.assertMcWorkspace(workspaceId, "listRoleBindings");
    checkAuthz(userRequest, SamConstants.SamResource.WORKSPACE, workspaceId.toString(), SamWorkspaceAction.READ_IAM);
    ResourcesApi resourceApi = samResourcesApi(userRequest.getRequiredToken());
    try {
        List<AccessPolicyResponseEntry> samResult = SamRetry.retry(() -> resourceApi.listResourcePolicies(SamConstants.SamResource.WORKSPACE, workspaceId.toString()));
        // callers.
        return samResult.stream().filter(entry -> !entry.getPolicyName().equals(WsmIamRole.MANAGER.toSamRole())).map(entry -> RoleBinding.builder().role(WsmIamRole.fromSam(entry.getPolicyName())).users(entry.getPolicy().getMemberEmails()).build()).collect(Collectors.toList());
    } catch (ApiException apiException) {
        throw SamExceptionFactory.create("Error listing role bindings in Sam", apiException);
    }
}
Also used : RoleBinding(bio.terra.workspace.service.iam.model.RoleBinding) CreateResourceRequestV2(org.broadinstitute.dsde.workbench.client.sam.model.CreateResourceRequestV2) WsmIamRole(bio.terra.workspace.service.iam.model.WsmIamRole) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) StatusApi(org.broadinstitute.dsde.workbench.client.sam.api.StatusApi) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) InternalServerErrorException(bio.terra.common.exception.InternalServerErrorException) SamRetry(bio.terra.common.sam.SamRetry) Map(java.util.Map) GoogleApi(org.broadinstitute.dsde.workbench.client.sam.api.GoogleApi) AccessPolicyResponseEntryV2(org.broadinstitute.dsde.workbench.client.sam.model.AccessPolicyResponseEntryV2) ImmutableSet(com.google.common.collect.ImmutableSet) ServiceAccountName(bio.terra.cloudres.google.iam.ServiceAccountName) Set(java.util.Set) FullyQualifiedResourceId(org.broadinstitute.dsde.workbench.client.sam.model.FullyQualifiedResourceId) UUID(java.util.UUID) SamWorkspaceAction(bio.terra.workspace.service.iam.model.SamConstants.SamWorkspaceAction) Collectors(java.util.stream.Collectors) ControlledResourceCategory(bio.terra.workspace.service.resource.controlled.model.ControlledResourceCategory) SamExceptionFactory(bio.terra.common.sam.exception.SamExceptionFactory) List(java.util.List) ControlledResourceIamRole(bio.terra.workspace.service.iam.model.ControlledResourceIamRole) Optional(java.util.Optional) SystemStatus(org.broadinstitute.dsde.workbench.client.sam.model.SystemStatus) SamConfiguration(bio.terra.workspace.app.configuration.external.SamConfiguration) HashMap(java.util.HashMap) ApiException(org.broadinstitute.dsde.workbench.client.sam.ApiException) GcpUtils(bio.terra.workspace.common.utils.GcpUtils) ArrayList(java.util.ArrayList) SamConstants(bio.terra.workspace.service.iam.model.SamConstants) ImmutableList(com.google.common.collect.ImmutableList) AccessPolicyMembershipV2(org.broadinstitute.dsde.workbench.client.sam.model.AccessPolicyMembershipV2) InternalLogicException(bio.terra.workspace.common.exception.InternalLogicException) Traced(io.opencensus.contrib.spring.aop.Traced) ResourcesApi(org.broadinstitute.dsde.workbench.client.sam.api.ResourcesApi) Nullable(javax.annotation.Nullable) AccessPolicyResponseEntry(org.broadinstitute.dsde.workbench.client.sam.model.AccessPolicyResponseEntry) Logger(org.slf4j.Logger) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) ApiClient(org.broadinstitute.dsde.workbench.client.sam.ApiClient) IOException(java.io.IOException) ResourceAndAccessPolicy(org.broadinstitute.dsde.workbench.client.sam.model.ResourceAndAccessPolicy) ForbiddenException(bio.terra.common.exception.ForbiddenException) HttpStatus(org.springframework.http.HttpStatus) Component(org.springframework.stereotype.Component) OkHttpClient(okhttp3.OkHttpClient) UsersApi(org.broadinstitute.dsde.workbench.client.sam.api.UsersApi) VisibleForTesting(com.google.common.annotations.VisibleForTesting) StageService(bio.terra.workspace.service.stage.StageService) AccessPolicyResponseEntry(org.broadinstitute.dsde.workbench.client.sam.model.AccessPolicyResponseEntry) 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 20 with Traced

use of io.opencensus.contrib.spring.aop.Traced in project terra-workspace-manager by DataBiosphere.

the class SamService method grantWorkspaceRole.

/**
 * Wrapper around Sam client to grant a role to the provided user.
 *
 * <p>This operation is only available to MC_WORKSPACE stage workspaces, as Rawls manages
 * permissions directly on other workspaces.
 *
 * @param workspaceId The workspace this operation takes place in
 * @param userRequest Credentials of the user requesting this operation. Only owners have
 *     permission to modify roles in a workspace.
 * @param role The role being granted.
 * @param email The user being granted a role.
 */
@Traced
public void grantWorkspaceRole(UUID workspaceId, AuthenticatedUserRequest userRequest, WsmIamRole role, String email) throws InterruptedException {
    stageService.assertMcWorkspace(workspaceId, "grantWorkspaceRole");
    checkAuthz(userRequest, SamConstants.SamResource.WORKSPACE, workspaceId.toString(), samActionToModifyRole(role));
    ResourcesApi resourceApi = samResourcesApi(userRequest.getRequiredToken());
    try {
        // GCP always uses lowercase email identifiers, so we do the same here for consistency.
        SamRetry.retry(() -> resourceApi.addUserToPolicy(SamConstants.SamResource.WORKSPACE, workspaceId.toString(), role.toSamRole(), email.toLowerCase()));
        logger.info("Granted role {} to user {} in workspace {}", role.toSamRole(), email, workspaceId);
    } catch (ApiException apiException) {
        throw SamExceptionFactory.create("Error granting workspace role 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

Traced (io.opencensus.contrib.spring.aop.Traced)23 ApiException (org.broadinstitute.dsde.workbench.client.sam.ApiException)11 ResourcesApi (org.broadinstitute.dsde.workbench.client.sam.api.ResourcesApi)11 Workspace (bio.terra.workspace.service.workspace.model.Workspace)5 JobBuilder (bio.terra.workspace.service.job.JobBuilder)3 CreateResourceRequestV2 (org.broadinstitute.dsde.workbench.client.sam.model.CreateResourceRequestV2)3 ForbiddenException (bio.terra.common.exception.ForbiddenException)2 ControlledResource (bio.terra.workspace.service.resource.controlled.model.ControlledResource)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 BufferApi (bio.terra.buffer.api.BufferApi)1 ApiException (bio.terra.buffer.client.ApiException)1 PoolInfo (bio.terra.buffer.model.PoolInfo)1 ServiceAccountName (bio.terra.cloudres.google.iam.ServiceAccountName)1 InternalServerErrorException (bio.terra.common.exception.InternalServerErrorException)1 SamRetry (bio.terra.common.sam.SamRetry)1 SamExceptionFactory (bio.terra.common.sam.exception.SamExceptionFactory)1 RepositoryApi (bio.terra.datarepo.api.RepositoryApi)1 ApiException (bio.terra.datarepo.client.ApiException)1