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;
}
}
}
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;
}
}
}
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;
}
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);
}
}
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;
}
Aggregations