@Test public void executeTest() throws Exception { mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( true); when(PacmanUtils.splitStringToAList(anyString(),anyString())).thenReturn(CommonTestUtils.getListString()); assertThat(premiseServerKernelComplianceRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); assertThat(premiseServerKernelComplianceRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getEmptyMapString()), is(notNullValue())); assertThat(premiseServerKernelComplianceRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getAnotherMapString("123")), is(notNullValue())); PowerMockito.when(DefaultTargetCriteriaDataProvider.getInstance(anyString())).thenReturn(defaultTargetCriteriaDataProvider); PowerMockito.when(DefaultTargetCriteriaDataProvider.getInstance(anyString()).getTargetCriterianData()).thenReturn(CommonTestUtils.getEmptyJsonObject()); assertThat(premiseServerKernelComplianceRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); PowerMockito.when(DefaultTargetCriteriaDataProvider.getInstance(anyString())).thenReturn(defaultTargetCriteriaDataProvider); PowerMockito.when(DefaultTargetCriteriaDataProvider.getInstance(anyString()).getTargetCriterianData()).thenReturn(CommonTestUtils.getJsonObject()); when(PacmanUtils.checkIsCompliant(anyString(),anyObject(), anyObject())).thenReturn( true); assertThat(premiseServerKernelComplianceRule.execute(CommonTestUtils.getFinalKernelReleaseAnotherMapString("r_123 "),CommonTestUtils.getFinalKernelReleaseAnotherMapString("123")), is(notNullValue())); when(PacmanUtils.checkIsCompliant(anyString(),anyObject(), anyObject())).thenReturn( true); assertThat(premiseServerKernelComplianceRule.execute(CommonTestUtils.getLastPatchedMapString("123 "),CommonTestUtils.getLastPatchedMapString("123")), is(notNullValue())); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(), anyString())).thenReturn( false); assertThatThrownBy( () -> premiseServerKernelComplianceRule.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("========OnPremiseServerKernelComplianceRule started========="); Gson gson = new Gson(); Set<String> keySet; boolean isCompliant = false; Annotation annotation = null; String lastPatched = null; String finalKernelRelease = null; int patchedQuarter = 0; int currentQuarter = 0; int patchedYear = 0; int currentYear = 0; LocalDate lastPatchedDate = null; DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String defaultKernelCriteriaUrl = ruleParam .get(PacmanRuleConstants.DEFAULT_KERNEL_CRITERIA_URL); String description = null; List<String> sourcesChecked = new ArrayList<>(); LinkedHashMap<String, Object> onpremSource = new LinkedHashMap<>(); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity, category, defaultKernelCriteriaUrl)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if ("true".equals(resourceAttributes.get(PacmanRuleConstants.IN_SCOPE))) { lastPatched = resourceAttributes .get(PacmanRuleConstants.FINAL_U_LAST_PATCHED); finalKernelRelease = resourceAttributes .get(PacmanRuleConstants.FINAL_KERNEL_RELEASE); JsonObject kernelVersionFromPacmanTable = DefaultTargetCriteriaDataProvider .getInstance(defaultKernelCriteriaUrl) .getTargetCriterianData(); if (kernelVersionFromPacmanTable != null && !kernelVersionFromPacmanTable.entrySet().isEmpty()) { sourcesChecked.add(PacmanRuleConstants.FINAL_U_LAST_PATCHED); sourcesChecked.add(PacmanRuleConstants.FINAL_KERNEL_RELEASE); HashMap<String, String> mapOfQuaterlyVersions = gson.fromJson( kernelVersionFromPacmanTable.toString(), HashMap.class); keySet = mapOfQuaterlyVersions.keySet(); if (!StringUtils.isNullOrEmpty(lastPatched)) { onpremSource.put(PacmanRuleConstants.FINAL_U_LAST_PATCHED, lastPatched); } else { onpremSource.put(PacmanRuleConstants.FINAL_U_LAST_PATCHED, "Not found"); } if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { onpremSource.put(PacmanRuleConstants.FINAL_KERNEL_RELEASE, finalKernelRelease); } else { onpremSource.put(PacmanRuleConstants.FINAL_KERNEL_RELEASE, "Not found"); } if (!StringUtils.isNullOrEmpty(lastPatched)) { lastPatchedDate = LocalDate.parse(lastPatched, formatter); patchedYear = lastPatchedDate.getYear(); currentYear = LocalDate.now().getYear(); patchedQuarter = lastPatchedDate .get(IsoFields.QUARTER_OF_YEAR); currentQuarter = LocalDate.now().get( IsoFields.QUARTER_OF_YEAR); if ((patchedYear == currentYear) && (currentQuarter == patchedQuarter)) { description = "resource got compliant through final_u_last_patched value " + lastPatchedDate; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } else if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { isCompliant = PacmanUtils.checkIsCompliant( finalKernelRelease, keySet, mapOfQuaterlyVersions); if (isCompliant) { description = "resource got compliant through final_kernel_release value " + finalKernelRelease; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } } } else if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { isCompliant = PacmanUtils.checkIsCompliant( finalKernelRelease, keySet, mapOfQuaterlyVersions); if (isCompliant) { description = "resource got compliant through final_kernel_release value " + finalKernelRelease; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } } if (!isCompliant) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Onprem kernel version is not compliant"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put(PacmanRuleConstants.FAILED_TYPES, sourcesChecked.toString()); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Onprem kernel version is not compliant"); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, String.join(",", sourcesChecked)); issue.put("onprem_sources", gson.toJson(onpremSource)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } else { logger.info("target kernel criteria not maintained"); annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Target Kernerl Criteria not maintained"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, PacmanRuleConstants.NO_DEFAULT_TARGET); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }
OnPremiseServerKernelComplianceRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========OnPremiseServerKernelComplianceRule started========="); Gson gson = new Gson(); Set<String> keySet; boolean isCompliant = false; Annotation annotation = null; String lastPatched = null; String finalKernelRelease = null; int patchedQuarter = 0; int currentQuarter = 0; int patchedYear = 0; int currentYear = 0; LocalDate lastPatchedDate = null; DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String defaultKernelCriteriaUrl = ruleParam .get(PacmanRuleConstants.DEFAULT_KERNEL_CRITERIA_URL); String description = null; List<String> sourcesChecked = new ArrayList<>(); LinkedHashMap<String, Object> onpremSource = new LinkedHashMap<>(); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity, category, defaultKernelCriteriaUrl)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if ("true".equals(resourceAttributes.get(PacmanRuleConstants.IN_SCOPE))) { lastPatched = resourceAttributes .get(PacmanRuleConstants.FINAL_U_LAST_PATCHED); finalKernelRelease = resourceAttributes .get(PacmanRuleConstants.FINAL_KERNEL_RELEASE); JsonObject kernelVersionFromPacmanTable = DefaultTargetCriteriaDataProvider .getInstance(defaultKernelCriteriaUrl) .getTargetCriterianData(); if (kernelVersionFromPacmanTable != null && !kernelVersionFromPacmanTable.entrySet().isEmpty()) { sourcesChecked.add(PacmanRuleConstants.FINAL_U_LAST_PATCHED); sourcesChecked.add(PacmanRuleConstants.FINAL_KERNEL_RELEASE); HashMap<String, String> mapOfQuaterlyVersions = gson.fromJson( kernelVersionFromPacmanTable.toString(), HashMap.class); keySet = mapOfQuaterlyVersions.keySet(); if (!StringUtils.isNullOrEmpty(lastPatched)) { onpremSource.put(PacmanRuleConstants.FINAL_U_LAST_PATCHED, lastPatched); } else { onpremSource.put(PacmanRuleConstants.FINAL_U_LAST_PATCHED, "Not found"); } if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { onpremSource.put(PacmanRuleConstants.FINAL_KERNEL_RELEASE, finalKernelRelease); } else { onpremSource.put(PacmanRuleConstants.FINAL_KERNEL_RELEASE, "Not found"); } if (!StringUtils.isNullOrEmpty(lastPatched)) { lastPatchedDate = LocalDate.parse(lastPatched, formatter); patchedYear = lastPatchedDate.getYear(); currentYear = LocalDate.now().getYear(); patchedQuarter = lastPatchedDate .get(IsoFields.QUARTER_OF_YEAR); currentQuarter = LocalDate.now().get( IsoFields.QUARTER_OF_YEAR); if ((patchedYear == currentYear) && (currentQuarter == patchedQuarter)) { description = "resource got compliant through final_u_last_patched value " + lastPatchedDate; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } else if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { isCompliant = PacmanUtils.checkIsCompliant( finalKernelRelease, keySet, mapOfQuaterlyVersions); if (isCompliant) { description = "resource got compliant through final_kernel_release value " + finalKernelRelease; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } } } else if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { isCompliant = PacmanUtils.checkIsCompliant( finalKernelRelease, keySet, mapOfQuaterlyVersions); if (isCompliant) { description = "resource got compliant through final_kernel_release value " + finalKernelRelease; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } } if (!isCompliant) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Onprem kernel version is not compliant"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put(PacmanRuleConstants.FAILED_TYPES, sourcesChecked.toString()); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Onprem kernel version is not compliant"); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, String.join(",", sourcesChecked)); issue.put("onprem_sources", gson.toJson(onpremSource)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } else { logger.info("target kernel criteria not maintained"); annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Target Kernerl Criteria not maintained"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, PacmanRuleConstants.NO_DEFAULT_TARGET); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } }
OnPremiseServerKernelComplianceRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========OnPremiseServerKernelComplianceRule started========="); Gson gson = new Gson(); Set<String> keySet; boolean isCompliant = false; Annotation annotation = null; String lastPatched = null; String finalKernelRelease = null; int patchedQuarter = 0; int currentQuarter = 0; int patchedYear = 0; int currentYear = 0; LocalDate lastPatchedDate = null; DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String defaultKernelCriteriaUrl = ruleParam .get(PacmanRuleConstants.DEFAULT_KERNEL_CRITERIA_URL); String description = null; List<String> sourcesChecked = new ArrayList<>(); LinkedHashMap<String, Object> onpremSource = new LinkedHashMap<>(); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity, category, defaultKernelCriteriaUrl)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if ("true".equals(resourceAttributes.get(PacmanRuleConstants.IN_SCOPE))) { lastPatched = resourceAttributes .get(PacmanRuleConstants.FINAL_U_LAST_PATCHED); finalKernelRelease = resourceAttributes .get(PacmanRuleConstants.FINAL_KERNEL_RELEASE); JsonObject kernelVersionFromPacmanTable = DefaultTargetCriteriaDataProvider .getInstance(defaultKernelCriteriaUrl) .getTargetCriterianData(); if (kernelVersionFromPacmanTable != null && !kernelVersionFromPacmanTable.entrySet().isEmpty()) { sourcesChecked.add(PacmanRuleConstants.FINAL_U_LAST_PATCHED); sourcesChecked.add(PacmanRuleConstants.FINAL_KERNEL_RELEASE); HashMap<String, String> mapOfQuaterlyVersions = gson.fromJson( kernelVersionFromPacmanTable.toString(), HashMap.class); keySet = mapOfQuaterlyVersions.keySet(); if (!StringUtils.isNullOrEmpty(lastPatched)) { onpremSource.put(PacmanRuleConstants.FINAL_U_LAST_PATCHED, lastPatched); } else { onpremSource.put(PacmanRuleConstants.FINAL_U_LAST_PATCHED, "Not found"); } if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { onpremSource.put(PacmanRuleConstants.FINAL_KERNEL_RELEASE, finalKernelRelease); } else { onpremSource.put(PacmanRuleConstants.FINAL_KERNEL_RELEASE, "Not found"); } if (!StringUtils.isNullOrEmpty(lastPatched)) { lastPatchedDate = LocalDate.parse(lastPatched, formatter); patchedYear = lastPatchedDate.getYear(); currentYear = LocalDate.now().getYear(); patchedQuarter = lastPatchedDate .get(IsoFields.QUARTER_OF_YEAR); currentQuarter = LocalDate.now().get( IsoFields.QUARTER_OF_YEAR); if ((patchedYear == currentYear) && (currentQuarter == patchedQuarter)) { description = "resource got compliant through final_u_last_patched value " + lastPatchedDate; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } else if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { isCompliant = PacmanUtils.checkIsCompliant( finalKernelRelease, keySet, mapOfQuaterlyVersions); if (isCompliant) { description = "resource got compliant through final_kernel_release value " + finalKernelRelease; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } } } else if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { isCompliant = PacmanUtils.checkIsCompliant( finalKernelRelease, keySet, mapOfQuaterlyVersions); if (isCompliant) { description = "resource got compliant through final_kernel_release value " + finalKernelRelease; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } } if (!isCompliant) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Onprem kernel version is not compliant"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put(PacmanRuleConstants.FAILED_TYPES, sourcesChecked.toString()); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Onprem kernel version is not compliant"); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, String.join(",", sourcesChecked)); issue.put("onprem_sources", gson.toJson(onpremSource)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } else { logger.info("target kernel criteria not maintained"); annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Target Kernerl Criteria not maintained"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, PacmanRuleConstants.NO_DEFAULT_TARGET); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  }
OnPremiseServerKernelComplianceRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========OnPremiseServerKernelComplianceRule started========="); Gson gson = new Gson(); Set<String> keySet; boolean isCompliant = false; Annotation annotation = null; String lastPatched = null; String finalKernelRelease = null; int patchedQuarter = 0; int currentQuarter = 0; int patchedYear = 0; int currentYear = 0; LocalDate lastPatchedDate = null; DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String defaultKernelCriteriaUrl = ruleParam .get(PacmanRuleConstants.DEFAULT_KERNEL_CRITERIA_URL); String description = null; List<String> sourcesChecked = new ArrayList<>(); LinkedHashMap<String, Object> onpremSource = new LinkedHashMap<>(); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity, category, defaultKernelCriteriaUrl)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if ("true".equals(resourceAttributes.get(PacmanRuleConstants.IN_SCOPE))) { lastPatched = resourceAttributes .get(PacmanRuleConstants.FINAL_U_LAST_PATCHED); finalKernelRelease = resourceAttributes .get(PacmanRuleConstants.FINAL_KERNEL_RELEASE); JsonObject kernelVersionFromPacmanTable = DefaultTargetCriteriaDataProvider .getInstance(defaultKernelCriteriaUrl) .getTargetCriterianData(); if (kernelVersionFromPacmanTable != null && !kernelVersionFromPacmanTable.entrySet().isEmpty()) { sourcesChecked.add(PacmanRuleConstants.FINAL_U_LAST_PATCHED); sourcesChecked.add(PacmanRuleConstants.FINAL_KERNEL_RELEASE); HashMap<String, String> mapOfQuaterlyVersions = gson.fromJson( kernelVersionFromPacmanTable.toString(), HashMap.class); keySet = mapOfQuaterlyVersions.keySet(); if (!StringUtils.isNullOrEmpty(lastPatched)) { onpremSource.put(PacmanRuleConstants.FINAL_U_LAST_PATCHED, lastPatched); } else { onpremSource.put(PacmanRuleConstants.FINAL_U_LAST_PATCHED, "Not found"); } if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { onpremSource.put(PacmanRuleConstants.FINAL_KERNEL_RELEASE, finalKernelRelease); } else { onpremSource.put(PacmanRuleConstants.FINAL_KERNEL_RELEASE, "Not found"); } if (!StringUtils.isNullOrEmpty(lastPatched)) { lastPatchedDate = LocalDate.parse(lastPatched, formatter); patchedYear = lastPatchedDate.getYear(); currentYear = LocalDate.now().getYear(); patchedQuarter = lastPatchedDate .get(IsoFields.QUARTER_OF_YEAR); currentQuarter = LocalDate.now().get( IsoFields.QUARTER_OF_YEAR); if ((patchedYear == currentYear) && (currentQuarter == patchedQuarter)) { description = "resource got compliant through final_u_last_patched value " + lastPatchedDate; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } else if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { isCompliant = PacmanUtils.checkIsCompliant( finalKernelRelease, keySet, mapOfQuaterlyVersions); if (isCompliant) { description = "resource got compliant through final_kernel_release value " + finalKernelRelease; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } } } else if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { isCompliant = PacmanUtils.checkIsCompliant( finalKernelRelease, keySet, mapOfQuaterlyVersions); if (isCompliant) { description = "resource got compliant through final_kernel_release value " + finalKernelRelease; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } } if (!isCompliant) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Onprem kernel version is not compliant"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put(PacmanRuleConstants.FAILED_TYPES, sourcesChecked.toString()); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Onprem kernel version is not compliant"); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, String.join(",", sourcesChecked)); issue.put("onprem_sources", gson.toJson(onpremSource)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } else { logger.info("target kernel criteria not maintained"); annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Target Kernerl Criteria not maintained"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, PacmanRuleConstants.NO_DEFAULT_TARGET); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); @Override String getHelpText(); }
OnPremiseServerKernelComplianceRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========OnPremiseServerKernelComplianceRule started========="); Gson gson = new Gson(); Set<String> keySet; boolean isCompliant = false; Annotation annotation = null; String lastPatched = null; String finalKernelRelease = null; int patchedQuarter = 0; int currentQuarter = 0; int patchedYear = 0; int currentYear = 0; LocalDate lastPatchedDate = null; DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String defaultKernelCriteriaUrl = ruleParam .get(PacmanRuleConstants.DEFAULT_KERNEL_CRITERIA_URL); String description = null; List<String> sourcesChecked = new ArrayList<>(); LinkedHashMap<String, Object> onpremSource = new LinkedHashMap<>(); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity, category, defaultKernelCriteriaUrl)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if ("true".equals(resourceAttributes.get(PacmanRuleConstants.IN_SCOPE))) { lastPatched = resourceAttributes .get(PacmanRuleConstants.FINAL_U_LAST_PATCHED); finalKernelRelease = resourceAttributes .get(PacmanRuleConstants.FINAL_KERNEL_RELEASE); JsonObject kernelVersionFromPacmanTable = DefaultTargetCriteriaDataProvider .getInstance(defaultKernelCriteriaUrl) .getTargetCriterianData(); if (kernelVersionFromPacmanTable != null && !kernelVersionFromPacmanTable.entrySet().isEmpty()) { sourcesChecked.add(PacmanRuleConstants.FINAL_U_LAST_PATCHED); sourcesChecked.add(PacmanRuleConstants.FINAL_KERNEL_RELEASE); HashMap<String, String> mapOfQuaterlyVersions = gson.fromJson( kernelVersionFromPacmanTable.toString(), HashMap.class); keySet = mapOfQuaterlyVersions.keySet(); if (!StringUtils.isNullOrEmpty(lastPatched)) { onpremSource.put(PacmanRuleConstants.FINAL_U_LAST_PATCHED, lastPatched); } else { onpremSource.put(PacmanRuleConstants.FINAL_U_LAST_PATCHED, "Not found"); } if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { onpremSource.put(PacmanRuleConstants.FINAL_KERNEL_RELEASE, finalKernelRelease); } else { onpremSource.put(PacmanRuleConstants.FINAL_KERNEL_RELEASE, "Not found"); } if (!StringUtils.isNullOrEmpty(lastPatched)) { lastPatchedDate = LocalDate.parse(lastPatched, formatter); patchedYear = lastPatchedDate.getYear(); currentYear = LocalDate.now().getYear(); patchedQuarter = lastPatchedDate .get(IsoFields.QUARTER_OF_YEAR); currentQuarter = LocalDate.now().get( IsoFields.QUARTER_OF_YEAR); if ((patchedYear == currentYear) && (currentQuarter == patchedQuarter)) { description = "resource got compliant through final_u_last_patched value " + lastPatchedDate; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } else if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { isCompliant = PacmanUtils.checkIsCompliant( finalKernelRelease, keySet, mapOfQuaterlyVersions); if (isCompliant) { description = "resource got compliant through final_kernel_release value " + finalKernelRelease; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } } } else if (!StringUtils.isNullOrEmpty(finalKernelRelease)) { isCompliant = PacmanUtils.checkIsCompliant( finalKernelRelease, keySet, mapOfQuaterlyVersions); if (isCompliant) { description = "resource got compliant through final_kernel_release value " + finalKernelRelease; return new RuleResult( PacmanSdkConstants.STATUS_SUCCESS, description); } } if (!isCompliant) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Onprem kernel version is not compliant"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put(PacmanRuleConstants.FAILED_TYPES, sourcesChecked.toString()); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Onprem kernel version is not compliant"); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, String.join(",", sourcesChecked)); issue.put("onprem_sources", gson.toJson(onpremSource)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } else { logger.info("target kernel criteria not maintained"); annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Target Kernerl Criteria not maintained"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, PacmanRuleConstants.NO_DEFAULT_TARGET); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); @Override String getHelpText();  }