8000 feat(securitycenter): Add Resource SCC Management API Org ETD Custom … · invertase/java-docs-samples@230665f · GitHub
[go: up one dir, main page]

Skip to content

Commit 230665f

Browse files
authored
feat(securitycenter): Add Resource SCC Management API Org ETD Custom Module code samples (Create, Delete, List, Get) (GoogleCloudPlatform#9743)
* sample codes for event threat detection custom modules * addressed comments * addressed comments * addressed comments * addressed comments
1 parent 3270e36 commit 230665f

File tree

5 files changed

+438
-0
lines changed
< 8000 span class="prc-TooltipV2-Tooltip-cYMVY" data-direction="s" aria-hidden="true" id=":R3t5dab:">Filter options

5 files changed

+438
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 management.api;
18+
19+
// [START securitycenter_create_event_threat_detection_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.CreateEventThreatDetectionCustomModuleRequest;
21+
import com.google.cloud.securitycentermanagement.v1.EventThreatDetectionCustomModule;
22+
import com.google.cloud.securitycentermanagement.v1.EventThreatDetectionCustomModule.EnablementState;
23+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
24+
import com.google.protobuf.ListValue;
25+
import com.google.protobuf.Struct;
26+
import com.google.protobuf.Value;
27+
import java.io.IOException;
28+
import java.util.Arrays;
29+
import java.util.HashMap;
30+
import java.util.List;
31+
import java.util.Map;
32+
33+
public class CreateEventThreatDetectionCustomModule {
34+
35+
public static void main(String[] args) throws IOException {
36+
// https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.eventThreatDetectionCustomModules/create
37+
// TODO: Developer should replace project_id with a real project ID before running this code
38+
String projectId = "project_id";
39+
40+
String customModuleDisplayName = "custom_module_display_name";
41+
42+
createEventThreatDetectionCustomModule(projectId, customModuleDisplayName);
43+
}
44+
45+
public static EventThreatDetectionCustomModule createEventThreatDetectionCustomModule(
46+
String projectId, String customModuleDisplayName) throws IOException {
47+
48+
// Initialize client that will be used to send requests. This client only needs
49+
// to be created
50+
// once, and can be reused for multiple requests.
51+
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
52+
53+
// define the metadata and other config parameters severity, description,
54+
// recommendation and ips below
55+
Map<String, Value> metadata = new HashMap<>();
56+
metadata.put("severity", Value.newBuilder().setStringValue("MEDIUM").build());
57+
metadata.put(
58+
"description", Value.newBuilder().setStringValue("add your description here").build());
59+
metadata.put(
60+
"recommendation",
61+
Value.newBuilder().setStringValue("add your recommendation here").build());
62+
List<Value> ips = Arrays.asList(Value.newBuilder().setStringValue("0.0.0.0").build());
63+
64+
Value metadataVal =
65+
Value.newBuilder()
66+
.setStructValue(Struct.newBuilder().putAllFields(metadata).build())
67+
.build();
68+
Value ipsValue =
69+
Value.newBuilder().setListValue(ListValue.newBuilder().addAllValues(ips).build()).build();
70+
71+
Struct configStruct =
72+
Struct.newBuilder().putFields("metadata", metadataVal).putFields("ips", ipsValue).build();
73+
74+
// define the Event Threat Detection custom module configuration, update the EnablementState
75+
// below
76+
EventThreatDetectionCustomModule eventThreatDetectionCustomModule =
77+
EventThreatDetectionCustomModule.newBuilder()
78+
.setConfig(configStruct)
79+
.setDisplayName(customModuleDisplayName)
80+
.setEnablementState(EnablementState.ENABLED)
81+
.setType("CONFIGURABLE_BAD_IP")
82+
.build();
83+
84+
CreateEventThreatDetectionCustomModuleRequest request =
85+
CreateEventThreatDetectionCustomModuleRequest.newBuilder()
86+
.setParent(String.format("projects/%s/locations/global", projectId))
87+
.setEventThreatDetectionCustomModule(eventThreatDetectionCustomModule)
88+
.build();
89+
90+
EventThreatDetectionCustomModule response =
91+
client.createEventThreatDetectionCustomModule(request);
92+
93+
return response;
94+
}
95+
}
96+
}
97+
// [END securitycenter_create_event_threat_detection_custom_module]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 management.api;
18+
19+
// [START securitycenter_delete_event_threat_detection_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.DeleteEventThreatDetectionCustomModuleRequest;
21+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22+
import java.io.IOException;
23+
24+
public class DeleteEventThreatDetectionCustomModule {
25+
26+
public static void main(String[] args) throws IOException {
27+
// https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.eventThreatDetectionCustomModules/delete
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+
deleteEventThreatDetectionCustomModule(projectId, customModuleId);
34+
}
35+
36+
public static boolean deleteEventThreatDetectionCustomModule(
37+
String projectId, String customModuleId) throws IOException {
38+
39+
// Initialize client that will be used to send requests. This client only needs
40+
// to be created
41+
// once, and can be reused for multiple requests.
42+
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
43+
44+
String name =
45+
String.format(
46+
"projects/%s/locations/global/eventThreatDetectionCustomModules/%s",
47+
projectId, customModuleId);
48+
49+
DeleteEventThreatDetectionCustomModuleRequest request =
50+
DeleteEventThreatDetectionCustomModuleRequest.newBuilder().setName(name).build();
51+
52+
client.deleteEventThreatDetectionCustomModule(request);
53+
54+
return true;
55+< 10000 div class="diff-text-inner"> }
56+
}
57+
}
58+
// [END securitycenter_delete_event_threat_detection_custom_module]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 management.api;
18+
19+
// [START securitycenter_get_event_threat_detection_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.EventThreatDetectionCustomModule;
21+
import com.google.cloud.securitycentermanagement.v1.GetEventThreatDetectionCustomModuleRequest;
22+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
23+
import java.io.IOException;
24+
25+
public class GetEventThreatDetectionCustomModule {
26+
27+
public static void main(String[] args) throws IOException {
28+
// https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.eventThreatDetectionCustomModules/get
29+
// TODO: Developer should replace project_id with a real project ID before running this code
30+
String projectId = "project_id";
31+
32+
String customModuleId = "custom_module_id";
33+
34+
getEventThreatDetectionCustomModule(projectId, customModuleId);
35+
}
36+
37+
public static EventThreatDetectionCustomModule getEventThreatDetectionCustomModule(
38+
String projectId, String customModuleId) 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 name =
46+
String.format(
47+
"projects/%s/locations/global/eventThreatDetectionCustomModules/%s",
48+
projectId, customModuleId);
49+
50+
GetEventThreatDetectionCustomModuleRequest request =
51+
GetEventThreatDetectionCustomModuleRequest.newBuilder().setName(name).build();
52+
53+
EventThreatDetectionCustomModule response =
54+
client.getEventThreatDetectionCustomModule(request);
55+
56+
return response;
57+
}
58+
}
59+
}
60+
// [END securitycenter_get_event_threat_detection_custom_module]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 management.api;
18+
19+
// [START securitycenter_list_event_threat_detection_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.ListEventThreatDetectionCustomModulesRequest;
21+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListEventThreatDetectionCustomModulesPagedResponse;
23+
import java.io.IOException;
24+
25+
public class ListEventThreatDetectionCustomModules {
26+
27+
public static void main(String[] args) throws IOException {
28+
// https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.eventThreatDetectionCustomModules/list
29+
// TODO: Developer should replace project_id with a real project ID before running this code
30+
String projectId = "project_id";
31+
32+
listEventThreatDetectionCustomModules(projectId);
33+
}
34+
35+
public static ListEventThreatDetectionCustomModulesPagedResponse
36+
listEventThreatDetectionCustomModules(String projectId) throws IOException {
37+
38+
// Initialize client that will be used to send requests. This client only needs
39+
// to be created
40+
// once, and can be reused for multiple requests.
41+
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
42+
43+
ListEventThreatDetectionCustomModulesRequest request =
44+
ListEventThreatDetectionCustomModulesRequest.newBuilder()
45+
.setParent(String.format("projects/%s/locations/global", projectId))
46+
.build();
47+
48+
ListEventThreatDetectionCustomModulesPagedResponse response =
49+
client.listEventThreatDetectionCustomModules(request);
50+
51+
return response;
52+
}
53+
}
54+
}
55+
// [END securitycenter_list_event_threat_detection_custom_module]

0 commit comments

Comments
 (0)
0