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);
}
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());
}
}
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);
}
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());
}
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());
}
Aggregations