Search in sources :

Example 1 with IpRange

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

the class TestAWSSecurityGroupService method testSecurityGroupRulesCalculation.

/*
     * Test calculating Rule range and Cidr based on the AWS IpPermission configuration
     */
@Test
public void testSecurityGroupRulesCalculation() throws Throwable {
    IpRange ipv4Range = new IpRange();
    ipv4Range.setCidrIp("0.0.0.0/0");
    Ipv6Range ipv6Range = new Ipv6Range();
    ipv6Range.setCidrIpv6("::/0");
    // {IpProtocol: -1,UserIdGroupPairs: [],Ipv6Ranges: [],PrefixListIds: [], Ipv4Ranges: [{CidrIp: 0.0.0.0/0}]}
    // expected output: cidr = "0.0.0.0/0"  range = "1-65535"
    IpPermission mockPermission1 = new IpPermission();
    mockPermission1.setFromPort(-1);
    mockPermission1.setToPort(-1);
    mockPermission1.setIpProtocol("-1");
    mockPermission1.setIpv4Ranges(Collections.singleton(ipv4Range));
    Rule outputRule1 = new Rule();
    outputRule1.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule1, mockPermission1.getIpProtocol());
    outputRule1.ports = AWSSecurityGroupUtils.calculatePorts(outputRule1, mockPermission1);
    outputRule1.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule1, mockPermission1);
    assertEquals("wrong ports range:", SecurityGroupService.ALL_PORTS, outputRule1.ports);
    assertEquals("wrong cidr:", "0.0.0.0/0", outputRule1.ipRangeCidr);
    // {IpProtocol: icmpv6,FromPort: -1,ToPort: -1,UserIdGroupPairs: [], Ipv6Ranges: [{CidrIpv6: ::/0}],
    // PrefixListIds: [],Ipv4Ranges: []}
    // expected output: cidr = "::/0"  range = "1-65535"
    IpPermission mockPermission2 = new IpPermission();
    mockPermission2.setFromPort(-1);
    mockPermission2.setToPort(-1);
    mockPermission2.setIpProtocol("icmpv6");
    mockPermission2.setIpv6Ranges(Collections.singleton(ipv6Range));
    Rule outputRule2 = new Rule();
    outputRule2.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule2, mockPermission2.getIpProtocol());
    outputRule2.ports = AWSSecurityGroupUtils.calculatePorts(outputRule2, mockPermission2);
    outputRule2.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule2, mockPermission2);
    assertEquals("wrong ports range:", SecurityGroupService.ALL_PORTS, outputRule2.ports);
    assertEquals("wrong cidr:", "::/0", outputRule2.ipRangeCidr);
    // {IpProtocol: icmp,FromPort: -1,ToPort: -1,UserIdGroupPairs: [], Ipv6Ranges: [{CidrIpv6: ::/0}],
    // PrefixListIds: [],Ipv4Ranges: []}
    // expected output: cidr = "::/0"  range = "1-65535"
    IpPermission mockPermission3 = new IpPermission();
    mockPermission3.setFromPort(-1);
    mockPermission3.setToPort(-1);
    mockPermission3.setIpProtocol("icmp");
    mockPermission3.setIpv6Ranges(Collections.singleton(ipv6Range));
    Rule outputRule3 = new Rule();
    outputRule3.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule3, mockPermission3.getIpProtocol());
    outputRule3.ports = AWSSecurityGroupUtils.calculatePorts(outputRule3, mockPermission3);
    outputRule3.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule3, mockPermission3);
    assertEquals("wrong ports range:", SecurityGroupService.ALL_PORTS, outputRule3.ports);
    assertEquals("wrong cidr:", "::/0", outputRule3.ipRangeCidr);
    // {IpProtocol: icmp,FromPort: 0,ToPort: -1,UserIdGroupPairs: [], Ipv6Ranges: [],
    // PrefixListIds: [],Ipv4Ranges: [{CidrIp: 0.0.0.0/0}]}
    // expected output: cidr = "0.0.0.0/0" range = "0"
    IpPermission mockPermission4 = new IpPermission();
    mockPermission4.setFromPort(0);
    mockPermission4.setToPort(-1);
    mockPermission4.setIpProtocol("icmp");
    mockPermission4.setIpv4Ranges(Collections.singleton(ipv4Range));
    Rule outputRule4 = new Rule();
    outputRule4.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule4, mockPermission4.getIpProtocol());
    outputRule4.ports = AWSSecurityGroupUtils.calculatePorts(outputRule4, mockPermission4);
    outputRule4.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule4, mockPermission4);
    assertEquals("wrong ports range:", "0", outputRule4.ports);
    assertEquals("wrong cidr:", "0.0.0.0/0", outputRule4.ipRangeCidr);
    // {IpProtocol: icmpv6,FromPort: -1,ToPort: -1,UserIdGroupPairs: [], Ipv6Ranges: [],PrefixListIds: [],
    // Ipv4Ranges: [{CidrIp: 0.0.0.0/0}]}
    // expected output: cidr = "*" range = "1-65535"
    IpPermission mockPermission5 = new IpPermission();
    mockPermission5.setFromPort(-1);
    mockPermission5.setToPort(-1);
    mockPermission5.setIpProtocol("icmpv6");
    mockPermission5.setIpv4Ranges(Collections.singleton(ipv4Range));
    Rule outputRule5 = new Rule();
    outputRule5.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule5, mockPermission5.getIpProtocol());
    outputRule5.ports = AWSSecurityGroupUtils.calculatePorts(outputRule5, mockPermission5);
    outputRule5.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule5, mockPermission5);
    assertEquals("wrong ports range:", SecurityGroupService.ALL_PORTS, outputRule5.ports);
    assertEquals("wrong cidr:", SecurityGroupService.ANY, outputRule5.ipRangeCidr);
    // {IpProtocol: icmp,FromPort: 3,ToPort: 0,UserIdGroupPairs: [], Ipv6Ranges: [],PrefixListIds: [],
    // Ipv4Ranges: [{CidrIp: 0.0.0.0/0}]}
    // expected output: protocol = "ICMPv4" cidr = "0.0.0.0/0" range = "3-0"
    IpPermission mockPermission6 = new IpPermission();
    mockPermission6.setFromPort(3);
    mockPermission6.setToPort(0);
    mockPermission6.setIpProtocol("icmp");
    mockPermission6.setIpv4Ranges(Collections.singleton(ipv4Range));
    Rule outputRule6 = new Rule();
    outputRule6.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule6, mockPermission6.getIpProtocol());
    outputRule6.ports = AWSSecurityGroupUtils.calculatePorts(outputRule6, mockPermission6);
    outputRule6.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule6, mockPermission6);
    assertEquals("wrong ports range:", "3-0", outputRule6.ports);
    assertEquals("wrong cidr:", "0.0.0.0/0", outputRule6.ipRangeCidr);
    // {IpProtocol: icmp,FromPort: 8,ToPort: -1,UserIdGroupPairs: [], Ipv6Ranges: [],PrefixListIds: [],
    // Ipv4Ranges: [{CidrIp: 0.0.0.0/0}]}
    // expected output: cidr = "0.0.0.0/0" range = "8"
    IpPermission mockPermission7 = new IpPermission();
    mockPermission7.setFromPort(8);
    mockPermission7.setToPort(-1);
    mockPermission7.setIpProtocol("icmp");
    mockPermission7.setIpv4Ranges(Collections.singleton(ipv4Range));
    Rule outputRule7 = new Rule();
    outputRule7.protocol = AWSSecurityGroupUtils.calculateProtocol(outputRule7, mockPermission7.getIpProtocol());
    outputRule7.ports = AWSSecurityGroupUtils.calculatePorts(outputRule7, mockPermission7);
    outputRule7.ipRangeCidr = AWSSecurityGroupUtils.calculateIpRangeCidr(outputRule7, mockPermission7);
    assertEquals("wrong ports range:", "8", outputRule7.ports);
    assertEquals("wrong cidr:", "0.0.0.0/0", outputRule7.ipRangeCidr);
}
Also used : IpRange(com.amazonaws.services.ec2.model.IpRange) Ipv6Range(com.amazonaws.services.ec2.model.Ipv6Range) IpPermission(com.amazonaws.services.ec2.model.IpPermission) Rule(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState.Rule) Test(org.junit.Test)

Example 2 with IpRange

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

the class TestAWSSecurityGroupService method testAllocateSecurityGroupUpdate.

/*
     * update an existing security group to the required default ports
     */
@Test
public void testAllocateSecurityGroupUpdate() throws Throwable {
    String groupId = this.client.createDefaultSecurityGroup(null);
    List<IpPermission> rules = new ArrayList<>();
    IpRange ipRange = new IpRange().withCidrIp(DEFAULT_ALLOWED_NETWORK);
    rules.add(new IpPermission().withIpProtocol(DEFAULT_PROTOCOL).withFromPort(22).withToPort(22).withIpv4Ranges(ipRange));
    this.client.addIngressRules(groupId, rules);
    SecurityGroup updatedGroup = this.client.getDefaultSecurityGroup(null);
    validateDefaultRules(updatedGroup.getIpPermissions());
    this.client.deleteSecurityGroup(groupId);
}
Also used : IpRange(com.amazonaws.services.ec2.model.IpRange) ArrayList(java.util.ArrayList) IpPermission(com.amazonaws.services.ec2.model.IpPermission) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) Test(org.junit.Test)

Example 3 with IpRange

use of software.amazon.awssdk.services.ec2.model.IpRange in project aws-doc-sdk-examples by awsdocs.

the class CreateSecurityGroup method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a group name, group description and vpc id\n" + "Ex: CreateSecurityGroup <group-name> <group-description> <vpc-id>\n";
    if (args.length != 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String group_name = args[0];
    String group_desc = args[1];
    String vpc_id = args[2];
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    CreateSecurityGroupRequest create_request = new CreateSecurityGroupRequest().withGroupName(group_name).withDescription(group_desc).withVpcId(vpc_id);
    CreateSecurityGroupResult create_response = ec2.createSecurityGroup(create_request);
    System.out.printf("Successfully created security group named %s", group_name);
    IpRange ip_range = new IpRange().withCidrIp("0.0.0.0/0");
    IpPermission ip_perm = new IpPermission().withIpProtocol("tcp").withToPort(80).withFromPort(80).withIpv4Ranges(ip_range);
    IpPermission ip_perm2 = new IpPermission().withIpProtocol("tcp").withToPort(22).withFromPort(22).withIpv4Ranges(ip_range);
    AuthorizeSecurityGroupIngressRequest auth_request = new AuthorizeSecurityGroupIngressRequest().withGroupName(group_name).withIpPermissions(ip_perm, ip_perm2);
    AuthorizeSecurityGroupIngressResult auth_response = ec2.authorizeSecurityGroupIngress(auth_request);
    System.out.printf("Successfully added ingress policy to security group %s", group_name);
}
Also used : IpRange(com.amazonaws.services.ec2.model.IpRange) AuthorizeSecurityGroupIngressResult(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressResult) CreateSecurityGroupResult(com.amazonaws.services.ec2.model.CreateSecurityGroupResult) AuthorizeSecurityGroupIngressRequest(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) IpPermission(com.amazonaws.services.ec2.model.IpPermission) CreateSecurityGroupRequest(com.amazonaws.services.ec2.model.CreateSecurityGroupRequest)

Example 4 with IpRange

use of software.amazon.awssdk.services.ec2.model.IpRange 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 5 with IpRange

use of software.amazon.awssdk.services.ec2.model.IpRange in project aws-doc-sdk-examples by awsdocs.

the class CreateSecurityGroup method createEC2SecurityGroup.

// snippet-start:[ec2.java2.create_security_group.main]
public static String createEC2SecurityGroup(Ec2Client ec2, String groupName, String groupDesc, String vpcId) {
    try {
        // snippet-start:[ec2.java2.create_security_group.create]
        CreateSecurityGroupRequest createRequest = CreateSecurityGroupRequest.builder().groupName(groupName).description(groupDesc).vpcId(vpcId).build();
        CreateSecurityGroupResponse resp = ec2.createSecurityGroup(createRequest);
        // snippet-end:[ec2.java2.create_security_group.create]
        // snippet-start:[ec2.java2.create_security_group.config]
        IpRange ipRange = IpRange.builder().cidrIp("0.0.0.0/0").build();
        IpPermission ipPerm = IpPermission.builder().ipProtocol("tcp").toPort(80).fromPort(80).ipRanges(ipRange).build();
        IpPermission ipPerm2 = IpPermission.builder().ipProtocol("tcp").toPort(22).fromPort(22).ipRanges(ipRange).build();
        AuthorizeSecurityGroupIngressRequest authRequest = AuthorizeSecurityGroupIngressRequest.builder().groupName(groupName).ipPermissions(ipPerm, ipPerm2).build();
        AuthorizeSecurityGroupIngressResponse authResponse = ec2.authorizeSecurityGroupIngress(authRequest);
        System.out.printf("Successfully added ingress policy to Security Group %s", groupName);
        return resp.groupId();
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : IpRange(software.amazon.awssdk.services.ec2.model.IpRange) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) AuthorizeSecurityGroupIngressRequest(software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressRequest) IpPermission(software.amazon.awssdk.services.ec2.model.IpPermission) AuthorizeSecurityGroupIngressResponse(software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressResponse) CreateSecurityGroupResponse(software.amazon.awssdk.services.ec2.model.CreateSecurityGroupResponse) CreateSecurityGroupRequest(software.amazon.awssdk.services.ec2.model.CreateSecurityGroupRequest)

Aggregations

IpPermission (com.amazonaws.services.ec2.model.IpPermission)5 IpRange (com.amazonaws.services.ec2.model.IpRange)5 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 AuthorizeSecurityGroupIngressRequest (com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest)2 CreateSecurityGroupRequest (com.amazonaws.services.ec2.model.CreateSecurityGroupRequest)2 Test (org.junit.Test)2 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 AuthorizeSecurityGroupIngressResult (com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressResult)1 CreateKeyPairRequest (com.amazonaws.services.ec2.model.CreateKeyPairRequest)1 CreateKeyPairResult (com.amazonaws.services.ec2.model.CreateKeyPairResult)1 CreateSecurityGroupResult (com.amazonaws.services.ec2.model.CreateSecurityGroupResult)1 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)1 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)1 DescribeKeyPairsRequest (com.amazonaws.services.ec2.model.DescribeKeyPairsRequest)1 DescribeKeyPairsResult (com.amazonaws.services.ec2.model.DescribeKeyPairsResult)1 Ipv6Range (com.amazonaws.services.ec2.model.Ipv6Range)1 MonitorInstancesRequest (com.amazonaws.services.ec2.model.MonitorInstancesRequest)1 RebootInstancesRequest (com.amazonaws.services.ec2.model.RebootInstancesRequest)1 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)1 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)1