Search in sources :

Example 11 with AuthenticationDto

use of com.sequenceiq.environment.environment.dto.AuthenticationDto in project cloudbreak by hortonworks.

the class EnvironmentModificationServiceTest method testEditAuthenticationIfChangedWhenNeedToCreateSshKey.

@Test
void testEditAuthenticationIfChangedWhenNeedToCreateSshKey() {
    AuthenticationDto authenticationDto = AuthenticationDto.builder().withPublicKey("ssh-key").build();
    EnvironmentEditDto environmentEditDto = EnvironmentEditDto.builder().withAuthentication(authenticationDto).build();
    Environment environment = new Environment();
    EnvironmentAuthentication originalEnvironmentAuthentication = new EnvironmentAuthentication();
    originalEnvironmentAuthentication.setPublicKey("original-ssh-key");
    originalEnvironmentAuthentication.setManagedKey(false);
    environment.setAuthentication(originalEnvironmentAuthentication);
    EnvironmentAuthentication newEnvironmentAuthentication = new EnvironmentAuthentication();
    newEnvironmentAuthentication.setPublicKey("new-ssh-key");
    when(environmentService.getValidatorService()).thenReturn(validatorService);
    when(validatorService.validateAuthenticationModification(environmentEditDto, environment)).thenReturn(validationResult);
    when(authenticationDtoConverter.dtoToAuthentication(authenticationDto)).thenReturn(newEnvironmentAuthentication);
    when(environmentResourceService.isExistingSshKeyUpdateSupported(environment)).thenReturn(true);
    when(environmentResourceService.isRawSshKeyUpdateSupported(environment)).thenReturn(false);
    when(environmentResourceService.createAndUpdateSshKey(environment)).thenReturn(true);
    environmentModificationServiceUnderTest.editAuthenticationIfChanged(environmentEditDto, environment);
    verify(environmentResourceService, times(1)).createAndUpdateSshKey(environment);
    verify(environmentResourceService, times(0)).deletePublicKey(environment, "old-public-key-id");
    assertEquals(environment.getAuthentication().getPublicKey(), "new-ssh-key");
}
Also used : EnvironmentAuthentication(com.sequenceiq.environment.environment.domain.EnvironmentAuthentication) AuthenticationDto(com.sequenceiq.environment.environment.dto.AuthenticationDto) Environment(com.sequenceiq.environment.environment.domain.Environment) EnvironmentEditDto(com.sequenceiq.environment.environment.dto.EnvironmentEditDto) Test(org.junit.jupiter.api.Test)

Example 12 with AuthenticationDto

use of com.sequenceiq.environment.environment.dto.AuthenticationDto 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

AuthenticationDto (com.sequenceiq.environment.environment.dto.AuthenticationDto)12 Environment (com.sequenceiq.environment.environment.domain.Environment)9 EnvironmentEditDto (com.sequenceiq.environment.environment.dto.EnvironmentEditDto)9 Test (org.junit.jupiter.api.Test)9 EnvironmentAuthentication (com.sequenceiq.environment.environment.domain.EnvironmentAuthentication)7 ValidationResult (com.sequenceiq.cloudbreak.validation.ValidationResult)4 Network (com.sequenceiq.cloudbreak.cloud.model.Network)2 NetworkCidr (com.sequenceiq.cloudbreak.cloud.network.NetworkCidr)2 CloudPlatform (com.sequenceiq.cloudbreak.common.mappable.CloudPlatform)2 Credential (com.sequenceiq.environment.credential.domain.Credential)2 EnvironmentBackup (com.sequenceiq.environment.environment.dto.EnvironmentBackup)2 SecurityAccessDto (com.sequenceiq.environment.environment.dto.SecurityAccessDto)2 EnvironmentTelemetry (com.sequenceiq.environment.environment.dto.telemetry.EnvironmentTelemetry)2 AwsNetwork (com.sequenceiq.environment.network.dao.domain.AwsNetwork)2 BaseNetwork (com.sequenceiq.environment.network.dao.domain.BaseNetwork)2 GcpNetwork (com.sequenceiq.environment.network.dao.domain.GcpNetwork)2 NetworkDto (com.sequenceiq.environment.network.dto.NetworkDto)2 EnvironmentNetworkConverter (com.sequenceiq.environment.network.v1.converter.EnvironmentNetworkConverter)2 ParametersDto (com.sequenceiq.environment.parameter.dto.ParametersDto)2 ArrayList (java.util.ArrayList)2