@Test public void executeTest() throws Exception { mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn( true); when(PacmanUtils.formatUrl(anyObject(),anyString())).thenReturn("host"); when(PacmanUtils.calculateLaunchedDuration(anyObject())).thenReturn(10l); when(PacmanUtils.checkInstanceIdFromElasticSearchForQualys(anyString(),anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getMapObject("123")); assertThat(ec2InstanceScannedByQualysRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.checkInstanceIdFromElasticSearchForQualys(anyString(),anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getEmptyMapObject()); assertThat(ec2InstanceScannedByQualysRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.checkInstanceIdFromElasticSearchForQualys(anyString(),anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getMapObject("123")); assertThat(ec2InstanceScannedByQualysRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getFinalKernelReleaseAnotherMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.checkInstanceIdFromElasticSearchForQualys(anyString(),anyString(),anyString(),anyString())).thenThrow(new Exception()); assertThatThrownBy( () -> ec2InstanceScannedByQualysRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(RuleExecutionFailedExeption.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> ec2InstanceScannedByQualysRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); }
public RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========ResourceScannedByQualysRule started========="); Annotation annotation = null; String instanceId = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String target = ruleParam.get(PacmanRuleConstants.TARGET); String firstDiscoveredOn = resourceAttributes.get(PacmanRuleConstants.FIRST_DISCOVERED_ON); String discoveredDaysRange = ruleParam.get(PacmanRuleConstants.DISCOVERED_DAYS_RANGE); if(!StringUtils.isNullOrEmpty(firstDiscoveredOn)){ firstDiscoveredOn= firstDiscoveredOn.substring(0,PacmanRuleConstants.FIRST_DISCOVERED_DATE_FORMAT_LENGTH); } String qualysApi = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_QUALYS_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ qualysApi = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(severity,category,qualysApi,target,discoveredDaysRange)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<LinkedHashMap<String,Object>> issueList = new ArrayList<>(); LinkedHashMap<String,Object> issue = new LinkedHashMap<>(); Gson gson = new Gson(); if (resourceAttributes != null && (PacmanRuleConstants.RUNNING_STATE.equalsIgnoreCase(resourceAttributes.get(PacmanRuleConstants.STATE_NAME)) || PacmanRuleConstants.RUNNING_STATE.equalsIgnoreCase(resourceAttributes.get(PacmanRuleConstants.STATUS)))) { String entityType = resourceAttributes.get(PacmanRuleConstants.ENTITY_TYPE); instanceId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.RESOURCE_ID)); if(PacmanUtils.calculateLaunchedDuration(firstDiscoveredOn)>Long.parseLong(discoveredDaysRange)){ Map<String,Object> ec2ScannesByQualysMap = new HashMap<>(); try{ ec2ScannesByQualysMap = PacmanUtils.checkInstanceIdFromElasticSearchForQualys(instanceId,qualysApi,"_resourceid",target); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!ec2ScannesByQualysMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,""+entityType+" instance not scanned by qualys found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, ""+entityType+" instance not scanned by qualys found"); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "_resourceid,"+PacmanRuleConstants.LAST_VULN_SCAN); issue.put(PacmanRuleConstants.FAILED_REASON_QUALYS, gson.toJson(ec2ScannesByQualysMap)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========ResourceScannedByQualysRule ended with annotation {} : =========" ,annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } } logger.debug("========ResourceScannedByQualysRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }
ResourceScannedByQualysRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========ResourceScannedByQualysRule started========="); Annotation annotation = null; String instanceId = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String target = ruleParam.get(PacmanRuleConstants.TARGET); String firstDiscoveredOn = resourceAttributes.get(PacmanRuleConstants.FIRST_DISCOVERED_ON); String discoveredDaysRange = ruleParam.get(PacmanRuleConstants.DISCOVERED_DAYS_RANGE); if(!StringUtils.isNullOrEmpty(firstDiscoveredOn)){ firstDiscoveredOn= firstDiscoveredOn.substring(0,PacmanRuleConstants.FIRST_DISCOVERED_DATE_FORMAT_LENGTH); } String qualysApi = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_QUALYS_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ qualysApi = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(severity,category,qualysApi,target,discoveredDaysRange)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<LinkedHashMap<String,Object>> issueList = new ArrayList<>(); LinkedHashMap<String,Object> issue = new LinkedHashMap<>(); Gson gson = new Gson(); if (resourceAttributes != null && (PacmanRuleConstants.RUNNING_STATE.equalsIgnoreCase(resourceAttributes.get(PacmanRuleConstants.STATE_NAME)) || PacmanRuleConstants.RUNNING_STATE.equalsIgnoreCase(resourceAttributes.get(PacmanRuleConstants.STATUS)))) { String entityType = resourceAttributes.get(PacmanRuleConstants.ENTITY_TYPE); instanceId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.RESOURCE_ID)); if(PacmanUtils.calculateLaunchedDuration(firstDiscoveredOn)>Long.parseLong(discoveredDaysRange)){ Map<String,Object> ec2ScannesByQualysMap = new HashMap<>(); try{ ec2ScannesByQualysMap = PacmanUtils.checkInstanceIdFromElasticSearchForQualys(instanceId,qualysApi,"_resourceid",target); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!ec2ScannesByQualysMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,""+entityType+" instance not scanned by qualys found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, ""+entityType+" instance not scanned by qualys found"); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "_resourceid,"+PacmanRuleConstants.LAST_VULN_SCAN); issue.put(PacmanRuleConstants.FAILED_REASON_QUALYS, gson.toJson(ec2ScannesByQualysMap)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========ResourceScannedByQualysRule ended with annotation {} : =========" ,annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } } logger.debug("========ResourceScannedByQualysRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); } }
ResourceScannedByQualysRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========ResourceScannedByQualysRule started========="); Annotation annotation = null; String instanceId = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String target = ruleParam.get(PacmanRuleConstants.TARGET); String firstDiscoveredOn = resourceAttributes.get(PacmanRuleConstants.FIRST_DISCOVERED_ON); String discoveredDaysRange = ruleParam.get(PacmanRuleConstants.DISCOVERED_DAYS_RANGE); if(!StringUtils.isNullOrEmpty(firstDiscoveredOn)){ firstDiscoveredOn= firstDiscoveredOn.substring(0,PacmanRuleConstants.FIRST_DISCOVERED_DATE_FORMAT_LENGTH); } String qualysApi = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_QUALYS_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ qualysApi = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(severity,category,qualysApi,target,discoveredDaysRange)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<LinkedHashMap<String,Object>> issueList = new ArrayList<>(); LinkedHashMap<String,Object> issue = new LinkedHashMap<>(); Gson gson = new Gson(); if (resourceAttributes != null && (PacmanRuleConstants.RUNNING_STATE.equalsIgnoreCase(resourceAttributes.get(PacmanRuleConstants.STATE_NAME)) || PacmanRuleConstants.RUNNING_STATE.equalsIgnoreCase(resourceAttributes.get(PacmanRuleConstants.STATUS)))) { String entityType = resourceAttributes.get(PacmanRuleConstants.ENTITY_TYPE); instanceId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.RESOURCE_ID)); if(PacmanUtils.calculateLaunchedDuration(firstDiscoveredOn)>Long.parseLong(discoveredDaysRange)){ Map<String,Object> ec2ScannesByQualysMap = new HashMap<>(); try{ ec2ScannesByQualysMap = PacmanUtils.checkInstanceIdFromElasticSearchForQualys(instanceId,qualysApi,"_resourceid",target); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!ec2ScannesByQualysMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,""+entityType+" instance not scanned by qualys found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, ""+entityType+" instance not scanned by qualys found"); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "_resourceid,"+PacmanRuleConstants.LAST_VULN_SCAN); issue.put(PacmanRuleConstants.FAILED_REASON_QUALYS, gson.toJson(ec2ScannesByQualysMap)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========ResourceScannedByQualysRule ended with annotation {} : =========" ,annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } } logger.debug("========ResourceScannedByQualysRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  }
ResourceScannedByQualysRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========ResourceScannedByQualysRule started========="); Annotation annotation = null; String instanceId = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String target = ruleParam.get(PacmanRuleConstants.TARGET); String firstDiscoveredOn = resourceAttributes.get(PacmanRuleConstants.FIRST_DISCOVERED_ON); String discoveredDaysRange = ruleParam.get(PacmanRuleConstants.DISCOVERED_DAYS_RANGE); if(!StringUtils.isNullOrEmpty(firstDiscoveredOn)){ firstDiscoveredOn= firstDiscoveredOn.substring(0,PacmanRuleConstants.FIRST_DISCOVERED_DATE_FORMAT_LENGTH); } String qualysApi = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_QUALYS_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ qualysApi = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(severity,category,qualysApi,target,discoveredDaysRange)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<LinkedHashMap<String,Object>> issueList = new ArrayList<>(); LinkedHashMap<String,Object> issue = new LinkedHashMap<>(); Gson gson = new Gson(); if (resourceAttributes != null && (PacmanRuleConstants.RUNNING_STATE.equalsIgnoreCase(resourceAttributes.get(PacmanRuleConstants.STATE_NAME)) || PacmanRuleConstants.RUNNING_STATE.equalsIgnoreCase(resourceAttributes.get(PacmanRuleConstants.STATUS)))) { String entityType = resourceAttributes.get(PacmanRuleConstants.ENTITY_TYPE); instanceId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.RESOURCE_ID)); if(PacmanUtils.calculateLaunchedDuration(firstDiscoveredOn)>Long.parseLong(discoveredDaysRange)){ Map<String,Object> ec2ScannesByQualysMap = new HashMap<>(); try{ ec2ScannesByQualysMap = PacmanUtils.checkInstanceIdFromElasticSearchForQualys(instanceId,qualysApi,"_resourceid",target); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!ec2ScannesByQualysMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,""+entityType+" instance not scanned by qualys found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, ""+entityType+" instance not scanned by qualys found"); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "_resourceid,"+PacmanRuleConstants.LAST_VULN_SCAN); issue.put(PacmanRuleConstants.FAILED_REASON_QUALYS, gson.toJson(ec2ScannesByQualysMap)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========ResourceScannedByQualysRule ended with annotation {} : =========" ,annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } } logger.debug("========ResourceScannedByQualysRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes); String getHelpText(); }
ResourceScannedByQualysRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========ResourceScannedByQualysRule started========="); Annotation annotation = null; String instanceId = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String target = ruleParam.get(PacmanRuleConstants.TARGET); String firstDiscoveredOn = resourceAttributes.get(PacmanRuleConstants.FIRST_DISCOVERED_ON); String discoveredDaysRange = ruleParam.get(PacmanRuleConstants.DISCOVERED_DAYS_RANGE); if(!StringUtils.isNullOrEmpty(firstDiscoveredOn)){ firstDiscoveredOn= firstDiscoveredOn.substring(0,PacmanRuleConstants.FIRST_DISCOVERED_DATE_FORMAT_LENGTH); } String qualysApi = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_QUALYS_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ qualysApi = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(severity,category,qualysApi,target,discoveredDaysRange)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<LinkedHashMap<String,Object>> issueList = new ArrayList<>(); LinkedHashMap<String,Object> issue = new LinkedHashMap<>(); Gson gson = new Gson(); if (resourceAttributes != null && (PacmanRuleConstants.RUNNING_STATE.equalsIgnoreCase(resourceAttributes.get(PacmanRuleConstants.STATE_NAME)) || PacmanRuleConstants.RUNNING_STATE.equalsIgnoreCase(resourceAttributes.get(PacmanRuleConstants.STATUS)))) { String entityType = resourceAttributes.get(PacmanRuleConstants.ENTITY_TYPE); instanceId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.RESOURCE_ID)); if(PacmanUtils.calculateLaunchedDuration(firstDiscoveredOn)>Long.parseLong(discoveredDaysRange)){ Map<String,Object> ec2ScannesByQualysMap = new HashMap<>(); try{ ec2ScannesByQualysMap = PacmanUtils.checkInstanceIdFromElasticSearchForQualys(instanceId,qualysApi,"_resourceid",target); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!ec2ScannesByQualysMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,""+entityType+" instance not scanned by qualys found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, ""+entityType+" instance not scanned by qualys found"); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "_resourceid,"+PacmanRuleConstants.LAST_VULN_SCAN); issue.put(PacmanRuleConstants.FAILED_REASON_QUALYS, gson.toJson(ec2ScannesByQualysMap)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========ResourceScannedByQualysRule ended with annotation {} : =========" ,annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } } logger.debug("========ResourceScannedByQualysRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes); String getHelpText();  }