Search in sources :

Example 26 with SecurityCenterClient

use of com.google.cloud.securitycenter.v1.SecurityCenterClient 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);
    }
}
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 27 with SecurityCenterClient

use of com.google.cloud.securitycenter.v1.SecurityCenterClient 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);
    }
}
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 28 with SecurityCenterClient

use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.

the class OrganizationSnippets method getOrganizationSettings.

/**
 * Gets current settings for an organization.
 *
 * @param organizationName The organization to get settings for.
 */
// [START securitycenter_get_org_settings]
static OrganizationSettings getOrganizationSettings(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to get OrganizationSettings for.
        // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
        GetOrganizationSettingsRequest.Builder request = GetOrganizationSettingsRequest.newBuilder().setName(organizationName.toString() + "/organizationSettings");
        // Call the API.
        OrganizationSettings response = client.getOrganizationSettings(request.build());
        System.out.println("Organization Settings:");
        System.out.println(response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : GetOrganizationSettingsRequest(com.google.cloud.securitycenter.v1.GetOrganizationSettingsRequest) OrganizationSettings(com.google.cloud.securitycenter.v1.OrganizationSettings) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 29 with SecurityCenterClient

use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.

the class OrganizationSnippets method updateOrganizationSettings.

// [END securitycenter_get_org_settings]
/**
 * Update Asset Discovery OrganizationSettings for an organization
 *
 * @param organizationName The organization to update settings for.
 */
// [START securitycenter_enable_asset_discovery]
static OrganizationSettings updateOrganizationSettings(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to update OrganizationSettings for.
        // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
        OrganizationSettings organizationSettings = OrganizationSettings.newBuilder().setName(organizationName.toString() + "/organizationSettings").setEnableAssetDiscovery(true).build();
        FieldMask updateMask = FieldMask.newBuilder().addPaths("enable_asset_discovery").build();
        UpdateOrganizationSettingsRequest.Builder request = UpdateOrganizationSettingsRequest.newBuilder().setOrganizationSettings(organizationSettings).setUpdateMask(updateMask);
        // Call the API.
        OrganizationSettings response = client.updateOrganizationSettings(request.build());
        System.out.println("Organization Settings have been updated:");
        System.out.println(response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : UpdateOrganizationSettingsRequest(com.google.cloud.securitycenter.v1.UpdateOrganizationSettingsRequest) OrganizationSettings(com.google.cloud.securitycenter.v1.OrganizationSettings) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) FieldMask(com.google.protobuf.FieldMask)

Example 30 with SecurityCenterClient

use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.

the class FindingSnippets method listFilteredFindings.

// [END securitycenter_list_all_findings]
/**
 * List filtered findings under a source.
 *
 * @param sourceName The source to list filtered findings for.
 */
// [START securitycenter_list_filtered_findings]
static ImmutableList<ListFindingsResult> listFilteredFindings(SourceName sourceName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // SourceName sourceName = SourceName.of(/*organizationId=*/"123234324",
        // /*sourceId=*/"423432321");
        // Create filter to category of MEDIUM_RISK_ONE
        String filter = "category=\"MEDIUM_RISK_ONE\"";
        ListFindingsRequest.Builder request = ListFindingsRequest.newBuilder().setParent(sourceName.toString()).setFilter(filter);
        // 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 : 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

SecurityCenterClient (com.google.cloud.securitycenter.v1.SecurityCenterClient)35 IOException (java.io.IOException)35 GroupResult (com.google.cloud.securitycenter.v1.GroupResult)8 FieldMask (com.google.protobuf.FieldMask)7 Instant (org.threeten.bp.Instant)6 GroupFindingsRequest (com.google.cloud.securitycenter.v1.GroupFindingsRequest)5 ListAssetsRequest (com.google.cloud.securitycenter.v1.ListAssetsRequest)5 ListAssetsResult (com.google.cloud.securitycenter.v1.ListAssetsResponse.ListAssetsResult)5 GroupFindingsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupFindingsPagedResponse)5 ListAssetsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.ListAssetsPagedResponse)5 Finding (com.google.cloud.securitycenter.v1.Finding)4 ListFindingsRequest (com.google.cloud.securitycenter.v1.ListFindingsRequest)4 ListFindingsResult (com.google.cloud.securitycenter.v1.ListFindingsResponse.ListFindingsResult)4 ListFindingsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.ListFindingsPagedResponse)4 SecurityMarks (com.google.cloud.securitycenter.v1.SecurityMarks)4 Source (com.google.cloud.securitycenter.v1.Source)4 UpdateSecurityMarksRequest (com.google.cloud.securitycenter.v1.UpdateSecurityMarksRequest)4 GroupAssetsRequest (com.google.cloud.securitycenter.v1.GroupAssetsRequest)3 GroupAssetsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupAssetsPagedResponse)3 OrganizationSettings (com.google.cloud.securitycenter.v1.OrganizationSettings)2