Skip to content

Commit 0d433bf

Browse files
vijaykanthmiennae
authored andcommitted
Add Resource v2 assets security marks
1 parent 06b3e8c commit 0d433bf

File tree

4 files changed

+234
-0
lines changed

4 files changed

+234
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2024 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+
// https://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+
15+
'use strict';
16+
17+
const {SecurityCenterClient} = require('@google-cloud/security-center');
18+
const {assert} = require('chai');
19+
const {describe, it, before} = require('mocha');
20+
const {execSync} = require('child_process');
21+
const exec = cmd => execSync(cmd, {encoding: 'utf8'});
22+
23+
// TODO(developers): update for your own environment
24+
const organizationId = '1081635000895';
25+
26+
describe('client with security marks for assets', async () => {
27+
let data;
28+
before(async () => {
29+
// Creates a new client.
30+
const client = new SecurityCenterClient();
31+
32+
const [assetResults] = await client.listAssets({
33+
parent: client.organizationPath(organizationId),
34+
});
35+
const randomAsset =
36+
assetResults[Math.floor(Math.random() * assetResults.length)].asset;
37+
console.log('random %j', randomAsset);
38+
data = {
39+
orgId: organizationId,
40+
assetName: randomAsset.name,
41+
};
42+
console.log('data %j', data);
43+
});
44+
it('client can add security marks to asset.', () => {
45+
const output = exec(`node v2/addSecurityMarks.js ${data.assetName}`);
46+
assert.include(output, data.assetName);
47+
assert.match(output, /key_a/);
48+
assert.match(output, /value_a/);
49+
assert.match(output, /key_b/);
50+
assert.match(output, /value_b/);
51+
assert.notMatch(output, /undefined/);
52+
});
53+
54+
it('client can add and delete security marks', () => {
55+
// Ensure marks are set.
56+
exec(`node v2/addSecurityMarks.js ${data.assetName}`);
57+
58+
const output = exec(`node v2/addDeleteSecurityMarks.js ${data.assetName}`);
59+
assert.match(output, /key_a/);
60+
assert.match(output, /new_value_a/);
61+
assert.notMatch(output, /key_b/);
62+
assert.notMatch(output, /undefined/);
63+
});
64+
65+
it('client can delete security marks', () => {
66+
// Ensure marks are set.
67+
exec(`node v2/addSecurityMarks.js ${data.assetName}`);
68+
69+
const output = exec(`node v2/deleteAssetsSecurityMarks.js ${data.assetName}`);
70+
assert.notMatch(output, /key_a/);
71+
assert.notMatch(output, /value_a/);
72+
assert.notMatch(output, /key_b/);
73+
assert.notMatch(output, /value_b/);
74+
assert.include(output, data.assetName);
75+
assert.include(output, data.assetName);
76+
assert.notMatch(output, /undefined/);
77+
});
78+
79+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2024 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+
* Demonstrates adding/updating at the same time as deleting security
18+
* marks from an asset.
19+
*/
20+
function main(assetName = 'full asset path to add marks to') {
21+
// [START securitycenter_add_delete_security_marks_v2]
22+
// Imports the Google Cloud client library.
23+
const {SecurityCenterClient} = require('@google-cloud/security-center').v2;
24+
25+
// Creates a new client.
26+
const client = new SecurityCenterClient();
27+
28+
async function addDeleteSecurityMarks() {
29+
// assetName is the full resource path for the asset to update.
30+
// Specify the value of 'assetName' in one of the following formats:
31+
// `organizations/${org-id}/assets/${asset-id}`;
32+
// `projects/${project-id}/assets/${asset-id}`;
33+
// `folders/${folder-id}/assets/${asset-id}`;
34+
const [newMarks] = await client.updateSecurityMarks({
35+
securityMarks: {
36+
name: `${assetName}/securityMarks`,
37+
marks: {key_a: 'new_value_a'},
38+
},
39+
// Only update the enableAssetDiscovery field.
40+
updateMask: {paths: ['marks.key_a', 'marks.key_b']},
41+
});
42+
43+
console.log('New marks: %j', newMarks);
44+
}
45+
addDeleteSecurityMarks();
46+
// [END securitycenter_add_delete_security_marks_v2]
47+
}
48+
49+
main(...process.argv.slice(2));
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2024 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+
15+
'use strict';
16+
17+
/**
18+
* Demostrates adding security marks to an asset.
19+
*/
20+
function main(assetName = 'full asset path to add marks to') {
21+
// [START securitycenter_add_security_marks_v2]
22+
// Imports the Google Cloud client library.
23+
const {SecurityCenterClient} = require('@google-cloud/security-center').v2;
24+
25+
// Creates a new client.
26+
const client = new SecurityCenterClient();
27+
28+
async function addSecurityMarks() {
29+
// assetName is the full resource path for the asset to update.
30+
/*
31+
* TODO(developer): Uncomment the following lines
32+
*/
33+
// Specify the value of 'assetName' in one of the following formats:
34+
// `organizations/${org-id}/assets/${asset-id}`;
35+
// `projects/${project-id}/assets/${asset-id}`;
36+
// `folders/${folder-id}/assets/${asset-id}`;
37+
// const assetName = "organizations/123123342/assets/12312321";
38+
const [newMarks] = await client.updateSecurityMarks({
39+
securityMarks: {
40+
name: `${assetName}/securityMarks`,
41+
marks: {key_a: 'value_a', key_b: 'value_b'},
42+
},
43+
// Only update the marks with these keys.
44+
updateMask: {paths: ['marks.key_a', 'marks.key_b']},
45+
});
46+
47+
console.log('New marks: %', newMarks);
48+
}
49+
addSecurityMarks();
50+
// [END securitycenter_add_security_marks_v2]
51+
}
52+
53+
main(...process.argv.slice(2));
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2024 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+
15+
'use strict';
16+
17+
/**
18+
* Demostrates deleting security marks on an asset.
19+
*/
20+
function main(assetName = 'full asset path to add marks to') {
21+
// [START securitycenter_delete_security_marks_v2]
22+
// Imports the Google Cloud client library.
23+
const {SecurityCenterClient} = require('@google-cloud/security-center').v2;
24+
25+
// Creates a new client.
26+
const client = new SecurityCenterClient();
27+
28+
async function deleteSecurityMarks() {
29+
// assetName is the full resource path for the asset to update.
30+
/*
31+
* TODO(developer): Uncomment the following lines
32+
*/
33+
// Specify the value of 'assetName' in one of the following formats:
34+
// `organizations/${org-id}/assets/${asset-id}`;
35+
// `projects/${project-id}/assets/${asset-id}`;
36+
// `folders/${folder-id}/assets/${asset-id}`;
37+
// const assetName = "organizations/123123342/assets/12312321";
38+
const [newMarks] = await client.updateSecurityMarks({
39+
securityMarks: {
40+
name: `${assetName}/securityMarks`,
41+
// Intentionally, not setting marks to delete them.
42+
},
43+
// Only delete marks for the following keys.
44+
updateMask: {paths: ['marks.key_a', 'marks.key_b']},
45+
});
46+
47+
console.log('Updated marks: %j', newMarks);
48+
}
49+
deleteSecurityMarks();
50+
// [END securitycenter_delete_security_marks_v2]
51+
}
52+
53+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)