Search in sources :

Example 1 with MachineUser

use of com.cloudera.thunderhead.service.usermanagement.UserManagementProto.MachineUser in project cloudbreak by hortonworks.

the class GrpcUmsClient method createMachineUser.

/**
 * Creates new machine user, it queries against the machine user if it has already exist
 *
 * @param machineUserName new machine user name
 * @param userCrn         the CRN of the user
 * @param requestId       an optional request Id
 * @return the machine user crn
 */
@Retryable(value = UmsOperationException.class, maxAttempts = 10, backoff = @Backoff(delay = 5000))
public Optional<String> createMachineUser(String machineUserName, String userCrn, String accountId, Optional<String> requestId) {
    try {
        UmsClient client = makeClient(channelWrapper.getChannel());
        String generatedRequestId = RequestIdUtil.getOrGenerate(requestId);
        LOGGER.debug("Creating machine user {} for {} using request ID {}", machineUserName, userCrn, generatedRequestId);
        Optional<String> machineUserCrn = client.createMachineUser(generatedRequestId, userCrn, accountId, machineUserName);
        if (machineUserCrn.isEmpty()) {
            MachineUser machineUser = client.getMachineUserForUser(RequestIdUtil.getOrGenerate(requestId), userCrn, accountId, machineUserName, true, true);
            machineUserCrn = Optional.of(machineUser.getCrn());
        }
        LOGGER.debug("Machine User information retrieved for userCrn: {}", machineUserCrn.orElse(null));
        return machineUserCrn;
    } catch (StatusRuntimeException ex) {
        if (Status.NOT_FOUND.getCode().equals(ex.getStatus().getCode())) {
            String errMessage = String.format("Machine user with name %s is not found yet", machineUserName);
            LOGGER.debug(errMessage, ex);
            throw new UmsOperationException(errMessage, ex);
        } else if (Status.UNAVAILABLE.getCode().equals(ex.getStatus().getCode())) {
            String errMessage = String.format("Cannot create machinue user '%s' for '%s' as " + "UMS API is UNAVAILABLE at the moment", machineUserName, userCrn);
            LOGGER.debug(errMessage, ex);
            throw new UmsOperationException(errMessage, ex);
        } else {
            throw ex;
        }
    }
}
Also used : UmsOperationException(com.sequenceiq.cloudbreak.auth.altus.exception.UmsOperationException) StatusRuntimeException(io.grpc.StatusRuntimeException) MachineUser(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.MachineUser) Retryable(org.springframework.retry.annotation.Retryable)

Example 2 with MachineUser

use of com.cloudera.thunderhead.service.usermanagement.UserManagementProto.MachineUser in project cloudbreak by hortonworks.

the class GrpcUmsClient method createMachineUser.

/**
 * Creates new machine user, it queries against the machine user if it has already exist
 *
 * @param machineUserName new machine user name
 * @param userCrn         the CRN of the user
 * @param requestId       an optional request Id
 * @return the machine user crn
 */
@Retryable(value = UmsOperationException.class, maxAttempts = 10, backoff = @Backoff(delay = 5000))
public Optional<String> createMachineUser(String machineUserName, String userCrn, String accountId, Optional<String> requestId, RegionAwareInternalCrnGeneratorFactory regionAwareInternalCrnGeneratorFactory) {
    try {
        UmsClient client = makeClient(channelWrapper.getChannel(), regionAwareInternalCrnGeneratorFactory);
        String generatedRequestId = RequestIdUtil.getOrGenerate(requestId);
        LOGGER.debug("Creating machine user {} for {} using request ID {}", machineUserName, userCrn, generatedRequestId);
        Optional<String> machineUserCrn = client.createMachineUser(generatedRequestId, userCrn, accountId, machineUserName);
        if (machineUserCrn.isEmpty()) {
            MachineUser machineUser = client.getMachineUserForUser(RequestIdUtil.getOrGenerate(requestId), userCrn, accountId, machineUserName, true, true);
            machineUserCrn = Optional.of(machineUser.getCrn());
        }
        LOGGER.debug("Machine User information retrieved for userCrn: {}", machineUserCrn.orElse(null));
        return machineUserCrn;
    } catch (StatusRuntimeException ex) {
        if (Status.NOT_FOUND.getCode().equals(ex.getStatus().getCode())) {
            String errMessage = String.format("Machine user with name %s is not found yet", machineUserName);
            LOGGER.debug(errMessage, ex);
            throw new UmsOperationException(errMessage, ex);
        } else if (Status.UNAVAILABLE.getCode().equals(ex.getStatus().getCode())) {
            String errMessage = String.format("Cannot create machinue user '%s' for '%s' as " + "UMS API is UNAVAILABLE at the moment", machineUserName, userCrn);
            LOGGER.debug(errMessage, ex);
            throw new UmsOperationException(errMessage, ex);
        } else {
            throw ex;
        }
    }
}
Also used : UmsOperationException(com.sequenceiq.cloudbreak.auth.altus.exception.UmsOperationException) StatusRuntimeException(io.grpc.StatusRuntimeException) MachineUser(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.MachineUser) Retryable(org.springframework.retry.annotation.Retryable)

Example 3 with MachineUser

use of com.cloudera.thunderhead.service.usermanagement.UserManagementProto.MachineUser in project cloudbreak by hortonworks.

the class UmsAuthenticationService method getCloudbreakUser.

@Override
public CloudbreakUser getCloudbreakUser(String userCrn, String principal) {
    String requestId = MDCBuilder.getOrGenerateRequestId();
    Crn crn;
    try {
        crn = Crn.safeFromString(userCrn);
    } catch (NullPointerException | CrnParseException e) {
        throw new UmsAuthenticationException(String.format("Invalid CRN has been provided: %s", userCrn));
    }
    CloudbreakUser cloudbreakUser;
    switch(crn.getResourceType()) {
        case USER:
            if (RegionAwareInternalCrnGeneratorUtil.isInternalCrn(userCrn)) {
                return RegionAwareInternalCrnGeneratorUtil.createInternalCrnUser(Crn.fromString(userCrn));
            } else {
                User userInfo = umsClient.getUserDetails(userCrn, Optional.ofNullable(requestId), regionAwareInternalCrnGeneratorFactory);
                String userName = principal != null ? principal : userInfo.getEmail();
                cloudbreakUser = new CloudbreakUser(userInfo.getUserId(), userCrn, userName, userInfo.getEmail(), crn.getAccountId());
            }
            break;
        case MACHINE_USER:
            MachineUser machineUserInfo = umsClient.getMachineUserDetails(userCrn, Crn.fromString(userCrn).getAccountId(), Optional.ofNullable(requestId), regionAwareInternalCrnGeneratorFactory);
            String machineUserName = principal != null ? principal : machineUserInfo.getMachineUserName();
            cloudbreakUser = new CloudbreakUser(machineUserInfo.getMachineUserId(), userCrn, machineUserName, machineUserInfo.getMachineUserName(), crn.getAccountId());
            break;
        default:
            throw new UmsAuthenticationException(String.format("Authentication is supported only with User and MachineUser CRN: %s", userCrn));
    }
    return cloudbreakUser;
}
Also used : CrnParseException(com.sequenceiq.cloudbreak.auth.crn.CrnParseException) UmsAuthenticationException(com.sequenceiq.cloudbreak.auth.altus.exception.UmsAuthenticationException) User(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.User) MachineUser(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.MachineUser) CrnUser(com.sequenceiq.cloudbreak.auth.CrnUser) CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) CloudbreakUser(com.sequenceiq.cloudbreak.common.user.CloudbreakUser) MachineUser(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.MachineUser) Crn(com.sequenceiq.cloudbreak.auth.crn.Crn)

Example 4 with MachineUser

use of com.cloudera.thunderhead.service.usermanagement.UserManagementProto.MachineUser in project cloudbreak by hortonworks.

the class AltusMachineUserService method deleteMachineUserForEnvironment.

@Retryable(value = Exception.class, maxAttempts = 5, backoff = @Backoff(delay = 10000))
public void deleteMachineUserForEnvironment(String accountId, String machineUserCrn, String environmentCrn) {
    if (environmentCrn != null && machineUserCrn != null) {
        MachineUser machineUser = getOrCreateAutoscaleMachineUser(environmentCrn, accountId);
        grpcUmsClient.deleteMachineUser(machineUser.getCrn(), regionAwareInternalCrnGeneratorFactory.iam().getInternalCrnForServiceAsString(), accountId, MDCUtils.getRequestId(), regionAwareInternalCrnGeneratorFactory);
        syncEnvironment(accountId, machineUserCrn, environmentCrn, Optional.of(machineUser.getWorkloadUsername()));
        LOGGER.info("Deleted MachineUser for machineUserCrn '{}', environment '{}'", machineUserCrn, environmentCrn);
    }
}
Also used : MachineUser(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.MachineUser) Retryable(org.springframework.retry.annotation.Retryable)

Example 5 with MachineUser

use of com.cloudera.thunderhead.service.usermanagement.UserManagementProto.MachineUser in project cloudbreak by hortonworks.

the class AltusMachineUserService method getOrCreateAutoscaleMachineUser.

private MachineUser getOrCreateAutoscaleMachineUser(String environmentCrn, String accountId) {
    // Idempotent api retrieves machine user or creates if missing.
    String autoscaleMachineUserName = String.format(AUTOSCALE_MACHINE_USER_NAME_PATTERN, Crn.fromString(environmentCrn).getResource());
    MachineUser machineUser = grpcUmsClient.getOrCreateMachineUserWithoutAccessKey(autoscaleMachineUserName, accountId, MDCUtils.getRequestId());
    LOGGER.info("Retrieved machineUser '{}' for machineUserName '{}' ", machineUser, autoscaleMachineUserName);
    return machineUser;
}
Also used : MachineUser(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.MachineUser)

Aggregations

MachineUser (com.cloudera.thunderhead.service.usermanagement.UserManagementProto.MachineUser)13 Retryable (org.springframework.retry.annotation.Retryable)4 UmsOperationException (com.sequenceiq.cloudbreak.auth.altus.exception.UmsOperationException)3 RegionAwareInternalCrnGeneratorFactory (com.sequenceiq.cloudbreak.auth.crn.RegionAwareInternalCrnGeneratorFactory)3 SynchronizeAllUsersRequest (com.sequenceiq.freeipa.api.v1.freeipa.user.model.SynchronizeAllUsersRequest)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3 Optional (java.util.Optional)3 Test (org.junit.jupiter.api.Test)3 Crn (com.sequenceiq.cloudbreak.auth.crn.Crn)2 Cluster (com.sequenceiq.periscope.domain.Cluster)2 Cacheable (org.springframework.cache.annotation.Cacheable)2 ListMachineUsersRequest (com.cloudera.thunderhead.service.usermanagement.UserManagementProto.ListMachineUsersRequest)1 ListMachineUsersResponse (com.cloudera.thunderhead.service.usermanagement.UserManagementProto.ListMachineUsersResponse)1 User (com.cloudera.thunderhead.service.usermanagement.UserManagementProto.User)1 LinkedHashMultimap (com.google.common.collect.LinkedHashMultimap)1 CrnUser (com.sequenceiq.cloudbreak.auth.CrnUser)1 UmsAuthenticationException (com.sequenceiq.cloudbreak.auth.altus.exception.UmsAuthenticationException)1 CrnParseException (com.sequenceiq.cloudbreak.auth.crn.CrnParseException)1 RegionAwareInternalCrnGenerator (com.sequenceiq.cloudbreak.auth.crn.RegionAwareInternalCrnGenerator)1 CloudbreakUser (com.sequenceiq.cloudbreak.common.user.CloudbreakUser)1