Search in sources :

Example 1 with PublicKeyConnector

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);
}
Also used : CloudConnector(com.sequenceiq.cloudbreak.cloud.CloudConnector) EnvironmentAuthentication(com.sequenceiq.environment.environment.domain.EnvironmentAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Environment(com.sequenceiq.environment.environment.domain.Environment) CloudPlatformVariant(com.sequenceiq.cloudbreak.cloud.model.CloudPlatformVariant) PublicKeyConnector(com.sequenceiq.cloudbreak.cloud.PublicKeyConnector) Test(org.junit.jupiter.api.Test)

Example 2 with PublicKeyConnector

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());
}
Also used : CloudConnector(com.sequenceiq.cloudbreak.cloud.CloudConnector) EnvironmentAuthentication(com.sequenceiq.environment.environment.domain.EnvironmentAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Environment(com.sequenceiq.environment.environment.domain.Environment) CloudPlatformVariant(com.sequenceiq.cloudbreak.cloud.model.CloudPlatformVariant) PublicKeyConnector(com.sequenceiq.cloudbreak.cloud.PublicKeyConnector) Test(org.junit.jupiter.api.Test)

Example 3 with PublicKeyConnector

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());
}
Also used : Environment(com.sequenceiq.environment.environment.domain.Environment) PublicKeyConnector(com.sequenceiq.cloudbreak.cloud.PublicKeyConnector) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult) EnvironmentEditDto(com.sequenceiq.environment.environment.dto.EnvironmentEditDto) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with PublicKeyConnector

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());
}
Also used : Environment(com.sequenceiq.environment.environment.domain.Environment) PublicKeyConnector(com.sequenceiq.cloudbreak.cloud.PublicKeyConnector) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult) EnvironmentEditDto(com.sequenceiq.environment.environment.dto.EnvironmentEditDto) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with PublicKeyConnector

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

Aggregations

PublicKeyConnector (com.sequenceiq.cloudbreak.cloud.PublicKeyConnector)9 Environment (com.sequenceiq.environment.environment.domain.Environment)6 Test (org.junit.jupiter.api.Test)6 CloudConnector (com.sequenceiq.cloudbreak.cloud.CloudConnector)4 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)4 CloudPlatformVariant (com.sequenceiq.cloudbreak.cloud.model.CloudPlatformVariant)4 EnvironmentAuthentication (com.sequenceiq.environment.environment.domain.EnvironmentAuthentication)4 ValidationResult (com.sequenceiq.cloudbreak.validation.ValidationResult)3 EnvironmentEditDto (com.sequenceiq.environment.environment.dto.EnvironmentEditDto)2 BadRequestException (javax.ws.rs.BadRequestException)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 PublicKeyRegisterRequest (com.sequenceiq.cloudbreak.cloud.model.publickey.PublicKeyRegisterRequest)1 PublicKeyUnregisterRequest (com.sequenceiq.cloudbreak.cloud.model.publickey.PublicKeyUnregisterRequest)1 NotFoundException (com.sequenceiq.cloudbreak.common.exception.NotFoundException)1 ValidationResultBuilder (com.sequenceiq.cloudbreak.validation.ValidationResult.ValidationResultBuilder)1 AuthenticationDto (com.sequenceiq.environment.environment.dto.AuthenticationDto)1