Search in sources :

Example 16 with IpPermission

use of software.amazon.awssdk.services.ec2.model.IpPermission in project photon-model by vmware.

the class AWSSecurityGroupClient method createRule.

private IpPermission createRule(int fromPort, int toPort, String subnet, String protocol) {
    IpRange ipRange = new IpRange().withCidrIp(subnet);
    protocol = protocol.equals(ALL_TRAFFIC) ? ALL_PROTOCOLS : protocol;
    return new IpPermission().withIpProtocol(protocol).withFromPort(fromPort).withToPort(toPort).withIpv4Ranges(ipRange);
}
Also used : IpRange(com.amazonaws.services.ec2.model.IpRange) IpPermission(com.amazonaws.services.ec2.model.IpPermission)

Example 17 with IpPermission

use of software.amazon.awssdk.services.ec2.model.IpPermission in project photon-model by vmware.

the class AWSSecurityGroupClient method buildRules.

/**
 * Builds the white list rules for the firewall
 */
public List<IpPermission> buildRules(List<Rule> allowRules) {
    ArrayList<IpPermission> awsRules = new ArrayList<>();
    for (Rule rule : allowRules) {
        int fromPort;
        int toPort;
        if (rule.ports.contains("-")) {
            String[] ports = rule.ports.split("-");
            fromPort = Integer.parseInt(ports[0]);
            toPort = Integer.parseInt(ports[1]);
        } else {
            fromPort = Integer.parseInt(rule.ports);
            toPort = fromPort;
        }
        awsRules.add(createRule(fromPort, toPort, rule.ipRangeCidr, rule.protocol));
    }
    return awsRules;
}
Also used : ArrayList(java.util.ArrayList) IpPermission(com.amazonaws.services.ec2.model.IpPermission) Rule(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState.Rule)

Example 18 with IpPermission

use of software.amazon.awssdk.services.ec2.model.IpPermission in project photon-model by vmware.

the class TestProvisionAWSSecurityGroup method isInternalRule.

private boolean isInternalRule(String sgId, List<IpPermission> ipPermissions) {
    boolean isInternalRule = false;
    assertNotNull(ipPermissions);
    for (IpPermission ipPermission : ipPermissions) {
        if (ipPermission.getUserIdGroupPairs() != null) {
            assertEquals(1, ipPermission.getUserIdGroupPairs().size());
            assertEquals(sgId, ipPermission.getUserIdGroupPairs().get(0).getGroupId());
            isInternalRule = true;
            break;
        }
    }
    return isInternalRule;
}
Also used : IpPermission(com.amazonaws.services.ec2.model.IpPermission)

Example 19 with IpPermission

use of software.amazon.awssdk.services.ec2.model.IpPermission in project photon-model by vmware.

the class TestAWSSecurityGroupService method testUpdateIngressRules.

/*
     * Test updating ingress rules with the Security Group Service Allow
     * object
     */
@Test
public void testUpdateIngressRules() throws Throwable {
    String groupID = this.client.createDefaultSecurityGroup(null);
    ArrayList<Rule> rules = TestUtils.getAllowIngressRules();
    this.client.addIngressRules(groupID, this.client.buildRules(rules));
    SecurityGroup awsSG = this.client.getSecurityGroupById(groupID);
    List<IpPermission> ingress = awsSG.getIpPermissions();
    for (IpPermission rule : ingress) {
        assertDefaultRules(rule);
    }
    this.client.deleteSecurityGroup(groupID);
}
Also used : Rule(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState.Rule) IpPermission(com.amazonaws.services.ec2.model.IpPermission) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) Test(org.junit.Test)

Example 20 with IpPermission

use of software.amazon.awssdk.services.ec2.model.IpPermission in project photon-model by vmware.

the class TestAWSSecurityGroupService method validateDefaultRules.

private void validateDefaultRules(List<IpPermission> rules) throws Throwable {
    ArrayList<Integer> ports = new ArrayList<>();
    for (int port : DEFAULT_ALLOWED_PORTS) {
        ports.add(port);
    }
    for (IpPermission rule : rules) {
        assertTrue(rule.getIpProtocol().equalsIgnoreCase(DEFAULT_PROTOCOL));
        if (rule.getFromPort() == 1) {
            assertTrue(rule.getIpv4Ranges().get(0).getCidrIp().equalsIgnoreCase(this.subnet));
            assertTrue(rule.getToPort() == 65535);
        } else {
            assertTrue(rule.getIpv4Ranges().get(0).getCidrIp().equalsIgnoreCase(DEFAULT_ALLOWED_NETWORK));
            assertEquals(rule.getFromPort(), rule.getToPort());
            assertTrue(ports.contains(rule.getToPort()));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IpPermission(com.amazonaws.services.ec2.model.IpPermission)

Aggregations

IpPermission (com.amazonaws.services.ec2.model.IpPermission)21 Test (org.junit.Test)11 AuthorizeSecurityGroupIngressRequest (com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest)8 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)8 ArrayList (java.util.ArrayList)7 CreateSecurityGroupRequest (com.amazonaws.services.ec2.model.CreateSecurityGroupRequest)5 IpRange (com.amazonaws.services.ec2.model.IpRange)5 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)3 GroupIdentifier (com.amazonaws.services.ec2.model.GroupIdentifier)3 Instance (com.amazonaws.services.ec2.model.Instance)3 PhotonModelMetricServices (com.vmware.photon.controller.model.PhotonModelMetricServices)3 PhotonModelServices (com.vmware.photon.controller.model.PhotonModelServices)3 AWS_VM_REQUEST_TIMEOUT_MINUTES (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AWS_VM_REQUEST_TIMEOUT_MINUTES)3 TestAWSSetupUtils.createAWSAuthentication (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSAuthentication)3 TestAWSSetupUtils.createAWSComputeHost (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSComputeHost)3 TestAWSSetupUtils.createAWSResourcePool (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSResourcePool)3 TestAWSSetupUtils.createAWSVMResource (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSVMResource)3 TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client)3 TestAWSSetupUtils.getAwsInstancesByIds (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getAwsInstancesByIds)3 TestAWSSetupUtils.getSecurityGroupsIdUsingEC2Client (com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getSecurityGroupsIdUsingEC2Client)3