From 98ea0a6c202e5d837ac1c2712d8dc92f8e60759e Mon Sep 17 00:00:00 2001 From: Gaurav Aggarwal Date: Mon, 6 Jan 2025 10:40:36 +0000 Subject: [PATCH] fix lint issue --- ...eateSecurityHealthAnalyticsCustomModule.js | 34 ++- ...tiveSecurityHealthAnalyticsCustomModule.js | 48 +-- .../getSecurityHealthAnalyticsCustomModule.js | 13 +- ...dateSecurityHealthAnalyticsCustomModule.js | 26 +- ...ecurityHealthAnalyticsCustomModule.test.js | 278 ++++++++++-------- 5 files changed, 222 insertions(+), 177 deletions(-) diff --git a/security-center/snippets/management_api/createSecurityHealthAnalyticsCustomModule.js b/security-center/snippets/management_api/createSecurityHealthAnalyticsCustomModule.js index 9a3bfda162..8a2b10a812 100644 --- a/security-center/snippets/management_api/createSecurityHealthAnalyticsCustomModule.js +++ b/security-center/snippets/management_api/createSecurityHealthAnalyticsCustomModule.js @@ -19,15 +19,22 @@ function main(organizationId, customModuleDisplayName, locationId = 'global') { // [START securitycenter_create_security_health_analytics_custom_module] // npm install '@google-cloud/securitycentermanagement' - const { SecurityCenterManagementClient, protos } = require('@google-cloud/securitycentermanagement'); + const { + SecurityCenterManagementClient, + protos, + } = require('@google-cloud/securitycentermanagement'); const client = new SecurityCenterManagementClient(); - const EnablementState = protos.google.cloud.securitycentermanagement.v1.SecurityHealthAnalyticsCustomModule.EnablementState; - const Severity = protos.google.cloud.securitycentermanagement.v1.CustomConfig.Severity; + const EnablementState = + protos.google.cloud.securitycentermanagement.v1 + .SecurityHealthAnalyticsCustomModule.EnablementState; + + const Severity = + protos.google.cloud.securitycentermanagement.v1.CustomConfig.Severity; /* - * Required. The name of the parent resource of security health analytics module. + * Required. The name of the parent resource of security health analytics module * Its format is * `organizations/[organization_id]/locations/[location_id]` * `folders/[folder_id]/locations/[location_id]` @@ -47,14 +54,12 @@ function main(organizationId, customModuleDisplayName, locationId = 'global') { // define the CEL expression here and this will scans for keys that have not been rotated in // the last 30 days, change it according to the your requirements const expr = { - expression: `has(resource.rotationPeriod) && (resource.rotationPeriod > duration('2592000s'))` + expression: `has(resource.rotationPeriod) && (resource.rotationPeriod > duration('2592000s'))`, }; // define the resource selector const resourceSelector = { - resourceTypes: [ - 'cloudkms.googleapis.com/CryptoKey' - ] + resourceTypes: ['cloudkms.googleapis.com/CryptoKey'], }; // define the custom module configuration, update the severity, description, @@ -64,7 +69,7 @@ function main(organizationId, customModuleDisplayName, locationId = 'global') { resourceSelector: resourceSelector, severity: Severity.MEDIUM, description: 'add your description here', - recommendation: 'add your recommendation here' + recommendation: 'add your recommendation here', }; // define the security health analytics custom module configuration, update the @@ -73,19 +78,22 @@ function main(organizationId, customModuleDisplayName, locationId = 'global') { name: name, displayName: customModuleDisplayName, enablementState: EnablementState.ENABLED, - customConfig: customConfig + customConfig: customConfig, }; async function createSecurityHealthAnalyticsCustomModule() { const [response] = await client.createSecurityHealthAnalyticsCustomModule({ parent: parent, - securityHealthAnalyticsCustomModule: securityHealthAnalyticsCustomModule + securityHealthAnalyticsCustomModule: securityHealthAnalyticsCustomModule, }); - console.log('Security Health Analytics Custom Module creation succeeded: ', response); + console.log( + 'Security Health Analytics Custom Module creation succeeded: ', + response + ); } createSecurityHealthAnalyticsCustomModule(); // [END securitycenter_create_security_health_analytics_custom_module] } -main(...process.argv.slice(2)); \ No newline at end of file +main(...process.argv.slice(2)); diff --git a/security-center/snippets/management_api/getEffectiveSecurityHealthAnalyticsCustomModule.js b/security-center/snippets/management_api/getEffectiveSecurityHealthAnalyticsCustomModule.js index af33b5af16..ebde44b88b 100644 --- a/security-center/snippets/management_api/getEffectiveSecurityHealthAnalyticsCustomModule.js +++ b/security-center/snippets/management_api/getEffectiveSecurityHealthAnalyticsCustomModule.js @@ -17,30 +17,36 @@ * Retrieve an existing effective security health analytics custom module */ function main(organizationId, customModuleId, locationId = 'global') { - // [START securitycenter_get_effective_security_health_analytics_custom_module] - // npm install '@google-cloud/securitycentermanagement' - const { SecurityCenterManagementClient } = require('@google-cloud/securitycentermanagement'); + // [START securitycenter_get_effective_security_health_analytics_custom_module] + // npm install '@google-cloud/securitycentermanagement' + const { + SecurityCenterManagementClient, + } = require('@google-cloud/securitycentermanagement'); - const client = new SecurityCenterManagementClient(); + const client = new SecurityCenterManagementClient(); - /* - * Required. Resource name of security health analytics module. - * Its format is - * `organizations/[organization_id]/locations/[location_id]/effectiveSecurityHealthAnalyticsCustomModules/[custom_module]` - * `folders/[folder_id]/locations/[location_id]/effectiveSecurityHealthAnalyticsCustomModules/[custom_module]` - * `projects/[project_id]/locations/[location_id]/effectiveSecurityHealthAnalyticsCustomModules/[custom_module]` - */ - const name = `organizations/${organizationId}/locations/${locationId}/effectiveSecurityHealthAnalyticsCustomModules/${customModuleId}`; + /* + * Required. Resource name of security health analytics module. + * Its format is + * `organizations/[organization_id]/locations/[location_id]/effectiveSecurityHealthAnalyticsCustomModules/[custom_module]` + * `folders/[folder_id]/locations/[location_id]/effectiveSecurityHealthAnalyticsCustomModules/[custom_module]` + * `projects/[project_id]/locations/[location_id]/effectiveSecurityHealthAnalyticsCustomModules/[custom_module]` + */ + const name = `organizations/${organizationId}/locations/${locationId}/effectiveSecurityHealthAnalyticsCustomModules/${customModuleId}`; - async function getEffectiveSecurityHealthAnalyticsCustomModule() { - const [response] = await client.getEffectiveSecurityHealthAnalyticsCustomModule({ - name: name - }); - console.log('Security Health Analytics Custom Module get effective succeeded: ', response); - } + async function getEffectiveSecurityHealthAnalyticsCustomModule() { + const [response] = + await client.getEffectiveSecurityHealthAnalyticsCustomModule({ + name: name, + }); + console.log( + 'Security Health Analytics Custom Module get effective succeeded: ', + response + ); + } - getEffectiveSecurityHealthAnalyticsCustomModule(); - // [END securitycenter_get_effective_security_health_analytics_custom_module] + getEffectiveSecurityHealthAnalyticsCustomModule(); + // [END securitycenter_get_effective_security_health_analytics_custom_module] } -main(...process.argv.slice(2)); \ No newline at end of file +main(...process.argv.slice(2)); diff --git a/security-center/snippets/management_api/getSecurityHealthAnalyticsCustomModule.js b/security-center/snippets/management_api/getSecurityHealthAnalyticsCustomModule.js index a85a517233..e0e6e6dcc5 100644 --- a/security-center/snippets/management_api/getSecurityHealthAnalyticsCustomModule.js +++ b/security-center/snippets/management_api/getSecurityHealthAnalyticsCustomModule.js @@ -19,7 +19,9 @@ function main(organizationId, customModuleId, locationId = 'global') { // [START securitycenter_get_security_health_analytics_custom_module] // npm install '@google-cloud/securitycentermanagement' - const { SecurityCenterManagementClient } = require('@google-cloud/securitycentermanagement'); + const { + SecurityCenterManagementClient, + } = require('@google-cloud/securitycentermanagement'); const client = new SecurityCenterManagementClient(); @@ -34,13 +36,16 @@ function main(organizationId, customModuleId, locationId = 'global') { async function getSecurityHealthAnalyticsCustomModule() { const [response] = await client.getSecurityHealthAnalyticsCustomModule({ - name: name + name: name, }); - console.log('Security Health Analytics Custom Module get succeeded: ', response); + console.log( + 'Security Health Analytics Custom Module get succeeded: ', + response + ); } getSecurityHealthAnalyticsCustomModule(); // [END securitycenter_get_security_health_analytics_custom_module] } -main(...process.argv.slice(2)); \ No newline at end of file +main(...process.argv.slice(2)); diff --git a/security-center/snippets/management_api/updateSecurityHealthAnalyticsCustomModule.js b/security-center/snippets/management_api/updateSecurityHealthAnalyticsCustomModule.js index d0ce237b95..07d9d8541c 100644 --- a/security-center/snippets/management_api/updateSecurityHealthAnalyticsCustomModule.js +++ b/security-center/snippets/management_api/updateSecurityHealthAnalyticsCustomModule.js @@ -19,11 +19,16 @@ function main(organizationId, customModuleId, locationId = 'global') { // [START securitycenter_update_security_health_analytics_custom_module] // npm install '@google-cloud/securitycentermanagement' - const { SecurityCenterManagementClient, protos } = require('@google-cloud/securitycentermanagement'); + const { + SecurityCenterManagementClient, + protos, + } = require('@google-cloud/securitycentermanagement'); const client = new SecurityCenterManagementClient(); - const EnablementState = protos.google.cloud.securitycentermanagement.v1.SecurityHealthAnalyticsCustomModule.EnablementState; + const EnablementState = + protos.google.cloud.securitycentermanagement.v1 + .SecurityHealthAnalyticsCustomModule.EnablementState; /* * Required. Resource name of security health analytics module. @@ -38,26 +43,27 @@ function main(organizationId, customModuleId, locationId = 'global') { // EnablementState below const securityHealthAnalyticsCustomModule = { name: name, - enablementState: EnablementState.DISABLED + enablementState: EnablementState.DISABLED, }; // Set the field mask to specify which properties should be updated. const fieldMask = { - paths: [ - 'enablement_state' - ] - } + paths: ['enablement_state'], + }; async function updateSecurityHealthAnalyticsCustomModule() { const [response] = await client.updateSecurityHealthAnalyticsCustomModule({ updateMask: fieldMask, - securityHealthAnalyticsCustomModule: securityHealthAnalyticsCustomModule + securityHealthAnalyticsCustomModule: securityHealthAnalyticsCustomModule, }); - console.log('Security Health Analytics Custom Module update succeeded: ', response); + console.log( + 'Security Health Analytics Custom Module update succeeded: ', + response + ); } updateSecurityHealthAnalyticsCustomModule(); // [END securitycenter_update_security_health_analytics_custom_module] } -main(...process.argv.slice(2)); \ No newline at end of file +main(...process.argv.slice(2)); diff --git a/security-center/snippets/system-test/managemnt_api/securityHealthAnalyticsCustomModule.test.js b/security-center/snippets/system-test/managemnt_api/securityHealthAnalyticsCustomModule.test.js index cc6733c7cb..8b5bc73ddd 100644 --- a/security-center/snippets/system-test/managemnt_api/securityHealthAnalyticsCustomModule.test.js +++ b/security-center/snippets/system-test/managemnt_api/securityHealthAnalyticsCustomModule.test.js @@ -14,142 +14,162 @@ 'use strict'; -const { SecurityCenterManagementClient, protos } = require('@google-cloud/securitycentermanagement'); +const { + SecurityCenterManagementClient, + protos, +} = require('@google-cloud/securitycentermanagement'); const uuidv1 = require('uuid').v1; -const { assert } = require('chai'); -const { describe, it, before, after } = require('mocha'); -const { execSync } = require('child_process'); -const exec = cmd => execSync(cmd, { encoding: 'utf8' }); +const {assert} = require('chai'); +const {describe, it, before, after} = require('mocha'); +const {execSync} = require('child_process'); +const exec = cmd => execSync(cmd, {encoding: 'utf8'}); // TODO(developers): update for your own environment const organizationId = '1081635000895'; const locationId = 'global'; -const customModuleDisplayName = 'security_health_analytics_test' + uuidv1().replace(/-/g, '_'); +const customModuleDisplayName = + 'security_health_analytics_test' + uuidv1().replace(/-/g, '_'); describe('security health analytics custom module', async () => { - - let data; - - before(async () => { - - const client = new SecurityCenterManagementClient(); - const EnablementState = protos.google.cloud.securitycentermanagement.v1.SecurityHealthAnalyticsCustomModule.EnablementState; - const Severity = protos.google.cloud.securitycentermanagement.v1.CustomConfig.Severity; - - const parent = `organizations/${organizationId}/locations/${locationId}`; - const name = `organizations/${organizationId}/locations/${locationId}/securityHealthAnalyticsCustomModules/custom_module`; - const expr = { - expression: `has(resource.rotationPeriod) && (resource.rotationPeriod > duration('2592000s'))` - }; - const resourceSelector = { - resourceTypes: [ - 'cloudkms.googleapis.com/CryptoKey' - ] - }; - const customConfig = { - predicate: expr, - resourceSelector: resourceSelector, - severity: Severity.MEDIUM, - description: 'add your description here', - recommendation: 'add your recommendation here' - }; - const securityHealthAnalyticsCustomModule = { - name: name, - displayName: customModuleDisplayName, - enablementState: EnablementState.ENABLED, - customConfig: customConfig - }; - - try { - const [createResponse] = await client.createSecurityHealthAnalyticsCustomModule({ - parent: parent, - securityHealthAnalyticsCustomModule: securityHealthAnalyticsCustomModule - }); - // extracts the custom module ID from the full name - const customModuleId = createResponse.name.split('/').pop(); - data = { - orgId: organizationId, - customModuleId: customModuleId, - customModuleName: createResponse.displayName, - }; - console.log('SecurityHealthAnalyticsCustomModule created : %j', createResponse); - } catch (error) { - console.error('Error creating SecurityHealthAnalyticsCustomModule:', error); - } - - }); - - after(async () => { - - const client = new SecurityCenterManagementClient(); - - // List security health analytics custom modules - const [listResponse] = await client.listSecurityHealthAnalyticsCustomModules({ - parent: `organizations/${organizationId}/locations/${locationId}` + let data; + + before(async () => { + const client = new SecurityCenterManagementClient(); + const EnablementState = + protos.google.cloud.securitycentermanagement.v1 + .SecurityHealthAnalyticsCustomModule.EnablementState; + const Severity = + protos.google.cloud.securitycentermanagement.v1.CustomConfig.Severity; + const parent = `organizations/${organizationId}/locations/${locationId}`; + const name = `organizations/${organizationId}/locations/${locationId}/securityHealthAnalyticsCustomModules/custom_module`; + const expr = { + expression: `has(resource.rotationPeriod) && (resource.rotationPeriod > duration('2592000s'))`, + }; + const resourceSelector = { + resourceTypes: ['cloudkms.googleapis.com/CryptoKey'], + }; + const customConfig = { + predicate: expr, + resourceSelector: resourceSelector, + severity: Severity.MEDIUM, + description: 'add your description here', + recommendation: 'add your recommendation here', + }; + const securityHealthAnalyticsCustomModule = { + name: name, + displayName: customModuleDisplayName, + enablementState: EnablementState.ENABLED, + customConfig: customConfig, + }; + + try { + const [createResponse] = + await client.createSecurityHealthAnalyticsCustomModule({ + parent: parent, + securityHealthAnalyticsCustomModule: + securityHealthAnalyticsCustomModule, }); - - for (const module of listResponse) { - try { - if (module.displayName === customModuleDisplayName) { - const customModuleId = module.name.split('/').pop(); - - // Proceed with deletion if module exist - if (customModuleId) { - await client.deleteSecurityHealthAnalyticsCustomModule({ - name: `organizations/${organizationId}/locations/${locationId}/securityHealthAnalyticsCustomModules/${customModuleId}` - }); - console.log(`Custome Module ${customModuleDisplayName} deleted successfully.`); - } - } - } catch (error) { - console.error('Error deleting SecurityHealthAnalyticsCustomModule:', error); - } + // extracts the custom module ID from the full name + const customModuleId = createResponse.name.split('/').pop(); + data = { + orgId: organizationId, + customModuleId: customModuleId, + customModuleName: createResponse.displayName, + }; + console.log( + 'SecurityHealthAnalyticsCustomModule created : %j', + createResponse + ); + } catch (error) { + console.error( + 'Error creating SecurityHealthAnalyticsCustomModule:', + error + ); + } + }); + + after(async () => { + const client = new SecurityCenterManagementClient(); + + // List security health analytics custom modules + const [listResponse] = + await client.listSecurityHealthAnalyticsCustomModules({ + parent: `organizations/${organizationId}/locations/${locationId}`, + }); + + for (const module of listResponse) { + try { + if (module.displayName === customModuleDisplayName) { + const customModuleId = module.name.split('/').pop(); + // Proceed with deletion if module exist + if (customModuleId) { + await client.deleteSecurityHealthAnalyticsCustomModule({ + name: `organizations/${organizationId}/locations/${locationId}/securityHealthAnalyticsCustomModules/${customModuleId}`, + }); + console.log( + `Custome Module ${customModuleDisplayName} deleted successfully.` + ); + } } - - }); - - it('create security health analytics custom module', done => { - const output = exec( - `node management_api/createSecurityHealthAnalyticsCustomModule.js ${data.orgId} ${data.customModuleName} ${locationId}` + } catch (error) { + console.error( + 'Error deleting SecurityHealthAnalyticsCustomModule:', + error ); - assert.include(output, data.customModuleName); - assert.match(output, /Security Health Analytics Custom Module creation succeeded/); - assert.notMatch(output, /undefined/); - done(); - }); - - it('update security health analytics custom module', done => { - - const output = exec( - `node management_api/updateSecurityHealthAnalyticsCustomModule.js ${data.orgId} ${data.customModuleId} ${locationId}` - ); - // assert.include(output, data.customModuleName); - assert.include(output, 'DISABLED'); - assert.match(output, /Security Health Analytics Custom Module update succeeded/); - assert.notMatch(output, /undefined/); - done(); - }); - - it('get security health analytics custom module', done => { - - const output = exec( - `node management_api/getSecurityHealthAnalyticsCustomModule.js ${data.orgId} ${data.customModuleId} ${locationId}` - ); - assert.include(output, data.customModuleName); - assert.match(output, /Security Health Analytics Custom Module get succeeded/); - assert.notMatch(output, /undefined/); - done(); - }); - - it('get effective security health analytics custom module', done => { - - const output = exec( - `node management_api/getEffectiveSecurityHealthAnalyticsCustomModule.js ${data.orgId} ${data.customModuleId} ${locationId}` - ); - assert.include(output, data.customModuleName); - assert.match(output, /Security Health Analytics Custom Module get effective succeeded/); - assert.notMatch(output, /undefined/); - done(); - }); - -}); \ No newline at end of file + } + } + }); + + it('create security health analytics custom module', done => { + const output = exec( + `node management_api/createSecurityHealthAnalyticsCustomModule.js ${data.orgId} ${data.customModuleName} ${locationId}` + ); + assert.include(output, data.customModuleName); + assert.match( + output, + /Security Health Analytics Custom Module creation succeeded/ + ); + assert.notMatch(output, /undefined/); + done(); + }); + + it('update security health analytics custom module', done => { + const output = exec( + `node management_api/updateSecurityHealthAnalyticsCustomModule.js ${data.orgId} ${data.customModuleId} ${locationId}` + ); + // assert.include(output, data.customModuleName); + assert.include(output, 'DISABLED'); + assert.match( + output, + /Security Health Analytics Custom Module update succeeded/ + ); + assert.notMatch(output, /undefined/); + done(); + }); + + it('get security health analytics custom module', done => { + const output = exec( + `node management_api/getSecurityHealthAnalyticsCustomModule.js ${data.orgId} ${data.customModuleId} ${locationId}` + ); + assert.include(output, data.customModuleName); + assert.match( + output, + /Security Health Analytics Custom Module get succeeded/ + ); + assert.notMatch(output, /undefined/); + done(); + }); + + it('get effective security health analytics custom module', done => { + const output = exec( + `node management_api/getEffectiveSecurityHealthAnalyticsCustomModule.js ${data.orgId} ${data.customModuleId} ${locationId}` + ); + assert.include(output, data.customModuleName); + assert.match( + output, + /Security Health Analytics Custom Module get effective succeeded/ + ); + assert.notMatch(output, /undefined/); + done(); + }); +});