Skip to content

Commit 74ad310

Browse files
author
Micah Kornfield
committed
Add snippets for asset discovery and group findings/assets
1 parent e015cc3 commit 74ad310

4 files changed

Lines changed: 400 additions & 2 deletions

File tree

google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/AssetSnippets.java

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,24 @@
1515
*/
1616
package com.google.cloud.examples.securitycenter.snippets;
1717

18+
import com.google.api.gax.longrunning.OperationFuture;
19+
import com.google.api.gax.rpc.ResourceExhaustedException;
20+
import com.google.cloud.securitycenter.v1.GroupAssetsRequest;
21+
import com.google.cloud.securitycenter.v1.GroupResult;
1822
import com.google.cloud.securitycenter.v1.ListAssetsRequest;
1923
import com.google.cloud.securitycenter.v1.ListAssetsResponse.ListAssetsResult;
2024
import com.google.cloud.securitycenter.v1.OrganizationName;
2125
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
26+
import com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupAssetsPagedResponse;
2227
import com.google.cloud.securitycenter.v1.SecurityCenterClient.ListAssetsPagedResponse;
2328
import com.google.common.base.MoreObjects;
2429
import com.google.common.base.Preconditions;
2530
import com.google.common.collect.ImmutableList;
31+
import com.google.protobuf.Empty;
2632
import java.io.IOException;
33+
import java.util.concurrent.ExecutionException;
34+
import java.util.concurrent.TimeUnit;
35+
import java.util.concurrent.TimeoutException;
2736
import org.threeten.bp.Duration;
2837
import org.threeten.bp.Instant;
2938

@@ -174,7 +183,134 @@ static ImmutableList<ListAssetsResult> listAssetAndStatusChanges(
174183
throw new RuntimeException("Couldn't create client.", e);
175184
}
176185
}
177-
// [END list_asset_changes_status_changes]
186+
// [END list_asset_changes_status_changes]\
187+
188+
189+
/**
190+
* Groups all assets by their specified properties (e.g. type) for an organization.
191+
*
192+
* @param organizationName The organization to group assets for.
193+
*/
194+
// [START group_all_assets]
195+
static ImmutableList<GroupResult> groupAssets(OrganizationName organizationName) {
196+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
197+
// Start setting up a request for to group all assets by type in an organization.
198+
// OrganizationName organizationName = OrganizationName.of("123234324");
199+
GroupAssetsRequest.Builder request =
200+
GroupAssetsRequest.newBuilder()
201+
.setGroupBy("security_center_properties.resource_type")
202+
.setParent(organizationName.toString());
203+
204+
// Call the API.
205+
GroupAssetsPagedResponse response = client.groupAssets(request.build());
206+
207+
// This creates one list for all assets. If your organization has a large number of assets
208+
// this can cause out of memory issues. You can process them batches by returning
209+
// the Iterable returned response.iterateAll() directly.
210+
ImmutableList<GroupResult> results = ImmutableList.copyOf(response.iterateAll());
211+
System.out.println("All assets:");
212+
System.out.println(results);
213+
return results;
214+
} catch (IOException e) {
215+
throw new RuntimeException("Couldn't create client.", e);
216+
}
217+
}
218+
// [END group_all_assets]
219+
220+
/**
221+
* Filters all assets by their specified properties and groups them by specified properties for an
222+
* organization.
223+
*
224+
* @param organizationName The organization to group assets for.
225+
*/
226+
// [START group_all_assets_with_filter]
227+
static ImmutableList<GroupResult> groupAssetsWithFilter(OrganizationName organizationName) {
228+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
229+
// Start setting up a request for to filter all assets by type and group them by project in an
230+
// organization.
231+
// OrganizationName organizationName = OrganizationName.of("123234324");
232+
GroupAssetsRequest.Builder request =
233+
GroupAssetsRequest.newBuilder()
234+
.setFilter(
235+
"security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"")
236+
.setGroupBy("security_center_properties.resource_project")
237+
.setParent(organizationName.toString());
238+
239+
// Call the API.
240+
GroupAssetsPagedResponse response = client.groupAssets(request.build());
241+
242+
// This creates one list for all assets. If your organization has a large number of assets
243+
// this can cause out of memory issues. You can process them batches by returning
244+
// the Iterable returned response.iterateAll() directly.
245+
ImmutableList<GroupResult> results = ImmutableList.copyOf(response.iterateAll());
246+
System.out.println("All assets:");
247+
System.out.println(results);
248+
return results;
249+
} catch (IOException e) {
250+
throw new RuntimeException("Couldn't create client.", e);
251+
}
252+
}
253+
// [END group_all_assets_with_filter]
254+
255+
/**
256+
* Groups all assets by their state_changes (ADDED/DELETED/ACTIVE) during a period of time for an
257+
* organization.
258+
*
259+
* @param organizationName The organization to group assets for.
260+
*/
261+
// [START group_all_assets_with_compare_duration]
262+
static ImmutableList<GroupResult> groupAssetsWithCompareDuration(
263+
OrganizationName organizationName, Duration duration) {
264+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
265+
// Start setting up a request for to group all assets during a period of time in an
266+
// organization.
267+
// OrganizationName organizationName = OrganizationName.of("123234324");
268+
GroupAssetsRequest.Builder request =
269+
GroupAssetsRequest.newBuilder()
270+
.setGroupBy("state_change")
271+
.setParent(organizationName.toString());
272+
request
273+
.getCompareDurationBuilder()
274+
.setSeconds(duration.getSeconds())
275+
.setNanos(duration.getNano());
276+
277+
// Call the API.
278+
GroupAssetsPagedResponse response = client.groupAssets(request.build());
279+
280+
// This creates one list for all assets. If your organization has a large number of assets
281+
// this can cause out of memory issues. You can process them batches by returning
282+
// the Iterable returned response.iterateAll() directly.
283+
ImmutableList<GroupResult> results = ImmutableList.copyOf(response.iterateAll());
284+
System.out.println("All assets:");
285+
System.out.println(results);
286+
return results;
287+
} catch (IOException e) {
288+
throw new RuntimeException("Couldn't create client.", e);
289+
}
290+
}
291+
// [END group_all_assets_with_compare_duration]
292+
293+
// [START run_asset_discovery]
294+
static void runAssetDiscovery(OrganizationName organizationName) {
295+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
296+
// Call the API. Note calls to runAssetDiscovery are throttled if too many requests
297+
// are made.
298+
OperationFuture<Empty, Empty> result = client
299+
.runAssetDiscoveryAsync(organizationName);
300+
301+
302+
// Uncomment this line to wait for a certain amount of time for the asset discovery run
303+
// to complete.
304+
// result.get(130, TimeUnit.SECONDS);
305+
System.out.println("Asset discovery runs asynchronously.");
306+
} catch (IOException e) {
307+
throw new RuntimeException("Couldn't create client.", e);
308+
} catch (ResourceExhaustedException e) {
309+
System.out.println("Asset discovery run already in progress.");
310+
}
311+
}
312+
// [END run_asset_discovery]
313+
178314

179315
public static void main(String... args) {
180316
String org_id = System.getenv("ORGANIZATION_ID");

google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/FindingSnippets.java

Lines changed: 188 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
import com.google.cloud.securitycenter.v1.Finding;
1919
import com.google.cloud.securitycenter.v1.Finding.State;
2020
import com.google.cloud.securitycenter.v1.FindingName;
21+
import com.google.cloud.securitycenter.v1.GroupFindingsRequest;
22+
import com.google.cloud.securitycenter.v1.GroupResult;
2123
import com.google.cloud.securitycenter.v1.ListFindingsRequest;
2224
import com.google.cloud.securitycenter.v1.ListFindingsResponse.ListFindingsResult;
2325
import com.google.cloud.securitycenter.v1.OrganizationName;
2426
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
27+
import com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupFindingsPagedResponse;
2528
import com.google.cloud.securitycenter.v1.SecurityCenterClient.ListFindingsPagedResponse;
2629
import com.google.cloud.securitycenter.v1.SourceName;
2730
import com.google.cloud.securitycenter.v1.UpdateFindingRequest;
@@ -33,6 +36,7 @@
3336
import com.google.protobuf.Value;
3437
import java.io.IOException;
3538
import java.util.ArrayList;
39+
import java.util.List;
3640
import org.threeten.bp.Duration;
3741
import 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

Comments
 (0)