Skip to content

Commit 5d93881

Browse files
infra scan audit enhancement (hygieia#94)
* infra scan audit updated * infra scan audit updated2 * request content format
1 parent 080ef49 commit 5d93881

File tree

4 files changed

+137
-54
lines changed

4 files changed

+137
-54
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<artifactId>api-audit</artifactId>
55
<packaging>jar</packaging>
66
<name>${project.groupId}:${project.artifactId}</name>
7-
<version>3.6.3-SNAPSHOT</version>
7+
<version>3.6.4-SNAPSHOT</version>
88
<description>Hygieia Audit Rest API Layer</description>
99
<url>https://github.com/Hygieia/${repository.name}</url>
1010

src/main/java/com/capitalone/dashboard/evaluator/InfrastructureEvaluator.java

+44-27
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@
1313
import org.apache.commons.collections.CollectionUtils;
1414
import org.springframework.beans.factory.annotation.Autowired;
1515
import org.springframework.stereotype.Component;
16+
import org.springframework.util.StringUtils;
1617

1718
import java.util.Collection;
1819
import java.util.Collections;
1920
import java.util.List;
2021
import java.util.Map;
2122
import java.util.Objects;
23+
import java.util.HashMap;
24+
import java.util.Comparator;
2225
import java.util.stream.Collectors;
2326

2427
@Component
2528
public class InfrastructureEvaluator extends Evaluator<InfrastructureAuditResponse> {
2629

30+
private static final String BUSINESS_SERVICE = "businessService";
2731
public static final String BUSINESS_COMPONENT = "businessComponent";
2832
private final InfrastructureScanRepository infrastructureScanRepository;
2933

@@ -40,7 +44,7 @@ public Collection<InfrastructureAuditResponse> evaluate(Dashboard dashboard, lon
4044
if (CollectionUtils.isEmpty(infrastructureScanItems)) {
4145
throw new AuditException("No Infrastructure scan items configured", AuditException.NO_COLLECTOR_ITEM_CONFIGURED);
4246
}
43-
return infrastructureScanItems.stream().map(item -> evaluate(item, beginDate, endDate, Collections.singletonMap(BUSINESS_COMPONENT, dashboard.getConfigurationItemBusAppName()))).collect(Collectors.toList());
47+
return infrastructureScanItems.stream().map(item -> evaluate(item, beginDate, endDate, getBusinessItemsMap(dashboard))).collect(Collectors.toList());
4448
}
4549

4650
@Override
@@ -51,46 +55,59 @@ public Collection<InfrastructureAuditResponse> evaluateNextGen(ArtifactAuditRequ
5155

5256
@Override
5357
public InfrastructureAuditResponse evaluate(CollectorItem collectorItem, long beginDate, long endDate, Map<?, ?> data) {
54-
return getInfrastructureScanResponse(collectorItem, beginDate, endDate, (String) data.get(BUSINESS_COMPONENT));
58+
return getInfrastructureScanResponse(collectorItem, beginDate, endDate, data);
5559
}
5660

5761

58-
private InfrastructureAuditResponse getInfrastructureScanResponse(CollectorItem collectorItem, long beginDate, long endDate, String businessComponent) {
62+
private InfrastructureAuditResponse getInfrastructureScanResponse(CollectorItem collectorItem, long beginDate, long endDate, Map<?, ?> data) {
63+
String businessService = (String) data.get(BUSINESS_SERVICE);
64+
String businessComponent = (String) data.get(BUSINESS_COMPONENT);
5965
InfrastructureAuditResponse infrastructureAuditResponse = new InfrastructureAuditResponse();
6066
infrastructureAuditResponse.setAuditEntity(collectorItem.getOptions());
6167
infrastructureAuditResponse.setLastUpdated(collectorItem.getLastUpdated());
6268

6369
List<InfrastructureScan> infrastructureScans = infrastructureScanRepository.findByCollectorItemIdAndTimestampIsBetweenOrderByTimestampDesc(collectorItem.getId(), beginDate - 1, endDate + 1);
64-
// filter all scans for businesssComponent
65-
List<InfrastructureScan> filteredForBAP = infrastructureScans.stream().filter(infrastructureScan -> infrastructureScan.getBusinessApplication().equalsIgnoreCase(businessComponent)).collect(Collectors.toList());
70+
List<InfrastructureScan> filteredForBAP = StringUtils.isEmpty(businessComponent) ? Collections.EMPTY_LIST :
71+
infrastructureScans.stream().filter(infrastructureScan -> infrastructureScan.getBusinessApplication().equalsIgnoreCase(businessComponent)).collect(Collectors.toList());
72+
6673
if (CollectionUtils.isNotEmpty(filteredForBAP)) {
67-
setInfraAudit(infrastructureAuditResponse, filteredForBAP, InfrastructureAuditStatus.INFRA_SCAN_BUSS_COMP_CRITICAL, InfrastructureAuditStatus.INFRA_SCAN_BUSS_COMP_HIGH, InfrastructureAuditStatus.INFRA_SCAN_BUSS_COMP_OK);
74+
InfrastructureScan infrastructureScanLatest = filteredForBAP.stream().sorted(Comparator.comparing(InfrastructureScan::getTimestamp).reversed()).findFirst().get();
75+
setInfraAudit(infrastructureAuditResponse, infrastructureScanLatest, InfrastructureAuditStatus.INFRA_SCAN_BUSS_COMP_CRITICAL, InfrastructureAuditStatus.INFRA_SCAN_BUSS_COMP_HIGH, InfrastructureAuditStatus.INFRA_SCAN_BUSS_COMP_OK);
76+
infrastructureAuditResponse.setInfrastructureScans(Collections.singletonList(infrastructureScanLatest));
6877
} else {
6978
infrastructureAuditResponse.addAuditStatus(InfrastructureAuditStatus.INFRA_SEC_SCAN_BUSS_COMP_NOT_FOUND);
79+
List<InfrastructureScan> filteredForASV = StringUtils.isEmpty(businessService) ? Collections.EMPTY_LIST :
80+
infrastructureScans.stream().filter(infrastructureScan -> infrastructureScan.getBusinessService().equalsIgnoreCase(businessService)).collect(Collectors.toList());
81+
if (CollectionUtils.isNotEmpty(filteredForASV)) {
82+
InfrastructureScan infrastructureScanLatest = filteredForASV.stream().sorted(Comparator.comparing(InfrastructureScan::getTimestamp).reversed()).findFirst().get();
83+
setInfraAudit(infrastructureAuditResponse, infrastructureScanLatest, InfrastructureAuditStatus.INFRA_SEC_SCAN_BUSS_APP_CRITICAL, InfrastructureAuditStatus.INFRA_SEC_SCAN_BUSS_APP_HIGH, InfrastructureAuditStatus.INFRA_SEC_SCAN_BUSS_APP_OK);
84+
infrastructureAuditResponse.setInfrastructureScans(Collections.singletonList(infrastructureScanLatest));
85+
} else {
86+
infrastructureAuditResponse.addAuditStatus(InfrastructureAuditStatus.INFRA_SEC_SCAN_BUSS_APP_NOT_FOUND);
87+
infrastructureAuditResponse.setInfrastructureScans(Collections.EMPTY_LIST);
88+
}
7089
}
71-
//
72-
if (CollectionUtils.isNotEmpty(infrastructureScans)) {
73-
setInfraAudit(infrastructureAuditResponse, infrastructureScans, InfrastructureAuditStatus.INFRA_SEC_SCAN_BUSS_APP_CRITICAL, InfrastructureAuditStatus.INFRA_SEC_SCAN_BUSS_APP_HIGH, InfrastructureAuditStatus.INFRA_SEC_SCAN_BUSS_APP_OK);
74-
} else {
75-
infrastructureAuditResponse.addAuditStatus(InfrastructureAuditStatus.INFRA_SEC_SCAN_BUSS_APP_NOT_FOUND);
76-
}
77-
infrastructureAuditResponse.setInfrastructureScans(infrastructureScans);
7890
return infrastructureAuditResponse;
7991
}
8092

81-
private void setInfraAudit(InfrastructureAuditResponse infrastructureAuditResponse, List<InfrastructureScan> filteredForBAP, InfrastructureAuditStatus infraScanBussCritical, InfrastructureAuditStatus infraScanBussHigh, InfrastructureAuditStatus infraScanOK) {
82-
filteredForBAP.stream().forEach(infrastructureScan -> {
83-
Vulnerability criticalVuln = CollectionUtils.isNotEmpty(infrastructureScan.getVulnerabilities()) ? infrastructureScan.getVulnerabilities().stream().filter(vulnerability -> vulnerability.getContextualizedRiskLabel().equalsIgnoreCase("CRITICAL")).findAny().orElse(null) : null;
84-
if (Objects.nonNull(criticalVuln)) {
85-
infrastructureAuditResponse.addAuditStatus(infraScanBussCritical);
86-
}
87-
Vulnerability highVuln = CollectionUtils.isNotEmpty(infrastructureScan.getVulnerabilities()) ? infrastructureScan.getVulnerabilities().stream().filter(vulnerability -> vulnerability.getContextualizedRiskLabel().equalsIgnoreCase("HIGH")).findAny().orElse(null) : null;
88-
if (Objects.nonNull(highVuln)) {
89-
infrastructureAuditResponse.addAuditStatus(infraScanBussHigh);
90-
}
91-
if(Objects.isNull(criticalVuln) && Objects.isNull(highVuln)){
92-
infrastructureAuditResponse.addAuditStatus(infraScanOK);
93-
}
94-
});
93+
private void setInfraAudit(InfrastructureAuditResponse infrastructureAuditResponse, InfrastructureScan infrastructureScan, InfrastructureAuditStatus infraScanBussCritical, InfrastructureAuditStatus infraScanBussHigh, InfrastructureAuditStatus infraScanOK) {
94+
Vulnerability criticalVuln = CollectionUtils.isNotEmpty(infrastructureScan.getVulnerabilities()) ? infrastructureScan.getVulnerabilities().stream().filter(vulnerability -> vulnerability.getContextualizedRiskLabel().equalsIgnoreCase("CRITICAL")).findAny().orElse(null) : null;
95+
if (Objects.nonNull(criticalVuln)) {
96+
infrastructureAuditResponse.addAuditStatus(infraScanBussCritical);
97+
}
98+
Vulnerability highVuln = CollectionUtils.isNotEmpty(infrastructureScan.getVulnerabilities()) ? infrastructureScan.getVulnerabilities().stream().filter(vulnerability -> vulnerability.getContextualizedRiskLabel().equalsIgnoreCase("HIGH")).findAny().orElse(null) : null;
99+
if (Objects.nonNull(highVuln)) {
100+
infrastructureAuditResponse.addAuditStatus(infraScanBussHigh);
101+
}
102+
if(Objects.isNull(criticalVuln) && Objects.isNull(highVuln)){
103+
infrastructureAuditResponse.addAuditStatus(infraScanOK);
104+
}
105+
}
106+
107+
private Map<?, ?> getBusinessItemsMap(Dashboard dashboard) {
108+
Map bMap = new HashMap();
109+
bMap.put(BUSINESS_SERVICE, dashboard.getConfigurationItemBusServName());
110+
bMap.put(BUSINESS_COMPONENT, dashboard.getConfigurationItemBusAppName());
111+
return bMap;
95112
}
96113
}

src/main/java/com/capitalone/dashboard/logging/LoggingFilter.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.apache.commons.io.output.TeeOutputStream;
1010
import org.apache.commons.lang3.StringUtils;
1111
import org.apache.log4j.Logger;
12+
import org.json.simple.parser.JSONParser;
13+
import org.json.simple.parser.ParseException;
1214
import org.springframework.beans.factory.annotation.Autowired;
1315
import org.springframework.core.annotation.Order;
1416
import org.springframework.stereotype.Component;
@@ -132,10 +134,11 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
132134

133135
chain.doFilter(bufferedRequest, bufferedResponse);
134136
requestLog.setResponseContentType(httpServletResponse.getContentType());
137+
JSONParser jsonParser = new JSONParser();
135138
try {
136139

137140
if ((httpServletRequest.getContentType() != null) && (new MimeType(httpServletRequest.getContentType()).match(new MimeType(APPLICATION_JSON_VALUE)))) {
138-
requestLog.setRequestBody(bufferedRequest.getRequestBody());
141+
requestLog.setRequestBody(getRawDataForLog(bufferedRequest.getRequestBody(), jsonParser));
139142
}
140143
// removing the logging of responses as the collection size is way too big
141144
} catch (MimeTypeParseException e) {
@@ -168,6 +171,13 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
168171
}
169172
}
170173

174+
private Object getRawDataForLog(String content, JSONParser parser) {
175+
try {
176+
return parser.parse(content);
177+
} catch (ParseException e) {
178+
return content;
179+
}
180+
}
171181

172182
private Map<String, String> getTypesafeRequestMap(HttpServletRequest request) {
173183
Map<String, String> typesafeRequestMap = new HashMap<>();

0 commit comments

Comments
 (0)