Search in sources :

Example 6 with PublicKeyConnector

use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector in project cloudbreak by hortonworks.

the class EnvironmentResourceService method deletePublicKey.

public void deletePublicKey(Environment environment, String publicKeyId) {
    try {
        LOGGER.info("Try to delete the ssh key ({}) for {}", publicKeyId, environment.getName());
        PublicKeyConnector publicKeyConnector = getPublicKeyConnector(environment.getCloudPlatform()).orElseThrow(() -> new BadRequestException("No public key connector for cloud platform: " + environment.getCloudPlatform()));
        PublicKeyUnregisterRequest request = createPublicKeyUnregisterRequest(environment, publicKeyId);
        publicKeyConnector.unregister(request);
        LOGGER.info("the ssh key ({}) deleted successfully for {}", publicKeyId, environment.getName());
    } catch (UnsupportedOperationException e) {
        LOGGER.info("Cloud platform {} does not support public key services", environment.getCloudPlatform());
    }
}
Also used : PublicKeyUnregisterRequest(com.sequenceiq.cloudbreak.cloud.model.publickey.PublicKeyUnregisterRequest) BadRequestException(javax.ws.rs.BadRequestException) PublicKeyConnector(com.sequenceiq.cloudbreak.cloud.PublicKeyConnector)

Example 7 with PublicKeyConnector

use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector in project cloudbreak by hortonworks.

the class EnvironmentResourceServiceTest method testCreateAndUpdateSshKeyWhenTooLongWithoutTimestamp.

@Test
void testCreateAndUpdateSshKeyWhenTooLongWithoutTimestamp() {
    Environment environment = new Environment();
    environment.setName(StringUtils.repeat("*", 200));
    environment.setResourceCrn(StringUtils.repeat("*", 100));
    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(1L);
    environmentResourceServiceUnderTest.createAndUpdateSshKey(environment);
    String first = StringUtils.repeat("*", 200);
    String second = StringUtils.repeat("*", 54);
    assertEquals(first + "-" + second, environment.getAuthentication().getPublicKeyId());
    // the split size should be equals 2, because we concat the name and crn only
    assertEquals(2, 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 8 with PublicKeyConnector

use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector in project cloudbreak by hortonworks.

the class EnvironmentResourceServiceTest method testCreateAndUpdateSshKeyWhenCreatedAndSet.

@Test
void testCreateAndUpdateSshKeyWhenCreatedAndSet() {
    Environment environment = new Environment();
    environment.setName("env-name");
    environment.setResourceCrn("res-crn");
    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(1L);
    environmentResourceServiceUnderTest.createAndUpdateSshKey(environment);
    assertEquals("env-name-res-crn-1", environment.getAuthentication().getPublicKeyId());
}
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 9 with PublicKeyConnector

use of com.sequenceiq.cloudbreak.cloud.PublicKeyConnector in project cloudbreak by hortonworks.

the class EnvironmentResourceService method createPublicKey.

private boolean createPublicKey(Environment environment, String publicKeyId) {
    try {
        PublicKeyConnector publicKeyConnector = getPublicKeyConnector(environment.getCloudPlatform()).orElseThrow(() -> new BadRequestException("No network connector for cloud platform: " + environment.getCloudPlatform()));
        PublicKeyRegisterRequest request = createPublicKeyRegisterRequest(environment, publicKeyId);
        publicKeyConnector.register(request);
        LOGGER.info("Public key id is registered with name of {}", publicKeyId);
        return true;
    } catch (UnsupportedOperationException e) {
        LOGGER.info("Cloud platform {} does not support public key services", environment.getCloudPlatform());
    } catch (Exception e) {
        LOGGER.info("Public key could not be registered. {}", e.getMessage(), e);
    }
    return false;
}
Also used : PublicKeyRegisterRequest(com.sequenceiq.cloudbreak.cloud.model.publickey.PublicKeyRegisterRequest) BadRequestException(javax.ws.rs.BadRequestException) PublicKeyConnector(com.sequenceiq.cloudbreak.cloud.PublicKeyConnector) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException)

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