Search in sources :

Example 21 with OrganizationName

use of com.google.cloud.securitycenter.v1.OrganizationName in project google-cloud-java by googleapis.

the class AssetSnippets method groupAssets.

// [END securitycenter_list_assets_and_changes]
/**
 * Groups all assets by their specified properties (e.g. type) for an organization.
 *
 * @param organizationName The organization to group assets for.
 */
// [START securitycenter_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);
    }
}
Also used : GroupAssetsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupAssetsPagedResponse) GroupResult(com.google.cloud.securitycenter.v1.GroupResult) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) GroupAssetsRequest(com.google.cloud.securitycenter.v1.GroupAssetsRequest)

Example 22 with OrganizationName

use of com.google.cloud.securitycenter.v1.OrganizationName in project google-cloud-java by googleapis.

the class AssetSnippets method listAssetsWithFilter.

// [END securitycenter_list_all_assets]
/**
 * Lists all project assets for an organization.
 *
 * @param organizationName The organization to list assets for.
 */
// [START securitycenter_list_assets_with_filter]
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(/*organizationId=*/"123234324");
        ListAssetsRequest.Builder request = ListAssetsRequest.newBuilder().setParent(organizationName.toString()).setFilter("security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"");
        // 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("Project assets:");
        System.out.println(results);
        return results;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : ListAssetsRequest(com.google.cloud.securitycenter.v1.ListAssetsRequest) ListAssetsResult(com.google.cloud.securitycenter.v1.ListAssetsResponse.ListAssetsResult) ListAssetsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.ListAssetsPagedResponse) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 23 with OrganizationName

use of com.google.cloud.securitycenter.v1.OrganizationName in project google-cloud-java by googleapis.

the class AssetSnippets method groupAssetsWithCompareDuration.

// [END securitycenter_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 securitycenter_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);
    }
}
Also used : GroupAssetsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupAssetsPagedResponse) GroupResult(com.google.cloud.securitycenter.v1.GroupResult) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) GroupAssetsRequest(com.google.cloud.securitycenter.v1.GroupAssetsRequest)

Example 24 with OrganizationName

use of com.google.cloud.securitycenter.v1.OrganizationName in project google-cloud-java by googleapis.

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.");
    }
}
Also used : ResourceExhaustedException(com.google.api.gax.rpc.ResourceExhaustedException) Empty(com.google.protobuf.Empty) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) RunAssetDiscoveryResponse(com.google.cloud.securitycenter.v1.RunAssetDiscoveryResponse)

Example 25 with OrganizationName

use of com.google.cloud.securitycenter.v1.OrganizationName in project google-cloud-java by googleapis.

the class FindingSnippets method listAllFindings.

// [END securitycenter_update_finding_state]
/**
 * List all findings under an organization.
 *
 * @param organizationName The source to list all findings for.
 */
// [START securitycenter_list_all_findings]
static ImmutableList<ListFindingsResult> listAllFindings(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
        // "-" Indicates listing across all sources.
        SourceName sourceName = SourceName.of(organizationName.getOrganization(), "-");
        ListFindingsRequest.Builder request = ListFindingsRequest.newBuilder().setParent(sourceName.toString());
        // Call the API.
        ListFindingsPagedResponse response = client.listFindings(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 in incrementally
        // by returning the Iterable returned response.iterateAll() directly.
        ImmutableList<ListFindingsResult> 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);
    }
}
Also used : SourceName(com.google.cloud.securitycenter.v1.SourceName) ListFindingsRequest(com.google.cloud.securitycenter.v1.ListFindingsRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) ListFindingsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.ListFindingsPagedResponse) ListFindingsResult(com.google.cloud.securitycenter.v1.ListFindingsResponse.ListFindingsResult)

Aggregations

Test (org.junit.Test)66 AbstractMessage (com.google.protobuf.AbstractMessage)35 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)31 SecurityCenterClient (com.google.cloud.securitycenter.v1.SecurityCenterClient)31 StatusRuntimeException (io.grpc.StatusRuntimeException)31 OrganizationName (com.google.monitoring.v3.OrganizationName)30 IOException (java.io.IOException)30 OrganizationName (com.google.logging.v2.OrganizationName)22 OrganizationName (com.google.privacy.dlp.v2.OrganizationName)12 ListAssetsRequest (com.google.cloud.securitycenter.v1.ListAssetsRequest)10 ListAssetsResult (com.google.cloud.securitycenter.v1.ListAssetsResponse.ListAssetsResult)10 ListAssetsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.ListAssetsPagedResponse)10 GroupResult (com.google.cloud.securitycenter.v1.GroupResult)8 LogExclusion (com.google.logging.v2.LogExclusion)7 LogSink (com.google.logging.v2.LogSink)7 ArrayList (java.util.ArrayList)7 GroupAssetsRequest (com.google.cloud.securitycenter.v1.GroupAssetsRequest)6 GroupAssetsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupAssetsPagedResponse)6 MetricDescriptor (com.google.api.MetricDescriptor)3 MockGrpcService (com.google.api.gax.grpc.testing.MockGrpcService)3