Skip to content

Commit c1643a1

Browse files
feat(security-center): Add Resource v2 API Mute Rule Samples (#3830)
* Add Resource v2 Mute Rule Samples * fix lint issues * Address Comments * remove unused variable * Use the project id from env variable --------- Co-authored-by: Adam Ross <[email protected]>
1 parent 8bf1e1b commit c1643a1

File tree

8 files changed

+464
-3
lines changed

8 files changed

+464
-3
lines changed

security-center/snippets/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"node": ">=16.0.0"
1010
},
1111
"scripts": {
12-
"test": "c8 mocha -p -j 2 --recursive --timeout 6000000 system-test/v2/findings.test.js"
12+
"test": "c8 mocha -p -j 2 --recursive --timeout 6000000 system-test/"
1313
},
1414
"license": "Apache-2.0",
1515
"dependencies": {
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
});

security-center/snippets/system-test/v2/notifications.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const {PubSub} = require('@google-cloud/pubsub');
2626
const exec = cmd => execSync(cmd, {encoding: 'utf8'});
2727

2828
// TODO(developers): update for your own environment
29-
const organizationId = '1081635000895';
30-
const projectId = 'long-door-651';
29+
const organizationId = process.env.GCLOUD_ORGANIZATION;
30+
const projectId = process.env.GOOGLE_SAMPLES_PROJECT;
3131
const location = 'global';
3232

3333
describe('Client with Notifications v2', async () => {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
'use strict';
17+
18+
/**
19+
* Creates a mute configuration in a project under a given location.
20+
*/
21+
function main(organizationId, location = 'global') {
22+
// [START securitycenter_create_mute_config_v2]
23+
// Imports the Google Cloud client library.
24+
const {SecurityCenterClient} = require('@google-cloud/security-center').v2;
25+
26+
// Create a Security Center client
27+
const client = new SecurityCenterClient();
28+
29+
/**
30+
* Required. Resource name of the new mute configs's parent. Its format is
31+
* "organizations/[organization_id]/locations/[location_id]",
32+
* "folders/[folder_id]/locations/[location_id]", or
33+
* "projects/[project_id]/locations/[location_id]".
34+
*/
35+
36+
/**
37+
* TODO(developer): Update the following references for your own environment before running the sample.
38+
*/
39+
// const organizationId = 'YOUR_ORGANIZATION_ID';
40+
// const location = 'LOCATION_ID';
41+
const parent = `organizations/${organizationId}/locations/${location}`;
42+
43+
/**
44+
* Required. Unique identifier provided by the client within the parent scope.
45+
* It must consist of only lowercase letters, numbers, and hyphens, must start
46+
* with a letter, must end with either a letter or a number, and must be 63
47+
* characters or less.
48+
*/
49+
const muteConfigId = 'muteid-' + Math.floor(Math.random() * 10000);
50+
51+
const name = `${parent}/muteConfigs/${muteConfigId}`;
52+
53+
// Build the muteRuleConfig object.
54+
const muteConfig = {
55+
name: name,
56+
description: "Mute low-medium IAM grants excluding 'compute' resources",
57+
filter:
58+
'severity="LOW" OR severity="MEDIUM" AND ' +
59+
'category="Persistence: IAM Anomalous Grant" AND ' +
60+
'-resource.type:"compute"',
61+
type: 'STATIC',
62+
};
63+
64+
// Build the create mute rule request.
65+
const createMuteRuleRequest = {
66+
parent,
67+
muteConfig,
68+
muteConfigId,
69+
};
70+
71+
async function createMuteRuleConfig() {
72+
// Call the API.
73+
const [muteConfig] = await client.createMuteConfig(createMuteRuleRequest);
74+
console.log('New mute rule config created: %j', muteConfig);
75+
}
76+
77+
createMuteRuleConfig();
78+
// [END securitycenter_create_mute_config_v2]
79+
}
80+
81+
main(...process.argv.slice(2));
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
'use strict';
17+
18+
/**
19+
* Deletes a mute configuration given its resource name.
20+
*/
21+
function main(organizationId, muteConfigId, location = 'global') {
22+
// [START securitycenter_delete_mute_config_v2]
23+
// Imports the Google Cloud client library.
24+
const {SecurityCenterClient} = require('@google-cloud/security-center').v2;
25+
26+
// Create a Security Center client
27+
const client = new SecurityCenterClient();
28+
29+
/**
30+
* Required. Name of the mute config to delete. The following list shows some
31+
* examples of the format:
32+
* `organizations/{organization}/muteConfigs/{config_id}`
33+
* `organizations/{organization}/locations/{location}/muteConfigs/{config_id}`
34+
* `folders/{folder}/muteConfigs/{config_id}`
35+
* `folders/{folder}/locations/{location}/muteConfigs/{config_id}`
36+
* `projects/{project}/muteConfigs/{config_id}`
37+
* `projects/{project}/locations/{location}/muteConfigs/{config_id}`
38+
*/
39+
40+
/**
41+
* TODO(developer): Update the following references for your own environment before running the sample.
42+
*/
43+
// const organizationId = 'YOUR_ORGANIZATION_ID';
44+
// const location = 'LOCATION_ID';
45+
// const muteConfigId = 'MUTE_CONFIG_ID';
46+
const name = `organizations/${organizationId}/locations/${location}/muteConfigs/${muteConfigId}`;
47+
48+
// Build the request.
49+
const deleteMuteRuleRequest = {
50+
name,
51+
};
52+
53+
async function deleteMuteConfig() {
54+
// Call the API.
55+
const [muteConfig] = await client.deleteMuteConfig(deleteMuteRuleRequest);
56+
console.log('Delete mute rule config: %j', muteConfig);
57+
}
58+
59+
deleteMuteConfig();
60+
// [END securitycenter_delete_mute_config_v2]
61+
}
62+
63+
main(...process.argv.slice(2));
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
'use strict';
17+
18+
/**
19+
* Retrieves a mute configuration given its resource name.
20+
*/
21+
function main(organizationId, muteConfigId) {
22+
// [START securitycenter_create_mute_config_v2]
23+
// Imports the Google Cloud client library.
24+
const {SecurityCenterClient} = require('@google-cloud/security-center').v2;
25+
26+
// Create a Security Center client
27+
const client = new SecurityCenterClient();
28+
29+
/**
30+
* Required. Name of the mute config to retrieve. The following list shows
31+
* some examples of the format:
32+
* `organizations/{organization}/muteConfigs/{config_id}`
33+
* `organizations/{organization}/locations/{location}/muteConfigs/{config_id}`
34+
* `folders/{folder}/muteConfigs/{config_id}`
35+
* `folders/{folder}/locations/{location}/muteConfigs/{config_id}`
36+
* `projects/{project}/muteConfigs/{config_id}`
37+
* `projects/{project}/locations/{location}/muteConfigs/{config_id}`
38+
*/
39+
40+
/**
41+
* TODO(developer): Update the following references for your own environment before running the sample.
42+
*/
43+
// const organizationId = 'YOUR_ORGANIZATION_ID';
44+
// const muteConfigId = 'MUTE_CONFIG_ID';
45+
46+
const name = `organizations/${organizationId}/muteConfigs/${muteConfigId}`;
47+
48+
// Build the request.
49+
const getMuteRuleRequest = {
50+
name,
51+
};
52+
53+
async function createMuteRuleConfig() {
54+
// Call the API.
55+
const [muteConfig] = await client.getMuteConfig(getMuteRuleRequest);
56+
console.log('Get mute rule config: %j', muteConfig);
57+
}
58+
59+
createMuteRuleConfig();
60+
// [END securitycenter_create_mute_config_v2]
61+
}
62+
63+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)