Search in sources :

Example 6 with SecurityRule

use of com.sequenceiq.cloudbreak.cloud.model.SecurityRule in project cloudbreak by hortonworks.

the class StackToCloudStackConverter method buildSecurity.

private Security buildSecurity(InstanceGroup ig) {
    List<SecurityRule> rules = new ArrayList<>();
    if (ig.getSecurityGroup() == null) {
        return new Security(rules, null);
    }
    Long id = ig.getSecurityGroup().getId();
    List<com.sequenceiq.cloudbreak.domain.SecurityRule> securityRules = securityRuleRepository.findAllBySecurityGroupId(id);
    for (com.sequenceiq.cloudbreak.domain.SecurityRule securityRule : securityRules) {
        List<PortDefinition> portDefinitions = new ArrayList<>();
        for (String actualPort : securityRule.getPorts()) {
            String[] segments = actualPort.split("-");
            if (segments.length > 1) {
                portDefinitions.add(new PortDefinition(segments[0], segments[1]));
            } else {
                portDefinitions.add(new PortDefinition(segments[0], segments[0]));
            }
        }
        rules.add(new SecurityRule(securityRule.getCidr(), portDefinitions.toArray(new PortDefinition[portDefinitions.size()]), securityRule.getProtocol()));
    }
    return new Security(rules, ig.getSecurityGroup().getSecurityGroupId());
}
Also used : PortDefinition(com.sequenceiq.cloudbreak.cloud.model.PortDefinition) ArrayList(java.util.ArrayList) SecurityRule(com.sequenceiq.cloudbreak.cloud.model.SecurityRule) Security(com.sequenceiq.cloudbreak.cloud.model.Security)

Example 7 with SecurityRule

use of com.sequenceiq.cloudbreak.cloud.model.SecurityRule in project cloudbreak by hortonworks.

the class GcpFirewallInResourceBuilder method getSourceRanges.

private List<String> getSourceRanges(Security security) {
    List<SecurityRule> rules = security.getRules();
    List<String> sourceRanges = new ArrayList<>(rules.size());
    for (SecurityRule securityRule : rules) {
        sourceRanges.add(securityRule.getCidr());
    }
    return sourceRanges;
}
Also used : ArrayList(java.util.ArrayList) SecurityRule(com.sequenceiq.cloudbreak.cloud.model.SecurityRule)

Example 8 with SecurityRule

use of com.sequenceiq.cloudbreak.cloud.model.SecurityRule in project cloudbreak by hortonworks.

the class HeatTemplateBuilderTest method setup.

@Before
public void setup() throws IOException, TemplateException {
    initMocks(this);
    FreeMarkerConfigurationFactoryBean factoryBean = new FreeMarkerConfigurationFactoryBean();
    factoryBean.setPreferFileSystemAccess(false);
    factoryBean.setTemplateLoaderPath("classpath:/");
    factoryBean.afterPropertiesSet();
    Configuration configuration = factoryBean.getObject();
    ReflectionTestUtils.setField(heatTemplateBuilder, "freemarkerConfiguration", configuration);
    ReflectionTestUtils.setField(heatTemplateBuilder, "openStackHeatTemplatePath", templatePath);
    stackName = "testStack";
    groups = new ArrayList<>(1);
    String name = "master";
    List<Volume> volumes = Arrays.asList(new Volume("/hadoop/fs1", "HDD", 1), new Volume("/hadoop/fs2", "HDD", 1));
    InstanceTemplate instanceTemplate = new InstanceTemplate("m1.medium", name, 0L, volumes, InstanceStatus.CREATE_REQUESTED, new HashMap<>(), 0L);
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    CloudInstance instance = new CloudInstance("SOME_ID", instanceTemplate, instanceAuthentication);
    List<SecurityRule> rules = Collections.singletonList(new SecurityRule("0.0.0.0/0", new PortDefinition[] { new PortDefinition("22", "22"), new PortDefinition("443", "443") }, "tcp"));
    Security security = new Security(rules, null);
    groups.add(new Group(name, InstanceGroupType.CORE, Collections.singletonList(instance), security, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey()));
    Map<InstanceGroupType, String> userData = ImmutableMap.of(InstanceGroupType.CORE, "CORE", InstanceGroupType.GATEWAY, "GATEWAY");
    Map<String, String> tags = new HashMap<>();
    tags.put(CloudbreakResourceType.DISK.templateVariable(), CloudbreakResourceType.DISK.key());
    tags.put(CloudbreakResourceType.INSTANCE.templateVariable(), CloudbreakResourceType.INSTANCE.key());
    tags.put(CloudbreakResourceType.IP.templateVariable(), CloudbreakResourceType.IP.key());
    tags.put(CloudbreakResourceType.NETWORK.templateVariable(), CloudbreakResourceType.NETWORK.key());
    tags.put(CloudbreakResourceType.SECURITY.templateVariable(), CloudbreakResourceType.SECURITY.key());
    tags.put(CloudbreakResourceType.STORAGE.templateVariable(), CloudbreakResourceType.STORAGE.key());
    tags.put(CloudbreakResourceType.TEMPLATE.templateVariable(), CloudbreakResourceType.TEMPLATE.key());
    when(defaultCostTaggingService.prepareInstanceTagging()).thenReturn(tags);
    image = new Image("cb-centos66-amb200-2015-05-25", userData, "redhat6", "url", "default", null);
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) PortDefinition(com.sequenceiq.cloudbreak.cloud.model.PortDefinition) FreeMarkerConfigurationFactoryBean(org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean) Configuration(freemarker.template.Configuration) InstanceGroupType(com.sequenceiq.cloudbreak.api.model.InstanceGroupType) HashMap(java.util.HashMap) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Matchers.containsString(org.hamcrest.Matchers.containsString) SecurityRule(com.sequenceiq.cloudbreak.cloud.model.SecurityRule) Security(com.sequenceiq.cloudbreak.cloud.model.Security) Image(com.sequenceiq.cloudbreak.cloud.model.Image) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Before(org.junit.Before)

Example 9 with SecurityRule

use of com.sequenceiq.cloudbreak.cloud.model.SecurityRule in project cloudbreak by hortonworks.

the class OpenStackSecurityGroupResourceBuilder method build.

@Override
public CloudResource build(OpenStackContext context, AuthenticatedContext auth, Group group, Network network, Security security, CloudResource resource) {
    try {
        OSClient<?> osClient = createOSClient(auth);
        ComputeSecurityGroupService securityGroupService = osClient.compute().securityGroups();
        SecGroupExtension securityGroup = securityGroupService.create(resource.getName(), "");
        String securityGroupId = securityGroup.getId();
        for (SecurityRule rule : security.getRules()) {
            IPProtocol osProtocol = getProtocol(rule.getProtocol());
            String cidr = rule.getCidr();
            for (PortDefinition portStr : rule.getPorts()) {
                int from = Integer.parseInt(portStr.getFrom());
                int to = Integer.parseInt(portStr.getTo());
                securityGroupService.createRule(createRule(securityGroupId, osProtocol, cidr, from, to));
            }
        }
        NeutronNetworkView neutronView = new NeutronNetworkView(network);
        String subnetCidr = neutronView.isExistingSubnet() ? utils.getExistingSubnetCidr(auth, neutronView) : network.getSubnet().getCidr();
        securityGroupService.createRule(createRule(securityGroupId, IPProtocol.TCP, subnetCidr, MIN_PORT, MAX_PORT));
        securityGroupService.createRule(createRule(securityGroupId, IPProtocol.UDP, subnetCidr, MIN_PORT, MAX_PORT));
        securityGroupService.createRule(createRule(securityGroupId, IPProtocol.ICMP, "0.0.0.0/0"));
        return createPersistedResource(resource, group.getName(), securityGroup.getId());
    } catch (OS4JException ex) {
        throw new OpenStackResourceException("SecurityGroup creation failed", resourceType(), resource.getName(), ex);
    }
}
Also used : PortDefinition(com.sequenceiq.cloudbreak.cloud.model.PortDefinition) SecGroupExtension(org.openstack4j.model.compute.SecGroupExtension) ComputeSecurityGroupService(org.openstack4j.api.compute.ComputeSecurityGroupService) IPProtocol(org.openstack4j.model.compute.IPProtocol) OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) SecurityRule(com.sequenceiq.cloudbreak.cloud.model.SecurityRule) NeutronNetworkView(com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView) OS4JException(org.openstack4j.api.exceptions.OS4JException)

Aggregations

SecurityRule (com.sequenceiq.cloudbreak.cloud.model.SecurityRule)9 PortDefinition (com.sequenceiq.cloudbreak.cloud.model.PortDefinition)8 Security (com.sequenceiq.cloudbreak.cloud.model.Security)6 InstanceGroupType (com.sequenceiq.cloudbreak.api.model.InstanceGroupType)5 Image (com.sequenceiq.cloudbreak.cloud.model.Image)5 Volume (com.sequenceiq.cloudbreak.cloud.model.Volume)5 ArrayList (java.util.ArrayList)5 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)4 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)4 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)4 Before (org.junit.Before)4 Group (com.sequenceiq.cloudbreak.cloud.model.Group)3 Configuration (freemarker.template.Configuration)3 HashMap (java.util.HashMap)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 FreeMarkerConfigurationFactoryBean (org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean)3 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)2 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)2 Network (com.sequenceiq.cloudbreak.cloud.model.Network)2 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)2