Search in sources :

Example 11 with SecurityAccessDto

use of com.sequenceiq.environment.environment.dto.SecurityAccessDto in project cloudbreak by hortonworks.

the class NetworkServiceTest method testRefreshMetadataFromGoogleCloudProviderMustUseSubnetName.

@Test
public void testRefreshMetadataFromGoogleCloudProviderMustUseSubnetName() {
    NetworkDto networkDto = mock(NetworkDto.class);
    AuthenticationDto authenticationDto = mock(AuthenticationDto.class);
    EnvironmentTelemetry environmentTelemetry = mock(EnvironmentTelemetry.class);
    EnvironmentBackup environmentBackup = mock(EnvironmentBackup.class);
    SecurityAccessDto securityAccessDto = mock(SecurityAccessDto.class);
    ParametersDto parametersDto = mock(ParametersDto.class);
    EnvironmentNetworkConverter environmentNetworkConverter = mock(EnvironmentNetworkConverter.class);
    Network network = mock(Network.class);
    Credential credential = mock(Credential.class);
    BaseNetwork baseNetwork = new GcpNetwork();
    baseNetwork.setRegistrationType(RegistrationType.EXISTING);
    Environment environment = new Environment();
    environment.setCloudPlatform("GCP");
    environment.setCredential(credential);
    EnvironmentEditDto environmentEditDto = new EnvironmentEditDto("description", "accountId", networkDto, authenticationDto, environmentTelemetry, environmentBackup, securityAccessDto, Tunnel.CCMV2, IdBrokerMappingSource.MOCK, CloudStorageValidation.ENABLED, "adminGroupName", parametersDto);
    when(environmentNetworkConverterMap.get(any(CloudPlatform.class))).thenReturn(environmentNetworkConverter);
    when(environmentNetworkConverter.convertToDto(baseNetwork)).thenReturn(networkDto);
    when(cloudNetworkService.retrieveSubnetMetadata(any(Environment.class), any(NetworkDto.class))).thenReturn(Map.of("s1", cloudSubnet("s1", "subnet1")));
    when(cloudNetworkService.retrieveEndpointGatewaySubnetMetadata(any(Environment.class), any(NetworkDto.class))).thenReturn(Map.of("s1", cloudSubnet("s1", "subnet1")));
    when(environmentNetworkConverter.convertToNetwork(any(BaseNetwork.class))).thenReturn(network);
    when(environmentNetworkService.getNetworkCidr(any(Network.class), anyString(), any(Credential.class))).thenReturn(new NetworkCidr("10.0.0.0", new ArrayList<>()));
    BaseNetwork result = underTest.refreshMetadataFromCloudProvider(baseNetwork, environmentEditDto, environment);
    Assertions.assertEquals(result.getSubnetMetas().keySet().stream().findFirst().get(), "subnet1");
    Assertions.assertEquals(result.getSubnetMetas().keySet().size(), 1);
}
Also used : NetworkDto(com.sequenceiq.environment.network.dto.NetworkDto) CloudPlatform(com.sequenceiq.cloudbreak.common.mappable.CloudPlatform) EnvironmentTelemetry(com.sequenceiq.environment.environment.dto.telemetry.EnvironmentTelemetry) Credential(com.sequenceiq.environment.credential.domain.Credential) BaseNetwork(com.sequenceiq.environment.network.dao.domain.BaseNetwork) EnvironmentBackup(com.sequenceiq.environment.environment.dto.EnvironmentBackup) GcpNetwork(com.sequenceiq.environment.network.dao.domain.GcpNetwork) ArrayList(java.util.ArrayList) EnvironmentEditDto(com.sequenceiq.environment.environment.dto.EnvironmentEditDto) EnvironmentNetworkConverter(com.sequenceiq.environment.network.v1.converter.EnvironmentNetworkConverter) NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) AuthenticationDto(com.sequenceiq.environment.environment.dto.AuthenticationDto) AwsNetwork(com.sequenceiq.environment.network.dao.domain.AwsNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) GcpNetwork(com.sequenceiq.environment.network.dao.domain.GcpNetwork) BaseNetwork(com.sequenceiq.environment.network.dao.domain.BaseNetwork) Environment(com.sequenceiq.environment.environment.domain.Environment) ParametersDto(com.sequenceiq.environment.parameter.dto.ParametersDto) SecurityAccessDto(com.sequenceiq.environment.environment.dto.SecurityAccessDto) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with SecurityAccessDto

use of com.sequenceiq.environment.environment.dto.SecurityAccessDto in project cloudbreak by hortonworks.

the class EnvironmentValidatorServiceTest method testValidateSecurityAccessModificationWhenEnvCidrIsNotEmptyButDefaultSecGroupAddedOnly.

@Test
void testValidateSecurityAccessModificationWhenEnvCidrIsNotEmptyButDefaultSecGroupAddedOnly() {
    Environment environment = new Environment();
    environment.setCidr("cidr");
    SecurityAccessDto securityAccessDto = SecurityAccessDto.builder().withDefaultSecurityGroupId("sec-group").build();
    ValidationResult validationResult = underTest.validateSecurityAccessModification(securityAccessDto, environment);
    assertTrue(validationResult.hasError());
    assertEquals("The CIDR can be replaced with the default and knox security groups, please add to the request", validationResult.getFormattedErrors());
}
Also used : Environment(com.sequenceiq.environment.environment.domain.Environment) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult) SecurityAccessDto(com.sequenceiq.environment.environment.dto.SecurityAccessDto) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with SecurityAccessDto

use of com.sequenceiq.environment.environment.dto.SecurityAccessDto in project cloudbreak by hortonworks.

the class EnvironmentValidatorServiceTest method testValidateSecurityAccessModificationWhenCidrAddedOnlyInRequest.

@Test
void testValidateSecurityAccessModificationWhenCidrAddedOnlyInRequest() {
    Environment environment = new Environment();
    SecurityAccessDto securityAccessDto = SecurityAccessDto.builder().withCidr("cidr").build();
    ValidationResult validationResult = underTest.validateSecurityAccessModification(securityAccessDto, environment);
    assertTrue(validationResult.hasError());
    assertEquals("1. Please add the default or knox security groups, we cannot edit with empty value.\n" + "2. The CIDR could not be updated in the environment", validationResult.getFormattedErrors());
}
Also used : Environment(com.sequenceiq.environment.environment.domain.Environment) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult) SecurityAccessDto(com.sequenceiq.environment.environment.dto.SecurityAccessDto) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with SecurityAccessDto

use of com.sequenceiq.environment.environment.dto.SecurityAccessDto in project cloudbreak by hortonworks.

the class EnvironmentModificationServiceTest method editByNameSecurityAccessChange.

@Test
void editByNameSecurityAccessChange() {
    SecurityAccessDto securityAccessDto = SecurityAccessDto.builder().withCidr("test").build();
    EnvironmentEditDto environmentDto = EnvironmentEditDto.builder().withAccountId(ACCOUNT_ID).withSecurityAccess(securityAccessDto).build();
    Environment value = new Environment();
    when(environmentService.findByNameAndAccountIdAndArchivedIsFalse(eq(ENVIRONMENT_NAME), eq(ACCOUNT_ID))).thenReturn(Optional.of(value));
    when(environmentService.getValidatorService()).thenReturn(validatorService);
    when(validatorService.validateSecurityAccessModification(any(), any())).thenReturn(validationResult);
    when(validatorService.validateSecurityGroups(any(), any())).thenReturn(validationResult);
    environmentModificationServiceUnderTest.editByName(ENVIRONMENT_NAME, environmentDto);
    ArgumentCaptor<Environment> environmentArgumentCaptor = ArgumentCaptor.forClass(Environment.class);
    verify(environmentService).save(environmentArgumentCaptor.capture());
    verify(environmentService).editSecurityAccess(eq(value), eq(securityAccessDto));
}
Also used : Environment(com.sequenceiq.environment.environment.domain.Environment) EnvironmentEditDto(com.sequenceiq.environment.environment.dto.EnvironmentEditDto) SecurityAccessDto(com.sequenceiq.environment.environment.dto.SecurityAccessDto) Test(org.junit.jupiter.api.Test)

Example 15 with SecurityAccessDto

use of com.sequenceiq.environment.environment.dto.SecurityAccessDto in project cloudbreak by hortonworks.

the class AwsEnvironmentSecurityGroupValidator method validate.

@Override
public void validate(EnvironmentValidationDto environmentValidationDto, ValidationResult.ValidationResultBuilder resultBuilder) {
    EnvironmentDto environmentDto = environmentValidationDto.getEnvironmentDto();
    SecurityAccessDto securityAccessDto = environmentDto.getSecurityAccess();
    if (securityAccessDto != null) {
        if (onlyOneSecurityGroupIdDefined(securityAccessDto)) {
            LOGGER.error("Only one existing security group definied by the user: {}", securityAccessDto);
            resultBuilder.error(securityGroupIdsMustBePresent());
        } else if (isSecurityGroupIdDefined(securityAccessDto)) {
            LOGGER.info("Both existing security group defined: {}", securityAccessDto);
            NetworkDto networkDto = environmentDto.getNetwork();
            if (RegistrationType.CREATE_NEW == networkDto.getRegistrationType()) {
                LOGGER.error("Both existing security group defined and user wants to create a new network with cidr: {}", networkDto.getNetworkCidr());
                resultBuilder.error(networkIdMustBePresent(getCloudPlatform().name()));
                return;
            }
            if (!Strings.isNullOrEmpty(securityAccessDto.getDefaultSecurityGroupId())) {
                LOGGER.info("Validate Security group {} that is related to {} network", securityAccessDto.getDefaultSecurityGroupId(), networkDto.getAws());
                checkSecurityGroupVpc(environmentDto, resultBuilder, environmentDto.getSecurityAccess().getDefaultSecurityGroupId());
            }
            if (!Strings.isNullOrEmpty(securityAccessDto.getSecurityGroupIdForKnox())) {
                LOGGER.info("Validate Security group {} that is related to {} network", securityAccessDto.getSecurityGroupIdForKnox(), networkDto.getAws());
                checkSecurityGroupVpc(environmentDto, resultBuilder, environmentDto.getSecurityAccess().getSecurityGroupIdForKnox());
            }
        }
    }
}
Also used : NetworkDto(com.sequenceiq.environment.network.dto.NetworkDto) EnvironmentDto(com.sequenceiq.environment.environment.dto.EnvironmentDto) SecurityAccessDto(com.sequenceiq.environment.environment.dto.SecurityAccessDto)

Aggregations

SecurityAccessDto (com.sequenceiq.environment.environment.dto.SecurityAccessDto)17 Environment (com.sequenceiq.environment.environment.domain.Environment)11 Test (org.junit.jupiter.api.Test)11 ValidationResult (com.sequenceiq.cloudbreak.validation.ValidationResult)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 EnvironmentEditDto (com.sequenceiq.environment.environment.dto.EnvironmentEditDto)5 EnvironmentDto (com.sequenceiq.environment.environment.dto.EnvironmentDto)4 NetworkDto (com.sequenceiq.environment.network.dto.NetworkDto)4 BadRequestException (javax.ws.rs.BadRequestException)3 Network (com.sequenceiq.cloudbreak.cloud.model.Network)2 NetworkCidr (com.sequenceiq.cloudbreak.cloud.network.NetworkCidr)2 CloudPlatform (com.sequenceiq.cloudbreak.common.mappable.CloudPlatform)2 Credential (com.sequenceiq.environment.credential.domain.Credential)2 AuthenticationDto (com.sequenceiq.environment.environment.dto.AuthenticationDto)2 EnvironmentBackup (com.sequenceiq.environment.environment.dto.EnvironmentBackup)2 EnvironmentTelemetry (com.sequenceiq.environment.environment.dto.telemetry.EnvironmentTelemetry)2 AwsNetwork (com.sequenceiq.environment.network.dao.domain.AwsNetwork)2 BaseNetwork (com.sequenceiq.environment.network.dao.domain.BaseNetwork)2 GcpNetwork (com.sequenceiq.environment.network.dao.domain.GcpNetwork)2 EnvironmentNetworkConverter (com.sequenceiq.environment.network.v1.converter.EnvironmentNetworkConverter)2