Search in sources :

Example 1 with EnvironmentValidatorService

use of com.sequenceiq.environment.environment.validation.EnvironmentValidatorService in project cloudbreak by hortonworks.

the class EnvironmentModificationService method editSecurityAccessIfChanged.

private void editSecurityAccessIfChanged(EnvironmentEditDto editDto, Environment environment) {
    SecurityAccessDto securityAccessDto = editDto.getSecurityAccess();
    if (securityAccessDto != null) {
        EnvironmentValidatorService validatorService = environmentService.getValidatorService();
        ValidationResult validationResult = validatorService.validateSecurityAccessModification(securityAccessDto, environment);
        if (validationResult.hasError()) {
            throw new BadRequestException(validationResult.getFormattedErrors());
        }
        validationResult = validatorService.validateSecurityGroups(editDto, environment);
        if (validationResult.hasError()) {
            throw new BadRequestException(validationResult.getFormattedErrors());
        }
        environmentService.editSecurityAccess(environment, securityAccessDto);
    }
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult) EnvironmentValidatorService(com.sequenceiq.environment.environment.validation.EnvironmentValidatorService) SecurityAccessDto(com.sequenceiq.environment.environment.dto.SecurityAccessDto)

Example 2 with EnvironmentValidatorService

use of com.sequenceiq.environment.environment.validation.EnvironmentValidatorService 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)

Aggregations

ValidationResult (com.sequenceiq.cloudbreak.validation.ValidationResult)2 EnvironmentValidatorService (com.sequenceiq.environment.environment.validation.EnvironmentValidatorService)2 BadRequestException (javax.ws.rs.BadRequestException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 EnvironmentAuthentication (com.sequenceiq.environment.environment.domain.EnvironmentAuthentication)1 AuthenticationDto (com.sequenceiq.environment.environment.dto.AuthenticationDto)1 SecurityAccessDto (com.sequenceiq.environment.environment.dto.SecurityAccessDto)1