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