Search in sources :

Example 21 with SecurityGroup

use of software.amazon.awssdk.services.ec2.model.SecurityGroup in project GNS by MobilityFirst.

the class AWSEC2 method findOrCreateSecurityGroup.

/**
   * Returns an existing security group of the given name or creates one if it does not exist.
   *
   * @param ec2
   * @param name
   * @return the name of the group
   */
public static SecurityGroup findOrCreateSecurityGroup(AmazonEC2 ec2, String name) {
    SecurityGroup result = findSecurityGroup(ec2, name);
    if (result == null) {
        createSecurityGroup(ec2, name);
        System.out.println("Created security group " + name);
    }
    return findSecurityGroup(ec2, name);
}
Also used : SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup)

Example 22 with SecurityGroup

use of software.amazon.awssdk.services.ec2.model.SecurityGroup in project Synapse-Stack-Builder by Sage-Bionetworks.

the class EC2SecuritySetup method describeResources.

public void describeResources() {
    DescribeSecurityGroupsRequest req = new DescribeSecurityGroupsRequest();
    req.setGroupNames(Arrays.asList(config.getElasticSecurityGroupName()));
    DescribeSecurityGroupsResult res = ec2Client.describeSecurityGroups(req);
    if ((res.getSecurityGroups() != null) && res.getSecurityGroups().size() == 1) {
        SecurityGroup grp = res.getSecurityGroups().get(0);
        resources.setElasticBeanstalkEC2SecurityGroup(grp);
        String kpName = config.getStackKeyPairName();
        KeyPairInfo inf = describeKeyPair();
        if (inf != null) {
            resources.setStackKeyPair(inf);
        }
    } else {
        throw new IllegalStateException("Did not find one and ony one EC2 secruity group with the name: " + req.getGroupNames());
    }
}
Also used : DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) KeyPairInfo(com.amazonaws.services.ec2.model.KeyPairInfo) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup)

Example 23 with SecurityGroup

use of software.amazon.awssdk.services.ec2.model.SecurityGroup in project Synapse-Stack-Builder by Sage-Bionetworks.

the class DatabaseSecuritySetupTest method before.

@Before
public void before() throws IOException {
    mockClient = factory.createRDSClient();
    config = TestHelper.createTestConfig("dev");
    elasticSecurityGroup = new SecurityGroup().withGroupName("ec2-security-group-name").withOwnerId("123");
    resources = new GeneratedResources();
    resources.setElasticBeanstalkEC2SecurityGroup(elasticSecurityGroup);
    databaseSecuritySetup = new DatabaseSecuritySetup(factory, config, resources);
}
Also used : SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) DBSecurityGroup(com.amazonaws.services.rds.model.DBSecurityGroup) Before(org.junit.Before)

Example 24 with SecurityGroup

use of software.amazon.awssdk.services.ec2.model.SecurityGroup in project Synapse-Stack-Builder by Sage-Bionetworks.

the class EC2SecuritySetupTest method testTeardownResources.

@Test
public void testTeardownResources() {
    resources.setElasticBeanstalkEC2SecurityGroup(new SecurityGroup().withGroupName(config.getElasticSecurityGroupName()));
    DeleteSecurityGroupRequest req = new DeleteSecurityGroupRequest().withGroupName(resources.getElasticBeanstalkEC2SecurityGroup().getGroupName());
    ec2SecuritySetup.teardownResources();
    assertNotNull(resources.getElasticBeanstalkEC2SecurityGroup());
}
Also used : DeleteSecurityGroupRequest(com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) Test(org.junit.Test)

Example 25 with SecurityGroup

use of software.amazon.awssdk.services.ec2.model.SecurityGroup in project Synapse-Stack-Builder by Sage-Bionetworks.

the class EC2SecuritySetupTest method testSetupElasticBeanstalkEC2SecutiryGroup.

@Test
public void testSetupElasticBeanstalkEC2SecutiryGroup() {
    String expectedDescription = config.getElasticSecurityGroupDescription();
    String expectedGroupName = config.getElasticSecurityGroupName();
    DescribeSecurityGroupsResult result = new DescribeSecurityGroupsResult();
    SecurityGroup expectedGroup = new SecurityGroup().withGroupName(expectedGroupName).withOwnerId("123");
    result.withSecurityGroups(expectedGroup);
    when(mockEC2Client.describeSecurityGroups(any(DescribeSecurityGroupsRequest.class))).thenReturn(result);
    DescribeKeyPairsResult kpr = new DescribeKeyPairsResult().withKeyPairs(new KeyPairInfo().withKeyName("123"));
    when(mockEC2Client.describeKeyPairs(any(DescribeKeyPairsRequest.class))).thenReturn(kpr);
    // Create the security group.
    ec2SecuritySetup.setupResources();
    SecurityGroup group = resources.getElasticBeanstalkEC2SecurityGroup();
    assertEquals(expectedGroup, group);
    String groupName = group.getGroupName();
    assertNotNull(groupName);
    assertEquals(expectedGroupName, groupName);
    CreateSecurityGroupRequest groupRequest = new CreateSecurityGroupRequest(expectedGroupName, expectedDescription);
    // The create group should be called
    verify(mockEC2Client).createSecurityGroup(groupRequest);
    // Three permission should be set
    // http
    List<IpPermission> list = new LinkedList<IpPermission>();
    list.add(new IpPermission().withIpProtocol(IP_PROTOCOL_TCP).withFromPort(PORT_HTTP).withToPort(PORT_HTTP).withIpRanges(CIDR_ALL_IP));
    AuthorizeSecurityGroupIngressRequest request = new AuthorizeSecurityGroupIngressRequest(groupName, list);
    verify(mockEC2Client).authorizeSecurityGroupIngress(request);
    // https
    list = new LinkedList<IpPermission>();
    list.add(new IpPermission().withIpProtocol(IP_PROTOCOL_TCP).withFromPort(PORT_HTTPS).withToPort(PORT_HTTPS).withIpRanges(CIDR_ALL_IP));
    request = new AuthorizeSecurityGroupIngressRequest(groupName, list);
    verify(mockEC2Client).authorizeSecurityGroupIngress(request);
    // ssh
    list = new LinkedList<IpPermission>();
    list.add(new IpPermission().withIpProtocol(IP_PROTOCOL_TCP).withFromPort(PORT_SSH).withToPort(PORT_SSH).withIpRanges(config.getCIDRForSSH()));
    request = new AuthorizeSecurityGroupIngressRequest(groupName, list);
    verify(mockEC2Client).authorizeSecurityGroupIngress(request);
    // Make sure this is set
    assertNotNull(resources.getElasticBeanstalkEC2SecurityGroup());
}
Also used : DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) KeyPairInfo(com.amazonaws.services.ec2.model.KeyPairInfo) DescribeKeyPairsRequest(com.amazonaws.services.ec2.model.DescribeKeyPairsRequest) DescribeKeyPairsResult(com.amazonaws.services.ec2.model.DescribeKeyPairsResult) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) CreateSecurityGroupRequest(com.amazonaws.services.ec2.model.CreateSecurityGroupRequest) LinkedList(java.util.LinkedList) AuthorizeSecurityGroupIngressRequest(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest) IpPermission(com.amazonaws.services.ec2.model.IpPermission) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult) Test(org.junit.Test)

Aggregations

SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)31 DescribeSecurityGroupsResult (com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)12 DescribeSecurityGroupsRequest (com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest)11 Test (org.junit.Test)10 IpPermission (com.amazonaws.services.ec2.model.IpPermission)9 ArrayList (java.util.ArrayList)8 Instance (com.amazonaws.services.ec2.model.Instance)7 HashMap (java.util.HashMap)7 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)6 SecurityGroupState (com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)6 Operation (com.vmware.xenon.common.Operation)6 Utils (com.vmware.xenon.common.Utils)6 Map (java.util.Map)6 Filter (com.amazonaws.services.ec2.model.Filter)5 Collections (java.util.Collections)5 List (java.util.List)5 TimeUnit (java.util.concurrent.TimeUnit)5 Level (java.util.logging.Level)5 Collectors (java.util.stream.Collectors)5 Before (org.junit.Before)5