diff --git a/CHANGELOG.md b/CHANGELOG.md index dcb4033d7..9950d2a7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +### [2.5.3](https://github.com/googleapis/java-securitycenter/compare/v2.5.2...v2.5.3) (2022-03-29) + + +### Documentation + +* **samples:** added samples for Mute config ([#719](https://github.com/googleapis/java-securitycenter/issues/719)) ([05018a5](https://github.com/googleapis/java-securitycenter/commit/05018a50e2f05ed093aa8a738bb8b78862774998)) + + +### Dependencies + +* update dependency com.google.cloud:google-cloud-shared-dependencies to v2.9.0 ([#791](https://github.com/googleapis/java-securitycenter/issues/791)) ([2d75136](https://github.com/googleapis/java-securitycenter/commit/2d7513657e0f46c7bb2b2127ea767ecdbe72cddf)) + ### [2.5.2](https://github.com/googleapis/java-securitycenter/compare/v2.5.1...v2.5.2) (2022-03-25) diff --git a/README.md b/README.md index 210137792..085ae6882 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file com.google.cloud google-cloud-securitycenter + 2.3.1 @@ -61,13 +62,13 @@ implementation 'com.google.cloud:google-cloud-securitycenter' If you are using Gradle without BOM, add this to your dependencies ```Groovy -implementation 'com.google.cloud:google-cloud-securitycenter:2.5.1' +implementation 'com.google.cloud:google-cloud-securitycenter:2.5.2' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-securitycenter" % "2.5.1" +libraryDependencies += "com.google.cloud" % "google-cloud-securitycenter" % "2.5.2" ``` ## Authentication @@ -117,6 +118,13 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-securitycente | List Notification Config Snippets | [source code](https://github.com/googleapis/java-securitycenter/blob/main/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/ListNotificationConfigSnippets.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-securitycenter&page=editor&open_in_editor=samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/ListNotificationConfigSnippets.java) | | Notification Receiver | [source code](https://github.com/googleapis/java-securitycenter/blob/main/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/NotificationReceiver.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-securitycenter&page=editor&open_in_editor=samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/NotificationReceiver.java) | | Update Notification Config Snippets | [source code](https://github.com/googleapis/java-securitycenter/blob/main/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/UpdateNotificationConfigSnippets.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-securitycenter&page=editor&open_in_editor=samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/UpdateNotificationConfigSnippets.java) | +| Bulk Mute Findings | [source code](https://github.com/googleapis/java-securitycenter/blob/main/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/BulkMuteFindings.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-securitycenter&page=editor&open_in_editor=samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/BulkMuteFindings.java) | +| Create Mute Rule | [source code](https://github.com/googleapis/java-securitycenter/blob/main/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/CreateMuteRule.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-securitycenter&page=editor&open_in_editor=samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/CreateMuteRule.java) | +| Delete Mute Rule | [source code](https://github.com/googleapis/java-securitycenter/blob/main/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/DeleteMuteRule.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-securitycenter&page=editor&open_in_editor=samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/DeleteMuteRule.java) | +| Get Mute Rule | [source code](https://github.com/googleapis/java-securitycenter/blob/main/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/GetMuteRule.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-securitycenter&page=editor&open_in_editor=samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/GetMuteRule.java) | +| List Mute Rules | [source code](https://github.com/googleapis/java-securitycenter/blob/main/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/ListMuteRules.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-securitycenter&page=editor&open_in_editor=samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/ListMuteRules.java) | +| Set Mute Unmute Finding | [source code](https://github.com/googleapis/java-securitycenter/blob/main/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/SetMuteUnmuteFinding.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-securitycenter&page=editor&open_in_editor=samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/SetMuteUnmuteFinding.java) | +| Update Mute Rule | [source code](https://github.com/googleapis/java-securitycenter/blob/main/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/UpdateMuteRule.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-securitycenter&page=editor&open_in_editor=samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/UpdateMuteRule.java) | diff --git a/google-cloud-securitycenter-bom/pom.xml b/google-cloud-securitycenter-bom/pom.xml index 8a87382d7..6bc96126c 100644 --- a/google-cloud-securitycenter-bom/pom.xml +++ b/google-cloud-securitycenter-bom/pom.xml @@ -3,12 +3,12 @@ 4.0.0 com.google.cloud google-cloud-securitycenter-bom - 2.5.2 + 2.5.3 pom com.google.cloud google-cloud-shared-config - 1.3.0 + 1.3.2 Google Cloud Security Command Center BOM @@ -53,37 +53,37 @@ com.google.cloud google-cloud-securitycenter - 2.5.2 + 2.5.3 com.google.api.grpc grpc-google-cloud-securitycenter-v1 - 2.5.2 + 2.5.3 com.google.api.grpc grpc-google-cloud-securitycenter-v1beta1 - 0.100.2 + 0.100.3 com.google.api.grpc grpc-google-cloud-securitycenter-v1p1beta1 - 0.100.2 + 0.100.3 com.google.api.grpc proto-google-cloud-securitycenter-v1 - 2.5.2 + 2.5.3 com.google.api.grpc proto-google-cloud-securitycenter-v1beta1 - 0.100.2 + 0.100.3 com.google.api.grpc proto-google-cloud-securitycenter-v1p1beta1 - 0.100.2 + 0.100.3 diff --git a/google-cloud-securitycenter/pom.xml b/google-cloud-securitycenter/pom.xml index 4e36a584f..35a9324e7 100644 --- a/google-cloud-securitycenter/pom.xml +++ b/google-cloud-securitycenter/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.google.cloud google-cloud-securitycenter - 2.5.2 + 2.5.3 jar Google Cloud Security Command Center https://github.com/googleapis/java-securitycenter @@ -11,7 +11,7 @@ com.google.cloud google-cloud-securitycenter-parent - 2.5.2 + 2.5.3 google-cloud-securitycenter diff --git a/grpc-google-cloud-securitycenter-v1/pom.xml b/grpc-google-cloud-securitycenter-v1/pom.xml index 6b04e4fc8..00c2d3d22 100644 --- a/grpc-google-cloud-securitycenter-v1/pom.xml +++ b/grpc-google-cloud-securitycenter-v1/pom.xml @@ -4,13 +4,13 @@ 4.0.0 com.google.api.grpc grpc-google-cloud-securitycenter-v1 - 2.5.2 + 2.5.3 grpc-google-cloud-securitycenter-v1 GRPC library for grpc-google-cloud-securitycenter-v1 com.google.cloud google-cloud-securitycenter-parent - 2.5.2 + 2.5.3 diff --git a/grpc-google-cloud-securitycenter-v1beta1/pom.xml b/grpc-google-cloud-securitycenter-v1beta1/pom.xml index f9872d909..b167060f0 100644 --- a/grpc-google-cloud-securitycenter-v1beta1/pom.xml +++ b/grpc-google-cloud-securitycenter-v1beta1/pom.xml @@ -4,13 +4,13 @@ 4.0.0 com.google.api.grpc grpc-google-cloud-securitycenter-v1beta1 - 0.100.2 + 0.100.3 grpc-google-cloud-securitycenter-v1beta1 GRPC library for grpc-google-cloud-securitycenter-v1beta1 com.google.cloud google-cloud-securitycenter-parent - 2.5.2 + 2.5.3 diff --git a/grpc-google-cloud-securitycenter-v1p1beta1/pom.xml b/grpc-google-cloud-securitycenter-v1p1beta1/pom.xml index f2b14a79c..0a279c60e 100644 --- a/grpc-google-cloud-securitycenter-v1p1beta1/pom.xml +++ b/grpc-google-cloud-securitycenter-v1p1beta1/pom.xml @@ -4,13 +4,13 @@ 4.0.0 com.google.api.grpc grpc-google-cloud-securitycenter-v1p1beta1 - 0.100.2 + 0.100.3 grpc-google-cloud-securitycenter-v1p1beta1 GRPC library for grpc-google-cloud-securitycenter-v1p1beta1 com.google.cloud google-cloud-securitycenter-parent - 2.5.2 + 2.5.3 diff --git a/pom.xml b/pom.xml index b35c720cc..beed6deeb 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.google.cloud google-cloud-securitycenter-parent pom - 2.5.2 + 2.5.3 Google Cloud Security Command Center Parent https://github.com/googleapis/java-securitycenter @@ -14,7 +14,7 @@ com.google.cloud google-cloud-shared-config - 1.3.0 + 1.3.2 @@ -61,43 +61,43 @@ com.google.api.grpc proto-google-cloud-securitycenter-v1 - 2.5.2 + 2.5.3 com.google.api.grpc proto-google-cloud-securitycenter-v1beta1 - 0.100.2 + 0.100.3 com.google.api.grpc proto-google-cloud-securitycenter-v1p1beta1 - 0.100.2 + 0.100.3 com.google.api.grpc grpc-google-cloud-securitycenter-v1 - 2.5.2 + 2.5.3 com.google.api.grpc grpc-google-cloud-securitycenter-v1beta1 - 0.100.2 + 0.100.3 com.google.api.grpc grpc-google-cloud-securitycenter-v1p1beta1 - 0.100.2 + 0.100.3 com.google.cloud google-cloud-securitycenter - 2.5.2 + 2.5.3 com.google.cloud google-cloud-shared-dependencies - 2.8.0 + 2.9.0 pom import diff --git a/proto-google-cloud-securitycenter-v1/pom.xml b/proto-google-cloud-securitycenter-v1/pom.xml index 51d4681ee..57e08461b 100644 --- a/proto-google-cloud-securitycenter-v1/pom.xml +++ b/proto-google-cloud-securitycenter-v1/pom.xml @@ -4,13 +4,13 @@ 4.0.0 com.google.api.grpc proto-google-cloud-securitycenter-v1 - 2.5.2 + 2.5.3 proto-google-cloud-securitycenter-v1 PROTO library for proto-google-cloud-securitycenter-v1 com.google.cloud google-cloud-securitycenter-parent - 2.5.2 + 2.5.3 diff --git a/proto-google-cloud-securitycenter-v1beta1/pom.xml b/proto-google-cloud-securitycenter-v1beta1/pom.xml index fb974ecc1..71ab24752 100644 --- a/proto-google-cloud-securitycenter-v1beta1/pom.xml +++ b/proto-google-cloud-securitycenter-v1beta1/pom.xml @@ -4,13 +4,13 @@ 4.0.0 com.google.api.grpc proto-google-cloud-securitycenter-v1beta1 - 0.100.2 + 0.100.3 proto-google-cloud-securitycenter-v1beta1 PROTO library for proto-google-cloud-securitycenter-v1beta1 com.google.cloud google-cloud-securitycenter-parent - 2.5.2 + 2.5.3 diff --git a/proto-google-cloud-securitycenter-v1p1beta1/pom.xml b/proto-google-cloud-securitycenter-v1p1beta1/pom.xml index e87c8cd7c..5ec2dc8d1 100644 --- a/proto-google-cloud-securitycenter-v1p1beta1/pom.xml +++ b/proto-google-cloud-securitycenter-v1p1beta1/pom.xml @@ -4,13 +4,13 @@ 4.0.0 com.google.api.grpc proto-google-cloud-securitycenter-v1p1beta1 - 0.100.2 + 0.100.3 proto-google-cloud-securitycenter-v1p1beta1 PROTO library for proto-google-cloud-securitycenter-v1p1beta1 com.google.cloud google-cloud-securitycenter-parent - 2.5.2 + 2.5.3 diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 773ac5f31..7682bff4e 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -41,6 +41,7 @@ com.google.cloud google-cloud-securitycenter + 2.3.1 diff --git a/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/BulkMuteFindings.java b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/BulkMuteFindings.java new file mode 100644 index 000000000..2a5d36932 --- /dev/null +++ b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/BulkMuteFindings.java @@ -0,0 +1,71 @@ +/* + * Copyright 2021 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 + * + * 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, + * 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.google.cloud.examples.securitycenter.snippets.muteconfig; + +// [START securitycenter_bulk_mute] + +import com.google.cloud.securitycenter.v1.BulkMuteFindingsRequest; +import com.google.cloud.securitycenter.v1.BulkMuteFindingsResponse; +import com.google.cloud.securitycenter.v1.SecurityCenterClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +public class BulkMuteFindings { + + public static void main(String[] args) { + // TODO: Replace the variables within {} + + // parentPath: Use any one of the following options: + // - organizations/{organization_id} + // - folders/{folder_id} + // - projects/{project_id} + String parentPath = String.format("projects/%s", "your-google-cloud-project-id"); + + // muteRule: Expression that identifies findings that should be muted. + // eg: "resource.project_display_name=\"PROJECT_ID\"" + String muteRule = "{filter-condition}"; + + bulkMute(parentPath, muteRule); + } + + // Kicks off a long-running operation (LRO) to bulk mute findings for a parent based on a filter. + // The parent can be either an organization, folder, or project. The findings + // matched by the filter will be muted after the LRO is done. + public static void bulkMute(String parentPath, String muteRule) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + BulkMuteFindingsRequest bulkMuteFindingsRequest = + BulkMuteFindingsRequest.newBuilder() + .setParent(parentPath) + // To create mute rules, see: + // https://cloud.google.com/security-command-center/docs/how-to-mute-findings#create_mute_rules + .setFilter(muteRule) + .build(); + + // ExecutionException is thrown if the below call fails. + BulkMuteFindingsResponse response = + client.bulkMuteFindingsAsync(bulkMuteFindingsRequest).get(); + System.out.println("Bulk mute findings completed successfully! " + response); + } catch (IOException | InterruptedException | ExecutionException e) { + System.out.println("Bulk mute findings failed! \n Exception: " + e); + } + } +} +// [END securitycenter_bulk_mute] diff --git a/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/CreateMuteRule.java b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/CreateMuteRule.java new file mode 100644 index 000000000..90e07ca14 --- /dev/null +++ b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/CreateMuteRule.java @@ -0,0 +1,79 @@ +/* + * Copyright 2021 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 + * + * 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, + * 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.google.cloud.examples.securitycenter.snippets.muteconfig; + +// [START securitycenter_create_mute_config] + +import com.google.cloud.securitycenter.v1.CreateMuteConfigRequest; +import com.google.cloud.securitycenter.v1.MuteConfig; +import com.google.cloud.securitycenter.v1.SecurityCenterClient; +import java.io.IOException; +import java.util.UUID; + +public class CreateMuteRule { + + public static void main(String[] args) { + // TODO: Replace the variables within {} + + // parentPath: Use any one of the following options: + // - organizations/{organization_id} + // - folders/{folder_id} + // - projects/{project_id} + String parentPath = String.format("projects/%s", "your-google-cloud-project-id"); + + // muteConfigId: Set a random id; max of 63 chars. + String muteConfigId = "random-mute-id-" + UUID.randomUUID(); + createMuteRule(parentPath, muteConfigId); + } + + // Creates a mute configuration under a given scope that will mute + // all new findings that match a given filter. + // Existing findings will not be muted. + public static void createMuteRule(String parentPath, String muteConfigId) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + MuteConfig muteConfig = + MuteConfig.newBuilder() + .setDescription("Mute low-medium IAM grants excluding 'compute' ") + // Set mute rule(s). + // To construct mute rules and for supported properties, see: + // https://cloud.google.com/security-command-center/docs/how-to-mute-findings#create_mute_rules + .setFilter( + "severity=\"LOW\" OR severity=\"MEDIUM\" AND " + + "category=\"Persistence: IAM Anomalous Grant\" AND " + + "-resource.type:\"compute\"") + .build(); + + CreateMuteConfigRequest request = + CreateMuteConfigRequest.newBuilder() + .setParent(parentPath) + .setMuteConfigId(muteConfigId) + .setMuteConfig(muteConfig) + .build(); + + // ExecutionException is thrown if the below call fails. + MuteConfig response = client.createMuteConfig(request); + System.out.println("Mute rule created successfully: " + response.getName()); + } catch (IOException e) { + System.out.println("Mute rule creation failed! \n Exception: " + e); + } + } +} +// [END securitycenter_create_mute_config] diff --git a/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/DeleteMuteRule.java b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/DeleteMuteRule.java new file mode 100644 index 000000000..e959269cf --- /dev/null +++ b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/DeleteMuteRule.java @@ -0,0 +1,60 @@ +/* + * Copyright 2022 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 + * + * 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, + * 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.google.cloud.examples.securitycenter.snippets.muteconfig; + +// [START securitycenter_delete_mute_config] + +import com.google.cloud.securitycenter.v1.MuteConfigName; +import com.google.cloud.securitycenter.v1.SecurityCenterClient; +import java.io.IOException; + +public class DeleteMuteRule { + + public static void main(String[] args) { + // TODO(Developer): Replace the following variables + // parentPath: Use any one of the following options: + // - organizations/{organization_id} + // - folders/{folder_id} + // - projects/{project_id} + String parentPath = String.format("projects/%s", "your-google-cloud-project-id"); + + // muteConfigId: Specify the name of the mute config to delete. + String muteConfigId = "mute-config-id"; + + deleteMuteRule(parentPath, muteConfigId); + } + + // Deletes a mute configuration given its resource name. + // Note: Previously muted findings are not affected when a mute config is deleted. + public static void deleteMuteRule(String projectId, String muteConfigId) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // Use appropriate MuteConfigName methods depending on the type of parent. + // org -> MuteConfigName.ofOrganizationMuteConfigName() + // folder -> MuteConfigName.ofFolderMuteConfigName() + // project -> MuteConfigName.ofProjectMuteConfigName) + client.deleteMuteConfig(MuteConfigName.ofProjectMuteConfigName(projectId, muteConfigId)); + + System.out.println("Mute rule deleted successfully: " + muteConfigId); + } catch (IOException e) { + System.out.println("Mute rule deletion failed! \n Exception: " + e); + } + } +} +// [END securitycenter_delete_mute_config] diff --git a/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/GetMuteRule.java b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/GetMuteRule.java new file mode 100644 index 000000000..4947d40e2 --- /dev/null +++ b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/GetMuteRule.java @@ -0,0 +1,62 @@ +/* + * Copyright 2022 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 + * + * 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, + * 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.google.cloud.examples.securitycenter.snippets.muteconfig; + +// [START securitycenter_get_mute_config] + +import com.google.cloud.securitycenter.v1.MuteConfig; +import com.google.cloud.securitycenter.v1.MuteConfigName; +import com.google.cloud.securitycenter.v1.SecurityCenterClient; +import java.io.IOException; + +public class GetMuteRule { + + public static void main(String[] args) { + // TODO(Developer): Replace the following variables + + // parentPath: Use any one of the following options: + // - organizations/{organization_id} + // - folders/{folder_id} + // - projects/{project_id} + String parentPath = String.format("projects/%s", "your-google-cloud-project-id"); + + // muteConfigId: Name of the mute config to retrieve. + String muteConfigId = "mute-config-id"; + + getMuteRule(parentPath, muteConfigId); + } + + // Retrieves a mute configuration given its resource name. + public static void getMuteRule(String projectId, String muteConfigId) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { + // Use appropriate MuteConfigName methods depending on the type of parent. + // (org -> MuteConfigName.ofOrganizationMuteConfigName() + // folder -> MuteConfigName.ofFolderMuteConfigName() + // project -> MuteConfigName.ofProjectMuteConfigName) + MuteConfig muteConfig = + client.getMuteConfig(MuteConfigName.ofProjectMuteConfigName(projectId, muteConfigId)); + + System.out.println("Retrieved the mute config: " + muteConfig); + } catch (IOException e) { + System.out.println("Mute rule retrieval failed! \n Exception: " + e); + } + } +} +// [END securitycenter_get_mute_config] diff --git a/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/ListMuteRules.java b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/ListMuteRules.java new file mode 100644 index 000000000..521d89394 --- /dev/null +++ b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/ListMuteRules.java @@ -0,0 +1,61 @@ +/* + * Copyright 2021 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 + * + * 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, + * 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.google.cloud.examples.securitycenter.snippets.muteconfig; + +// [START securitycenter_list_mute_configs] + +import com.google.cloud.securitycenter.v1.ListMuteConfigsRequest; +import com.google.cloud.securitycenter.v1.MuteConfig; +import com.google.cloud.securitycenter.v1.SecurityCenterClient; +import java.io.IOException; + +public class ListMuteRules { + + public static void main(String[] args) { + // TODO: Replace variables enclosed within {} + + // parent: Use any one of the following resource paths to list mute configurations: + // - organizations/{organization_id} + // - folders/{folder_id} + // - projects/{project_id} + String parentPath = String.format("projects/%s", "your-google-cloud-project-id"); + listMuteRules(parentPath); + } + + // Listing mute configs at the organization level will return all the configs + // at the org, folder, and project levels. + // Similarly, listing configs at folder level will list all the configs + // at the folder and project levels. + public static void listMuteRules(String parent) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + ListMuteConfigsRequest listMuteConfigsRequest = + ListMuteConfigsRequest.newBuilder().setParent(parent).build(); + + // List all mute configs present in the resource. + for (MuteConfig muteConfig : client.listMuteConfigs(listMuteConfigsRequest).iterateAll()) { + System.out.println(muteConfig.getName()); + } + } catch (IOException e) { + System.out.println("Listing Mute rule failed! \n Exception: " + e); + } + } +} +// [END securitycenter_list_mute_configs] diff --git a/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/SetMuteUnmuteFinding.java b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/SetMuteUnmuteFinding.java new file mode 100644 index 000000000..5091fba88 --- /dev/null +++ b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/SetMuteUnmuteFinding.java @@ -0,0 +1,62 @@ +/* + * Copyright 2021 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 + * + * 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, + * 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.google.cloud.examples.securitycenter.snippets.muteconfig; + +// [START securitycenter_set_mute_unmute] + +import com.google.cloud.securitycenter.v1.Finding; +import com.google.cloud.securitycenter.v1.Finding.Mute; +import com.google.cloud.securitycenter.v1.SecurityCenterClient; +import com.google.cloud.securitycenter.v1.SetMuteRequest; +import java.io.IOException; + +public class SetMuteUnmuteFinding { + + public static void main(String[] args) { + // TODO: Replace the variables within {} + + // findingPath: The relative resource name of the finding. See: + // https://cloud.google.com/apis/design/resource_names#relative_resource_name + // Use any one of the following formats: + // - organizations/{organization_id}/sources/{source_id}/finding/{finding_id} + // - folders/{folder_id}/sources/{source_id}/finding/{finding_id} + // - projects/{project_id}/sources/{source_id}/finding/{finding_id} + String findingPath = "{path-to-the-finding}"; + setMute(findingPath); + } + + // Mute/unmute an individual finding. + // If a finding is already muted, muting it again has no effect. + // Similarly, unmuting a finding that isn't muted has no effect. + // Various mute states are: MUTE_UNSPECIFIED/MUTE/UNMUTE. + public static void setMute(String findingPath) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + SetMuteRequest setMuteRequest = + SetMuteRequest.newBuilder().setName(findingPath).setMute(Mute.MUTED).build(); + + Finding finding = client.setMute(setMuteRequest); + System.out.println("Mute value for the finding: " + finding.getMute()); + } catch (IOException e) { + System.out.println("Failed to set the specified mute value. \n Exception: " + e); + } + } +} +// [END securitycenter_set_mute_unmute] diff --git a/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/UpdateMuteRule.java b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/UpdateMuteRule.java new file mode 100644 index 000000000..507c87770 --- /dev/null +++ b/samples/snippets/src/main/java/com/google/cloud/examples/securitycenter/snippets/muteconfig/UpdateMuteRule.java @@ -0,0 +1,74 @@ +/* + * Copyright 2022 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 + * + * 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, + * 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.google.cloud.examples.securitycenter.snippets.muteconfig; + +// [START securitycenter_update_mute_config] + +import com.google.cloud.securitycenter.v1.MuteConfig; +import com.google.cloud.securitycenter.v1.SecurityCenterClient; +import com.google.cloud.securitycenter.v1.UpdateMuteConfigRequest; +import com.google.protobuf.FieldMask; +import java.io.IOException; + +public class UpdateMuteRule { + + public static void main(String[] args) { + // TODO: Replace the variables within {} + + // Specify the name of the mute config to delete. + // muteConfigName: Use any one of the following formats: + // - organizations/{organization}/muteConfigs/{config_id} + // - folders/{folder}/muteConfigs/{config_id} + // - projects/{project}/muteConfigs/{config_id} + String muteConfigName = "{any-one-of-the-above-formats}"; + updateMuteRule(muteConfigName); + } + + // Updates an existing mute configuration. + // The following can be updated in a mute config: description and filter. + public static void updateMuteRule(String muteConfigName) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient securityCenterClient = SecurityCenterClient.create()) { + + MuteConfig updateMuteConfig = + MuteConfig.newBuilder() + .setName(muteConfigName) + .setDescription("Updated mute config description") + .build(); + + UpdateMuteConfigRequest updateMuteConfigRequest = + UpdateMuteConfigRequest.newBuilder() + .setMuteConfig(updateMuteConfig) + // Set the update mask to specify which properties of the mute config should be + // updated. + // If empty, all mutable fields will be updated. + // Make sure that the mask fields match the properties changed in 'updateMuteConfig'. + // For more info on constructing update mask path, see the proto or: + // https://cloud.google.com/security-command-center/docs/reference/rest/v1/folders.muteConfigs/patch?hl=en#query-parameters + .setUpdateMask(FieldMask.newBuilder().addPaths("description").build()) + .build(); + + MuteConfig response = securityCenterClient.updateMuteConfig(updateMuteConfigRequest); + System.out.println(response); + } catch (IOException e) { + System.out.println("Mute rule update failed! \n Exception: " + e); + } + } +} +// [END securitycenter_update_mute_config] diff --git a/samples/snippets/src/test/java/com/google/cloud/examples/securitycenter/snippets/MuteFindingIT.java b/samples/snippets/src/test/java/com/google/cloud/examples/securitycenter/snippets/MuteFindingIT.java new file mode 100644 index 000000000..80f9e335b --- /dev/null +++ b/samples/snippets/src/test/java/com/google/cloud/examples/securitycenter/snippets/MuteFindingIT.java @@ -0,0 +1,237 @@ +/* + * Copyright 2022 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 + * + * 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, + * 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.google.cloud.examples.securitycenter.snippets; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; + +import com.google.cloud.examples.securitycenter.snippets.muteconfig.BulkMuteFindings; +import com.google.cloud.examples.securitycenter.snippets.muteconfig.CreateMuteRule; +import com.google.cloud.examples.securitycenter.snippets.muteconfig.DeleteMuteRule; +import com.google.cloud.examples.securitycenter.snippets.muteconfig.GetMuteRule; +import com.google.cloud.examples.securitycenter.snippets.muteconfig.ListMuteRules; +import com.google.cloud.examples.securitycenter.snippets.muteconfig.SetMuteUnmuteFinding; +import com.google.cloud.examples.securitycenter.snippets.muteconfig.UpdateMuteRule; +import com.google.cloud.securitycenter.v1.CreateSourceRequest; +import com.google.cloud.securitycenter.v1.Finding; +import com.google.cloud.securitycenter.v1.Finding.FindingClass; +import com.google.cloud.securitycenter.v1.Finding.Mute; +import com.google.cloud.securitycenter.v1.Finding.Severity; +import com.google.cloud.securitycenter.v1.Finding.State; +import com.google.cloud.securitycenter.v1.ListFindingsRequest; +import com.google.cloud.securitycenter.v1.ListFindingsResponse.ListFindingsResult; +import com.google.cloud.securitycenter.v1.SecurityCenterClient; +import com.google.cloud.securitycenter.v1.SecurityCenterClient.ListFindingsPagedResponse; +import com.google.cloud.securitycenter.v1.Source; +import com.google.protobuf.Timestamp; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.time.Instant; +import java.util.UUID; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class MuteFindingIT { + + // TODO(Developer): Replace the below variables. + private static final String PROJECT_ID = System.getenv("SCC_PROJECT_ID"); + private static final String ORGANIZATION_ID = System.getenv("SCC_PROJECT_ORG_ID"); + + private static final String MUTE_RULE_CREATE = "random-mute-id-" + UUID.randomUUID(); + private static final String MUTE_RULE_UPDATE = "random-mute-id-" + UUID.randomUUID(); + private static Source SOURCE; + // The findings will be used to test bulk mute. + private static Finding FINDING_1; + private static Finding FINDING_2; + private static Finding FINDING_3; + + private static ByteArrayOutputStream stdOut; + + // Check if the required environment variables are set. + public static void requireEnvVar(String envVarName) { + assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) + .that(System.getenv(envVarName)) + .isNotEmpty(); + } + + @BeforeClass + public static void setUp() throws IOException { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("SCC_PROJECT_ID"); + requireEnvVar("SCC_PROJECT_ORG_ID"); + + // Create mute rules. + CreateMuteRule.createMuteRule(String.format("projects/%s", PROJECT_ID), MUTE_RULE_CREATE); + CreateMuteRule.createMuteRule(String.format("projects/%s", PROJECT_ID), MUTE_RULE_UPDATE); + // Create source. + SOURCE = createSource(ORGANIZATION_ID); + // Create findings within the source. + String uuid = UUID.randomUUID().toString().split("-")[0]; + FINDING_1 = createFinding(SOURCE.getName(), "1testingscc" + uuid); + FINDING_2 = createFinding(SOURCE.getName(), "2testingscc" + uuid); + FINDING_3 = createFinding(SOURCE.getName(), "3testingscc" + uuid); + } + + @AfterClass + public static void cleanUp() { + final PrintStream out = System.out; + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + DeleteMuteRule.deleteMuteRule(PROJECT_ID, MUTE_RULE_CREATE); + assertThat(stdOut.toString()).contains("Mute rule deleted successfully: " + MUTE_RULE_CREATE); + DeleteMuteRule.deleteMuteRule(PROJECT_ID, MUTE_RULE_UPDATE); + assertThat(stdOut.toString()).contains("Mute rule deleted successfully: " + MUTE_RULE_UPDATE); + stdOut = null; + System.setOut(out); + } + + public static Source createSource(String organizationId) throws IOException { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + Source source = + Source.newBuilder() + .setDisplayName("Custom display name") + .setDescription("A source that does X") + .build(); + + CreateSourceRequest createSourceRequest = + CreateSourceRequest.newBuilder() + .setParent(String.format("organizations/%s", organizationId)) + .setSource(source) + .build(); + + Source response = client.createSource(createSourceRequest); + System.out.println("Created source : " + response.getName()); + return response; + } + } + + public static Finding createFinding(String sourceName, String findingId) throws IOException { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + Instant eventTime = Instant.now(); + + // The resource this finding applies to. The Cloud Security Command Center UI can link + // the findings for a resource to the corresponding asset of a resource + // if there are matches. + // TODO(Developer): Replace the sample resource name + String resourceName = "//cloudresourcemanager.googleapis.com/organizations/11232"; + + // Set up a request to create a finding in a source. + Finding finding = + Finding.newBuilder() + .setParent(sourceName) + .setState(State.ACTIVE) + .setSeverity(Severity.LOW) + .setMute(Mute.UNMUTED) + .setFindingClass(FindingClass.OBSERVATION) + .setResourceName(resourceName) + .setEventTime( + Timestamp.newBuilder() + .setSeconds(eventTime.getEpochSecond()) + .setNanos(eventTime.getNano())) + .setCategory("LOW_RISK_ONE") + .build(); + + Finding response = client.createFinding(sourceName, findingId, finding); + + System.out.println("Created Finding: " + response); + return response; + } + } + + public static ListFindingsPagedResponse getAllFindings(String sourceName) throws IOException { + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + ListFindingsRequest request = ListFindingsRequest.newBuilder().setParent(sourceName).build(); + + return client.listFindings(request); + } + } + + private static String getOrganizationId() { + return "1081635000895"; + } + + private static String getProject() { + return "project-a-id"; + } + + @Before + public void beforeEach() { + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + } + + @After + public void afterEach() { + stdOut = null; + System.setOut(null); + } + + @Test + public void testGetMuteRule() { + GetMuteRule.getMuteRule(PROJECT_ID, MUTE_RULE_CREATE); + assertThat(stdOut.toString()).contains("Retrieved the mute config: "); + assertThat(stdOut.toString()).contains(MUTE_RULE_CREATE); + } + + @Test + public void testListMuteRules() { + ListMuteRules.listMuteRules(String.format("projects/%s", PROJECT_ID)); + assertThat(stdOut.toString()).contains(MUTE_RULE_CREATE); + assertThat(stdOut.toString()).contains(MUTE_RULE_UPDATE); + } + + @Test + public void testUpdateMuteRules() { + UpdateMuteRule.updateMuteRule( + String.format("projects/%s/muteConfigs/%s", PROJECT_ID, MUTE_RULE_UPDATE)); + GetMuteRule.getMuteRule(PROJECT_ID, MUTE_RULE_UPDATE); + assertThat(stdOut.toString()).contains("Updated mute config description"); + } + + @Test + public void testSetMuteFinding() { + SetMuteUnmuteFinding.setMute(FINDING_1.getName()); + assertThat(stdOut.toString()).contains("Mute value for the finding: MUTED"); + } + + @Test + public void testBulkMuteFindings() throws IOException { + // Mute findings that belong to this project. + BulkMuteFindings.bulkMute( + String.format("projects/%s", PROJECT_ID), + String.format("resource.project_display_name=\"%s\"", PROJECT_ID)); + + // Get all findings in the source to check if they are muted. + ListFindingsPagedResponse response = + getAllFindings( + String.format("projects/%s/sources/%s", PROJECT_ID, SOURCE.getName().split("/")[3])); + for (ListFindingsResult finding : response.iterateAll()) { + Assert.assertEquals(finding.getFinding().getMute(), Mute.MUTED); + } + } +} diff --git a/versions.txt b/versions.txt index d406e4f75..794a7528c 100644 --- a/versions.txt +++ b/versions.txt @@ -1,10 +1,10 @@ # Format: # module:released-version:current-version -google-cloud-securitycenter:2.5.2:2.5.2 -grpc-google-cloud-securitycenter-v1:2.5.2:2.5.2 -grpc-google-cloud-securitycenter-v1beta1:0.100.2:0.100.2 -grpc-google-cloud-securitycenter-v1p1beta1:0.100.2:0.100.2 -proto-google-cloud-securitycenter-v1:2.5.2:2.5.2 -proto-google-cloud-securitycenter-v1beta1:0.100.2:0.100.2 -proto-google-cloud-securitycenter-v1p1beta1:0.100.2:0.100.2 +google-cloud-securitycenter:2.5.3:2.5.3 +grpc-google-cloud-securitycenter-v1:2.5.3:2.5.3 +grpc-google-cloud-securitycenter-v1beta1:0.100.3:0.100.3 +grpc-google-cloud-securitycenter-v1p1beta1:0.100.3:0.100.3 +proto-google-cloud-securitycenter-v1:2.5.3:2.5.3 +proto-google-cloud-securitycenter-v1beta1:0.100.3:0.100.3 +proto-google-cloud-securitycenter-v1p1beta1:0.100.3:0.100.3