Search in sources :

Example 31 with CloudbreakUser

use of com.sequenceiq.cloudbreak.common.user.CloudbreakUser in project cloudbreak by hortonworks.

the class WorkspaceConfiguratorFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    CloudbreakUser cloudbreakUser = authenticatedUserService.getCbUser(request);
    try {
        if (cloudbreakUser != null) {
            if (ThreadBasedUserCrnProvider.getUserCrn() != null && !ThreadBasedUserCrnProvider.getUserCrn().equals(cloudbreakUser.getUserCrn())) {
                LOGGER.debug("Before:There is a difference between:: Spring security context: {} and header-based-actor: '{}'", cloudbreakUser.getUserCrn(), ThreadBasedUserCrnProvider.getUserCrn());
                logHeadersSafely(request);
            }
            User user = userService.getOrCreate(cloudbreakUser);
            String accountId = Crn.fromString(cloudbreakUser.getUserCrn()).getAccountId();
            // default workspaceName is always the accountId
            Optional<Workspace> tenantDefaultWorkspace = workspaceService.getByName(accountId, user);
            if (!tenantDefaultWorkspace.isPresent()) {
                throw new IllegalStateException("Tenant default workspace does not exist!");
            }
            if (ThreadBasedUserCrnProvider.getUserCrn() != null && !ThreadBasedUserCrnProvider.getUserCrn().equals(user.getUserCrn())) {
                LOGGER.debug("Before:There is a difference between:: CB user context: {} and header-based-actor: '{}'", user.getUserCrn(), ThreadBasedUserCrnProvider.getUserCrn());
            }
            Long workspaceId = tenantDefaultWorkspace.get().getId();
            restRequestThreadLocalService.setRequestedWorkspaceId(workspaceId);
            WorkspaceIdModifiedRequest modifiedRequest = new WorkspaceIdModifiedRequest(request, workspaceId);
            LOGGER.debug("Before:CloudbreakRestRequestThreadLocalContext: {}", restRequestThreadLocalService.getRestThreadLocalContextAsString());
            filterChain.doFilter(modifiedRequest, response);
            LOGGER.debug("After:CloudbreakRestRequestThreadLocalContext: {}", restRequestThreadLocalService.getRestThreadLocalContextAsString());
        } else {
            filterChain.doFilter(request, response);
        }
    } finally {
        restRequestThreadLocalService.removeRequestedWorkspaceId();
    }
}
Also used : CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) User(com.sequenceiq.cloudbreak.workspace.model.User) CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) Workspace(com.sequenceiq.cloudbreak.workspace.model.Workspace)

Example 32 with CloudbreakUser

use of com.sequenceiq.cloudbreak.common.user.CloudbreakUser in project cloudbreak by hortonworks.

the class DefaultClusterTemplateCache method defaultClusterTemplatesByNames.

public List<ClusterTemplate> defaultClusterTemplatesByNames(Collection<String> templateNamesMissingFromDb, Set<Blueprint> blueprints) {
    List<ClusterTemplate> defaultTemplates = new ArrayList<>();
    CloudbreakUser cloudbreakUser = restRequestThreadLocalService.getCloudbreakUser();
    User user = userService.getOrCreate(cloudbreakUser);
    Workspace workspace = workspaceService.get(restRequestThreadLocalService.getRequestedWorkspaceId(), user);
    boolean internalTenant = entitlementService.internalTenant(workspace.getTenant().getName());
    defaultClusterTemplateRequests().forEach((key, value) -> {
        if (templateNamesMissingFromDb.contains(key)) {
            String defaultTemplateJson = new String(Base64.getDecoder().decode(value));
            DefaultClusterTemplateV4Request defaultClusterTemplate = getDefaultClusterTemplate(defaultTemplateJson);
            if (internalClusterTemplateValidator.shouldPopulate(defaultClusterTemplate, internalTenant)) {
                ClusterTemplate clusterTemplate = defaultClusterTemplateV4RequestToClusterTemplateConverter.convert(defaultClusterTemplate);
                clusterTemplate.setWorkspace(workspace);
                Optional<Blueprint> blueprint = blueprints.stream().filter(e -> e.getName().equals(defaultClusterTemplate.getDistroXTemplate().getCluster().getBlueprintName())).findFirst();
                if (blueprint.isPresent()) {
                    clusterTemplate.setClouderaRuntimeVersion(blueprint.get().getStackVersion());
                }
                defaultTemplates.add(clusterTemplate);
            }
        }
    });
    return defaultTemplates;
}
Also used : Arrays(java.util.Arrays) FileReaderUtils.readFileFromClasspath(com.sequenceiq.cloudbreak.util.FileReaderUtils.readFileFromClasspath) EntitlementService(com.sequenceiq.cloudbreak.auth.altus.EntitlementService) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) JsonUtil(com.sequenceiq.cloudbreak.common.json.JsonUtil) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) CloudbreakRestRequestThreadLocalService(com.sequenceiq.cloudbreak.structuredevent.CloudbreakRestRequestThreadLocalService) InternalClusterTemplateValidator(com.sequenceiq.distrox.v1.distrox.service.InternalClusterTemplateValidator) Service(org.springframework.stereotype.Service) Map(java.util.Map) Blueprint(com.sequenceiq.cloudbreak.domain.Blueprint) CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) Logger(org.slf4j.Logger) DefaultClusterTemplateV4RequestToClusterTemplateConverter(com.sequenceiq.cloudbreak.converter.v4.clustertemplate.DefaultClusterTemplateV4RequestToClusterTemplateConverter) Collection(java.util.Collection) Set(java.util.Set) UserService(com.sequenceiq.cloudbreak.service.user.UserService) WorkspaceService(com.sequenceiq.cloudbreak.service.workspace.WorkspaceService) IOException(java.io.IOException) DefaultClusterTemplateV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.clustertemplate.requests.DefaultClusterTemplateV4Request) Collectors(java.util.stream.Collectors) File(java.io.File) Workspace(com.sequenceiq.cloudbreak.workspace.model.Workspace) Json(com.sequenceiq.cloudbreak.common.json.Json) Base64(java.util.Base64) List(java.util.List) User(com.sequenceiq.cloudbreak.workspace.model.User) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) ClusterTemplate(com.sequenceiq.cloudbreak.domain.stack.cluster.ClusterTemplate) ClusterTemplate(com.sequenceiq.cloudbreak.domain.stack.cluster.ClusterTemplate) CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) User(com.sequenceiq.cloudbreak.workspace.model.User) DefaultClusterTemplateV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.clustertemplate.requests.DefaultClusterTemplateV4Request) Blueprint(com.sequenceiq.cloudbreak.domain.Blueprint) ArrayList(java.util.ArrayList) CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) Workspace(com.sequenceiq.cloudbreak.workspace.model.Workspace)

Example 33 with CloudbreakUser

use of com.sequenceiq.cloudbreak.common.user.CloudbreakUser in project cloudbreak by hortonworks.

the class NotificationController method executeAndNotify.

protected final void executeAndNotify(Consumer<CloudbreakUser> consumer, ResourceEvent resourceEvent) {
    CloudbreakUser cloudbreakUser = restRequestThreadLocalService.getCloudbreakUser();
    consumer.accept(cloudbreakUser);
    notify(resourceEvent);
}
Also used : CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser)

Example 34 with CloudbreakUser

use of com.sequenceiq.cloudbreak.common.user.CloudbreakUser in project cloudbreak by hortonworks.

the class UtilV4Controller method postNotificationTest.

@Override
@CheckPermissionByAccount(action = AuthorizationResourceAction.POWERUSER_ONLY)
public ResourceEventResponse postNotificationTest() {
    CloudbreakUser cloudbreakUser = restRequestThreadLocalService.getCloudbreakUser();
    notificationSender.sendTestNotification(cloudbreakUser.getUserId());
    ResourceEventResponse response = new ResourceEventResponse();
    response.setEvent(ResourceEvent.CREDENTIAL_CREATED);
    return response;
}
Also used : ResourceEventResponse(com.sequenceiq.cloudbreak.api.endpoint.v4.util.responses.ResourceEventResponse) CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) CheckPermissionByAccount(com.sequenceiq.authorization.annotation.CheckPermissionByAccount)

Example 35 with CloudbreakUser

use of com.sequenceiq.cloudbreak.common.user.CloudbreakUser in project cloudbreak by hortonworks.

the class DistroXServiceTest method testWhenEnvExistsAndItIsAvailable.

@Test
@DisplayName("When the environment that has the given name is exist and also in the state AVAILABLE then no exception should come")
void testWhenEnvExistsAndItIsAvailable() throws IllegalAccessException {
    String envName = "someAwesomeEnvironment";
    DistroXV1Request r = new DistroXV1Request();
    r.setEnvironmentName(envName);
    DetailedEnvironmentResponse envResponse = new DetailedEnvironmentResponse();
    envResponse.setEnvironmentStatus(AVAILABLE);
    envResponse.setCrn("crn");
    DescribeFreeIpaResponse freeipa = new DescribeFreeIpaResponse();
    freeipa.setAvailabilityStatus(AvailabilityStatus.AVAILABLE);
    freeipa.setStatus(com.sequenceiq.freeipa.api.v1.freeipa.stack.model.common.Status.AVAILABLE);
    when(freeipaClientService.getByEnvironmentCrn("crn")).thenReturn(freeipa);
    when(environmentClientService.getByName(envName)).thenReturn(envResponse);
    StackV4Request converted = new StackV4Request();
    CloudbreakUser cloudbreakUser = mock(CloudbreakUser.class);
    when(stackRequestConverter.convert(r)).thenReturn(converted);
    when(platformAwareSdxConnector.listSdxCrns(any(), any())).thenReturn(Set.of(DATALAKE_CRN));
    when(restRequestThreadLocalService.getCloudbreakUser()).thenReturn(cloudbreakUser);
    underTest.post(r);
    verify(environmentClientService, calledOnce()).getByName(any());
    verify(environmentClientService, calledOnce()).getByName(envName);
    verify(stackOperations, calledOnce()).post(any(), any(), any(), anyBoolean());
    verify(stackOperations, calledOnce()).post(eq(USER_ID), eq(cloudbreakUser), eq(converted), eq(true));
    verify(workspaceService, calledOnce()).getForCurrentUser();
    verify(stackRequestConverter, calledOnce()).convert(any(DistroXV1Request.class));
    verify(stackRequestConverter, calledOnce()).convert(r);
}
Also used : DescribeFreeIpaResponse(com.sequenceiq.freeipa.api.v1.freeipa.stack.model.describe.DescribeFreeIpaResponse) StackV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) DistroXV1Request(com.sequenceiq.distrox.api.v1.distrox.model.DistroXV1Request) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

CloudbreakUser (com.sequenceiq.cloudbreak.common.user.CloudbreakUser)47 User (com.sequenceiq.cloudbreak.workspace.model.User)24 Test (org.junit.jupiter.api.Test)10 Workspace (com.sequenceiq.cloudbreak.workspace.model.Workspace)8 Optional (java.util.Optional)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Test (org.junit.Test)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 StackV4Request (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request)3 CrnUser (com.sequenceiq.cloudbreak.auth.CrnUser)3 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)3 Collection (java.util.Collection)3 UserManagementProto (com.cloudera.thunderhead.service.usermanagement.UserManagementProto)2 Crn (com.sequenceiq.cloudbreak.auth.crn.Crn)2 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)2 Json (com.sequenceiq.cloudbreak.common.json.Json)2 TransactionExecutionException (com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException)2 TransactionRuntimeExecutionException (com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionRuntimeExecutionException)2 Blueprint (com.sequenceiq.cloudbreak.domain.Blueprint)2 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)2