.withGroupNames(securityGroupName);
@Override public Collection<IpRule> getRules( final String name, final boolean inbound ) { DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest().withGroupNames( name ); DescribeSecurityGroupsResult result = client.describeSecurityGroups( request ); if( result.getSecurityGroups().size() != 1 ) { return null; } Collection<IpRule> ipRules = new ArrayList<IpRule>(); List<IpPermission> permissions; if( inbound ) { permissions = result.getSecurityGroups().get( 0 ).getIpPermissions(); } else { permissions = result.getSecurityGroups().get( 0 ).getIpPermissionsEgress(); } for( IpPermission permission : permissions ) { ipRules.add( toIpRule( permission ) ); } return ipRules; }
.withGroupNames( Collections.singletonList(config.getACLGroupName())); DescribeSecurityGroupsResult result = client.describeSecurityGroups(req);
.withGroupNames(securityGroupName);
/** * * @param name * @param ec2Client * @return */ public SecurityGroup getSecurityGroupForName(String name, AmazonEC2 ec2Client) { SecurityGroup group = null; try { DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest() .withGroupNames(name); DescribeSecurityGroupsResult result = ec2Client.describeSecurityGroups(request); if (result != null && result.getSecurityGroups() != null) { group = result.getSecurityGroups().get(0); } } catch (AmazonServiceException e) { log.warn("Could not find Security Group with name " + name, e); if (!e.getErrorCode().equals("InvalidGroup.NotFound")) { throw e; } } return group; }
/** * This will return the owner id of the given connection to EC2 by describing the "default" * security group. This is the only reliable way of getting the owner id since there is * no way to describe the value in the SDK * * @param ec2Client * @return ownerId - the ID of the current user (as determined by the credentials the * ec2Client was made with) */ public String getCurrentOwnerId(AmazonEC2 ec2Client) { String ownerId = null; DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest() .withGroupNames("default"); DescribeSecurityGroupsResult result = ec2Client.describeSecurityGroups(request); if (result != null) { // there should always be a "default" security group if there is a vpc, but there may not be a vpc if (result.getSecurityGroups() != null) { ownerId = result.getSecurityGroups().get(0).getOwnerId(); } } return ownerId; }
request.withGroupNames(DEFAULT_SECURITY_GROUP_NAME);
/** * List SG ACL's */ public List<String> listACL(int from, int to) { AmazonEC2 client = null; try { client = getEc2Client(); List<String> ipPermissions = new ArrayList<String>(); DescribeSecurityGroupsRequest req = new DescribeSecurityGroupsRequest().withGroupNames(Arrays.asList(config.getACLGroupName())); DescribeSecurityGroupsResult result = client.describeSecurityGroups(req); for (SecurityGroup group : result.getSecurityGroups()) { for (IpPermission perm : group.getIpPermissions()) { if (perm.getFromPort() == from && perm.getToPort() == to) { ipPermissions.addAll(perm.getIpRanges()); } } } return ipPermissions; } finally { if (client != null) client.shutdown(); } }