@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); }
public static Map<String, Boolean> checkAccessibleToAll( Set<GroupIdentifier> secuityGroups, String portToCheck, String sgRulesUrl, String cidrIp) throws Exception { String responseJson = null; String fromPort = null; String toPort = null; String ipprotocol = null; JsonParser jsonParser; JsonObject resultJson; HashMap<String, Boolean> openPorts = new HashMap<>(); StringBuilder urlToQueryBuffer = new StringBuilder(sgRulesUrl); for (GroupIdentifier securityGrp : secuityGroups) { StringBuilder requestBody = new StringBuilder( "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"groupid.keyword\":{\"value\":\"" + securityGrp.getGroupId() + "\"}}},{\"term\":{\"cidrip.keyword\":{\"value\":\"" + cidrIp + "\"}}},{\"term\":{\"type.keyword\":{\"value\":\"inbound\"}}}]}}}"); try { responseJson = PacmanUtils.doHttpPost( urlToQueryBuffer.toString(), requestBody.toString()); } catch (RuleExecutionFailedExeption e) { throw new RuleExecutionFailedExeption(e.getMessage()); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get( "hits").toString()); JsonArray hitsArray = hitsJson.getAsJsonArray("hits"); logger.info(sgRulesUrl); logger.info(securityGrp.getGroupId()); logger.info(portToCheck); for (int i = 0; i < hitsArray.size(); i++) { JsonObject source = hitsArray.get(i).getAsJsonObject() .get("_source").getAsJsonObject(); fromPort = source.get("fromport").getAsString(); toPort = source.get("toport").getAsString(); ipprotocol = source.get("ipprotocol").getAsString(); logger.info(fromPort); logger.info(toPort); logger.info(ipprotocol); if (!org.apache.commons.lang.StringUtils.isEmpty(fromPort) && !org.apache.commons.lang.StringUtils.isEmpty(toPort)) { String fPort = "FromPort_" + fromPort + "_" + ipprotocol; String tPort = "ToPort_" + toPort + "_" + ipprotocol; openPorts.put(fPort + "-" + tPort, true); } } } return openPorts; }
PacmanEc2Utils { public static Map<String, Boolean> checkAccessibleToAll( Set<GroupIdentifier> secuityGroups, String portToCheck, String sgRulesUrl, String cidrIp) throws Exception { String responseJson = null; String fromPort = null; String toPort = null; String ipprotocol = null; JsonParser jsonParser; JsonObject resultJson; HashMap<String, Boolean> openPorts = new HashMap<>(); StringBuilder urlToQueryBuffer = new StringBuilder(sgRulesUrl); for (GroupIdentifier securityGrp : secuityGroups) { StringBuilder requestBody = new StringBuilder( "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"groupid.keyword\":{\"value\":\"" + securityGrp.getGroupId() + "\"}}},{\"term\":{\"cidrip.keyword\":{\"value\":\"" + cidrIp + "\"}}},{\"term\":{\"type.keyword\":{\"value\":\"inbound\"}}}]}}}"); try { responseJson = PacmanUtils.doHttpPost( urlToQueryBuffer.toString(), requestBody.toString()); } catch (RuleExecutionFailedExeption e) { throw new RuleExecutionFailedExeption(e.getMessage()); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get( "hits").toString()); JsonArray hitsArray = hitsJson.getAsJsonArray("hits"); logger.info(sgRulesUrl); logger.info(securityGrp.getGroupId()); logger.info(portToCheck); for (int i = 0; i < hitsArray.size(); i++) { JsonObject source = hitsArray.get(i).getAsJsonObject() .get("_source").getAsJsonObject(); fromPort = source.get("fromport").getAsString(); toPort = source.get("toport").getAsString(); ipprotocol = source.get("ipprotocol").getAsString(); logger.info(fromPort); logger.info(toPort); logger.info(ipprotocol); if (!org.apache.commons.lang.StringUtils.isEmpty(fromPort) && !org.apache.commons.lang.StringUtils.isEmpty(toPort)) { String fPort = "FromPort_" + fromPort + "_" + ipprotocol; String tPort = "ToPort_" + toPort + "_" + ipprotocol; openPorts.put(fPort + "-" + tPort, true); } } } return openPorts; } }
PacmanEc2Utils { public static Map<String, Boolean> checkAccessibleToAll( Set<GroupIdentifier> secuityGroups, String portToCheck, String sgRulesUrl, String cidrIp) throws Exception { String responseJson = null; String fromPort = null; String toPort = null; String ipprotocol = null; JsonParser jsonParser; JsonObject resultJson; HashMap<String, Boolean> openPorts = new HashMap<>(); StringBuilder urlToQueryBuffer = new StringBuilder(sgRulesUrl); for (GroupIdentifier securityGrp : secuityGroups) { StringBuilder requestBody = new StringBuilder( "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"groupid.keyword\":{\"value\":\"" + securityGrp.getGroupId() + "\"}}},{\"term\":{\"cidrip.keyword\":{\"value\":\"" + cidrIp + "\"}}},{\"term\":{\"type.keyword\":{\"value\":\"inbound\"}}}]}}}"); try { responseJson = PacmanUtils.doHttpPost( urlToQueryBuffer.toString(), requestBody.toString()); } catch (RuleExecutionFailedExeption e) { throw new RuleExecutionFailedExeption(e.getMessage()); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get( "hits").toString()); JsonArray hitsArray = hitsJson.getAsJsonArray("hits"); logger.info(sgRulesUrl); logger.info(securityGrp.getGroupId()); logger.info(portToCheck); for (int i = 0; i < hitsArray.size(); i++) { JsonObject source = hitsArray.get(i).getAsJsonObject() .get("_source").getAsJsonObject(); fromPort = source.get("fromport").getAsString(); toPort = source.get("toport").getAsString(); ipprotocol = source.get("ipprotocol").getAsString(); logger.info(fromPort); logger.info(toPort); logger.info(ipprotocol); if (!org.apache.commons.lang.StringUtils.isEmpty(fromPort) && !org.apache.commons.lang.StringUtils.isEmpty(toPort)) { String fPort = "FromPort_" + fromPort + "_" + ipprotocol; String tPort = "ToPort_" + toPort + "_" + ipprotocol; openPorts.put(fPort + "-" + tPort, true); } } } return openPorts; } private  PacmanEc2Utils(); }
PacmanEc2Utils { public static Map<String, Boolean> checkAccessibleToAll( Set<GroupIdentifier> secuityGroups, String portToCheck, String sgRulesUrl, String cidrIp) throws Exception { String responseJson = null; String fromPort = null; String toPort = null; String ipprotocol = null; JsonParser jsonParser; JsonObject resultJson; HashMap<String, Boolean> openPorts = new HashMap<>(); StringBuilder urlToQueryBuffer = new StringBuilder(sgRulesUrl); for (GroupIdentifier securityGrp : secuityGroups) { StringBuilder requestBody = new StringBuilder( "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"groupid.keyword\":{\"value\":\"" + securityGrp.getGroupId() + "\"}}},{\"term\":{\"cidrip.keyword\":{\"value\":\"" + cidrIp + "\"}}},{\"term\":{\"type.keyword\":{\"value\":\"inbound\"}}}]}}}"); try { responseJson = PacmanUtils.doHttpPost( urlToQueryBuffer.toString(), requestBody.toString()); } catch (RuleExecutionFailedExeption e) { throw new RuleExecutionFailedExeption(e.getMessage()); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get( "hits").toString()); JsonArray hitsArray = hitsJson.getAsJsonArray("hits"); logger.info(sgRulesUrl); logger.info(securityGrp.getGroupId()); logger.info(portToCheck); for (int i = 0; i < hitsArray.size(); i++) { JsonObject source = hitsArray.get(i).getAsJsonObject() .get("_source").getAsJsonObject(); fromPort = source.get("fromport").getAsString(); toPort = source.get("toport").getAsString(); ipprotocol = source.get("ipprotocol").getAsString(); logger.info(fromPort); logger.info(toPort); logger.info(ipprotocol); if (!org.apache.commons.lang.StringUtils.isEmpty(fromPort) && !org.apache.commons.lang.StringUtils.isEmpty(toPort)) { String fPort = "FromPort_" + fromPort + "_" + ipprotocol; String tPort = "ToPort_" + toPort + "_" + ipprotocol; openPorts.put(fPort + "-" + tPort, true); } } } return openPorts; } private  PacmanEc2Utils(); static List<Volume> collectAllVolumes(AmazonEC2 ec2ServiceClient,
            DescribeVolumesRequest request); static List<FlowLog> getFlowLogs(AmazonEC2 ec2ServiceClient,
            DescribeFlowLogsRequest describeFlowLogsRequest); static Filter setFilters(String filterName, String filterValue); static Map<String, Boolean> checkAccessibleToAll(
            Set<GroupIdentifier> secuityGroups, String portToCheck,
            String sgRulesUrl, String cidrIp); }
PacmanEc2Utils { public static Map<String, Boolean> checkAccessibleToAll( Set<GroupIdentifier> secuityGroups, String portToCheck, String sgRulesUrl, String cidrIp) throws Exception { String responseJson = null; String fromPort = null; String toPort = null; String ipprotocol = null; JsonParser jsonParser; JsonObject resultJson; HashMap<String, Boolean> openPorts = new HashMap<>(); StringBuilder urlToQueryBuffer = new StringBuilder(sgRulesUrl); for (GroupIdentifier securityGrp : secuityGroups) { StringBuilder requestBody = new StringBuilder( "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"groupid.keyword\":{\"value\":\"" + securityGrp.getGroupId() + "\"}}},{\"term\":{\"cidrip.keyword\":{\"value\":\"" + cidrIp + "\"}}},{\"term\":{\"type.keyword\":{\"value\":\"inbound\"}}}]}}}"); try { responseJson = PacmanUtils.doHttpPost( urlToQueryBuffer.toString(), requestBody.toString()); } catch (RuleExecutionFailedExeption e) { throw new RuleExecutionFailedExeption(e.getMessage()); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get( "hits").toString()); JsonArray hitsArray = hitsJson.getAsJsonArray("hits"); logger.info(sgRulesUrl); logger.info(securityGrp.getGroupId()); logger.info(portToCheck); for (int i = 0; i < hitsArray.size(); i++) { JsonObject source = hitsArray.get(i).getAsJsonObject() .get("_source").getAsJsonObject(); fromPort = source.get("fromport").getAsString(); toPort = source.get("toport").getAsString(); ipprotocol = source.get("ipprotocol").getAsString(); logger.info(fromPort); logger.info(toPort); logger.info(ipprotocol); if (!org.apache.commons.lang.StringUtils.isEmpty(fromPort) && !org.apache.commons.lang.StringUtils.isEmpty(toPort)) { String fPort = "FromPort_" + fromPort + "_" + ipprotocol; String tPort = "ToPort_" + toPort + "_" + ipprotocol; openPorts.put(fPort + "-" + tPort, true); } } } return openPorts; } private  PacmanEc2Utils(); static List<Volume> collectAllVolumes(AmazonEC2 ec2ServiceClient,
            DescribeVolumesRequest request); static List<FlowLog> getFlowLogs(AmazonEC2 ec2ServiceClient,
            DescribeFlowLogsRequest describeFlowLogsRequest); static Filter setFilters(String filterName, String filterValue); static Map<String, Boolean> checkAccessibleToAll(
            Set<GroupIdentifier> secuityGroups, String portToCheck,
            String sgRulesUrl, String cidrIp);  }