1818import com .google .cloud .securitycenter .v1 .Finding ;
1919import com .google .cloud .securitycenter .v1 .Finding .State ;
2020import com .google .cloud .securitycenter .v1 .FindingName ;
21+ import com .google .cloud .securitycenter .v1 .GroupFindingsRequest ;
22+ import com .google .cloud .securitycenter .v1 .GroupResult ;
2123import com .google .cloud .securitycenter .v1 .ListFindingsRequest ;
2224import com .google .cloud .securitycenter .v1 .ListFindingsResponse .ListFindingsResult ;
2325import com .google .cloud .securitycenter .v1 .OrganizationName ;
2426import com .google .cloud .securitycenter .v1 .SecurityCenterClient ;
27+ import com .google .cloud .securitycenter .v1 .SecurityCenterClient .GroupFindingsPagedResponse ;
2528import com .google .cloud .securitycenter .v1 .SecurityCenterClient .ListFindingsPagedResponse ;
2629import com .google .cloud .securitycenter .v1 .SourceName ;
2730import com .google .cloud .securitycenter .v1 .UpdateFindingRequest ;
3336import com .google .protobuf .Value ;
3437import java .io .IOException ;
3538import java .util .ArrayList ;
39+ import java .util .List ;
3640import org .threeten .bp .Duration ;
3741import org .threeten .bp .Instant ;
3842
@@ -329,7 +333,7 @@ static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) {
329333 // /*sourceId=*/"423432321");
330334
331335 // Iam permission to test.
332- ArrayList permissionsToTest = new ArrayList <>();
336+ List < String > permissionsToTest = new ArrayList <>();
333337 permissionsToTest .add ("securitycenter.findings.update" );
334338
335339 // Call the API.
@@ -344,4 +348,187 @@ static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) {
344348 }
345349 }
346350 // [END test_iam_permissions]
351+
352+ /**
353+ * Group all findings under an organization across all sources by their specified properties (e.g.
354+ * category).
355+ *
356+ * @param organizationName The organizatoin to group all findings for.
357+ */
358+ // [START group_all_findings]
359+ static ImmutableList <GroupResult > groupFindings (OrganizationName organizationName ) {
360+ try (SecurityCenterClient client = SecurityCenterClient .create ()) {
361+ // OrganizationName organizationName = OrganizationName.of("123234324");
362+ SourceName sourceName = SourceName .of (organizationName .getOrganization (), "-" );
363+
364+ GroupFindingsRequest .Builder request =
365+ GroupFindingsRequest .newBuilder ().setParent (sourceName .toString ()).setGroupBy ("category" );
366+
367+ // Call the API.
368+ GroupFindingsPagedResponse response = client .groupFindings (request .build ());
369+
370+ // This creates one list for all findings. If your organization has a large number of
371+ // findings
372+ // this can cause out of memory issues. You can process them batches by returning
373+ // the Iterable returned response.iterateAll() directly.
374+ ImmutableList <GroupResult > results = ImmutableList .copyOf (response .iterateAll ());
375+ System .out .println ("Findings:" );
376+ System .out .println (results );
377+ return results ;
378+ } catch (IOException e ) {
379+ throw new RuntimeException ("Couldn't create client." , e );
380+ }
381+ }
382+ // [END group_all_findings]
383+
384+ /**
385+ * Group findings under an organization and a source by their specified properties (e.g.
386+ * category).
387+ *
388+ * @param sourceName The source to limit the findings to.
389+ */
390+ // [START group_findings_with_source]
391+ static ImmutableList <GroupResult > groupFindingsWithSource (SourceName sourceName ) {
392+ try (SecurityCenterClient client = SecurityCenterClient .create ()) {
393+ // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
394+ // "423432321");
395+
396+ GroupFindingsRequest .Builder request =
397+ GroupFindingsRequest .newBuilder ().setParent (sourceName .toString ()).setGroupBy ("category" );
398+
399+ // Call the API.
400+ GroupFindingsPagedResponse response = client .groupFindings (request .build ());
401+
402+ // This creates one list for all findings. If your organization has a large number of
403+ // findings
404+ // this can cause out of memory issues. You can process them batches by returning
405+ // the Iterable returned response.iterateAll() directly.
406+ ImmutableList <GroupResult > results = ImmutableList .copyOf (response .iterateAll ());
407+ System .out .println ("Findings:" );
408+ System .out .println (results );
409+ return results ;
410+ } catch (IOException e ) {
411+ throw new RuntimeException ("Couldn't create client." , e );
412+ }
413+ }
414+ // [END group_findings_with_source]
415+
416+ /**
417+ * Group active findings under an organization and a source by their specified properties (e.g.
418+ * category).
419+ *
420+ * @param sourceName The source to limit the findings to.
421+ */
422+ // [START group_active_findings_with_source]
423+ static ImmutableList <GroupResult > groupActiveFindingsWithSource (
424+ SourceName sourceName ) {
425+ try (SecurityCenterClient client = SecurityCenterClient .create ()) {
426+ // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
427+ // "423432321");
428+
429+ GroupFindingsRequest .Builder request =
430+ GroupFindingsRequest .newBuilder ()
431+ .setParent (sourceName .toString ())
432+ .setGroupBy ("category" )
433+ .setFilter ("state=\" ACTIVE\" " );
434+
435+ // Call the API.
436+ GroupFindingsPagedResponse response = client .groupFindings (request .build ());
437+
438+ // This creates one list for all findings. If your organization has a large number of
439+ // findings
440+ // this can cause out of memory issues. You can process them batches by returning
441+ // the Iterable returned response.iterateAll() directly.
442+ ImmutableList <GroupResult > results = ImmutableList .copyOf (response .iterateAll ());
443+ System .out .println ("Findings:" );
444+ System .out .println (results );
445+ return results ;
446+ } catch (IOException e ) {
447+ throw new RuntimeException ("Couldn't create client." , e );
448+ }
449+ }
450+ // [END group_active_findings_with_source]
451+
452+ /**
453+ * Group active findings under an organization and a source by their specified properties (e.g.
454+ * category) at a specified time.
455+ *
456+ * @param sourceName The source to limit the findings to.
457+ */
458+ // [START group_active_findings_with_source_at_time]
459+ static ImmutableList <GroupResult > groupActiveFindingsWithSourceAtTime (
460+ SourceName sourceName ) {
461+ try (SecurityCenterClient client = SecurityCenterClient .create ()) {
462+ // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
463+ // "423432321");
464+
465+ // 1 day ago
466+ Instant oneDayAgo = Instant .now ().minusSeconds (60 * 60 * 24 );
467+
468+ GroupFindingsRequest .Builder request =
469+ GroupFindingsRequest .newBuilder ()
470+ .setParent (sourceName .toString ())
471+ .setGroupBy ("category" )
472+ .setFilter ("state=\" ACTIVE\" " )
473+ .setReadTime (
474+ Timestamp .newBuilder ()
475+ .setSeconds (oneDayAgo .getEpochSecond ())
476+ .setNanos (oneDayAgo .getNano ()));
477+
478+ // Call the API.
479+ GroupFindingsPagedResponse response = client .groupFindings (request .build ());
480+
481+ // This creates one list for all findings. If your organization has a large number of
482+ // findings
483+ // this can cause out of memory issues. You can process them batches by returning
484+ // the Iterable returned response.iterateAll() directly.
485+ ImmutableList <GroupResult > results = ImmutableList .copyOf (response .iterateAll ());
486+ System .out .println ("Findings:" );
487+ System .out .println (results );
488+ return results ;
489+ } catch (IOException e ) {
490+ throw new RuntimeException ("Couldn't create client." , e );
491+ }
492+ }
493+ // [END group_active_findings_with_source_at_time]
494+
495+ /**
496+ * Group active findings under an organization and a source by their state_changes
497+ * (ADDED/CHANGED/UNCHANGED) during a period.
498+ *
499+ * @param sourceName The source to limit the findings to.
500+ */
501+ // [START group_active_findings_with_source_and_compare_duration]
502+ static ImmutableList <GroupResult > groupActiveFindingsWithSourceAndCompareDuration (
503+ SourceName sourceName , Duration duration ) {
504+ try (SecurityCenterClient client = SecurityCenterClient .create ()) {
505+ // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
506+ // "423432321");
507+
508+ GroupFindingsRequest .Builder request =
509+ GroupFindingsRequest .newBuilder ()
510+ .setParent (sourceName .toString ())
511+ .setGroupBy ("state_change" )
512+ .setFilter ("state=\" ACTIVE\" " );
513+ request
514+ .getCompareDurationBuilder ()
515+ .setSeconds (duration .getSeconds ())
516+ .setNanos (duration .getNano ());
517+
518+ // Call the API.
519+ GroupFindingsPagedResponse response = client .groupFindings (request .build ());
520+
521+ // This creates one list for all findings. If your organization has a large number of
522+ // findings
523+ // this can cause out of memory issues. You can process them batches by returning
524+ // the Iterable returned response.iterateAll() directly.
525+ ImmutableList <GroupResult > results = ImmutableList .copyOf (response .iterateAll ());
526+ System .out .println ("Findings:" );
527+ System .out .println (results );
528+ return results ;
529+ } catch (IOException e ) {
530+ throw new RuntimeException ("Couldn't create client." , e );
531+ }
532+ }
533+ // [END group_active_findings_with_source_and_compare_duration]
347534}
0 commit comments