Search in sources :

Example 31 with EnvironmentAuthentication

use of com.sequenceiq.environment.environment.domain.EnvironmentAuthentication in project cloudbreak by hortonworks.

the class EnvironmentModificationService method editAuthenticationIfChanged.

@VisibleForTesting
void editAuthenticationIfChanged(EnvironmentEditDto editDto, Environment environment) {
    AuthenticationDto authenticationDto = editDto.getAuthentication();
    if (authenticationDto != null) {
        EnvironmentValidatorService validatorService = environmentService.getValidatorService();
        ValidationResult validationResult = validatorService.validateAuthenticationModification(editDto, environment);
        if (validationResult.hasError()) {
            throw new BadRequestException(validationResult.getFormattedErrors());
        }
        EnvironmentAuthentication originalAuthentication = environment.getAuthentication();
        if (environmentResourceService.isRawSshKeyUpdateSupported(environment)) {
            EnvironmentAuthentication updated = authenticationDtoConverter.dtoToSshUpdatedAuthentication(authenticationDto);
            updated.setLoginUserName(originalAuthentication.getLoginUserName());
            updated.setId(originalAuthentication.getId());
            environment.setAuthentication(updated);
        } else if (environmentResourceService.isExistingSshKeyUpdateSupported(environment)) {
            environment.setAuthentication(authenticationDtoConverter.dtoToAuthentication(authenticationDto));
            boolean cleanupOldSshKey = true;
            if (StringUtils.isNotEmpty(authenticationDto.getPublicKey())) {
                cleanupOldSshKey = environmentResourceService.createAndUpdateSshKey(environment);
            }
            if (cleanupOldSshKey) {
                String oldSshKeyId = originalAuthentication.getPublicKeyId();
                LOGGER.info("The '{}' of ssh key is replaced with {}", oldSshKeyId, environment.getAuthentication().getPublicKeyId());
                if (originalAuthentication.isManagedKey()) {
                    environmentResourceService.deletePublicKey(environment, oldSshKeyId);
                }
            } else {
                LOGGER.info("Authentication modification was unsuccessful. The authentication was reverted to the previous version.");
                environment.setAuthentication(originalAuthentication);
            }
        }
    }
}
Also used : EnvironmentAuthentication(com.sequenceiq.environment.environment.domain.EnvironmentAuthentication) AuthenticationDto(com.sequenceiq.environment.environment.dto.AuthenticationDto) BadRequestException(javax.ws.rs.BadRequestException) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult) EnvironmentValidatorService(com.sequenceiq.environment.environment.validation.EnvironmentValidatorService) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 32 with EnvironmentAuthentication

use of com.sequenceiq.environment.environment.domain.EnvironmentAuthentication in project cloudbreak by hortonworks.

the class AuthenticationDtoConverter method dtoToAuthentication.

public EnvironmentAuthentication dtoToAuthentication(AuthenticationDto authenticationDto) {
    EnvironmentAuthentication environmentAuthentication = new EnvironmentAuthentication();
    if (isValidSshKey(authenticationDto.getPublicKey())) {
        List<String> parts = Arrays.asList(StringUtils.split(authenticationDto.getPublicKey(), " "));
        environmentAuthentication.setPublicKey(String.format("%s %s %s", parts.get(0), parts.get(1), authenticationDto.getLoginUserName()));
    }
    environmentAuthentication.setLoginUserName(authenticationDto.getLoginUserName());
    environmentAuthentication.setPublicKeyId(authenticationDto.getPublicKeyId());
    environmentAuthentication.setManagedKey(authenticationDto.isManagedKey());
    return environmentAuthentication;
}
Also used : EnvironmentAuthentication(com.sequenceiq.environment.environment.domain.EnvironmentAuthentication)

Aggregations

EnvironmentAuthentication (com.sequenceiq.environment.environment.domain.EnvironmentAuthentication)32 Test (org.junit.jupiter.api.Test)27 Environment (com.sequenceiq.environment.environment.domain.Environment)24 Credential (com.sequenceiq.environment.credential.domain.Credential)9 EnvironmentCreationDto (com.sequenceiq.environment.environment.dto.EnvironmentCreationDto)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 AuthenticationDto (com.sequenceiq.environment.environment.dto.AuthenticationDto)7 EnvironmentEditDto (com.sequenceiq.environment.environment.dto.EnvironmentEditDto)6 CloudConnector (com.sequenceiq.cloudbreak.cloud.CloudConnector)5 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)5 CloudPlatformVariant (com.sequenceiq.cloudbreak.cloud.model.CloudPlatformVariant)5 ValidationResultBuilder (com.sequenceiq.cloudbreak.validation.ValidationResult.ValidationResultBuilder)5 AwsDiskEncryptionParametersDto (com.sequenceiq.environment.parameter.dto.AwsDiskEncryptionParametersDto)5 AwsParametersDto (com.sequenceiq.environment.parameter.dto.AwsParametersDto)5 AzureParametersDto (com.sequenceiq.environment.parameter.dto.AzureParametersDto)5 AzureResourceEncryptionParametersDto (com.sequenceiq.environment.parameter.dto.AzureResourceEncryptionParametersDto)5 ParametersDto (com.sequenceiq.environment.parameter.dto.ParametersDto)5 PublicKeyConnector (com.sequenceiq.cloudbreak.cloud.PublicKeyConnector)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 Headers (reactor.bus.Event.Headers)3