Search in sources :

Example 1 with AuthenticationDto

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

the class EnvironmentModificationServiceTest method testEditAuthenticationIfChangedWhenDontDeleteOldKey.

@Test
void testEditAuthenticationIfChangedWhenDontDeleteOldKey() {
    AuthenticationDto authenticationDto = AuthenticationDto.builder().build();
    EnvironmentEditDto environmentEditDto = EnvironmentEditDto.builder().withAuthentication(authenticationDto).build();
    Environment environment = new Environment();
    EnvironmentAuthentication originalEnvironmentAuthentication = new EnvironmentAuthentication();
    originalEnvironmentAuthentication.setManagedKey(false);
    originalEnvironmentAuthentication.setPublicKeyId("old-public-key-id");
    environment.setAuthentication(originalEnvironmentAuthentication);
    EnvironmentAuthentication newEnvironmentAuthentication = new EnvironmentAuthentication();
    when(environmentService.getValidatorService()).thenReturn(validatorService);
    when(validatorService.validateAuthenticationModification(environmentEditDto, environment)).thenReturn(validationResult);
    when(environmentResourceService.isExistingSshKeyUpdateSupported(environment)).thenReturn(true);
    when(environmentResourceService.isRawSshKeyUpdateSupported(environment)).thenReturn(false);
    when(authenticationDtoConverter.dtoToAuthentication(authenticationDto)).thenReturn(newEnvironmentAuthentication);
    environmentModificationServiceUnderTest.editAuthenticationIfChanged(environmentEditDto, environment);
    verify(environmentResourceService, times(0)).deletePublicKey(environment, "old-public-key-id");
    verify(environmentResourceService, times(0)).createAndUpdateSshKey(environment);
}
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 2 with AuthenticationDto

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

the class EnvironmentModificationServiceTest method testEditAuthenticationIfChangedWhenHasValidationError.

@Test
void testEditAuthenticationIfChangedWhenHasValidationError() {
    AuthenticationDto authenticationDto = AuthenticationDto.builder().build();
    EnvironmentEditDto environmentEditDto = EnvironmentEditDto.builder().withAuthentication(authenticationDto).build();
    Environment environment = new Environment();
    ValidationResult validationResult = ValidationResult.builder().error("Error").build();
    when(environmentResourceService.isExistingSshKeyUpdateSupported(environment)).thenReturn(true);
    when(environmentResourceService.isRawSshKeyUpdateSupported(environment)).thenReturn(false);
    when(environmentService.getValidatorService()).thenReturn(validatorService);
    when(validatorService.validateAuthenticationModification(environmentEditDto, environment)).thenReturn(validationResult);
    BadRequestException badRequestException = assertThrows(BadRequestException.class, () -> environmentModificationServiceUnderTest.editAuthenticationIfChanged(environmentEditDto, environment));
    assertEquals(badRequestException.getMessage(), "Error");
}
Also used : AuthenticationDto(com.sequenceiq.environment.environment.dto.AuthenticationDto) Environment(com.sequenceiq.environment.environment.domain.Environment) BadRequestException(javax.ws.rs.BadRequestException) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult) EnvironmentEditDto(com.sequenceiq.environment.environment.dto.EnvironmentEditDto) Test(org.junit.jupiter.api.Test)

Example 3 with AuthenticationDto

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

the class EnvironmentModificationServiceTest method testEditAuthenticationIfChangedWhenNeedToCreateSshKeyAndDeleteOldOne.

@Test
void testEditAuthenticationIfChangedWhenNeedToCreateSshKeyAndDeleteOldOne() {
    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(true);
    originalEnvironmentAuthentication.setPublicKeyId("old-public-key-id");
    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(1)).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 4 with AuthenticationDto

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

the class EnvironmentValidatorService method validateAuthenticationModification.

public ValidationResult validateAuthenticationModification(EnvironmentEditDto editDto, Environment environment) {
    ValidationResultBuilder validationResultBuilder = ValidationResult.builder();
    AuthenticationDto authenticationDto = editDto.getAuthentication();
    String publicKeyId = authenticationDto.getPublicKeyId();
    Optional<PublicKeyConnector> publicKeyConnector = environmentResourceService.getPublicKeyConnector(environment.getCloudPlatform());
    if (publicKeyConnector.isEmpty() && isNotEmpty(publicKeyId)) {
        validationResultBuilder.error("The change of publicKeyId is not supported on " + environment.getCloudPlatform());
    } else {
        String publicKey = authenticationDto.getPublicKey();
        if (isNotEmpty(publicKeyId) && isNotEmpty(publicKey)) {
            validationResultBuilder.error("You should define either publicKey or publicKeyId only, but not both.");
        }
        if (StringUtils.isEmpty(publicKeyId) && StringUtils.isEmpty(publicKey)) {
            validationResultBuilder.error("You should define either the publicKey or the publicKeyId.");
        }
        if (isNotEmpty(publicKeyId) && !environmentResourceService.isPublicKeyIdExists(environment, publicKeyId)) {
            validationResultBuilder.error(String.format("The publicKeyId with name of '%s' does not exist on the provider.", publicKeyId));
        }
        if (isNotEmpty(publicKey)) {
            ValidationResult validationResult = publicKeyValidator.validatePublicKey(publicKey);
            validationResultBuilder.merge(validationResult);
        }
    }
    return validationResultBuilder.build();
}
Also used : AuthenticationDto(com.sequenceiq.environment.environment.dto.AuthenticationDto) ValidationResultBuilder(com.sequenceiq.cloudbreak.validation.ValidationResult.ValidationResultBuilder) PublicKeyConnector(com.sequenceiq.cloudbreak.cloud.PublicKeyConnector) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult)

Example 5 with AuthenticationDto

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

the class EnvironmentAuthenticationValidator method validate.

public ValidationResult validate(EnvironmentValidationDto environmentValidationDto) {
    EnvironmentDto environmentDto = environmentValidationDto.getEnvironmentDto();
    ValidationResult.ValidationResultBuilder resultBuilder = ValidationResult.builder();
    AuthenticationDto authentication = environmentDto.getAuthentication();
    if (!StringUtils.hasText(authentication.getPublicKeyId())) {
        validate(authentication.getPublicKey(), resultBuilder);
    }
    return resultBuilder.build();
}
Also used : EnvironmentDto(com.sequenceiq.environment.environment.dto.EnvironmentDto) AuthenticationDto(com.sequenceiq.environment.environment.dto.AuthenticationDto) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult)

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