3
3
import com .amazonaws .auth .AWSStaticCredentialsProvider ;
4
4
import com .amazonaws .services .s3 .AmazonS3 ;
5
5
import com .amazonaws .services .s3 .AmazonS3ClientBuilder ;
6
- import com .amazonaws .services .s3 .model .*;
6
+ import com .amazonaws .services .s3 .model .GetObjectRequest ;
7
+ import com .amazonaws .services .s3 .model .S3Object ;
7
8
import com .amazonaws .util .StringUtils ;
8
9
import com .fasterxml .jackson .core .type .TypeReference ;
9
10
import com .fasterxml .jackson .databind .ObjectMapper ;
@@ -30,8 +31,6 @@ public class VulnerabilityAssociationManager {
30
31
private static final String BUCKET_NAME = System .getProperty ("s3" );
31
32
private static final String DATA_PATH = System .getProperty ("s3.data" );
32
33
private static final String DATE_FORMAT_SEC = "yyyy-MM-dd HH:mm:00Z" ;
33
- private static final String VUL_FILE_SUFFIX = "-vulnerabilities.data" ;
34
- private static final String DETECTION_FILE_SUFFIX = "-detections.data" ;
35
34
private static final Map <String , String > sourceFileToIndexMapping = new HashMap <>(2 );
36
35
37
36
static {
@@ -51,33 +50,31 @@ public List<Map<String, String>> uploadVulnerabilityInfo(String dataSource) {
51
50
String indexName = String .format (entry .getValue (), dataSource );
52
51
String filePrefix = String .format (entry .getKey (), dataSource );
53
52
List <Map <String , Object >> entities ;
54
- ListObjectsV2Request listReq = new ListObjectsV2Request ().
55
- withBucketName (BUCKET_NAME )
56
- .withPrefix (DATA_PATH ); // List only files inside this folder
57
- ListObjectsV2Result result = s3Client .listObjectsV2 (listReq );
58
- if (result != null && result .getKeyCount () > 0 ) {
59
- String loadDate = new SimpleDateFormat (DATE_FORMAT_SEC ).format (new java .util .Date ());
60
- for (S3ObjectSummary object : result .getObjectSummaries ()) {
61
- String fileName = object .getKey ();
62
- S3Object entitiesData = s3Client .getObject (BUCKET_NAME , fileName );
63
- if (fileName .endsWith (VUL_FILE_SUFFIX ) && entry .getKey ().contains ("vulnerabilities" ) ||
64
- fileName .endsWith (DETECTION_FILE_SUFFIX ) && entry .getKey ().contains ("-detections" )) {
65
- try (BufferedReader reader = new BufferedReader (new InputStreamReader (entitiesData .getObjectContent ()))) {
66
- entities = objectMapper .readValue (reader .lines ().collect (Collectors .joining ("\n " )), new TypeReference <List <Map <String , Object >>>() {
67
- });
68
- if (Objects .isNull (entities )) {
69
- LOGGER .info ("{} object is empty for dataSource - {}" , filePrefix , dataSource );
70
- continue ;
71
- }
72
- uploadEntity (entities , indexName , loadDate );
73
- } catch (Exception e ) {
74
- LOGGER .info ("{} data is empty" , filePrefix );
75
- }
76
- }
77
- }
78
-
79
- }
80
-
53
+ S3Object entitiesData = s3Client .getObject (new GetObjectRequest (BUCKET_NAME , DATA_PATH + "/" + filePrefix + ".data" ));
54
+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (entitiesData .getObjectContent ()))) {
55
+ entities = objectMapper .readValue (reader .lines ().collect (Collectors .joining ("\n " )), new TypeReference <List <Map <String , Object >>>() {
56
+ });
57
+ } catch (Exception e ) {
58
+ LOGGER .info ("{} data is empty" , filePrefix );
59
+ continue ;
60
+ }
61
+ if (Objects .isNull (entities )) {
62
+ LOGGER .info ("{} object is empty for dataSource - {}" , filePrefix , dataSource );
63
+ continue ;
64
+ }
65
+ String url = ESUtils .getEsUrl ();
66
+ if (!ESUtils .isValidIndex (url , indexName )) {
67
+ ESUtils .createIndex (url , indexName );
68
+ }
69
+ String loaddate = new SimpleDateFormat (DATE_FORMAT_SEC ).format (new java .util .Date ());
70
+ entities .parallelStream ().filter (obj -> obj .get ("closedDate" ) == null || StringUtils .isNullOrEmpty (obj .get ("closedDate" ).toString ()))
71
+ .forEach ((obj ) -> {
72
+ obj .remove ("closedDate" );
73
+ obj .put ("_loaddate" , loaddate );
74
+ });
75
+ LOGGER .info ("Collected vulnerabilities: {}" , entities .size ());
76
+ ESManager .uploadVulnerabilityData (indexName , entities );
77
+ ESManager .deleteOldDocuments (indexName , null , "_loaddate.keyword" , loaddate );
81
78
} catch (Exception e ) {
82
79
LOGGER .error ("Error in shipping vulnerability data for dataSource - {}" , dataSource );
83
80
Map <String , String > errorMap = new HashMap <>();
@@ -90,20 +87,4 @@ public List<Map<String, String>> uploadVulnerabilityInfo(String dataSource) {
90
87
LOGGER .info ("Completed Vulnerability collection for {}" , dataSource );
91
88
return errorList ;
92
89
}
93
-
94
-
95
- public static void uploadEntity (List <Map <String , Object >> entities , String indexName , String loadDate ) throws Exception {
96
- String url = ESUtils .getEsUrl ();
97
- if (!ESUtils .isValidIndex (url , indexName )) {
98
- ESUtils .createIndex (url , indexName );
99
- }
100
- entities .parallelStream ().filter (obj -> obj .get ("closedDate" ) == null || StringUtils .isNullOrEmpty (obj .get ("closedDate" ).toString ()))
101
- .forEach ((obj ) -> {
102
- obj .remove ("closedDate" );
103
- obj .put ("_loaddate" , loadDate );
104
- });
105
- LOGGER .info ("Collected vulnerabilities: {}" , entities .size ());
106
- ESManager .uploadVulnerabilityData (indexName , entities );
107
- ESManager .deleteOldDocuments (indexName , null , "_loaddate.keyword" , loadDate );
108
- }
109
90
}
0 commit comments