use of com.google.cloud.securitycenter.v1.OrganizationName in project google-cloud-java by GoogleCloudPlatform.
the class AssetSnippets method groupAssetsWithFilter.
// [END securitycenter_group_all_assets]
/**
* Filters all assets by their specified properties and groups them by specified properties for an
* organization.
*
* @param organizationName The organization to group assets for.
*/
// [START securitycenter_group_all_assets_with_filter]
static ImmutableList<GroupResult> groupAssetsWithFilter(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Start setting up a request for to filter all assets by type and group them by project in an
// organization.
// OrganizationName organizationName = OrganizationName.of("123234324");
GroupAssetsRequest.Builder request = GroupAssetsRequest.newBuilder().setFilter("security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"").setGroupBy("security_center_properties.resource_project").setParent(organizationName.toString());
// Call the API.
GroupAssetsPagedResponse response = client.groupAssets(request.build());
// This creates one list for all assets. If your organization has a large number of assets
// this can cause out of memory issues. You can process them batches by returning
// the Iterable returned response.iterateAll() directly.
ImmutableList<GroupResult> results = ImmutableList.copyOf(response.iterateAll());
System.out.println("All assets:");
System.out.println(results);
return results;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.securitycenter.v1.OrganizationName in project google-cloud-java by GoogleCloudPlatform.
the class AssetSnippets method listAssetsAsOfYesterday.
// [END securitycenter_list_assets_with_filter]
/**
* Lists all project assets for an organization at a given point in time.
*
* @param organizationName The organization to list assets for.
* @param asOf The snapshot time to query for assets. If null defaults to one day ago.
*/
// [START securitycenter_list_assets_at_time]
static ImmutableList<ListAssetsResult> listAssetsAsOfYesterday(OrganizationName organizationName, Instant asOf) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Start setting up a request for to search for all assets in an organization.
// OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
// Initialize the builder with the organization and filter
ListAssetsRequest.Builder request = ListAssetsRequest.newBuilder().setParent(organizationName.toString()).setFilter("security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"");
// Set read time to either the instant passed in or one day ago.
asOf = MoreObjects.firstNonNull(asOf, Instant.now().minus(Duration.ofDays(1)));
request.getReadTimeBuilder().setSeconds(asOf.getEpochSecond()).setNanos(asOf.getNano());
// Call the API.
ListAssetsPagedResponse response = client.listAssets(request.build());
// This creates one list for all assets. If your organization has a large number of assets
// this can cause out of memory issues. You can process them incrementally by returning
// the Iterable returned response.iterateAll() directly.
ImmutableList<ListAssetsResult> results = ImmutableList.copyOf(response.iterateAll());
System.out.println("Projects:");
System.out.println(results);
return results;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.securitycenter.v1.OrganizationName in project google-cloud-java by GoogleCloudPlatform.
the class AssetSnippets method listAssets.
/**
* Lists all assets for an organization.
*
* @param organizationName The organization to list assets for.
*/
// [START securitycenter_list_all_assets]
static ImmutableList<ListAssetsResult> listAssets(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Start setting up a request for to search for all assets in an organization.
// OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
ListAssetsRequest.Builder request = ListAssetsRequest.newBuilder().setParent(organizationName.toString());
// Call the API.
ListAssetsPagedResponse response = client.listAssets(request.build());
// This creates one list for all assets. If your organization has a large number of assets
// this can cause out of memory issues. You can process them incrementally by returning
// the Iterable returned response.iterateAll() directly.
ImmutableList<ListAssetsResult> results = ImmutableList.copyOf(response.iterateAll());
System.out.println("All assets:");
System.out.println(results);
return results;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.securitycenter.v1.OrganizationName in project google-cloud-java by GoogleCloudPlatform.
the class AssetSnippets method runAssetDiscovery.
// [END securitycenter_group_all_assets_with_compare_duration]
// [START securitycenter_run_asset_discovery]
static void runAssetDiscovery(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Call the API. Note calls to runAssetDiscovery are throttled if too many requests
// are made.
OperationFuture<RunAssetDiscoveryResponse, Empty> result = client.runAssetDiscoveryAsync(organizationName);
// Uncomment this line to wait for a certain amount of time for the asset discovery run
// to complete.
// result.get(130, TimeUnit.SECONDS);
System.out.println("Asset discovery runs asynchronously.");
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
} catch (ResourceExhaustedException e) {
System.out.println("Asset discovery run already in progress.");
}
}
use of com.google.cloud.securitycenter.v1.OrganizationName in project google-cloud-java by GoogleCloudPlatform.
the class FindingSnippets method groupFindings.
// [END securitycenter_test_iam]
/**
* Group all findings under an organization across all sources by their specified properties (e.g.
* category).
*
* @param organizationName The organizatoin to group all findings for.
*/
// [START securitycenter_group_all_findings]
static ImmutableList<GroupResult> groupFindings(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// OrganizationName organizationName = OrganizationName.of("123234324");
SourceName sourceName = SourceName.of(organizationName.getOrganization(), "-");
GroupFindingsRequest.Builder request = GroupFindingsRequest.newBuilder().setParent(sourceName.toString()).setGroupBy("category");
// Call the API.
GroupFindingsPagedResponse response = client.groupFindings(request.build());
// This creates one list for all findings. If your organization has a large number of
// findings
// this can cause out of memory issues. You can process them batches by returning
// the Iterable returned response.iterateAll() directly.
ImmutableList<GroupResult> results = ImmutableList.copyOf(response.iterateAll());
System.out.println("Findings:");
System.out.println(results);
return results;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
Aggregations