Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.

Commit 05018a5

Browse files
Sita04gcf-owl-bot[bot]Neenu1995
authored
docs(samples): added samples for Mute config (#719)
* docs(scc-samples): init add CRUD mute config samples * added bulk mute and mute/unmute samples * refactor(samples): modified class name * refactor(samples): added muteConfigId and refactored class name * test(samples): added tests for mute config samples * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * docs(samples): updated samples and test files * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * docs(samples): lint fix * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * docs(samples): applied documentation review comments * docs(samples): applied review comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * docs(samples): added test env variables * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * docs(samples): added test env variables * updated config typo * test: try changing the env var name * Update samples.cfg * Update samples.cfg * updated samples.cfg * docs(samples): updated acc to review comments * lint fix * lint fix * lint fix Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Neenu Shaji <[email protected]>
1 parent 7b83366 commit 05018a5

File tree

11 files changed

+718
-1
lines changed

11 files changed

+718
-1
lines changed

.kokoro/presubmit/samples.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ env_vars: {
2929

3030
env_vars: {
3131
key: "SECRET_MANAGER_KEYS"
32-
value: "java-docs-samples-service-account"
32+
value: "java-docs-samples-service-account,java-scc-samples-secrets"
3333
}

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file
3030
<dependency>
3131
<groupId>com.google.cloud</groupId>
3232
<artifactId>google-cloud-securitycenter</artifactId>
33+
<version>2.3.1</version>
3334
</dependency>
3435

3536
<dependency>
@@ -56,6 +57,7 @@ If you are using Gradle 5.x or later, add this to your dependencies
5657
```Groovy
5758
implementation platform('com.google.cloud:libraries-bom:25.0.0')
5859
60+
5961
implementation 'com.google.cloud:google-cloud-securitycenter'
6062
```
6163
If you are using Gradle without BOM, add this to your dependencies
@@ -117,6 +119,13 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-securitycente
117119
| 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) |
118120
| 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) |
119121
| 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) |
122+
| 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) |
123+
| 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) |
124+
| 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) |
125+
| 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) |
126+
| 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) |
127+
| 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) |
128+
| 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) |
120129

121130

122131

samples/snippets/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<dependency>
4242
<groupId>com.google.cloud</groupId>
4343
<artifactId>google-cloud-securitycenter</artifactId>
44+
<version>2.3.1</version>
4445
</dependency>
4546

4647
<dependency>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.securitycenter.snippets.muteconfig;
18+
19+
// [START securitycenter_bulk_mute]
20+
21+
import com.google.cloud.securitycenter.v1.BulkMuteFindingsRequest;
22+
import com.google.cloud.securitycenter.v1.BulkMuteFindingsResponse;
23+
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
27+
public class BulkMuteFindings {
28+
29+
public static void main(String[] args) {
30+
// TODO: Replace the variables within {}
31+
32+
// parentPath: Use any one of the following options:
33+
// - organizations/{organization_id}
34+
// - folders/{folder_id}
35+
// - projects/{project_id}
36+
String parentPath = String.format("projects/%s", "your-google-cloud-project-id");
37+
38+
// muteRule: Expression that identifies findings that should be muted.
39+
// eg: "resource.project_display_name=\"PROJECT_ID\""
40+
String muteRule = "{filter-condition}";
41+
42+
bulkMute(parentPath, muteRule);
43+
}
44+
45+
// Kicks off a long-running operation (LRO) to bulk mute findings for a parent based on a filter.
46+
// The parent can be either an organization, folder, or project. The findings
47+
// matched by the filter will be muted after the LRO is done.
48+
public static void bulkMute(String parentPath, String muteRule) {
49+
// Initialize client that will be used to send requests. This client only needs to be created
50+
// once, and can be reused for multiple requests. After completing all of your requests, call
51+
// the "close" method on the client to safely clean up any remaining background resources.
52+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
53+
54+
BulkMuteFindingsRequest bulkMuteFindingsRequest =
55+
BulkMuteFindingsRequest.newBuilder()
56+
.setParent(parentPath)
57+
// To create mute rules, see:
58+
// https://cloud.google.com/security-command-center/docs/how-to-mute-findings#create_mute_rules
59+
.setFilter(muteRule)
60+
.build();
61+
62+
// ExecutionException is thrown if the below call fails.
63+
BulkMuteFindingsResponse response =
64+
client.bulkMuteFindingsAsync(bulkMuteFindingsRequest).get();
65+
System.out.println("Bulk mute findings completed successfully! " + response);
66+
} catch (IOException | InterruptedException | ExecutionException e) {
67+
System.out.println("Bulk mute findings failed! \n Exception: " + e);
68+
}
69+
}
70+
}
71+
// [END securitycenter_bulk_mute]
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.securitycenter.snippets.muteconfig;
18+
19+
// [START securitycenter_create_mute_config]
20+
21+
import com.google.cloud.securitycenter.v1.CreateMuteConfigRequest;
22+
import com.google.cloud.securitycenter.v1.MuteConfig;
23+
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
24+
import java.io.IOException;
25+
import java.util.UUID;
26+
27+
public class CreateMuteRule {
28+
29+
public static void main(String[] args) {
30+
// TODO: Replace the variables within {}
31+
32+
// parentPath: Use any one of the following options:
33+
// - organizations/{organization_id}
34+
// - folders/{folder_id}
35+
// - projects/{project_id}
36+
String parentPath = String.format("projects/%s", "your-google-cloud-project-id");
37+
38+
// muteConfigId: Set a random id; max of 63 chars.
39+
String muteConfigId = "random-mute-id-" + UUID.randomUUID();
40+
createMuteRule(parentPath, muteConfigId);
41+
}
42+
43+
// Creates a mute configuration under a given scope that will mute
44+
// all new findings that match a given filter.
45+
// Existing findings will not be muted.
46+
public static void createMuteRule(String parentPath, String muteConfigId) {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// once, and can be reused for multiple requests. After completing all of your requests, call
49+
// the "close" method on the client to safely clean up any remaining background resources.
50+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
51+
52+
MuteConfig muteConfig =
53+
MuteConfig.newBuilder()
54+
.setDescription("Mute low-medium IAM grants excluding 'compute' ")
55+
// Set mute rule(s).
56+
// To construct mute rules and for supported properties, see:
57+
// https://cloud.google.com/security-command-center/docs/how-to-mute-findings#create_mute_rules
58+
.setFilter(
59+
"severity=\"LOW\" OR severity=\"MEDIUM\" AND "
60+
+ "category=\"Persistence: IAM Anomalous Grant\" AND "
61+
+ "-resource.type:\"compute\"")
62+
.build();
63+
64+
CreateMuteConfigRequest request =
65+
CreateMuteConfigRequest.newBuilder()
66+
.setParent(parentPath)
67+
.setMuteConfigId(muteConfigId)
68+
.setMuteConfig(muteConfig)
69+
.build();
70+
71+
// ExecutionException is thrown if the below call fails.
72+
MuteConfig response = client.createMuteConfig(request);
73+
System.out.println("Mute rule created successfully: " + response.getName());
74+
} catch (IOException e) {
75+
System.out.println("Mute rule creation failed! \n Exception: " + e);
76+
}
77+
}
78+
}
79+
// [END securitycenter_create_mute_config]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.securitycenter.snippets.muteconfig;
18+
19+
// [START securitycenter_delete_mute_config]
20+
21+
import com.google.cloud.securitycenter.v1.MuteConfigName;
22+
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
23+
import java.io.IOException;
24+
25+
public class DeleteMuteRule {
26+
27+
public static void main(String[] args) {
28+
// TODO(Developer): Replace the following variables
29+
// parentPath: Use any one of the following options:
30+
// - organizations/{organization_id}
31+
// - folders/{folder_id}
32+
// - projects/{project_id}
33+
String parentPath = String.format("projects/%s", "your-google-cloud-project-id");
34+
35+
// muteConfigId: Specify the name of the mute config to delete.
36+
String muteConfigId = "mute-config-id";
37+
38+
deleteMuteRule(parentPath, muteConfigId);
39+
}
40+
41+
// Deletes a mute configuration given its resource name.
42+
// Note: Previously muted findings are not affected when a mute config is deleted.
43+
public static void deleteMuteRule(String projectId, String muteConfigId) {
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests. After completing all of your requests, call
46+
// the "close" method on the client to safely clean up any remaining background resources.
47+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
48+
// Use appropriate MuteConfigName methods depending on the type of parent.
49+
// org -> MuteConfigName.ofOrganizationMuteConfigName()
50+
// folder -> MuteConfigName.ofFolderMuteConfigName()
51+
// project -> MuteConfigName.ofProjectMuteConfigName)
52+
client.deleteMuteConfig(MuteConfigName.ofProjectMuteConfigName(projectId, muteConfigId));
53+
54+
System.out.println("Mute rule deleted successfully: " + muteConfigId);
55+
} catch (IOException e) {
56+
System.out.println("Mute rule deletion failed! \n Exception: " + e);
57+
}
58+
}
59+
}
60+
// [END securitycenter_delete_mute_config]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.securitycenter.snippets.muteconfig;
18+
19+
// [START securitycenter_get_mute_config]
20+
21+
import com.google.cloud.securitycenter.v1.MuteConfig;
22+
import com.google.cloud.securitycenter.v1.MuteConfigName;
23+
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
24+
import java.io.IOException;
25+
26+
public class GetMuteRule {
27+
28+
public static void main(String[] args) {
29+
// TODO(Developer): Replace the following variables
30+
31+
// parentPath: Use any one of the following options:
32+
// - organizations/{organization_id}
33+
// - folders/{folder_id}
34+
// - projects/{project_id}
35+
String parentPath = String.format("projects/%s", "your-google-cloud-project-id");
36+
37+
// muteConfigId: Name of the mute config to retrieve.
38+
String muteConfigId = "mute-config-id";
39+
40+
getMuteRule(parentPath, muteConfigId);
41+
}
42+
43+
// Retrieves a mute configuration given its resource name.
44+
public static void getMuteRule(String projectId, String muteConfigId) {
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests. After completing all of your requests, call
47+
// the "close" method on the client to safely clean up any remaining background resources.
48+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
49+
// Use appropriate MuteConfigName methods depending on the type of parent.
50+
// (org -> MuteConfigName.ofOrganizationMuteConfigName()
51+
// folder -> MuteConfigName.ofFolderMuteConfigName()
52+
// project -> MuteConfigName.ofProjectMuteConfigName)
53+
MuteConfig muteConfig =
54+
client.getMuteConfig(MuteConfigName.ofProjectMuteConfigName(projectId, muteConfigId));
55+
56+
System.out.println("Retrieved the mute config: " + muteConfig);
57+
} catch (IOException e) {
58+
System.out.println("Mute rule retrieval failed! \n Exception: " + e);
59+
}
60+
}
61+
}
62+
// [END securitycenter_get_mute_config]

0 commit comments

Comments
 (0)