|
| 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 | +const {SecurityCenterClient} = require('@google-cloud/security-center').v2; |
| 18 | +const {assert} = require('chai'); |
| 19 | +const {execSync} = require('child_process'); |
| 20 | +const exec = cmd => execSync(cmd, {encoding: 'utf8'}); |
| 21 | +const {describe, it, before} = require('mocha'); |
| 22 | + |
| 23 | +// TODO(developers): update for your own environment |
| 24 | +const organizationId = process.env.GCLOUD_ORGANIZATION; |
| 25 | +const location = 'global'; |
| 26 | + |
| 27 | +describe('Client with mute rule V2', async () => { |
| 28 | + let data; |
| 29 | + before(async () => { |
| 30 | + // Creates a new client. |
| 31 | + const client = new SecurityCenterClient(); |
| 32 | + |
| 33 | + // Build the create mute rule request. |
| 34 | + const muteId = 'muteid-' + Math.floor(Math.random() * 10000); |
| 35 | + const createMuteRuleRequest = { |
| 36 | + parent: `organizations/${organizationId}/locations/${location}`, |
| 37 | + muteConfigId: muteId, |
| 38 | + muteConfig: { |
| 39 | + name: `organizations/${organizationId}/locations/${location}/muteConfigs/${muteId}`, |
| 40 | + description: "Mute low-medium IAM grants excluding 'compute' resources", |
| 41 | + filter: |
| 42 | + 'severity="LOW" OR severity="MEDIUM" AND ' + |
| 43 | + 'category="Persistence: IAM Anomalous Grant" AND ' + |
| 44 | + '-resource.type:"compute"', |
| 45 | + type: 'STATIC', |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + const [muteConfigResponse] = await client |
| 50 | + .createMuteConfig(createMuteRuleRequest) |
| 51 | + .catch(error => console.error(error)); |
| 52 | + |
| 53 | + const muteConfigId = muteConfigResponse.name.split('/')[5]; |
| 54 | + |
| 55 | + data = { |
| 56 | + orgId: organizationId, |
| 57 | + muteConfigId: muteConfigId, |
| 58 | + muteConfigName: muteConfigResponse.name, |
| 59 | + untouchedMuteConfigName: '', |
| 60 | + }; |
| 61 | + console.log('My data muteConfig:: %j', data); |
| 62 | + }); |
| 63 | + |
| 64 | + it('client can create mute rule V2', done => { |
| 65 | + const output = exec(`node v2/createMuteRule.js ${data.orgId}`); |
| 66 | + assert(output.includes(data.orgId)); |
| 67 | + assert.match(output, /New mute rule config created/); |
| 68 | + assert.notMatch(output, /undefined/); |
| 69 | + done(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('client can list all mute rules V2', done => { |
| 73 | + const output = exec(`node v2/listAllMuteRules.js ${data.orgId}`); |
| 74 | + assert(output.includes(data.orgId)); |
| 75 | + assert(output.includes(data.untouchedMuteConfigName)); |
| 76 | + assert.notMatch(output, /undefined/); |
| 77 | + done(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('client can get a mute rule V2', done => { |
| 81 | + const output = exec( |
| 82 | + `node v2/getMuteRule.js ${data.orgId} ${data.muteConfigId}` |
| 83 | + ); |
| 84 | + assert(output.includes(data.muteConfigName)); |
| 85 | + assert.match(output, /Get mute rule config/); |
| 86 | + assert.notMatch(output, /undefined/); |
| 87 | + done(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('client can update a mute rule V2', done => { |
| 91 | + const output = exec( |
| 92 | + `node v2/updateMuteRule.js ${data.orgId} ${data.muteConfigId}` |
| 93 | + ); |
| 94 | + assert.match(output, /Update mute rule config/); |
| 95 | + assert.notMatch(output, /undefined/); |
| 96 | + done(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('client can delete a mute rule V2', done => { |
| 100 | + const output = exec( |
| 101 | + `node v2/deleteMuteRule.js ${data.orgId} ${data.muteConfigId}` |
| 102 | + ); |
| 103 | + assert.match(output, /Delete mute rule config/); |
| 104 | + assert.notMatch(output, /undefined/); |
| 105 | + done(); |
| 106 | + }); |
| 107 | +}); |
0 commit comments