Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -15,14 +15,20 @@
*/
package com.google.cloud.examples.securitycenter.snippets;

import com.google.cloud.securitycenter.v1beta1.ListAssetsRequest;
import com.google.cloud.securitycenter.v1beta1.ListAssetsResponse.ListAssetsResult;
import com.google.cloud.securitycenter.v1beta1.OrganizationName;
import com.google.cloud.securitycenter.v1beta1.SecurityCenterClient;
import com.google.cloud.securitycenter.v1beta1.SecurityCenterClient.ListAssetsPagedResponse;
import com.google.api.gax.longrunning.OperationFuture;
import com.google.api.gax.rpc.ResourceExhaustedException;
import com.google.cloud.securitycenter.v1.GroupAssetsRequest;
import com.google.cloud.securitycenter.v1.GroupResult;
import com.google.cloud.securitycenter.v1.ListAssetsRequest;
import com.google.cloud.securitycenter.v1.ListAssetsResponse.ListAssetsResult;
import com.google.cloud.securitycenter.v1.OrganizationName;
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
import com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupAssetsPagedResponse;
import com.google.cloud.securitycenter.v1.SecurityCenterClient.ListAssetsPagedResponse;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.protobuf.Empty;
import java.io.IOException;
import org.threeten.bp.Duration;
import org.threeten.bp.Instant;
Expand All @@ -40,15 +46,15 @@ private AssetSnippets() {}
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("123234324");
// 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 batches by returning
// 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:");
Expand All @@ -69,22 +75,21 @@ static ImmutableList<ListAssetsResult> listAssets(OrganizationName organizationN
static ImmutableList<ListAssetsResult> listAssetsWithFilter(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("123234324");
ListAssetsRequest request =
// OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
ListAssetsRequest.Builder request =
ListAssetsRequest.newBuilder()
.setParent(organizationName.toString())
.setFilter(
"security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"")
.build();
"security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"");

// Call the API.
ListAssetsPagedResponse response = client.listAssets(request);
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 batches by returning
// 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("Project assets:");
System.out.println(results);
return results;
} catch (IOException e) {
Expand All @@ -104,7 +109,7 @@ 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("123234324");
// OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");

// Initialize the builder with the organization and filter
ListAssetsRequest.Builder request =
Expand All @@ -121,7 +126,7 @@ static ImmutableList<ListAssetsResult> listAssetsAsOfYesterday(
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 batches by returning
// 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:");
Expand All @@ -146,7 +151,7 @@ static ImmutableList<ListAssetsResult> listAssetAndStatusChanges(
try (SecurityCenterClient client = SecurityCenterClient.create()) {

// Start setting up a request for to search for all assets in an organization.
// OrganizationName organizationName = OrganizationName.of("123234324");
// OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
ListAssetsRequest.Builder request =
ListAssetsRequest.newBuilder()
.setParent(organizationName.toString())
Expand All @@ -165,7 +170,7 @@ static ImmutableList<ListAssetsResult> listAssetAndStatusChanges(
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 batches by returning
// 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:");
Expand All @@ -177,6 +182,129 @@ static ImmutableList<ListAssetsResult> listAssetAndStatusChanges(
}
// [END list_asset_changes_status_changes]

/**
* Groups all assets by their specified properties (e.g. type) for an organization.
*
* @param organizationName The organization to group assets for.
*/
// [START group_all_assets]
static ImmutableList<GroupResult> groupAssets(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Start setting up a request for to group all assets by type in an organization.
// OrganizationName organizationName = OrganizationName.of("123234324");
GroupAssetsRequest.Builder request =
GroupAssetsRequest.newBuilder()
.setGroupBy("security_center_properties.resource_type")
.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);
}
}
// [END 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 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);
}
}
// [END group_all_assets_with_filter]

/**
* Groups all assets by their state_changes (ADDED/DELETED/ACTIVE) during a period of time for an
* organization.
*
* @param organizationName The organization to group assets for.
*/
// [START group_all_assets_with_compare_duration]
static ImmutableList<GroupResult> groupAssetsWithCompareDuration(
OrganizationName organizationName, Duration duration) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Start setting up a request for to group all assets during a period of time in an
// organization.
// OrganizationName organizationName = OrganizationName.of("123234324");
GroupAssetsRequest.Builder request =
GroupAssetsRequest.newBuilder()
.setGroupBy("state_change")
.setParent(organizationName.toString());
request
.getCompareDurationBuilder()
.setSeconds(duration.getSeconds())
.setNanos(duration.getNano());

// 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);
}
}
// [END group_all_assets_with_compare_duration]

// [START 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<Empty, 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.");
}
}
// [END run_asset_discovery]

public static void main(String... args) {
String org_id = System.getenv("ORGANIZATION_ID");
if (args.length > 0) {
Expand Down
Loading