From 81d3c05ae24c12729467846a2ab65e6302670884 Mon Sep 17 00:00:00 2001 From: Kevin Gordillo Date: Fri, 19 Jun 2026 17:04:57 +0000 Subject: [PATCH 1/6] feat: remove deprecated setReadTime method, moved Security Command Center snippets and integration tests to java docs sample --- securitycenter/snippets/pom.xml | 54 ++ .../securitycenter/FindingSnippets.java | 565 ++++++++++++++++++ .../securitycenter/SourceSnippets.java | 222 +++++++ .../securitycenter/ITFindingSnippets.java | 126 ++++ .../securitycenter/ITSourceSnippets.java | 75 +++ 5 files changed, 1042 insertions(+) create mode 100644 securitycenter/snippets/pom.xml create mode 100644 securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java create mode 100644 securitycenter/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java create mode 100644 securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java create mode 100644 securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java diff --git a/securitycenter/snippets/pom.xml b/securitycenter/snippets/pom.xml new file mode 100644 index 00000000000..caf5a8eed2d --- /dev/null +++ b/securitycenter/snippets/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + com.example.securitycenter + securitycenter-snippets + jar + Google Security Center Snippets + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + 1.8 + 1.8 + UTF-8 + + + + + + com.google.cloud + libraries-bom + 26.33.0 + pom + import + + + + + + + com.google.cloud + google-cloud-securitycenter + + + + junit + junit + 4.13.2 + test + + + com.google.truth + truth + 1.4.0 + test + + + \ No newline at end of file diff --git a/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java b/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java new file mode 100644 index 00000000000..ef9c928a58e --- /dev/null +++ b/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java @@ -0,0 +1,565 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * 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, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.securitycenter; + +import com.google.cloud.securitycenter.v1.Finding; +import com.google.cloud.securitycenter.v1.Finding.State; +import com.google.cloud.securitycenter.v1.FindingName; +import com.google.cloud.securitycenter.v1.GroupFindingsRequest; +import com.google.cloud.securitycenter.v1.GroupResult; +import com.google.cloud.securitycenter.v1.ListFindingsRequest; +import com.google.cloud.securitycenter.v1.ListFindingsResponse.ListFindingsResult; +import com.google.cloud.securitycenter.v1.OrganizationName; +import com.google.cloud.securitycenter.v1.SecurityCenterClient; +import com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupFindingsPagedResponse; +import com.google.cloud.securitycenter.v1.SecurityCenterClient.ListFindingsPagedResponse; +import com.google.cloud.securitycenter.v1.SourceName; +import com.google.cloud.securitycenter.v1.UpdateFindingRequest; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.iam.v1.TestIamPermissionsResponse; +import com.google.protobuf.FieldMask; +import com.google.protobuf.Timestamp; +import com.google.protobuf.Value; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.threeten.bp.Duration; +import org.threeten.bp.Instant; + +/** + * Snippets for how to work with Findings in Security Command Center. + */ +public class FindingSnippets { + + private FindingSnippets() { + } + + /** + * Create a finding under a source. + * + * @param sourceName The source for the finding. + */ + // [START securitycenter_create_finding] + static Finding createFinding(SourceName sourceName, String findingId) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ + // "423432321"); + // String findingId = "samplefindingid"; + + // Use the current time as the finding "event time". + Instant eventTime = Instant.now(); + + // The resource this finding applies to. The CSCC UI can link + // the findings for a resource to the corresponding Asset of a resource + // if there are matches. + String resourceName = "//cloudresourcemanager.googleapis.com/organizations/11232"; + + // Start setting up a request to create a finding in a source. + Finding finding = + Finding.newBuilder() + .setParent(sourceName.toString()) + .setState(State.ACTIVE) + .setResourceName(resourceName) + .setEventTime( + Timestamp.newBuilder() + .setSeconds(eventTime.getEpochSecond()) + .setNanos(eventTime.getNano())) + .setCategory("MEDIUM_RISK_ONE") + .build(); + + // Call the API. + Finding response = client.createFinding(sourceName, findingId, finding); + + System.out.println("Created Finding: " + response); + return response; + } catch (IOException e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END securitycenter_create_finding] + + /** + * Create a finding with source properties under a source. + * + * @param sourceName The source for the finding. + */ + // [START securitycenter_create_finding_with_source_properties] + static Finding createFindingWithSourceProperties(SourceName sourceName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ + // "423432321"); + + // Use the current time as the finding "event time". + Instant eventTime = Instant.now(); + + // Controlled by caller. + String findingId = "samplefindingid2"; + + // The resource this finding applies to. The CSCC UI can link + // the findings for a resource to the corresponding Asset of a resource + // if there are matches. + String resourceName = "//cloudresourcemanager.googleapis.com/organizations/11232"; + + // Define source properties values as protobuf "Value" objects. + Value stringValue = Value.newBuilder().setStringValue("stringExample").build(); + Value numValue = Value.newBuilder().setNumberValue(1234).build(); + ImmutableMap sourceProperties = + ImmutableMap.of("stringKey", stringValue, "numKey", numValue); + + // Start setting up a request to create a finding in a source. + Finding finding = + Finding.newBuilder() + .setParent(sourceName.toString()) + .setState(State.ACTIVE) + .setResourceName(resourceName) + .setEventTime( + Timestamp.newBuilder() + .setSeconds(eventTime.getEpochSecond()) + .setNanos(eventTime.getNano())) + .putAllSourceProperties(sourceProperties) + .build(); + + // Call the API. + Finding response = client.createFinding(sourceName, findingId, finding); + + System.out.println("Created Finding with Source Properties: " + response); + return response; + } catch (IOException e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END securitycenter_create_finding_with_source_properties] + + /** + * Update a finding's source properties. + * + * @param findingName The finding to update. + */ + // [START securitycenter_update_finding_source_properties] + static Finding updateFinding(FindingName findingName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // FindingName findingName = FindingName.of(/*organization=*/"123234324", + // /*source=*/"423432321", /*findingId=*/"samplefindingid2"); + + // Use the current time as the finding "event time". + Instant eventTime = Instant.now(); + + // Define source properties values as protobuf "Value" objects. + Value stringValue = Value.newBuilder().setStringValue("value").build(); + + FieldMask updateMask = + FieldMask.newBuilder() + .addPaths("event_time") + .addPaths("source_properties.stringKey") + .build(); + + Finding finding = + Finding.newBuilder() + .setName(findingName.toString()) + .setEventTime( + Timestamp.newBuilder() + .setSeconds(eventTime.getEpochSecond()) + .setNanos(eventTime.getNano())) + .putSourceProperties("stringKey", stringValue) + .build(); + + UpdateFindingRequest.Builder request = + UpdateFindingRequest.newBuilder().setFinding(finding).setUpdateMask(updateMask); + + // Call the API. + Finding response = client.updateFinding(request.build()); + + System.out.println("Updated Finding: " + response); + return response; + } catch (IOException e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END securitycenter_update_finding_source_properties] + + /** + * Updates a finding's state to INACTIVE. + * + * @param findingName The finding to update. + */ + // [START securitycenter_update_finding_state] + static Finding setFindingState(FindingName findingName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // FindingName findingName = FindingName.of(/*organization=*/"123234324", + // /*source=*/"423432321", /*findingId=*/"samplefindingid2"); + + // Use the current time as the finding "event time". + Instant eventTime = Instant.now(); + + Finding response = + client.setFindingState( + findingName, + State.INACTIVE, + Timestamp.newBuilder() + .setSeconds(eventTime.getEpochSecond()) + .setNanos(eventTime.getNano()) + .build()); + + System.out.println("Updated Finding: " + response); + return response; + } catch (IOException e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [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 listAllFindings(OrganizationName organizationName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // Input parameters for SourceName must be in one of the following formats: + // * OrganizationName organizationName = OrganizationName.of("organization-id"); + // organizationName.getOrganization(); + // * ProjectName projectName = ProjectName.of("project-id"); + // projectName.getProject(); + // * FolderName folderName = FolderName.of("folder-id"); + // folderName.getFolder(); + // + // "-" 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 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); + } + } + // [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 listFilteredFindings(SourceName sourceName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // parentId: must be one of the following: + // "organization-id" + // "project-id" + // "folder-id" + // SourceName sourceName = SourceName.of(parentId, sourceId); + + // 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 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); + } + } + // [END securitycenter_list_filtered_findings] + + /** + * List findings at a specific time under a source. + * + * @param sourceName The source to list findings at a specific time for. + */ + // [START securitycenter_list_findings_at_time] + static ImmutableList listFindingsAtTime(SourceName sourceName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // parentId: must be one of the following: + // "organization-id" + // "project-id" + // "folder-id" + // SourceName sourceName = SourceName.of(parentId, sourceId); + + // 5 days ago + Instant fiveDaysAgo = Instant.now().minus(Duration.ofDays(5)); + + // Create filter to only list findings that occurred before five days ago. + String filter = String.format("event_time < \"%s\"", fiveDaysAgo.toString()); + + 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 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); + } + } + // [END securitycenter_list_findings_at_time] + + /** + * Demonstrate calling testIamPermissions to determin if the service account has the correct + * permissions. + * + * @param sourceName The source to create a finding for. + */ + // [START securitycenter_test_iam] + static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // SourceName sourceName = SourceName.of(/*organizationId=*/"123234324", + // /*sourceId=*/"423432321"); + + // Iam permission to test. + List permissionsToTest = new ArrayList<>(); + permissionsToTest.add("securitycenter.findings.update"); + + // Call the API. + TestIamPermissionsResponse response = + client.testIamPermissions(sourceName.toString(), permissionsToTest); + System.out.println("IAM Permission:"); + System.out.println(response); + + return response; + } catch (IOException e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END securitycenter_test_iam] + + /** + * Group all findings under an organization across all sources by their specified properties (e.g. + * category). + * + * @param organizationName: The organization to group all findings for. + */ + // [START securitycenter_group_all_findings] + static ImmutableList groupFindings(OrganizationName organizationName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // Input parameters for 'SourceName' must be in one of the following formats: + // * OrganizationName organizationName = OrganizationName.of("organization-id"); + // organizationName.getOrganization(); + // * ProjectName projectName = ProjectName.of("project-id"); + // projectName.getProject(); + // * FolderName folderName = FolderName.of("folder-id"); + // folderName.getFolder(); + 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 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); + } + } + // [END securitycenter_group_all_findings] + + /** + * Group findings under an organization and a source by their specified properties (e.g. + * category). + * + * @param sourceName The source to limit the findings to. + */ + // [START securitycenter_group_findings_with_source] + static ImmutableList groupFindingsWithSource(SourceName sourceName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // parentId: must be one of the following: + // "organization-id" + // "project-id" + // "folder-id" + // SourceName sourceName = SourceName.of(parentId, sourceId); + + 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 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); + } + } + // [END securitycenter_group_findings_with_source] + + /** + * Group active findings under an organization and a source by their specified properties (e.g. + * category). + * + * @param sourceName The source to limit the findings to. + */ + // [START securitycenter_group_active_findings_with_source] + static ImmutableList groupActiveFindingsWithSource(SourceName sourceName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // parentId: must be one of the following: + // "organization-id" + // "project-id" + // "folder-id" + // SourceName sourceName = SourceName.of(parentId, sourceId); + + GroupFindingsRequest.Builder request = + GroupFindingsRequest.newBuilder() + .setParent(sourceName.toString()) + .setGroupBy("category") + .setFilter("state=\"ACTIVE\""); + + // 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 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); + } + } + // [END securitycenter_group_active_findings_with_source] + + /** + * Group active findings under an organization and a source by their specified properties (e.g. + * category) at a specified time. + * + * @param sourceName The source to limit the findings to. + */ + // [START securitycenter_group_active_findings_with_source_at_time] + static ImmutableList groupActiveFindingsWithSourceAtTime(SourceName sourceName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // parentId: must be one of the following: + // "organization-id" + // "project-id" + // "folder-id" + // SourceName sourceName = SourceName.of(parentId, sourceId); + + // 1 day ago + Instant oneDayAgo = Instant.now().minusSeconds(60 * 60 * 24); + + GroupFindingsRequest.Builder request = + GroupFindingsRequest.newBuilder() + .setParent(sourceName.toString()) + .setGroupBy("category") + .setFilter("state=\"ACTIVE\"") + .setReadTime( + Timestamp.newBuilder() + .setSeconds(oneDayAgo.getEpochSecond()) + .setNanos(oneDayAgo.getNano())); + + // 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 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); + } + } + // [END securitycenter_group_active_findings_with_source_at_time] + + /** + * Group active findings under an organization and a source by their state_changes + * (ADDED/CHANGED/UNCHANGED) during a period. + * + * @param sourceName The source to limit the findings to. + */ + // [START securitycenter_group_active_findings_with_source_and_compare_duration] + static ImmutableList groupActiveFindingsWithSourceAndCompareDuration( + SourceName sourceName, Duration duration) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // parentId: must be one of the following: + // "organization-id" + // "project-id" + // "folder-id" + // SourceName sourceName = SourceName.of(parentId, sourceId); + + GroupFindingsRequest.Builder request = + GroupFindingsRequest.newBuilder() + .setParent(sourceName.toString()) + .setGroupBy("state_change") + .setFilter("state=\"ACTIVE\""); + request + .getCompareDurationBuilder() + .setSeconds(duration.getSeconds()) + .setNanos(duration.getNano()); + + // 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 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); + } + } + // [END securitycenter_group_active_findings_with_source_and_compare_duration] +} diff --git a/securitycenter/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java b/securitycenter/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java new file mode 100644 index 00000000000..41e2684d137 --- /dev/null +++ b/securitycenter/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java @@ -0,0 +1,222 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * 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, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.securitycenter; + +import com.google.cloud.securitycenter.v1.CreateSourceRequest; +import com.google.cloud.securitycenter.v1.GetSourceRequest; +import com.google.cloud.securitycenter.v1.ListSourcesRequest; +import com.google.cloud.securitycenter.v1.OrganizationName; +import com.google.cloud.securitycenter.v1.SecurityCenterClient; +import com.google.cloud.securitycenter.v1.SecurityCenterClient.ListSourcesPagedResponse; +import com.google.cloud.securitycenter.v1.Source; +import com.google.cloud.securitycenter.v1.SourceName; +import com.google.cloud.securitycenter.v1.UpdateSourceRequest; +import com.google.common.collect.ImmutableList; +import com.google.iam.v1.Binding; +import com.google.iam.v1.GetIamPolicyRequest; +import com.google.iam.v1.Policy; +import com.google.iam.v1.SetIamPolicyRequest; +import com.google.protobuf.FieldMask; +import java.io.IOException; + +/** + * Snippets for how to work with Sources in Security Command Center. + */ +public class SourceSnippets { + + private SourceSnippets() { + } + + /** + * Create a source under an organization. + * + * @param organizationName The organization for the source. + */ + // [START securitycenter_create_source] + static Source createSource(OrganizationName organizationName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // Start setting up a request to create a source in an organization. + // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324"); + Source source = + Source.newBuilder() + .setDisplayName("Customized Display Name") + .setDescription("A new custom source that does X") + .build(); + + CreateSourceRequest.Builder request = + CreateSourceRequest.newBuilder().setParent(organizationName.toString()).setSource(source); + + // Call the API. + Source response = client.createSource(request.build()); + + System.out.println("Created Source: " + response); + return response; + } catch (IOException e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END securitycenter_create_source] + + /** + * List sources under an organization. + * + * @param organizationName The organization for the source. + */ + // [START securitycenter_list_sources] + static ImmutableList listSources(OrganizationName organizationName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // Start setting up a request to list sources in an organization, project, or folder. + // Parent must be in one of the following formats: + // OrganizationName organizationName = OrganizationName.of("organization-id"); + // ProjectName projectName = ProjectName.of("project-id"); + // FolderName folderName = FolderName.of("folder-id"); + 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 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); + } + } + // [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); + } + } + // [END securitycenter_update_source] + + /** + * Get a source under an organization. + * + * @param sourceName The source to get. + */ + // [START securitycenter_get_source] + static Source getSource(SourceName sourceName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // Start setting up a request to get a source. + // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ + // "423432321"); + GetSourceRequest.Builder request = + GetSourceRequest.newBuilder().setName(sourceName.toString()); + + // Call the API. + Source response = client.getSource(request.build()); + + System.out.println("Source: " + response); + return response; + } catch (IOException e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [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); + } + } + // [END securitycenter_set_source_iam] + + /** + * Get IAM policy for a source. + * + * @param sourceName The source to set IAM Policy for. + */ + // [START securitycenter_get_source_iam] + static Policy getIamPolicySource(SourceName sourceName) { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // Start setting up a request to get IAM policy for a source. + // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ + // "423432321"); + GetIamPolicyRequest request = + GetIamPolicyRequest.newBuilder().setResource(sourceName.toString()).build(); + + // Call the API. + Policy response = client.getIamPolicy(request); + + System.out.println("Policy: " + response); + return response; + } catch (IOException e) { + throw new RuntimeException("Couldn't create client.", e); + } + } + // [END securitycenter_get_source_iam] + +} \ No newline at end of file diff --git a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java new file mode 100644 index 00000000000..fa0920e2016 --- /dev/null +++ b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java @@ -0,0 +1,126 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * 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, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.securitycenter; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertTrue; + +import com.google.cloud.securitycenter.v1.Finding.State; +import com.google.cloud.securitycenter.v1.FindingName; +import com.google.cloud.securitycenter.v1.OrganizationName; +import com.google.cloud.securitycenter.v1.SourceName; +import com.google.protobuf.Value; +import java.io.IOException; +import org.junit.BeforeClass; +import org.junit.Test; +import org.threeten.bp.Duration; + +/** Smoke tests for {@link com.google.cloud.examples.securitycenter.snippets.FindingSnippets} */ +public class ITFindingSnippets { + + private static SourceName SOURCE_NAME; + private static FindingName FINDING_NAME; + + @BeforeClass + public static void setUp() throws IOException { + SOURCE_NAME = SourceName.parse(SourceSnippets.createSource(getOrganizationId()).getName()); + FINDING_NAME = + FindingName.parse(FindingSnippets.createFinding(SOURCE_NAME, "testfindingid").getName()); + } + + @Test + public void testCreateFinding() throws IOException { + assertNotNull(FindingSnippets.createFinding(SOURCE_NAME, "samplefindingid")); + } + + @Test + public void testCreateFindingWithSourceProperties() throws IOException { + assertNotNull(FindingSnippets.createFindingWithSourceProperties(SOURCE_NAME)); + } + + @Test + public void testUpdateFinding() throws IOException { + Value stringValue = Value.newBuilder().setStringValue("value").build(); + assertTrue( + FindingSnippets.updateFinding(FINDING_NAME) + .getSourcePropertiesMap() + .get("stringKey") + .equals(stringValue)); + } + + @Test + public void testUpdateFindingState() throws IOException { + Value stringValue = Value.newBuilder().setStringValue("value").build(); + assertTrue(FindingSnippets.setFindingState(FINDING_NAME).getState().equals(State.INACTIVE)); + } + + @Test + public void testListAllFindings() throws IOException { + assertTrue(FindingSnippets.listAllFindings(getOrganizationId()).size() > 1); + } + + @Test + public void testListFilteredFindings() throws IOException { + assertTrue(FindingSnippets.listFilteredFindings(SOURCE_NAME).size() > 0); + } + + @Test + public void testListFindingsAtTime() throws IOException { + assertTrue(FindingSnippets.listFindingsAtTime(SOURCE_NAME).size() == 0); + } + + @Test + public void testTestIamPermissions() throws IOException { + assertTrue( + FindingSnippets.testIamPermissions(SOURCE_NAME) + .getPermissions(0) + .equals("securitycenter.findings.update")); + } + + @Test + public void testGroupFindings() throws IOException { + assertTrue(FindingSnippets.groupFindings(getOrganizationId()).size() > 0); + } + + @Test + public void testGroupFindingsWithSource() throws IOException { + assertTrue(FindingSnippets.groupFindingsWithSource(SOURCE_NAME).size() > 0); + } + + @Test + public void testGroupActiveFindingsWithSource() throws IOException { + assertTrue(FindingSnippets.groupActiveFindingsWithSource(SOURCE_NAME).size() > 0); + } + + @Test + public void testGroupActiveFindingsWithSourceAtTime() throws IOException { + assertEquals(0, FindingSnippets.groupActiveFindingsWithSourceAtTime(SOURCE_NAME).size()); + } + + @Test + public void testGroupActiveFindingsWithSourceAndCompareDuration() throws IOException { + assertTrue( + FindingSnippets.groupActiveFindingsWithSourceAndCompareDuration( + SOURCE_NAME, Duration.ofDays(1)) + .size() + > 0); + } + + private static OrganizationName getOrganizationId() { + return OrganizationName.of(System.getenv("GCLOUD_ORGANIZATION")); + } +} \ No newline at end of file diff --git a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java new file mode 100644 index 00000000000..a56bf8631a3 --- /dev/null +++ b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java @@ -0,0 +1,75 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * 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, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.securitycenter; + +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertTrue; + +import com.google.cloud.securitycenter.v1.OrganizationName; +import com.google.cloud.securitycenter.v1.SourceName; +import java.io.IOException; +import org.junit.BeforeClass; +import org.junit.Test; + +/** Smoke tests for {@link com.google.cloud.examples.securitycenter.snippets.SourceSnippets} */ +public class ITSourceSnippets { + + private static SourceName SOURCE_NAME; + + @BeforeClass + public static void setUp() throws IOException { + SOURCE_NAME = SourceName.parse(SourceSnippets.createSource(getOrganizationId()).getName()); + } + + @Test + public void testCreateSource() throws IOException { + assertNotNull(SourceSnippets.createSource(getOrganizationId())); + } + + @Test + public void testListSources() throws IOException { + assertTrue(SourceSnippets.listSources(getOrganizationId()).size() > 1); + } + + @Test + public void testUpdateSource() throws IOException { + assertTrue( + SourceSnippets.updateSource(SOURCE_NAME).getDisplayName().equals("Updated Display Name")); + } + + @Test + public void testGetSource() throws IOException { + assertTrue(SourceSnippets.getSource(SOURCE_NAME).getName().equals(SOURCE_NAME.toString())); + } + + @Test + public void testSetSourceIamPolicy() throws IOException { + assertTrue( + SourceSnippets.setIamPolicySource(SOURCE_NAME, "csccclienttest@gmail.com") + .getBindings(0) + .getRole() + .equals("roles/securitycenter.findingsEditor")); + } + + @Test + public void testGetSourceIamPolicy() throws IOException { + assertNotNull(SourceSnippets.getIamPolicySource(SOURCE_NAME)); + } + + private static OrganizationName getOrganizationId() { + return OrganizationName.of(System.getenv("GCLOUD_ORGANIZATION")); + } +} From 000eb5cc12936060c3be8edab6ad06d8e603cd36 Mon Sep 17 00:00:00 2001 From: Kevin Gordillo Date: Fri, 19 Jun 2026 21:46:50 +0000 Subject: [PATCH 2/6] chore: skip integration tests when GCLOUD_ORGANIZATION is unset and fix minor documentation and license URL formatting --- .../java/com/example/securitycenter/FindingSnippets.java | 5 +++-- .../java/com/example/securitycenter/SourceSnippets.java | 3 ++- .../java/com/example/securitycenter/ITFindingSnippets.java | 6 +++++- .../java/com/example/securitycenter/ITSourceSnippets.java | 6 +++++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java b/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java index ef9c928a58e..76286cd637e 100644 --- a/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java +++ b/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java @@ -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 * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.example.securitycenter; import com.google.cloud.securitycenter.v1.Finding; @@ -368,7 +369,7 @@ static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) { * Group all findings under an organization across all sources by their specified properties (e.g. * category). * - * @param organizationName: The organization to group all findings for. + * @param organizationName The organization to group all findings for. */ // [START securitycenter_group_all_findings] static ImmutableList groupFindings(OrganizationName organizationName) { diff --git a/securitycenter/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java b/securitycenter/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java index 41e2684d137..08109ca0d0e 100644 --- a/securitycenter/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java +++ b/securitycenter/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java @@ -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 * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.example.securitycenter; import com.google.cloud.securitycenter.v1.CreateSourceRequest; diff --git a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java index fa0920e2016..3c6e11b3b8d 100644 --- a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java +++ b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java @@ -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 * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.example.securitycenter; import static junit.framework.TestCase.assertEquals; @@ -37,6 +38,9 @@ public class ITFindingSnippets { @BeforeClass public static void setUp() throws IOException { + org.junit.Assume.assumeTrue( + "Skipping tests: GCLOUD_ORGANIZATION env var is not set.", + System.getenv("GCLOUD_ORGANIZATION") != null); SOURCE_NAME = SourceName.parse(SourceSnippets.createSource(getOrganizationId()).getName()); FINDING_NAME = FindingName.parse(FindingSnippets.createFinding(SOURCE_NAME, "testfindingid").getName()); diff --git a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java index a56bf8631a3..c2f546cba1d 100644 --- a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java +++ b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java @@ -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 * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.example.securitycenter; import static junit.framework.TestCase.assertNotNull; @@ -31,6 +32,9 @@ public class ITSourceSnippets { @BeforeClass public static void setUp() throws IOException { + org.junit.Assume.assumeTrue( + "Skipping tests: GCLOUD_ORGANIZATION env var is not set.", + System.getenv("GCLOUD_ORGANIZATION") != null); SOURCE_NAME = SourceName.parse(SourceSnippets.createSource(getOrganizationId()).getName()); } From f0e8f87368f3757b1e73ca767ce7a57789ec7c9f Mon Sep 17 00:00:00 2001 From: Kevin Gordillo Date: Fri, 19 Jun 2026 15:58:34 -0600 Subject: [PATCH 3/6] Apply suggestions from code review Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- .../securitycenter/FindingSnippets.java | 8 ++++---- .../securitycenter/ITFindingSnippets.java | 18 +++++++++--------- .../securitycenter/ITSourceSnippets.java | 9 ++++----- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java b/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java index 76286cd637e..f21ec153717 100644 --- a/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java +++ b/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java @@ -38,8 +38,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.threeten.bp.Duration; -import org.threeten.bp.Instant; +import java.time.Duration; +import java.time.Instant; /** * Snippets for how to work with Findings in Security Command Center. @@ -349,8 +349,8 @@ static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) { // /*sourceId=*/"423432321"); // Iam permission to test. - List permissionsToTest = new ArrayList<>(); - permissionsToTest.add("securitycenter.findings.update"); + List permissionsToTest = + ImmutableList.of("securitycenter.findings.update"); // Call the API. TestIamPermissionsResponse response = diff --git a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java index 3c6e11b3b8d..f8c980b28c2 100644 --- a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java +++ b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java @@ -16,9 +16,9 @@ package com.example.securitycenter; -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertNotNull; -import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import com.google.cloud.securitycenter.v1.Finding.State; import com.google.cloud.securitycenter.v1.FindingName; @@ -28,7 +28,7 @@ import java.io.IOException; import org.junit.BeforeClass; import org.junit.Test; -import org.threeten.bp.Duration; +import java.time.Duration; /** Smoke tests for {@link com.google.cloud.examples.securitycenter.snippets.FindingSnippets} */ public class ITFindingSnippets { @@ -59,17 +59,17 @@ public void testCreateFindingWithSourceProperties() throws IOException { @Test public void testUpdateFinding() throws IOException { Value stringValue = Value.newBuilder().setStringValue("value").build(); - assertTrue( + assertEquals( + stringValue, FindingSnippets.updateFinding(FINDING_NAME) .getSourcePropertiesMap() - .get("stringKey") - .equals(stringValue)); + .get("stringKey")); } @Test public void testUpdateFindingState() throws IOException { Value stringValue = Value.newBuilder().setStringValue("value").build(); - assertTrue(FindingSnippets.setFindingState(FINDING_NAME).getState().equals(State.INACTIVE)); + assertEquals(State.INACTIVE, FindingSnippets.setFindingState(FINDING_NAME).getState()); } @Test @@ -84,7 +84,7 @@ public void testListFilteredFindings() throws IOException { @Test public void testListFindingsAtTime() throws IOException { - assertTrue(FindingSnippets.listFindingsAtTime(SOURCE_NAME).size() == 0); + assertEquals(0, FindingSnippets.listFindingsAtTime(SOURCE_NAME).size()); } @Test diff --git a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java index c2f546cba1d..c8bf5752dc1 100644 --- a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java +++ b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java @@ -16,8 +16,8 @@ package com.example.securitycenter; -import static junit.framework.TestCase.assertNotNull; -import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import com.google.cloud.securitycenter.v1.OrganizationName; import com.google.cloud.securitycenter.v1.SourceName; @@ -50,9 +50,8 @@ public void testListSources() throws IOException { @Test public void testUpdateSource() throws IOException { - assertTrue( - SourceSnippets.updateSource(SOURCE_NAME).getDisplayName().equals("Updated Display Name")); - } + assertEquals( + "Updated Display Name", SourceSnippets.updateSource(SOURCE_NAME).getDisplayName()); @Test public void testGetSource() throws IOException { From be945c31f73e23951eb29b55613a77246de2d08c Mon Sep 17 00:00:00 2001 From: Kevin Gordillo Date: Fri, 19 Jun 2026 21:59:06 +0000 Subject: [PATCH 4/6] chore: remove google truth dependency from securitycenter snippets pom.xml --- securitycenter/snippets/pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/securitycenter/snippets/pom.xml b/securitycenter/snippets/pom.xml index caf5a8eed2d..d216b83b341 100644 --- a/securitycenter/snippets/pom.xml +++ b/securitycenter/snippets/pom.xml @@ -44,11 +44,5 @@ 4.13.2 test - - com.google.truth - truth - 1.4.0 - test - \ No newline at end of file From 71e3861b22108a1213f02ad61f2c6631166c15a2 Mon Sep 17 00:00:00 2001 From: Kevin Gordillo Date: Fri, 19 Jun 2026 22:08:02 +0000 Subject: [PATCH 5/6] chore: remove region tags that will not be migrated from security center sample --- .../securitycenter/FindingSnippets.java | 24 ------------------- .../securitycenter/SourceSnippets.java | 12 ---------- 2 files changed, 36 deletions(-) diff --git a/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java b/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java index f21ec153717..1b4857dc359 100644 --- a/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java +++ b/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java @@ -54,7 +54,6 @@ private FindingSnippets() { * * @param sourceName The source for the finding. */ - // [START securitycenter_create_finding] static Finding createFinding(SourceName sourceName, String findingId) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ @@ -91,14 +90,12 @@ static Finding createFinding(SourceName sourceName, String findingId) { throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_create_finding] /** * Create a finding with source properties under a source. * * @param sourceName The source for the finding. */ - // [START securitycenter_create_finding_with_source_properties] static Finding createFindingWithSourceProperties(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ @@ -143,14 +140,12 @@ static Finding createFindingWithSourceProperties(SourceName sourceName) { throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_create_finding_with_source_properties] /** * Update a finding's source properties. * * @param findingName The finding to update. */ - // [START securitycenter_update_finding_source_properties] static Finding updateFinding(FindingName findingName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // FindingName findingName = FindingName.of(/*organization=*/"123234324", @@ -190,14 +185,12 @@ static Finding updateFinding(FindingName findingName) { throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_update_finding_source_properties] /** * Updates a finding's state to INACTIVE. * * @param findingName The finding to update. */ - // [START securitycenter_update_finding_state] static Finding setFindingState(FindingName findingName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // FindingName findingName = FindingName.of(/*organization=*/"123234324", @@ -221,14 +214,12 @@ static Finding setFindingState(FindingName findingName) { throw new RuntimeException("Couldn't create client.", e); } } - // [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 listAllFindings(OrganizationName organizationName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Input parameters for SourceName must be in one of the following formats: @@ -259,14 +250,12 @@ static ImmutableList listAllFindings(OrganizationName organi throw new RuntimeException("Couldn't create client.", e); } } - // [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 listFilteredFindings(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // parentId: must be one of the following: @@ -295,7 +284,6 @@ static ImmutableList listFilteredFindings(SourceName sourceN throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_list_filtered_findings] /** * List findings at a specific time under a source. @@ -342,7 +330,6 @@ static ImmutableList listFindingsAtTime(SourceName sourceNam * * @param sourceName The source to create a finding for. */ - // [START securitycenter_test_iam] static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // SourceName sourceName = SourceName.of(/*organizationId=*/"123234324", @@ -363,7 +350,6 @@ static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) { throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_test_iam] /** * Group all findings under an organization across all sources by their specified properties (e.g. @@ -371,7 +357,6 @@ static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) { * * @param organizationName The organization to group all findings for. */ - // [START securitycenter_group_all_findings] static ImmutableList groupFindings(OrganizationName organizationName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Input parameters for 'SourceName' must be in one of the following formats: @@ -401,7 +386,6 @@ static ImmutableList groupFindings(OrganizationName organizationNam throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_group_all_findings] /** * Group findings under an organization and a source by their specified properties (e.g. @@ -409,7 +393,6 @@ static ImmutableList groupFindings(OrganizationName organizationNam * * @param sourceName The source to limit the findings to. */ - // [START securitycenter_group_findings_with_source] static ImmutableList groupFindingsWithSource(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // parentId: must be one of the following: @@ -436,7 +419,6 @@ static ImmutableList groupFindingsWithSource(SourceName sourceName) throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_group_findings_with_source] /** * Group active findings under an organization and a source by their specified properties (e.g. @@ -444,7 +426,6 @@ static ImmutableList groupFindingsWithSource(SourceName sourceName) * * @param sourceName The source to limit the findings to. */ - // [START securitycenter_group_active_findings_with_source] static ImmutableList groupActiveFindingsWithSource(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // parentId: must be one of the following: @@ -474,7 +455,6 @@ static ImmutableList groupActiveFindingsWithSource(SourceName sourc throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_group_active_findings_with_source] /** * Group active findings under an organization and a source by their specified properties (e.g. @@ -482,7 +462,6 @@ static ImmutableList groupActiveFindingsWithSource(SourceName sourc * * @param sourceName The source to limit the findings to. */ - // [START securitycenter_group_active_findings_with_source_at_time] static ImmutableList groupActiveFindingsWithSourceAtTime(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // parentId: must be one of the following: @@ -519,7 +498,6 @@ static ImmutableList groupActiveFindingsWithSourceAtTime(SourceName throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_group_active_findings_with_source_at_time] /** * Group active findings under an organization and a source by their state_changes @@ -527,7 +505,6 @@ static ImmutableList groupActiveFindingsWithSourceAtTime(SourceName * * @param sourceName The source to limit the findings to. */ - // [START securitycenter_group_active_findings_with_source_and_compare_duration] static ImmutableList groupActiveFindingsWithSourceAndCompareDuration( SourceName sourceName, Duration duration) { try (SecurityCenterClient client = SecurityCenterClient.create()) { @@ -562,5 +539,4 @@ static ImmutableList groupActiveFindingsWithSourceAndCompareDuratio throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_group_active_findings_with_source_and_compare_duration] } diff --git a/securitycenter/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java b/securitycenter/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java index 08109ca0d0e..1553e461a23 100644 --- a/securitycenter/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java +++ b/securitycenter/snippets/src/main/java/com/example/securitycenter/SourceSnippets.java @@ -46,7 +46,6 @@ private SourceSnippets() { * * @param organizationName The organization for the source. */ - // [START securitycenter_create_source] static Source createSource(OrganizationName organizationName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Start setting up a request to create a source in an organization. @@ -69,14 +68,12 @@ static Source createSource(OrganizationName organizationName) { throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_create_source] /** * List sources under an organization. * * @param organizationName The organization for the source. */ - // [START securitycenter_list_sources] static ImmutableList listSources(OrganizationName organizationName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Start setting up a request to list sources in an organization, project, or folder. @@ -101,14 +98,12 @@ static ImmutableList listSources(OrganizationName organizationName) { throw new RuntimeException("Couldn't create client.", e); } } - // [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. @@ -133,14 +128,12 @@ static Source updateSource(SourceName sourceName) { throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_update_source] /** * Get a source under an organization. * * @param sourceName The source to get. */ - // [START securitycenter_get_source] static Source getSource(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Start setting up a request to get a source. @@ -158,14 +151,12 @@ static Source getSource(SourceName sourceName) { throw new RuntimeException("Couldn't create client.", e); } } - // [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" @@ -193,14 +184,12 @@ static Policy setIamPolicySource(SourceName sourceName, String userEmail) { throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_set_source_iam] /** * Get IAM policy for a source. * * @param sourceName The source to set IAM Policy for. */ - // [START securitycenter_get_source_iam] static Policy getIamPolicySource(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { // Start setting up a request to get IAM policy for a source. @@ -218,6 +207,5 @@ static Policy getIamPolicySource(SourceName sourceName) { throw new RuntimeException("Couldn't create client.", e); } } - // [END securitycenter_get_source_iam] } \ No newline at end of file From a3ac6ed51f12c8ff8750b4664915a54d2a2a2536 Mon Sep 17 00:00:00 2001 From: Kevin Gordillo Date: Fri, 19 Jun 2026 22:22:09 +0000 Subject: [PATCH 6/6] refactor: clean up unused imports and test variables in Security Center snippets --- .../main/java/com/example/securitycenter/FindingSnippets.java | 3 +-- .../java/com/example/securitycenter/ITFindingSnippets.java | 3 +-- .../test/java/com/example/securitycenter/ITSourceSnippets.java | 2 ++ 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java b/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java index 1b4857dc359..b2f0b71c10d 100644 --- a/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java +++ b/securitycenter/snippets/src/main/java/com/example/securitycenter/FindingSnippets.java @@ -36,10 +36,9 @@ import com.google.protobuf.Timestamp; import com.google.protobuf.Value; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import java.time.Duration; import java.time.Instant; +import java.util.List; /** * Snippets for how to work with Findings in Security Command Center. diff --git a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java index f8c980b28c2..2653285cef5 100644 --- a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java +++ b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITFindingSnippets.java @@ -26,9 +26,9 @@ import com.google.cloud.securitycenter.v1.SourceName; import com.google.protobuf.Value; import java.io.IOException; +import java.time.Duration; import org.junit.BeforeClass; import org.junit.Test; -import java.time.Duration; /** Smoke tests for {@link com.google.cloud.examples.securitycenter.snippets.FindingSnippets} */ public class ITFindingSnippets { @@ -68,7 +68,6 @@ public void testUpdateFinding() throws IOException { @Test public void testUpdateFindingState() throws IOException { - Value stringValue = Value.newBuilder().setStringValue("value").build(); assertEquals(State.INACTIVE, FindingSnippets.setFindingState(FINDING_NAME).getState()); } diff --git a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java index c8bf5752dc1..9348a2f45dd 100644 --- a/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java +++ b/securitycenter/snippets/src/test/java/com/example/securitycenter/ITSourceSnippets.java @@ -16,6 +16,7 @@ package com.example.securitycenter; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -52,6 +53,7 @@ public void testListSources() throws IOException { public void testUpdateSource() throws IOException { assertEquals( "Updated Display Name", SourceSnippets.updateSource(SOURCE_NAME).getDisplayName()); + } @Test public void testGetSource() throws IOException {