Search in sources :

Example 11 with SecurityCenterClient

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

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 12 with SecurityCenterClient

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

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 13 with SecurityCenterClient

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

the class SourceSnippets method listSources.

// [END securitycenter_create_source]
/**
 * List sources under an organization.
 *
 * @param organizationName The organization for the source.
 */
// [START securitycenter_list_sources]
static ImmutableList<Source> listSources(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to list sources in an organization.
        // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
        ListSourcesRequest.Builder request = ListSourcesRequest.newBuilder().setParent(organizationName.toString());
        // Call the API.
        ListSourcesPagedResponse response = client.listSources(request.build());
        // This creates one list for all sources.  If your organization has a large number of sources
        // this can cause out of memory issues.  You can process them batches by returning
        // the Iterable returned response.iterateAll() directly.
        ImmutableList<Source> results = ImmutableList.copyOf(response.iterateAll());
        System.out.println("Sources:");
        System.out.println(results);
        return results;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : ListSourcesRequest(com.google.cloud.securitycenter.v1.ListSourcesRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) ListSourcesPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.ListSourcesPagedResponse) Source(com.google.cloud.securitycenter.v1.Source)

Example 14 with SecurityCenterClient

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

the class SourceSnippets method setIamPolicySource.

// [END securitycenter_get_source]
/**
 * Set IAM policy for a source.
 *
 * @param sourceName The source to set IAM Policy for.
 */
// [START securitycenter_set_source_iam]
static Policy setIamPolicySource(SourceName sourceName, String userEmail) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // userEmail = "someuser@domain.com"
        // Set up IAM Policy for the user userMail to use the role findingsEditor.
        // The user must be a valid google account.
        Policy oldPolicy = client.getIamPolicy(sourceName.toString());
        Binding bindings = Binding.newBuilder().setRole("roles/securitycenter.findingsEditor").addMembers("user:" + userEmail).build();
        Policy policy = oldPolicy.toBuilder().addBindings(bindings).build();
        // Start setting up a request to set IAM policy for a source.
        // SourceName sourceName = SourceName.of("123234324", "423432321");
        SetIamPolicyRequest.Builder request = SetIamPolicyRequest.newBuilder().setPolicy(policy).setResource(sourceName.toString());
        // Call the API.
        Policy response = client.setIamPolicy(request.build());
        System.out.println("Policy: " + response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) SetIamPolicyRequest(com.google.iam.v1.SetIamPolicyRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 15 with SecurityCenterClient

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

the class SourceSnippets method updateSource.

// [END securitycenter_list_sources]
/**
 * Update a source under an organization.
 *
 * @param sourceName The source to update.
 */
// [START securitycenter_update_source]
static Source updateSource(SourceName sourceName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to update a source.
        // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
        // "423432321");
        Source source = Source.newBuilder().setDisplayName("Updated Display Name").setName(sourceName.toString()).build();
        FieldMask updateMask = FieldMask.newBuilder().addPaths("display_name").build();
        UpdateSourceRequest.Builder request = UpdateSourceRequest.newBuilder().setSource(source).setUpdateMask(updateMask);
        // Call the API.
        Source response = client.updateSource(request.build());
        System.out.println("Updated Source: " + response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : UpdateSourceRequest(com.google.cloud.securitycenter.v1.UpdateSourceRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) Source(com.google.cloud.securitycenter.v1.Source) FieldMask(com.google.protobuf.FieldMask)

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