Skip to content

Commit be1a4af

Browse files
authored
feat(securitycenter): Add Resource SCC Management API Org ETD Custom Module code samples (Update, Get Eff, List Eff, List Desc, Validate) (GoogleCloudPlatform#9912)
* sample codes for event threat detection custom modules * fixed lint * addressed comments * lint fix * addressed comments
1 parent 66ab30f commit be1a4af

10 files changed

+418
-8
lines changed

security-command-center/snippets/src/main/java/management/api/CreateEventThreatDetectionCustomModule.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public static EventThreatDetectionCustomModule createEventThreatDetectionCustomM
5050
// once, and can be reused for multiple requests.
5151
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
5252

53+
String parent = String.format("projects/%s/locations/global", projectId);
54+
5355
// define the metadata and other config parameters severity, description,
5456
// recommendation and ips below
5557
Map<String, Value> metadata = new HashMap<>();
@@ -83,7 +85,7 @@ public static EventThreatDetectionCustomModule createEventThreatDetectionCustomM
8385

8486
CreateEventThreatDetectionCustomModuleRequest request =
8587
CreateEventThreatDetectionCustomModuleRequest.newBuilder()
86-
.setParent(String.format("projects/%s/locations/global", projectId))
88+
.setParent(parent)
8789
.setEventThreatDetectionCustomModule(eventThreatDetectionCustomModule)
8890
.build();
8991

security-command-center/snippets/src/main/java/management/api/DeleteEventThreatDetectionCustomModule.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ public static boolean deleteEventThreatDetectionCustomModule(
4141
// once, and can be reused for multiple requests.
4242
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
4343

44-
String name =
44+
String qualifiedModuleName =
4545
String.format(
4646
"projects/%s/locations/global/eventThreatDetectionCustomModules/%s",
4747
projectId, customModuleId);
4848

4949
DeleteEventThreatDetectionCustomModuleRequest request =
50-
DeleteEventThreatDetectionCustomModuleRequest.newBuilder().setName(name).build();
50+
DeleteEventThreatDetectionCustomModuleRequest.newBuilder()
51+
.setName(qualifiedModuleName)
52+
.build();
5153

5254
client.deleteEventThreatDetectionCustomModule(request);
5355

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2025 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 management.api;
18+
19+
// [START securitycenter_get_effective_event_threat_detection_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.EffectiveEventThreatDetectionCustomModule;
21+
import com.google.cloud.securitycentermanagement.v1.GetEffectiveEventThreatDetectionCustomModuleRequest;
22+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
23+
import java.io.IOException;
24+
25+
public class GetEffectiveEventThreatDetectionCustomModule {
26+
27+
public static void main(String[] args) throws IOException {
28+
// TODO: Developer should replace project_id with a real project ID before running this code
29+
String projectId = "project_id";
30+
31+
String customModuleId = "custom_module_id";
32+
33+
getEffectiveEventThreatDetectionCustomModule(projectId, customModuleId);
34+
}
35+
36+
public static EffectiveEventThreatDetectionCustomModule
37+
getEffectiveEventThreatDetectionCustomModule(String projectId, String customModuleId)
38+
throws IOException {
39+
40+
// Initialize client that will be used to send requests. This client only needs
41+
// to be created
42+
// once, and can be reused for multiple requests.
43+
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
44+
45+
String qualifiedModuleName =
46+
String.format(
47+
"projects/%s/locations/global/effectiveEventThreatDetectionCustomModules/%s",
48+
projectId, customModuleId);
49+
50+
GetEffectiveEventThreatDetectionCustomModuleRequest request =
51+
GetEffectiveEventThreatDetectionCustomModuleRequest.newBuilder()
52+
.setName(qualifiedModuleName)
53+
.build();
54+
55+
EffectiveEventThreatDetectionCustomModule response =
56+
client.getEffectiveEventThreatDetectionCustomModule(request);
57+
58+
return response;
59+
}
60+
}
61+
}
62+
// [END securitycenter_get_effective_event_threat_detection_custom_module]

security-command-center/snippets/src/main/java/management/api/GetEventThreatDetectionCustomModule.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ public static EventThreatDetectionCustomModule getEventThreatDetectionCustomModu
4242
// once, and can be reused for multiple requests.
4343
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
4444

45-
String name =
45+
String qualifiedModuleName =
4646
String.format(
4747
"projects/%s/locations/global/eventThreatDetectionCustomModules/%s",
4848
projectId, customModuleId);
4949

5050
GetEventThreatDetectionCustomModuleRequest request =
51-
GetEventThreatDetectionCustomModuleRequest.newBuilder().setName(name).build();
51+
GetEventThreatDetectionCustomModuleRequest.newBuilder()
52+
.setName(qualifiedModuleName)
53+
.build();
5254

5355
EventThreatDetectionCustomModule response =
5456
client.getEventThreatDetectionCustomModule(request);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2025 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 management.api;
18+
19+
// [START securitycenter_list_descendant_event_threat_detection_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.ListDescendantEventThreatDetectionCustomModulesRequest;
21+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListDescendantEventThreatDetectionCustomModulesPagedResponse;
23+
import java.io.IOException;
24+
25+
public class ListDescendantEventThreatDetectionCustomModules {
26+
27+
public static void main(String[] args) throws IOException {
28+
// TODO: Developer should replace project_id with a real project ID before running this code
29+
String projectId = "project_id";
30+
31+
listDescendantEventThreatDetectionCustomModules(projectId);
32+
}
33+
34+
public static ListDescendantEventThreatDetectionCustomModulesPagedResponse
35+
listDescendantEventThreatDetectionCustomModules(String projectId) throws IOException {
36+
37+
// Initialize client that will be used to send requests. This client only needs
38+
// to be created
39+
// once, and can be reused for multiple requests.
40+
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
41+
42+
String parent = String.format("projects/%s/locations/global", projectId);
43+
44+
ListDescendantEventThreatDetectionCustomModulesRequest request =
45+
ListDescendantEventThreatDetectionCustomModulesRequest.newBuilder()
46+
.setParent(parent)
47+
.build();
48+
49+
ListDescendantEventThreatDetectionCustomModulesPagedResponse response =
50+
client.listDescendantEventThreatDetectionCustomModules(request);
51+
52+
return response;
53+
}
54+
}
55+
}
56+
// [END securitycenter_list_descendant_event_threat_detection_custom_module]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2025 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 management.api;
18+
19+
// [START securitycenter_list_effective_event_threat_detection_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.ListEffectiveEventThreatDetectionCustomModulesRequest;
21+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListEffectiveEventThreatDetectionCustomModulesPagedResponse;
23+
import java.io.IOException;
24+
25+
public class ListEffectiveEventThreatDetectionCustomModules {
26+
27+
public static void main(String[] args) throws IOException {
28+
// TODO: Developer should replace project_id with a real project ID before running this code
29+
String projectId = "project_id";
30+
31+
listEffectiveEventThreatDetectionCustomModules(projectId);
32+
}
33+
34+
public static ListEffectiveEventThreatDetectionCustomModulesPagedResponse
35+
listEffectiveEventThreatDetectionCustomModules(String projectId) throws IOException {
36+
37+
// Initialize client that will be used to send requests. This client only needs
38+
// to be created
39+
// once, and can be reused for multiple requests.
40+
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
41+
42+
String parent = String.format("projects/%s/locations/global", projectId);
43+
44+
ListEffectiveEventThreatDetectionCustomModulesRequest request =
45+
ListEffectiveEventThreatDetectionCustomModulesRequest.newBuilder()
46+
.setParent(parent)
47+
.build();
48+
49+
ListEffectiveEventThreatDetectionCustomModulesPagedResponse response =
50+
client.listEffectiveEventThreatDetectionCustomModules(request);
51+
52+
return response;
53+
}
54+
}
55+
}
56+
// [END securitycenter_list_effective_event_threat_detection_custom_module]

security-command-center/snippets/src/main/java/management/api/ListEventThreatDetectionCustomModules.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public static void main(String[] args) throws IOException {
4040
// once, and can be reused for multiple requests.
4141
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
4242

43+
String parent = String.format("projects/%s/locations/global", projectId);
44+
4345
ListEventThreatDetectionCustomModulesRequest request =
44-
ListEventThreatDetectionCustomModulesRequest.newBuilder()
45-
.setParent(String.format("projects/%s/locations/global", projectId))
46-
.build();
46+
ListEventThreatDetectionCustomModulesRequest.newBuilder().setParent(parent).build();
4747

4848
ListEventThreatDetectionCustomModulesPagedResponse response =
4949
client.listEventThreatDetectionCustomModules(request);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2025 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 management.api;
18+
19+
// [START securitycenter_update_event_threat_detection_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.EventThreatDetectionCustomModule;
21+
import com.google.cloud.securitycentermanagement.v1.EventThreatDetectionCustomModule.EnablementState;
22+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
23+
import com.google.cloud.securitycentermanagement.v1.UpdateEventThreatDetectionCustomModuleRequest;
24+
import com.google.protobuf.FieldMask;
25+
import java.io.IOException;
26+
27+
public class UpdateEventThreatDetectionCustomModule {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO: Developer should replace project_id with a real project ID before running this code
31+
String projectId = "project_id";
32+
33+
String customModuleId = "custom_module_id";
34+
35+
updateEventThreatDetectionCustomModule(projectId, customModuleId);
36+
}
37+
38+
public static EventThreatDetectionCustomModule updateEventThreatDetectionCustomModule(
39+
String projectId, String customModuleId) throws IOException {
40+
41+
// Initialize client that will be used to send requests. This client only needs
42+
// to be created
43+
// once, and can be reused for multiple requests.
44+
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
45+
46+
String qualifiedModuleName =
47+
String.format(
48+
"projects/%s/locations/global/eventThreatDetectionCustomModules/%s",
49+
projectId, customModuleId);
50+
51+
// Define the event threat detection custom module configuration, update the
52+
// DisplayName and EnablementState accordingly.
53+
EventThreatDetectionCustomModule eventThreatDetectionCustomModule =
54+
EventThreatDetectionCustomModule.newBuilder()
55+
.setName(qualifiedModuleName)
56+
.setDisplayName("updated_custom_module_name")
57+
.setEnablementState(EnablementState.DISABLED)
58+
.build();
59+
60+
// Set the field mask to specify which properties should be updated. In the below example we
61+
// are updating displayName and EnablementState
62+
// https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.eventThreatDetectionCustomModules/patch#query-parameters
63+
// https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask
64+
FieldMask fieldMask =
65+
FieldMask.newBuilder().addPaths("display_name").addPaths("enablement_state").build();
66+
67+
UpdateEventThreatDetectionCustomModuleRequest request =
68+
UpdateEventThreatDetectionCustomModuleRequest.newBuilder()
69+
.setEventThreatDetectionCustomModule(eventThreatDetectionCustomModule)
70+
.setUpdateMask(fieldMask)
71+
.build();
72+
73+
EventThreatDetectionCustomModule response =
74+
client.updateEventThreatDetectionCustomModule(request);
75+
76+
return response;
77+
}
78+
}
79+
}
80+
// [END securitycenter_update_event_threat_detection_custom_module]

0 commit comments

Comments
 (0)