Skip to content

Commit 0638695

Browse files
authored
feat(scc): add v2 samples - muteconfig and findings (GoogleCloudPlatform#8983)
* feat(scc-samples): add scc v2 samples * add more v2 samples and test * move files * update samples and tests * lint fix - update package name * update acc to review * Update comment and bom version * Update pom.xml * adding multiple attempts rule * increase backoff time * introduce latency in starting tests to avoid quota issue * test by ignoring the test * ignore testcase * revert ignored testcase and update listallfindings to include sourceId * lint fix * lint fix * update acc to review * Update pom.xml * update package name for lint error * update package name * return finding from samples * change assert statement * update assert checks as global (drz) is returned for all mute rules
1 parent 8764841 commit 0638695

19 files changed

+1254
-19
lines changed

security-command-center/snippets/pom.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<dependency>
3232
<groupId>com.google.cloud</groupId>
3333
<artifactId>libraries-bom</artifactId>
34-
<version>26.32.0</version>
34+
<version>26.33.0</version>
3535
<type>pom</type>
3636
<scope>import</scope>
3737
</dependency>
@@ -42,7 +42,8 @@
4242
<dependency>
4343
<groupId>com.google.cloud</groupId>
4444
<artifactId>google-cloud-securitycenter</artifactId>
45-
</dependency>
45+
<version>2.45.0</version>
46+
</dependency>
4647

4748
<dependency>
4849
<groupId>com.google.cloud</groupId>
@@ -58,7 +59,7 @@
5859
<dependency>
5960
<groupId>com.google.protobuf</groupId>
6061
<artifactId>protobuf-java-util</artifactId>
61-
</dependency>
62+
</dependency>
6263

6364
<dependency>
6465
<groupId>junit</groupId>
@@ -73,4 +74,4 @@
7374
<scope>test</scope>
7475
</dependency>
7576
</dependencies>
76-
</project>
77+
</project>

security-command-center/snippets/src/main/java/muteconfig/SetMuteFinding.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
public class SetMuteFinding {
2828

29-
public static void main(String[] args) {
29+
public static void main(String[] args) throws IOException {
3030
// TODO: Replace the variables within {}
3131

3232
// findingPath: The relative resource name of the finding. See:
@@ -42,7 +42,7 @@ public static void main(String[] args) {
4242
// Mute an individual finding.
4343
// If a finding is already muted, muting it again has no effect.
4444
// Various mute states are: MUTE_UNSPECIFIED/MUTE/UNMUTE.
45-
public static void setMute(String findingPath) {
45+
public static Finding setMute(String findingPath) throws IOException {
4646
// Initialize client that will be used to send requests. This client only needs to be created
4747
// once, and can be reused for multiple requests. After completing all of your requests, call
4848
// the "close" method on the client to safely clean up any remaining background resources.
@@ -54,8 +54,7 @@ public static void setMute(String findingPath) {
5454
Finding finding = client.setMute(setMuteRequest);
5555
System.out.println(
5656
"Mute value for the finding " + finding.getName() + " is: " + finding.getMute());
57-
} catch (IOException e) {
58-
System.out.println("Failed to set the specified mute value. \n Exception: " + e);
57+
return finding;
5958
}
6059
}
6160
}

security-command-center/snippets/src/main/java/muteconfig/SetUnmuteFinding.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
public class SetUnmuteFinding {
2828

29-
public static void main(String[] args) {
29+
public static void main(String[] args) throws IOException {
3030
// TODO: Replace the variables within {}
3131

3232
// findingPath: The relative resource name of the finding. See:
@@ -42,7 +42,7 @@ public static void main(String[] args) {
4242
// Unmute an individual finding.
4343
// Unmuting a finding that isn't muted has no effect.
4444
// Various mute states are: MUTE_UNSPECIFIED/MUTE/UNMUTE.
45-
public static void setUnmute(String findingPath) {
45+
public static Finding setUnmute(String findingPath) throws IOException {
4646
// Initialize client that will be used to send requests. This client only needs to be created
4747
// once, and can be reused for multiple requests. After completing all of your requests, call
4848
// the "close" method on the client to safely clean up any remaining background resources.
@@ -54,8 +54,7 @@ public static void setUnmute(String findingPath) {
5454
Finding finding = client.setMute(setMuteRequest);
5555
System.out.println(
5656
"Mute value for the finding " + finding.getName() + " is: " + finding.getMute());
57-
} catch (IOException e) {
58-
System.out.println("Failed to set the specified mute value. \n Exception: " + e);
57+
return finding;
5958
}
6059
}
6160
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.findings;
18+
19+
// [START securitycenter_group_all_findings_v2]
20+
21+
import com.google.cloud.securitycenter.v2.GroupFindingsRequest;
22+
import com.google.cloud.securitycenter.v2.GroupResult;
23+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
24+
import java.io.IOException;
25+
26+
public class GroupFindings {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO: Replace the variables within {}
30+
// organizationId: Google Cloud Organization id.
31+
String organizationId = "google-cloud-organization-id";
32+
33+
// Specify the location to scope the findings to.
34+
String location = "global";
35+
36+
// The source id corresponding to the finding.
37+
String sourceId = "source-id";
38+
39+
groupFindings(organizationId, sourceId, location);
40+
}
41+
42+
// Group all findings under a parent type across all sources by their specified properties
43+
// (e.g category, state).
44+
public static void groupFindings(String organizationId, String sourceId, String location)
45+
throws IOException {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests.
48+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
49+
// Use any one of the following formats:
50+
// * organizations/{organization_id}/sources/{source_id}/locations/{location}
51+
// * folders/{folder_id}/sources/{source_id}/locations/{location}
52+
// * projects/{project_id}/sources/{source_id}/locations/{location}
53+
String parent = String.format("organizations/%s/sources/%s/locations/%s",
54+
organizationId,
55+
sourceId,
56+
location);
57+
58+
GroupFindingsRequest request =
59+
GroupFindingsRequest.newBuilder()
60+
.setParent(parent)
61+
// Supported grouping properties: resource_name/ category/ state/ parent/ severity.
62+
// Multiple properties should be separated by comma.
63+
.setGroupBy("category, state")
64+
.build();
65+
66+
for (GroupResult result : client.groupFindings(request).iterateAll()) {
67+
System.out.println(result.getPropertiesMap());
68+
}
69+
System.out.println("Listed grouped findings.");
70+
}
71+
}
72+
}
73+
// [END securitycenter_group_all_findings_v2]
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.findings;
18+
19+
// [START securitycenter_group_filtered_findings_v2]
20+
21+
import com.google.cloud.securitycenter.v2.GroupFindingsRequest;
22+
import com.google.cloud.securitycenter.v2.GroupResult;
23+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
24+
import java.io.IOException;
25+
26+
public class GroupFindingsWithFilter {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO: Replace the variables within {}
30+
// organizationId: Google Cloud Organization id.
31+
String organizationId = "google-cloud-organization-id";
32+
33+
// Specify the location to scope the findings to.
34+
String location = "global";
35+
36+
// The source id corresponding to the finding.
37+
String sourceId = "source-id";
38+
39+
groupFilteredFindings(organizationId, sourceId, location);
40+
}
41+
42+
// Group filtered findings under a parent type across all sources by their specified properties
43+
// (e.g. category, state).
44+
public static void groupFilteredFindings(String organizationId, String sourceId, String location)
45+
throws IOException {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests.
48+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
49+
// Use any one of the following formats:
50+
// * organizations/{organization_id}/sources/{source_id}/locations/{location}
51+
// * folders/{folder_id}/sources/{source_id}/locations/{location}
52+
// * projects/{project_id}/sources/{source_id}/locations/{location}
53+
String parent = String.format("organizations/%s/sources/%s/locations/%s", organizationId,
54+
sourceId,
55+
location);
56+
57+
// Group all findings of category "MEDIUM_RISK_ONE".
58+
String filter = "category=\"MEDIUM_RISK_ONE\"";
59+
60+
GroupFindingsRequest request =
61+
GroupFindingsRequest.newBuilder()
62+
.setParent(parent)
63+
// Supported grouping properties: resource_name/ category/ state/ parent/ severity.
64+
// Multiple properties should be separated by comma.
65+
.setGroupBy("state, category")
66+
.setFilter(filter)
67+
.build();
68+
69+
for (GroupResult result : client.groupFindings(request).iterateAll()) {
70+
System.out.println(result);
71+
}
72+
System.out.println("Listed all filtered and grouped findings.");
73+
}
74+
}
75+
}
76+
// [END securitycenter_group_filtered_findings_v2]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.findings;
18+
19+
// [START securitycenter_list_all_findings_v2]
20+
21+
import com.google.cloud.securitycenter.v2.ListFindingsRequest;
22+
import com.google.cloud.securitycenter.v2.ListFindingsResponse.ListFindingsResult;
23+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
24+
import java.io.IOException;
25+
26+
public class ListAllFindings {
27+
28+
public static void main(String[] args) throws IOException {
29+
// organizationId: The source to list all findings for.
30+
// You can also use project/ folder as the parent resource.
31+
String organizationId = "google-cloud-organization-id";
32+
33+
// Specify the location to list the findings.
34+
String location = "global";
35+
36+
// The source id to scope the findings.
37+
String sourceId = "source-id";
38+
39+
listAllFindings(organizationId, sourceId, location);
40+
}
41+
42+
// List all findings under a given parent resource.
43+
public static void listAllFindings(String organizationId, String sourceId, String location)
44+
throws IOException {
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.
47+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
48+
ListFindingsRequest request =
49+
ListFindingsRequest.newBuilder()
50+
// To list findings across all sources, use "-".
51+
.setParent(
52+
String.format("organizations/%s/sources/%s/locations/%s", organizationId,
53+
sourceId,
54+
location))
55+
.build();
56+
57+
for (ListFindingsResult result : client.listFindings(request).iterateAll()) {
58+
System.out.printf("Finding: %s", result.getFinding().getName());
59+
}
60+
System.out.println("\nListing complete.");
61+
}
62+
}
63+
}
64+
// [END securitycenter_list_all_findings_v2]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.findings;
18+
19+
// [START securitycenter_list_filtered_findings_v2]
20+
21+
import com.google.cloud.securitycenter.v2.ListFindingsRequest;
22+
import com.google.cloud.securitycenter.v2.ListFindingsResponse.ListFindingsResult;
23+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
24+
import java.io.IOException;
25+
26+
public class ListFindingsWithFilter {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO: Replace the variables within {}
30+
// organizationId: Google Cloud Organization id.
31+
// You can also use project/ folder as the parent resource.
32+
String organizationId = "google-cloud-organization-id";
33+
34+
// Specify the location to list the findings.
35+
String location = "global";
36+
37+
// The source id to scope the findings.
38+
String sourceId = "source-id";
39+
40+
listFilteredFindings(organizationId, sourceId, location);
41+
}
42+
43+
// List filtered findings under a source.
44+
public static void listFilteredFindings(String organizationId, String sourceId, String location)
45+
throws IOException {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests.
48+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
49+
50+
// Use any one of the following formats:
51+
// * organizations/{organization_id}/sources/{source_id}/locations/{location}
52+
// * folders/{folder_id}/sources/{source_id}/locations/{location}
53+
// * projects/{project_id}/sources/{source_id}/locations/{location}
54+
String parent = String.format("organizations/%s/sources/%s/locations/%s", organizationId,
55+
sourceId,
56+
location);
57+
58+
// Listing all findings of category "MEDIUM_RISK_ONE".
59+
String filter = "category=\"MEDIUM_RISK_ONE\"";
60+
61+
ListFindingsRequest request =
62+
ListFindingsRequest.newBuilder()
63+
.setParent(parent)
64+
.setFilter(filter)
65+
.build();
66+
67+
for (ListFindingsResult result : client.listFindings(request).iterateAll()) {
68+
System.out.printf("Finding: %s", result.getFinding().getName());
69+
}
70+
System.out.println("\nListing complete.");
71+
}
72+
}
73+
}
74+
// [END securitycenter_list_filtered_findings_v2]

0 commit comments

Comments
 (0)