13
13
import org .apache .commons .collections .CollectionUtils ;
14
14
import org .springframework .beans .factory .annotation .Autowired ;
15
15
import org .springframework .stereotype .Component ;
16
+ import org .springframework .util .StringUtils ;
16
17
17
18
import java .util .Collection ;
18
19
import java .util .Collections ;
19
20
import java .util .List ;
20
21
import java .util .Map ;
21
22
import java .util .Objects ;
23
+ import java .util .HashMap ;
24
+ import java .util .Comparator ;
22
25
import java .util .stream .Collectors ;
23
26
24
27
@ Component
25
28
public class InfrastructureEvaluator extends Evaluator <InfrastructureAuditResponse > {
26
29
30
+ private static final String BUSINESS_SERVICE = "businessService" ;
27
31
public static final String BUSINESS_COMPONENT = "businessComponent" ;
28
32
private final InfrastructureScanRepository infrastructureScanRepository ;
29
33
@@ -40,7 +44,7 @@ public Collection<InfrastructureAuditResponse> evaluate(Dashboard dashboard, lon
40
44
if (CollectionUtils .isEmpty (infrastructureScanItems )) {
41
45
throw new AuditException ("No Infrastructure scan items configured" , AuditException .NO_COLLECTOR_ITEM_CONFIGURED );
42
46
}
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 ());
44
48
}
45
49
46
50
@ Override
@@ -51,46 +55,59 @@ public Collection<InfrastructureAuditResponse> evaluateNextGen(ArtifactAuditRequ
51
55
52
56
@ Override
53
57
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 );
55
59
}
56
60
57
61
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 );
59
65
InfrastructureAuditResponse infrastructureAuditResponse = new InfrastructureAuditResponse ();
60
66
infrastructureAuditResponse .setAuditEntity (collectorItem .getOptions ());
61
67
infrastructureAuditResponse .setLastUpdated (collectorItem .getLastUpdated ());
62
68
63
69
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
+
66
73
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 ));
68
77
} else {
69
78
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
+ }
70
89
}
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 );
78
90
return infrastructureAuditResponse ;
79
91
}
80
92
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 ;
95
112
}
96
113
}
0 commit comments