|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Create security health analytics custom module |
| 18 | + */ |
| 19 | +function main(organizationId, customModuleDisplayName, locationId = 'global') { |
| 20 | + // [START securitycenter_create_security_health_analytics_custom_module] |
| 21 | + // npm install '@google-cloud/securitycentermanagement' |
| 22 | + const { |
| 23 | + SecurityCenterManagementClient, |
| 24 | + protos, |
| 25 | + } = require('@google-cloud/securitycentermanagement'); |
| 26 | + |
| 27 | + const client = new SecurityCenterManagementClient(); |
| 28 | + |
| 29 | + const EnablementState = |
| 30 | + protos.google.cloud.securitycentermanagement.v1 |
| 31 | + .SecurityHealthAnalyticsCustomModule.EnablementState; |
| 32 | + |
| 33 | + const Severity = |
| 34 | + protos.google.cloud.securitycentermanagement.v1.CustomConfig.Severity; |
| 35 | + |
| 36 | + /* |
| 37 | + * Required. The name of the parent resource of security health analytics module |
| 38 | + * Its format is |
| 39 | + * `organizations/[organization_id]/locations/[location_id]` |
| 40 | + * `folders/[folder_id]/locations/[location_id]` |
| 41 | + * `projects/[project_id]/locations/[location_id]` |
| 42 | + */ |
| 43 | + const parent = `organizations/${organizationId}/locations/${locationId}`; |
| 44 | + |
| 45 | + /* |
| 46 | + * Required. Resource name of security health analytics module. |
| 47 | + * Its format is |
| 48 | + * `organizations/[organization_id]/locations/[location_id]/securityHealthAnalyticsCustomModules/[custom_module]` |
| 49 | + * `folders/[folder_id]/locations/[location_id]/securityHealthAnalyticsCustomModules/[custom_module]` |
| 50 | + * `projects/[project_id]/locations/[location_id]/securityHealthAnalyticsCustomModules/[custom_module]` |
| 51 | + */ |
| 52 | + const name = `organizations/${organizationId}/locations/${locationId}/securityHealthAnalyticsCustomModules/custom_module`; |
| 53 | + |
| 54 | + // define the CEL expression here and this will scans for keys that have not been rotated in |
| 55 | + // the last 30 days, change it according to your requirements |
| 56 | + const expr = { |
| 57 | + expression: `has(resource.rotationPeriod) && (resource.rotationPeriod > duration('2592000s'))`, |
| 58 | + }; |
| 59 | + |
| 60 | + // define the resource selector |
| 61 | + const resourceSelector = { |
| 62 | + resourceTypes: ['cloudkms.googleapis.com/CryptoKey'], |
| 63 | + }; |
| 64 | + |
| 65 | + // define the custom module configuration, update the severity, description, |
| 66 | + // recommendation below |
| 67 | + const customConfig = { |
| 68 | + predicate: expr, |
| 69 | + resourceSelector: resourceSelector, |
| 70 | + severity: Severity.MEDIUM, |
| 71 | + description: 'add your description here', |
| 72 | + recommendation: 'add your recommendation here', |
| 73 | + }; |
| 74 | + |
| 75 | + // define the security health analytics custom module configuration, update the |
| 76 | + // EnablementState below |
| 77 | + const securityHealthAnalyticsCustomModule = { |
| 78 | + name: name, |
| 79 | + displayName: customModuleDisplayName, |
| 80 | + enablementState: EnablementState.ENABLED, |
| 81 | + customConfig: customConfig, |
| 82 | + }; |
| 83 | + |
| 84 | + async function createSecurityHealthAnalyticsCustomModule() { |
| 85 | + const [response] = await client.createSecurityHealthAnalyticsCustomModule({ |
| 86 | + parent: parent, |
| 87 | + securityHealthAnalyticsCustomModule: securityHealthAnalyticsCustomModule, |
| 88 | + }); |
| 89 | + console.log( |
| 90 | + 'Security Health Analytics Custom Module creation succeeded: ', |
| 91 | + response |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + createSecurityHealthAnalyticsCustomModule(); |
| 96 | + // [END securitycenter_create_security_health_analytics_custom_module] |
| 97 | +} |
| 98 | + |
| 99 | +main(...process.argv.slice(2)); |
0 commit comments