use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector in project cloudbreak by hortonworks.
the class EnvironmentResourceServiceTest method testCreateAndUpdateSshKeyWhenTooLongWithTimestamp.
@Test
void testCreateAndUpdateSshKeyWhenTooLongWithTimestamp() {
Environment environment = new Environment();
environment.setName(StringUtils.repeat("*", 200));
environment.setResourceCrn(StringUtils.repeat("*", 50));
environment.setCloudPlatform("CP");
environment.setLocation("location");
EnvironmentAuthentication authentication = new EnvironmentAuthentication();
authentication.setPublicKey("ssh-key");
environment.setAuthentication(authentication);
CloudConnector connector = mock(CloudConnector.class);
PublicKeyConnector publicKeyConnector = mock(PublicKeyConnector.class);
when(cloudPlatformConnectors.get(new CloudPlatformVariant(Platform.platform("CP"), Variant.variant("CP")))).thenReturn(connector);
when(connector.publicKey()).thenReturn(publicKeyConnector);
when(credentialToCloudCredentialConverter.convert(environment.getCredential())).thenReturn(mock(CloudCredential.class));
when(clock.getCurrentTimeMillis()).thenReturn(1234L);
environmentResourceServiceUnderTest.createAndUpdateSshKey(environment);
String first = StringUtils.repeat("*", 200);
String second = StringUtils.repeat("*", 50);
assertEquals(first + "-" + second + "-123", environment.getAuthentication().getPublicKeyId());
// the split size should be equals 3, because we concat the name, crn and timestamp
assertEquals(3, environment.getAuthentication().getPublicKeyId().split("-").length);
}
use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector in project cloudbreak by hortonworks.
the class EnvironmentResourceServiceTest method testCreateAndUpdateSshKeyWhenPublicKeyIdExactMatchWith255.
@Test
void testCreateAndUpdateSshKeyWhenPublicKeyIdExactMatchWith255() {
Environment environment = new Environment();
environment.setName(StringUtils.repeat("*", 200));
environment.setResourceCrn(StringUtils.repeat("*", 50));
environment.setCloudPlatform("CP");
environment.setLocation("location");
EnvironmentAuthentication authentication = new EnvironmentAuthentication();
authentication.setPublicKey("ssh-key");
environment.setAuthentication(authentication);
CloudConnector connector = mock(CloudConnector.class);
PublicKeyConnector publicKeyConnector = mock(PublicKeyConnector.class);
when(cloudPlatformConnectors.get(new CloudPlatformVariant(Platform.platform("CP"), Variant.variant("CP")))).thenReturn(connector);
when(connector.publicKey()).thenReturn(publicKeyConnector);
when(credentialToCloudCredentialConverter.convert(environment.getCredential())).thenReturn(mock(CloudCredential.class));
when(clock.getCurrentTimeMillis()).thenReturn(123L);
environmentResourceServiceUnderTest.createAndUpdateSshKey(environment);
String first = StringUtils.repeat("*", 200);
String second = StringUtils.repeat("*", 50);
assertEquals(first + "-" + second + "-123", environment.getAuthentication().getPublicKeyId());
// the split size should be equals 3, because we concat the name, crn and timestamp
assertEquals(3, environment.getAuthentication().getPublicKeyId().split("-").length);
assertEquals(255, environment.getAuthentication().getPublicKeyId().length());
}
use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector in project cloudbreak by hortonworks.
the class EnvironmentValidatorServiceTest method testValidateAuthenticationModificationWhenHasPublicKeyIdButNotExists.
@Test
void testValidateAuthenticationModificationWhenHasPublicKeyIdButNotExists() {
Environment environment = new Environment();
environment.setCloudPlatform("AWS");
EnvironmentEditDto environmentEditDto = EnvironmentEditDto.builder().withAuthentication(AuthenticationDto.builder().withPublicKeyId("pub-key-id").build()).build();
PublicKeyConnector connector = mock(PublicKeyConnector.class);
when(environmentResourceService.isPublicKeyIdExists(environment, "pub-key-id")).thenReturn(false);
when(environmentResourceService.getPublicKeyConnector(environment.getCloudPlatform())).thenReturn(Optional.of(connector));
ValidationResult validationResult = underTest.validateAuthenticationModification(environmentEditDto, environment);
assertEquals("The publicKeyId with name of 'pub-key-id' does not exist on the provider.", validationResult.getFormattedErrors());
}
use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector in project cloudbreak by hortonworks.
the class EnvironmentValidatorServiceTest method testValidateAuthenticationModificationWhenHasPublicKeyAndPublicKeyIdAsWell.
@Test
void testValidateAuthenticationModificationWhenHasPublicKeyAndPublicKeyIdAsWell() {
Environment environment = new Environment();
environment.setCloudPlatform("AWS");
EnvironmentEditDto environmentEditDto = EnvironmentEditDto.builder().withAuthentication(AuthenticationDto.builder().withPublicKeyId("pub-key-id").withPublicKey("ssh-key").build()).build();
PublicKeyConnector connector = mock(PublicKeyConnector.class);
when(environmentResourceService.isPublicKeyIdExists(environment, "pub-key-id")).thenReturn(true);
when(environmentResourceService.getPublicKeyConnector(environment.getCloudPlatform())).thenReturn(Optional.of(connector));
when(publicKeyValidator.validatePublicKey(anyString())).thenReturn(ValidationResult.empty());
ValidationResult validationResult = underTest.validateAuthenticationModification(environmentEditDto, environment);
assertEquals("You should define either publicKey or publicKeyId only, but not both.", validationResult.getFormattedErrors());
}
use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector 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();
}
Aggregations