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