Skip to content

Commit 601aa2a

Browse files
hegemonictelpirion
andauthored
feat(security-command-center): add v2 version of SetMuteUndefinedFinding.java (#9589)
* feat(security-command-center): add v2 version of SetMuteUndefinedFinding.java * revisions per style --------- Co-authored-by: Eric Schmidt <[email protected]>
1 parent d34c8b7 commit 601aa2a

File tree

2 files changed

+86
-13
lines changed

2 files changed

+86
-13
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2024 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 vtwo.muteconfig;
18+
19+
// [START securitycenter_set_mute_undefined_v2]
20+
21+
import com.google.cloud.securitycenter.v2.Finding;
22+
import com.google.cloud.securitycenter.v2.Finding.Mute;
23+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
24+
import com.google.cloud.securitycenter.v2.SetMuteRequest;
25+
import java.io.IOException;
26+
27+
public class SetMuteUndefinedFinding {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO: Replace the variables within {}
31+
32+
// findingPath: The relative resource name of the finding. See:
33+
// https://cloud.google.com/apis/design/resource_names#relative_resource_name
34+
// Use any one of the following formats:
35+
// - organizations/{organization_id}/sources/{source_id}/finding/{finding_id}
36+
// - folders/{folder_id}/sources/{source_id}/finding/{finding_id}
37+
// - projects/{project_id}/sources/{source_id}/finding/{finding_id}
38+
String findingPath = "{path-to-the-finding}";
39+
setMuteUndefined(findingPath);
40+
}
41+
42+
// Reset mute state of an individual finding.
43+
// If a finding is already reset, resetting it again has no effect.
44+
// Various mute states are: MUTE_UNSPECIFIED/MUTE/UNMUTE/UNDEFINED.
45+
public static Finding setMuteUndefined(String findingPath) throws IOException {
46+
// Initialize client that will be used to send requests. This client only needs
47+
// to be created once, and can be reused for multiple requests.
48+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
49+
50+
SetMuteRequest setMuteRequest =
51+
SetMuteRequest.newBuilder()
52+
.setName(findingPath)
53+
.setMute(Mute.UNDEFINED)
54+
.build();
55+
56+
Finding finding = client.setMute(setMuteRequest);
57+
System.out.println(
58+
"Mute value for the finding " + finding.getName() + " is: " + finding.getMute());
59+
return finding;
60+
}
61+
}
62+
}
63+
// [END securitycenter_set_mute_undefined_v2]

security-command-center/snippets/src/test/java/vtwo/MuteFindingIT.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import vtwo.muteconfig.GetMuteRule;
5252
import vtwo.muteconfig.ListMuteRules;
5353
import vtwo.muteconfig.SetMuteFinding;
54+
import vtwo.muteconfig.SetMuteUndefinedFinding;
5455
import vtwo.muteconfig.SetUnmuteFinding;
5556
import vtwo.muteconfig.UpdateMuteRule;
5657
import vtwo.source.CreateSource;
@@ -74,9 +75,8 @@ public class MuteFindingIT {
7475
private static ByteArrayOutputStream stdOut;
7576

7677
@Rule
77-
public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(
78-
MAX_ATTEMPT_COUNT,
79-
INITIAL_BACKOFF_MILLIS);
78+
public final MultipleAttemptsRule multipleAttemptsRule =
79+
new MultipleAttemptsRule(MAX_ATTEMPT_COUNT, INITIAL_BACKOFF_MILLIS);
8080

8181
// Check if the required environment variables are set.
8282
public static void requireEnvVar(String envVarName) {
@@ -104,12 +104,22 @@ public static void setUp() throws IOException, InterruptedException {
104104

105105
// Create findings within the source.
106106
String uuid = UUID.randomUUID().toString().split("-")[0];
107-
FINDING_1 = CreateFindings.createFinding(ORGANIZATION_ID, LOCATION, "testfindingv2" + uuid,
108-
SOURCE.getName().split("/")[3], Optional.of("MEDIUM_RISK_ONE"));
107+
FINDING_1 =
108+
CreateFindings.createFinding(
109+
ORGANIZATION_ID,
110+
LOCATION,
111+
"testfindingv2" + uuid,
112+
SOURCE.getName().split("/")[3],
113+
Optional.of("MEDIUM_RISK_ONE"));
109114

110115
uuid = UUID.randomUUID().toString().split("-")[0];
111-
FINDING_2 = CreateFindings.createFinding(ORGANIZATION_ID, LOCATION, "testfindingv2" + uuid,
112-
SOURCE.getName().split("/")[3], Optional.empty());
116+
FINDING_2 =
117+
CreateFindings.createFinding(
118+
ORGANIZATION_ID,
119+
LOCATION,
120+
"testfindingv2" + uuid,
121+
SOURCE.getName().split("/")[3],
122+
Optional.empty());
113123

114124
stdOut = null;
115125
System.setOut(out);
@@ -132,9 +142,7 @@ public static void cleanUp() throws IOException {
132142
public static ListFindingsPagedResponse getAllFindings(String sourceName) throws IOException {
133143
try (SecurityCenterClient client = SecurityCenterClient.create()) {
134144

135-
ListFindingsRequest request = ListFindingsRequest.newBuilder()
136-
.setParent(sourceName)
137-
.build();
145+
ListFindingsRequest request = ListFindingsRequest.newBuilder().setParent(sourceName).build();
138146

139147
return client.listFindings(request);
140148
}
@@ -173,18 +181,20 @@ public void testUpdateMuteRules() throws IOException {
173181
}
174182

175183
@Test
176-
public void testMuteUnmuteFinding() throws IOException {
184+
public void testSetMuteFinding() throws IOException {
177185
Finding finding = SetMuteFinding.setMute(FINDING_1.getName());
178186
assertThat(finding.getMute()).isEqualTo(Mute.MUTED);
179187
finding = SetUnmuteFinding.setUnmute(FINDING_1.getName());
180188
assertThat(finding.getMute()).isEqualTo(Mute.UNMUTED);
189+
finding = SetMuteUndefinedFinding.setMuteUndefined(FINDING_1.getName());
190+
assertThat(finding.getMute()).isEqualTo(Mute.UNDEFINED);
181191
}
182192

183193
@Test
184194
public void testBulkMuteFindings() throws IOException, ExecutionException, InterruptedException {
185195
// Mute findings that belong to this project.
186-
BulkMuteFindings.bulkMute(PROJECT_ID, LOCATION,
187-
String.format("resource.project_display_name=\"%s\"", PROJECT_ID));
196+
BulkMuteFindings.bulkMute(
197+
PROJECT_ID, LOCATION, String.format("resource.project_display_name=\"%s\"", PROJECT_ID));
188198

189199
// Get all findings in the source to check if they are muted.
190200
ListFindingsPagedResponse response =

0 commit comments

Comments
 (0)