diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/AssetSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/AssetSnippets.java index d101ae186c0b..c65ad31cee71 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/AssetSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/AssetSnippets.java @@ -34,9 +34,13 @@ import org.threeten.bp.Duration; import org.threeten.bp.Instant; -/** Snippets for how to work with Assets in Cloud Security Command Center. */ +/** + * Snippets for how to work with Assets in Security Command Center. + */ public class AssetSnippets { - private AssetSnippets() {} + + private AssetSnippets() { + } /** * Lists all assets for an organization. @@ -46,8 +50,12 @@ private AssetSnippets() {} // [START securitycenter_list_all_assets] static ImmutableList listAssets(OrganizationName organizationName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { - // Start setting up a request for to search for all assets in an organization. - // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324"); + // Start setting up a request to search for all assets 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"); ListAssetsRequest.Builder request = ListAssetsRequest.newBuilder().setParent(organizationName.toString()); @@ -75,8 +83,12 @@ static ImmutableList listAssets(OrganizationName organizationN // [START securitycenter_list_assets_with_filter] static ImmutableList 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"); + // Start setting up a request to search for all assets 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"); ListAssetsRequest.Builder request = ListAssetsRequest.newBuilder() .setParent(organizationName.toString()) @@ -109,10 +121,13 @@ static ImmutableList listAssetsWithFilter(OrganizationName org static ImmutableList 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 + // Start setting up a request to search for all assets 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"); + // Initialize the builder with the parent and filter ListAssetsRequest.Builder request = ListAssetsRequest.newBuilder() .setParent(organizationName.toString()) @@ -151,8 +166,12 @@ static ImmutableList listAssetAndStatusChanges( OrganizationName organizationName, Duration timeSpan, 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"); + // Start setting up a request to search for all assets 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"); ListAssetsRequest.Builder request = ListAssetsRequest.newBuilder() .setParent(organizationName.toString()) @@ -191,8 +210,12 @@ static ImmutableList listAssetAndStatusChanges( // [START securitycenter_group_all_assets] static ImmutableList 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"); + // Start setting up a request to group all assets by type 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"); GroupAssetsRequest.Builder request = GroupAssetsRequest.newBuilder() .setGroupBy("security_center_properties.resource_type") @@ -223,9 +246,10 @@ static ImmutableList groupAssets(OrganizationName organizationName) // [START securitycenter_group_all_assets_with_filter] static ImmutableList 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 + // Start setting up a request to filter all assets by type and group them by project in an // organization. - // OrganizationName organizationName = OrganizationName.of("123234324"); + // You can also use a project or folder as a parent resource and filter assets in them + // respectively. GroupAssetsRequest.Builder request = GroupAssetsRequest.newBuilder() .setFilter( @@ -259,9 +283,13 @@ static ImmutableList groupAssetsWithFilter(OrganizationName organiz static ImmutableList 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"); + // Start setting up a request to group all assets during a period of time 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"); GroupAssetsRequest.Builder request = GroupAssetsRequest.newBuilder() .setGroupBy("state_change") diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/FindingSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/FindingSnippets.java index 9cf8cd4c9e99..e2f3fd10e39d 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/FindingSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/FindingSnippets.java @@ -40,9 +40,13 @@ import org.threeten.bp.Duration; import org.threeten.bp.Instant; -/** Snippets for how to work with Findings in Cloud Security Command Center. */ +/** + * Snippets for how to work with Findings in Security Command Center. + */ public class FindingSnippets { - private FindingSnippets() {} + + private FindingSnippets() { + } /** * Create a finding under a source. @@ -226,7 +230,14 @@ static Finding setFindingState(FindingName findingName) { // [START securitycenter_list_all_findings] static ImmutableList listAllFindings(OrganizationName organizationName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { - // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324"); + // 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(), "-"); @@ -257,8 +268,11 @@ static ImmutableList listAllFindings(OrganizationName organi // [START securitycenter_list_filtered_findings] static ImmutableList listFilteredFindings(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { - // SourceName sourceName = SourceName.of(/*organizationId=*/"123234324", - // /*sourceId=*/"423432321"); + // 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\""; @@ -290,8 +304,11 @@ static ImmutableList listFilteredFindings(SourceName sourceN // [START securitycenter_list_findings_at_time] static ImmutableList listFindingsAtTime(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { - // SourceName sourceName = SourceName.of(/*organizationId=*/"123234324", - // /*sourceId=*/"423432321"); + // 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)); @@ -353,12 +370,18 @@ static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) { * Group all findings under an organization across all sources by their specified properties (e.g. * category). * - * @param organizationName The organizatoin to group all findings for. + * @param organizationName: The organization to group all findings for. */ // [START securitycenter_group_all_findings] static ImmutableList groupFindings(OrganizationName organizationName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { - // OrganizationName organizationName = OrganizationName.of("123234324"); + // 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 = @@ -390,8 +413,11 @@ static ImmutableList groupFindings(OrganizationName organizationNam // [START securitycenter_group_findings_with_source] static ImmutableList groupFindingsWithSource(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { - // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ - // "423432321"); + // 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"); @@ -422,8 +448,11 @@ static ImmutableList groupFindingsWithSource(SourceName sourceName) // [START securitycenter_group_active_findings_with_source] static ImmutableList groupActiveFindingsWithSource(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { - // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ - // "423432321"); + // parentId: must be one of the following: + // "organization-id" + // "project-id" + // "folder-id" + // SourceName sourceName = SourceName.of(parentId, sourceId); GroupFindingsRequest.Builder request = GroupFindingsRequest.newBuilder() @@ -457,8 +486,11 @@ static ImmutableList groupActiveFindingsWithSource(SourceName sourc // [START securitycenter_group_active_findings_with_source_at_time] static ImmutableList groupActiveFindingsWithSourceAtTime(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { - // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ - // "423432321"); + // 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); @@ -500,8 +532,11 @@ static ImmutableList groupActiveFindingsWithSourceAtTime(SourceName static ImmutableList groupActiveFindingsWithSourceAndCompareDuration( SourceName sourceName, Duration duration) { try (SecurityCenterClient client = SecurityCenterClient.create()) { - // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ - // "423432321"); + // parentId: must be one of the following: + // "organization-id" + // "project-id" + // "folder-id" + // SourceName sourceName = SourceName.of(parentId, sourceId); GroupFindingsRequest.Builder request = GroupFindingsRequest.newBuilder() diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/OrganizationSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/OrganizationSnippets.java index ca45abfe2334..1bff145a738c 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/OrganizationSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/OrganizationSnippets.java @@ -23,7 +23,7 @@ import com.google.protobuf.FieldMask; import java.io.IOException; -/** Snippets for how to work with Organizations in Cloud Security Command Center. */ +/** Snippets for how to work with Organizations in Security Command Center. */ public class OrganizationSnippets { private OrganizationSnippets() {} diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/SecurityMarkSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/SecurityMarkSnippets.java index 611ad9e43844..b0c8bf4799ba 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/SecurityMarkSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/SecurityMarkSnippets.java @@ -34,7 +34,8 @@ public class SecurityMarkSnippets { - private SecurityMarkSnippets() {} + private SecurityMarkSnippets() { + } /** * Add security mark to an asset. @@ -44,7 +45,11 @@ private SecurityMarkSnippets() {} // [START securitycenter_add_security_marks] static SecurityMarks addToAsset(String assetName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { - // String assetName = "organizations/123123342/assets/12312321"; + // Specify the value of 'assetName' in one of the following formats: + // String assetName = "organizations/{org-id}/assets/{asset-id}"; + // String assetName = "projects/{project-id}/assets/{asset-id}"; + // String assetName = "folders/{folder-id}/assets/{asset-id}"; + // // Start setting up a request to add security marks for an asset. ImmutableMap markMap = ImmutableMap.of("key_a", "value_a", "key_b", "value_b"); @@ -82,8 +87,11 @@ static SecurityMarks addToAsset(String assetName) { */ // [START securitycenter_delete_security_marks] static SecurityMarks clearFromAsset(String assetName) { - // String assetName = "organizations/123123342/assets/12312321"; try (SecurityCenterClient client = SecurityCenterClient.create()) { + // Specify the value of 'assetName' in one of the following formats: + // String assetName = "organizations/{org-id}/assets/{asset-id}"; + // String assetName = "projects/{project-id}/assets/{asset-id}"; + // String assetName = "folders/{folder-id}/assets/{asset-id}"; // Start setting up a request to clear security marks for an asset. // Create security mark and field mask for clearing security marks. SecurityMarks securityMarks = @@ -116,8 +124,11 @@ static SecurityMarks clearFromAsset(String assetName) { */ // [START securitycenter_add_delete_security_marks] static SecurityMarks deleteAndUpdateMarks(String assetName) { - // String assetName = "organizations/123123342/assets/12312321"; try (SecurityCenterClient client = SecurityCenterClient.create()) { + // Specify the value of 'assetName' in one of the following formats: + // String assetName = "organizations/{org-id}/assets/{asset-id}"; + // String assetName = "projects/{project-id}/assets/{asset-id}"; + // String assetName = "folders/{folder-id}/assets/{asset-id}"; // Start setting up a request to clear and update security marks for an asset. // Create security mark and field mask for clearing security marks. SecurityMarks securityMarks = @@ -153,12 +164,14 @@ static SecurityMarks deleteAndUpdateMarks(String assetName) { */ // [START securitycenter_add_finding_security_marks] static SecurityMarks addToFinding(FindingName findingName) { - // FindingName findingName = FindingName.of(/*organization=*/"123234324", - // /*source=*/"423432321", /*findingId=*/"samplefindingid2"); try (SecurityCenterClient client = SecurityCenterClient.create()) { // Start setting up a request to add security marks for a finding. ImmutableMap markMap = ImmutableMap.of("key_a", "value_a", "key_b", "value_b"); + // Specify the value of 'findingName' in one of the following formats: + // FindingName.ofOrganizationSourceFindingName("org-id", "source", "finding-id"); + // FindingName.ofProjectSourceFindingName("project-id", "source", "finding-id"); + // FindingName.ofFolderSourceFindingName("folder-id", "source", "finding-id"); // Add security marks and field mask for security marks. SecurityMarks securityMarks = SecurityMarks.newBuilder() @@ -195,8 +208,11 @@ static SecurityMarks addToFinding(FindingName findingName) { static ImmutableList listAssetsWithQueryMarks( OrganizationName organizationName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { - // Start setting up a request for to list all assets filtered by a specific security mark. - // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324"); + // Start setting up a request to list all assets filtered by a specific security mark. + // 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"); ListAssetsRequest request = ListAssetsRequest.newBuilder() .setParent(organizationName.toString()) @@ -227,9 +243,16 @@ static ImmutableList listAssetsWithQueryMarks( // [START securitycenter_list_findings_with_security_marks] static ImmutableList listFindingsWithQueryMarks(SourceName sourceName) { try (SecurityCenterClient client = SecurityCenterClient.create()) { - // Start setting up a request for to list all findings filtered by a specific security mark. - // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/ - // "423432321"); + // Start setting up a request to list all findings filtered by a specific security mark. + // + // 'parent' must be in one of the following formats: + // * OrganizationName organizationName = OrganizationName.of("organization-id"); + // String parent = organizationName.getOrganization(); + // * ProjectName projectName = ProjectName.of("project-id"); + // String parent = projectName.getProject(); + // * FolderName folderName = FolderName.of("folder-id"); + // String parent = folderName.getFolder(); + // SourceName sourceName = SourceName.of(parent, {source-id}); String filter = "NOT security_marks.marks.key_a=\"value_a\""; diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/SourceSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/SourceSnippets.java index 06abf90c5c77..b8eb18301013 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/SourceSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/securitycenter/snippets/SourceSnippets.java @@ -32,9 +32,13 @@ import com.google.protobuf.FieldMask; import java.io.IOException; -/** Snippets for how to work with Sources in Cloud Security Command Center. */ +/** + * Snippets for how to work with Sources in Security Command Center. + */ public class SourceSnippets { - private SourceSnippets() {} + + private SourceSnippets() { + } /** * Create a source under an organization. @@ -74,8 +78,11 @@ static Source createSource(OrganizationName organizationName) { // [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. - // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324"); + // 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());