|
| 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] |
0 commit comments