Search in sources :

Example 26 with SecurityGroup

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

the class AWSUtils method getOrCreateDefaultSecurityGroup.

public static List<String> getOrCreateDefaultSecurityGroup(AmazonEC2AsyncClient amazonEC2Client, AWSNicContext nicCtx) {
    AWSSecurityGroupClient client = new AWSSecurityGroupClient(amazonEC2Client);
    // in case no group is configured in the properties, attempt to discover the default one
    if (nicCtx != null && nicCtx.vpc != null) {
        try {
            SecurityGroup group = client.getSecurityGroup(DEFAULT_SECURITY_GROUP_NAME, nicCtx.vpc.getVpcId());
            if (group != null) {
                return Arrays.asList(group.getGroupId());
            }
        } catch (AmazonServiceException t) {
            if (!t.getMessage().contains(DEFAULT_SECURITY_GROUP_NAME)) {
                throw t;
            }
        }
    }
    // if the group doesn't exist an exception is thrown. We won't throw a
    // missing group exception
    // we will continue and create the group
    String groupId = client.createDefaultSecurityGroupWithDefaultRules(nicCtx.vpc);
    return Collections.singletonList(groupId);
}
Also used : AWSSecurityGroupClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient) AmazonServiceException(com.amazonaws.AmazonServiceException) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup)

Example 27 with SecurityGroup

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

the class AWSSecurityGroupClient method getSecurityGroupById.

public SecurityGroup getSecurityGroupById(String groupId) {
    SecurityGroup cellGroup = null;
    DescribeSecurityGroupsRequest req = new DescribeSecurityGroupsRequest().withGroupIds(groupId);
    DescribeSecurityGroupsResult cellGroups = this.client.describeSecurityGroups(req);
    if (cellGroups != null) {
        cellGroup = cellGroups.getSecurityGroups().get(0);
    }
    return cellGroup;
}
Also used : DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)

Example 28 with SecurityGroup

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

the class TestAWSSetupUtils method deleteSecurityGroupUsingEC2Client.

public static void deleteSecurityGroupUsingEC2Client(AmazonEC2AsyncClient client, VerificationHost host, String awsGroupId) {
    host.log(Level.INFO, "Starting to delete aws Security group with id %s", awsGroupId);
    if (awsGroupId == null) {
        return;
    }
    try {
        DeleteSecurityGroupRequest deleteSecurityGroupRequest = new DeleteSecurityGroupRequest().withGroupId(awsGroupId);
        client.deleteSecurityGroup(deleteSecurityGroupRequest);
        host.waitFor("Timeout waiting for AWS to delete a SecurityGroup with name " + awsGroupId, () -> {
            // Check if the SG is actually not present on AWS after the delete operation
            SecurityGroup discoveredSGOnAWS = getSecurityGroupsIdUsingEC2Client(client, awsGroupId);
            if (discoveredSGOnAWS != null) {
                // Requested SG was not deleted from AWS
                return false;
            }
            host.log("Deleted SG with id: %s", awsGroupId);
            return true;
        });
    } catch (Exception e) {
        String message = e.getMessage();
        if (!message.contains("The security group '" + awsGroupId + "' already exists")) {
            throw e;
        }
    }
}
Also used : DeleteSecurityGroupRequest(com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) UnknownHostException(java.net.UnknownHostException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) TimeoutException(java.util.concurrent.TimeoutException)

Example 29 with SecurityGroup

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

the class TestProvisionAWSSecurityGroup method validateAWSArtifacts.

private void validateAWSArtifacts(String securityGroupDescriptionLink, AuthCredentialsServiceState creds) throws Throwable {
    SecurityGroupState securityGroup = getSecurityGroupState(securityGroupDescriptionLink);
    AWSSecurityGroupClient client = new AWSSecurityGroupClient(AWSUtils.getAsyncClient(creds, this.region, getExecutor()));
    // if any artifact is not present then an error will be thrown
    SecurityGroup sg = client.getSecurityGroupById(securityGroup.customProperties.get(AWSSecurityGroupService.SECURITY_GROUP_ID));
    assertNotNull(sg);
    assertNotNull(sg.getIpPermissions());
    assertTrue(sg.getIpPermissions().size() == 2);
    // check that there is a rule that enables internal communication
    assertTrue(isInternalRule(sg.getGroupId(), sg.getIpPermissions()));
    assertNotNull(sg.getIpPermissionsEgress());
    // there are two egress rules (one that was added as part of this test, and the default one)
    assertTrue(sg.getIpPermissionsEgress().size() == 2);
    // check that there is a rule that enables internal communication
    assertTrue(isInternalRule(sg.getGroupId(), sg.getIpPermissionsEgress()));
}
Also used : SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) AWSSecurityGroupClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup)

Example 30 with SecurityGroup

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

the class TestAWSSecurityGroupService method testAllocateSecurityGroup.

/*
     * create a new security group via the allocation method
     */
@Test
public void testAllocateSecurityGroup() throws Throwable {
    this.client.createDefaultSecurityGroup(null);
    SecurityGroup group = this.client.getDefaultSecurityGroup(null);
    validateDefaultRules(group.getIpPermissions());
    this.client.deleteSecurityGroup(group.getGroupId());
}
Also used : SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) 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