Java Code Examples for com.amazonaws.services.ec2.model.GroupIdentifier#setGroupId()

The following examples show how to use com.amazonaws.services.ec2.model.GroupIdentifier#setGroupId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PacmanUtils.java    From pacbot with Apache License 2.0 6 votes vote down vote up
public static List<GroupIdentifier> getSecurityGroupsByInstanceId(String instanceId, String esUrl) throws Exception {
    List<GroupIdentifier> list = new ArrayList<>();
    JsonParser jsonParser = new JsonParser();
    Map<String, Object> mustFilter = new HashMap<>();
    Map<String, Object> mustNotFilter = new HashMap<>();
    HashMultimap<String, Object> shouldFilter = HashMultimap.create();
    Map<String, Object> mustTermsFilter = new HashMap<>();
    mustFilter.put(convertAttributetoKeyword(PacmanRuleConstants.INSTANCEID), instanceId);
    JsonObject resultJson = RulesElasticSearchRepositoryUtil.getQueryDetailsFromES(esUrl, mustFilter,
            mustNotFilter, shouldFilter, null, 0, mustTermsFilter, null,null);
    if (resultJson != null && resultJson.has(PacmanRuleConstants.HITS)) {
        JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(PacmanRuleConstants.HITS).toString());
        JsonArray hitsArray = hitsJson.getAsJsonArray(PacmanRuleConstants.HITS);
        for (int i = 0; i < hitsArray.size(); i++) {
            JsonObject source = hitsArray.get(i).getAsJsonObject().get(PacmanRuleConstants.SOURCE)
                    .getAsJsonObject();
            String securitygroupid = source.get(PacmanRuleConstants.EC2_WITH_SECURITYGROUP_ID).getAsString();
            GroupIdentifier groupIdentifier = new GroupIdentifier();
            if (!com.amazonaws.util.StringUtils.isNullOrEmpty(securitygroupid)) {
                groupIdentifier.setGroupId(securitygroupid);
                list.add(groupIdentifier);
            }
        }
    }
    return list;
}
 
Example 2
Source File: PacmanEc2UtilsTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("static-access")
@Test
public void checkAccessibleToAllTest() throws Exception {
    GroupIdentifier identifier = new GroupIdentifier();
    identifier.setGroupId("sg-5414b52c");
    Set<GroupIdentifier> secuityGroups = new HashSet<GroupIdentifier>();
    secuityGroups.add(identifier);
    mockStatic(PacmanUtils.class);
    when(PacmanUtils.doHttpPost(anyString(),anyString())).thenReturn("{\"took\":67,\"timed_out\":false,\"_shards\":{\"total\":3,\"successful\":3,\"failed\":0},\"hits\":{\"total\":1,\"max_score\":12.365102,\"hits\":[{\"_index\":\"_index\",\"_type\":\"_type\",\"_id\":\"_id\",\"_score\":12.365102,\"_routing\":\"_routing\",\"_parent\":\"_parent\",\"_source\":{\"discoverydate\":\"2018-07-31 08:00:00+00\",\"accountid\":\"accountid\",\"region\":\"region\",\"groupid\":\"groupid\",\"type\":\"inbound\",\"ipprotocol\":\"tcp\",\"fromport\":\"80\",\"toport\":\"80\",\"cidrip\":\"0.0.0.0\0\",\"cidripv6\":\"\",\"accountname\":\"accountname\",\"_loaddate\":\"2018-07-31 9:24:00+0000\"}}]}}");
    
    assertThat(pacmanEc2Utils.checkAccessibleToAll(secuityGroups,"11","url","describeFlowLogsRequest"),is(notNullValue()));
    
    when(PacmanUtils.doHttpPost(anyString(),anyString())).thenThrow(new RuleExecutionFailedExeption());
    assertThatThrownBy( 
            () -> pacmanEc2Utils.checkAccessibleToAll(secuityGroups,"11","url","describeFlowLogsRequest")).isInstanceOf(RuleExecutionFailedExeption.class);
}
 
Example 3
Source File: PacmanUtils.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the security grouplist.
 *
 * @param securityGroupId the security group id
 * @param delimeter the delimeter
 * @param securityGrouplist the security grouplist
 * @return the security grouplist
 */
public static List<GroupIdentifier> getSecurityGrouplist(String securityGroupId, String delimeter,
        List<GroupIdentifier> securityGrouplist) {
    List<String> sgList = new ArrayList(Arrays.asList(securityGroupId.split(delimeter)));
    for (String sg : sgList) {
        GroupIdentifier groupIdentifier = new GroupIdentifier();
        groupIdentifier.setGroupId(sg);
        securityGrouplist.add(groupIdentifier);
    }
    return securityGrouplist;
}
 
Example 4
Source File: PacmanUtils.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the security groups by resource id.
 *
 * @param resourceId the resourceId
 * @param esUrl the es url
 * @param resourceField the resource field
 * @param sgField the sgField
 * @param sgStatusField the sgStatusField
 * @return the security groups by resource id
 * @throws Exception the exception
 */
public static List<GroupIdentifier> getSecurityGroupsByResourceId(String resourceId, String esUrl,String resourceField,String sgField,String sgStatusField) throws Exception {
    List<GroupIdentifier> list = new ArrayList<>();
    JsonParser jsonParser = new JsonParser();
    Map<String, Object> mustFilter = new HashMap<>();
    Map<String, Object> mustNotFilter = new HashMap<>();
    HashMultimap<String, Object> shouldFilter = HashMultimap.create();
    Map<String, Object> mustTermsFilter = new HashMap<>();
    mustFilter.put(convertAttributetoKeyword(resourceField), resourceId);
    JsonObject resultJson = RulesElasticSearchRepositoryUtil.getQueryDetailsFromES(esUrl, mustFilter,
            mustNotFilter, shouldFilter, null, 0, mustTermsFilter, null,null);
    if (resultJson != null && resultJson.has(PacmanRuleConstants.HITS)) {
        JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(PacmanRuleConstants.HITS).toString());
        JsonArray hitsArray = hitsJson.getAsJsonArray(PacmanRuleConstants.HITS);
        for (int i = 0; i < hitsArray.size(); i++) {
            JsonObject source = hitsArray.get(i).getAsJsonObject().get(PacmanRuleConstants.SOURCE)
                    .getAsJsonObject();
            String securitygroupid = source.get(sgField).getAsString();
            String vpcSecuritygroupStatus = source.get(sgStatusField).getAsString();
            if("active".equals(vpcSecuritygroupStatus)){
            GroupIdentifier groupIdentifier = new GroupIdentifier();
            if (!com.amazonaws.util.StringUtils.isNullOrEmpty(securitygroupid)) {
                groupIdentifier.setGroupId(securitygroupid);
                list.add(groupIdentifier);
            }
        }
        }
    }
    return list;
}
 
Example 5
Source File: CommonTestUtils.java    From pacbot with Apache License 2.0 4 votes vote down vote up
public static GroupIdentifier getGroupIdentifier(String groupId){
    GroupIdentifier groupIdentifier = new GroupIdentifier();
    groupIdentifier.setGroupId(groupId);
    return groupIdentifier;
}
 
Example 6
Source File: CommonTestUtils.java    From pacbot with Apache License 2.0 4 votes vote down vote up
public static GroupIdentifier getGroupIdentifier(String groupId){
    GroupIdentifier groupIdentifier = new GroupIdentifier();
    groupIdentifier.setGroupId(groupId);
    return groupIdentifier;
}