@Test public void getCerticatesExpiryByApplicationTest_NoApps() throws Exception { String response = "{\"aggregations\":{\"apps\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[]}}}"; mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(response); ReflectionTestUtils.setField(certificateRepository, "esUrl", "dummyEsURL"); Map<String, Object> cert = certificateRepository.getCertificatesExpiryByApplication("ag"); assertTrue(cert.size() == 0); }
public Map<String, Object> getCertificatesExpiryByApplication( String assetGroup) throws DataException { Map<String, Object> expiryRules = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append( assetGroup); urlToQuery.append(CERT_SEARCH); String requestBody = "{\"size\":0,\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":" + TEN_THOUSAND + "}," + "\"aggs\":{\"certs\":{\"children\":{\"type\":\"issue_cert\"},\"aggs\":{\"openfilter\":{\"filter\":{\"term\":{\"issueStatus\":\"open\"}}," + "\"aggs\":{\"rules\":{\"terms\":{\"field\":\"ruleId.keyword\",\"size\":10}}}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitiesDistribution from ES", e); throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( "aggregations").toString()); JsonArray outerBuckets = aggsJson.getAsJsonObject("apps") .getAsJsonArray(BUCKETS); if (outerBuckets.size() > 0) { for (int i = 0; i < outerBuckets.size(); i++) { String appName = outerBuckets.get(i).getAsJsonObject() .get("key").getAsString(); Map<String, Integer> expiryDetails = new HashMap<>(); expiryDetails.put(EXP_IN_45_DAYS, 0); expiryDetails.put(EXP_IN_30_DAYS, 0); JsonArray rules = outerBuckets.get(i).getAsJsonObject() .getAsJsonObject("certs").getAsJsonObject("openfilter") .getAsJsonObject("rules").getAsJsonArray(BUCKETS); if (rules.size() > 0) { for (int j = 0; j < rules.size(); j++) { if (SSL_CERT_45_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { expiryDetails.put( EXP_IN_45_DAYS, Integer.valueOf(rules.get(j) .getAsJsonObject().get(DOC_COUNT) .toString())); } if (SSL_CERT_30_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { expiryDetails.put( EXP_IN_30_DAYS, Integer.valueOf(rules.get(j) .getAsJsonObject().get(DOC_COUNT) .toString())); } } } if (StringUtils.isNotBlank(appName)) { expiryRules.put(appName, expiryDetails); } } } return expiryRules; }
CertificateRepository implements Constants { public Map<String, Object> getCertificatesExpiryByApplication( String assetGroup) throws DataException { Map<String, Object> expiryRules = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append( assetGroup); urlToQuery.append(CERT_SEARCH); String requestBody = "{\"size\":0,\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":" + TEN_THOUSAND + "}," + "\"aggs\":{\"certs\":{\"children\":{\"type\":\"issue_cert\"},\"aggs\":{\"openfilter\":{\"filter\":{\"term\":{\"issueStatus\":\"open\"}}," + "\"aggs\":{\"rules\":{\"terms\":{\"field\":\"ruleId.keyword\",\"size\":10}}}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitiesDistribution from ES", e); throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( "aggregations").toString()); JsonArray outerBuckets = aggsJson.getAsJsonObject("apps") .getAsJsonArray(BUCKETS); if (outerBuckets.size() > 0) { for (int i = 0; i < outerBuckets.size(); i++) { String appName = outerBuckets.get(i).getAsJsonObject() .get("key").getAsString(); Map<String, Integer> expiryDetails = new HashMap<>(); expiryDetails.put(EXP_IN_45_DAYS, 0); expiryDetails.put(EXP_IN_30_DAYS, 0); JsonArray rules = outerBuckets.get(i).getAsJsonObject() .getAsJsonObject("certs").getAsJsonObject("openfilter") .getAsJsonObject("rules").getAsJsonArray(BUCKETS); if (rules.size() > 0) { for (int j = 0; j < rules.size(); j++) { if (SSL_CERT_45_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { expiryDetails.put( EXP_IN_45_DAYS, Integer.valueOf(rules.get(j) .getAsJsonObject().get(DOC_COUNT) .toString())); } if (SSL_CERT_30_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { expiryDetails.put( EXP_IN_30_DAYS, Integer.valueOf(rules.get(j) .getAsJsonObject().get(DOC_COUNT) .toString())); } } } if (StringUtils.isNotBlank(appName)) { expiryRules.put(appName, expiryDetails); } } } return expiryRules; } }
CertificateRepository implements Constants { public Map<String, Object> getCertificatesExpiryByApplication( String assetGroup) throws DataException { Map<String, Object> expiryRules = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append( assetGroup); urlToQuery.append(CERT_SEARCH); String requestBody = "{\"size\":0,\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":" + TEN_THOUSAND + "}," + "\"aggs\":{\"certs\":{\"children\":{\"type\":\"issue_cert\"},\"aggs\":{\"openfilter\":{\"filter\":{\"term\":{\"issueStatus\":\"open\"}}," + "\"aggs\":{\"rules\":{\"terms\":{\"field\":\"ruleId.keyword\",\"size\":10}}}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitiesDistribution from ES", e); throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( "aggregations").toString()); JsonArray outerBuckets = aggsJson.getAsJsonObject("apps") .getAsJsonArray(BUCKETS); if (outerBuckets.size() > 0) { for (int i = 0; i < outerBuckets.size(); i++) { String appName = outerBuckets.get(i).getAsJsonObject() .get("key").getAsString(); Map<String, Integer> expiryDetails = new HashMap<>(); expiryDetails.put(EXP_IN_45_DAYS, 0); expiryDetails.put(EXP_IN_30_DAYS, 0); JsonArray rules = outerBuckets.get(i).getAsJsonObject() .getAsJsonObject("certs").getAsJsonObject("openfilter") .getAsJsonObject("rules").getAsJsonArray(BUCKETS); if (rules.size() > 0) { for (int j = 0; j < rules.size(); j++) { if (SSL_CERT_45_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { expiryDetails.put( EXP_IN_45_DAYS, Integer.valueOf(rules.get(j) .getAsJsonObject().get(DOC_COUNT) .toString())); } if (SSL_CERT_30_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { expiryDetails.put( EXP_IN_30_DAYS, Integer.valueOf(rules.get(j) .getAsJsonObject().get(DOC_COUNT) .toString())); } } } if (StringUtils.isNotBlank(appName)) { expiryRules.put(appName, expiryDetails); } } } return expiryRules; }  }
CertificateRepository implements Constants { public Map<String, Object> getCertificatesExpiryByApplication( String assetGroup) throws DataException { Map<String, Object> expiryRules = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append( assetGroup); urlToQuery.append(CERT_SEARCH); String requestBody = "{\"size\":0,\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":" + TEN_THOUSAND + "}," + "\"aggs\":{\"certs\":{\"children\":{\"type\":\"issue_cert\"},\"aggs\":{\"openfilter\":{\"filter\":{\"term\":{\"issueStatus\":\"open\"}}," + "\"aggs\":{\"rules\":{\"terms\":{\"field\":\"ruleId.keyword\",\"size\":10}}}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitiesDistribution from ES", e); throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( "aggregations").toString()); JsonArray outerBuckets = aggsJson.getAsJsonObject("apps") .getAsJsonArray(BUCKETS); if (outerBuckets.size() > 0) { for (int i = 0; i < outerBuckets.size(); i++) { String appName = outerBuckets.get(i).getAsJsonObject() .get("key").getAsString(); Map<String, Integer> expiryDetails = new HashMap<>(); expiryDetails.put(EXP_IN_45_DAYS, 0); expiryDetails.put(EXP_IN_30_DAYS, 0); JsonArray rules = outerBuckets.get(i).getAsJsonObject() .getAsJsonObject("certs").getAsJsonObject("openfilter") .getAsJsonObject("rules").getAsJsonArray(BUCKETS); if (rules.size() > 0) { for (int j = 0; j < rules.size(); j++) { if (SSL_CERT_45_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { expiryDetails.put( EXP_IN_45_DAYS, Integer.valueOf(rules.get(j) .getAsJsonObject().get(DOC_COUNT) .toString())); } if (SSL_CERT_30_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { expiryDetails.put( EXP_IN_30_DAYS, Integer.valueOf(rules.get(j) .getAsJsonObject().get(DOC_COUNT) .toString())); } } } if (StringUtils.isNotBlank(appName)) { expiryRules.put(appName, expiryDetails); } } } return expiryRules; }  Map<String, Object> getCertificatesExpiryByApplication(
            String assetGroup); @SuppressWarnings("unchecked") List<Map<String, Object>> getCertificatesDetails(String assetGroup,
            String searchText, Map<String, String> filter); Map<String, Object> getCertificatesSummary(String assetGroup); }
CertificateRepository implements Constants { public Map<String, Object> getCertificatesExpiryByApplication( String assetGroup) throws DataException { Map<String, Object> expiryRules = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append( assetGroup); urlToQuery.append(CERT_SEARCH); String requestBody = "{\"size\":0,\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":" + TEN_THOUSAND + "}," + "\"aggs\":{\"certs\":{\"children\":{\"type\":\"issue_cert\"},\"aggs\":{\"openfilter\":{\"filter\":{\"term\":{\"issueStatus\":\"open\"}}," + "\"aggs\":{\"rules\":{\"terms\":{\"field\":\"ruleId.keyword\",\"size\":10}}}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitiesDistribution from ES", e); throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( "aggregations").toString()); JsonArray outerBuckets = aggsJson.getAsJsonObject("apps") .getAsJsonArray(BUCKETS); if (outerBuckets.size() > 0) { for (int i = 0; i < outerBuckets.size(); i++) { String appName = outerBuckets.get(i).getAsJsonObject() .get("key").getAsString(); Map<String, Integer> expiryDetails = new HashMap<>(); expiryDetails.put(EXP_IN_45_DAYS, 0); expiryDetails.put(EXP_IN_30_DAYS, 0); JsonArray rules = outerBuckets.get(i).getAsJsonObject() .getAsJsonObject("certs").getAsJsonObject("openfilter") .getAsJsonObject("rules").getAsJsonArray(BUCKETS); if (rules.size() > 0) { for (int j = 0; j < rules.size(); j++) { if (SSL_CERT_45_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { expiryDetails.put( EXP_IN_45_DAYS, Integer.valueOf(rules.get(j) .getAsJsonObject().get(DOC_COUNT) .toString())); } if (SSL_CERT_30_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { expiryDetails.put( EXP_IN_30_DAYS, Integer.valueOf(rules.get(j) .getAsJsonObject().get(DOC_COUNT) .toString())); } } } if (StringUtils.isNotBlank(appName)) { expiryRules.put(appName, expiryDetails); } } } return expiryRules; }  Map<String, Object> getCertificatesExpiryByApplication(
            String assetGroup); @SuppressWarnings("unchecked") List<Map<String, Object>> getCertificatesDetails(String assetGroup,
            String searchText, Map<String, String> filter); Map<String, Object> getCertificatesSummary(String assetGroup);  }