-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(security-center): Add Resource v2 API Assets Security Marks Samples #3916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
129b04d
dbe226b
a948f68
5d8a033
91e531e
0201997
f29640b
a2eba15
9a85b6b
35e7fb7
2fac912
00edc7f
5094475
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,37 +36,51 @@ describe('Client with Notifications', async () => { | |
| before(async () => { | ||
| const client = new SecurityCenterClient(); | ||
| async function createNotificationConfig(configId) { | ||
| /*eslint no-unused-vars: ["error", { "varsIgnorePattern": "^_" }]*/ | ||
| const [_response] = await client.createNotificationConfig({ | ||
| parent: orgName, | ||
| configId: configId, | ||
| notificationConfig: { | ||
| description: 'Sample config for node.js', | ||
| pubsubTopic: pubsubTopic, | ||
| streamingConfig: {filter: 'state = "ACTIVE"'}, | ||
| }, | ||
| }); | ||
| try { | ||
| /*eslint no-unused-vars: ["error", { "varsIgnorePattern": "^_" }]*/ | ||
| const [_response] = await client.createNotificationConfig({ | ||
| parent: orgName, | ||
| configId: configId, | ||
| notificationConfig: { | ||
| description: 'Sample config for node.js', | ||
| pubsubTopic: pubsubTopic, | ||
| streamingConfig: {filter: 'state = "ACTIVE"'}, | ||
| }, | ||
| }); | ||
| } catch (err) { | ||
| console.error(`Error creating config ${configId}:`, err.message); | ||
| } | ||
| } | ||
|
|
||
| await createNotificationConfig(deleteConfig); | ||
| await new Promise(resolve => setTimeout(resolve, 1000)); // 1-second delay | ||
| await createNotificationConfig(getConfig); | ||
| await new Promise(resolve => setTimeout(resolve, 1000)); // 1-second delay | ||
| await createNotificationConfig(listConfig); | ||
| await new Promise(resolve => setTimeout(resolve, 1000)); // 1-second delay | ||
| await createNotificationConfig(updateConfig); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed |
||
| }); | ||
|
|
||
| after(async () => { | ||
| const client = new SecurityCenterClient(); | ||
| async function deleteNotificationConfig(configId) { | ||
| const name = client.organizationNotificationConfigPath( | ||
| organizationId, | ||
| configId | ||
| ); | ||
| await client.deleteNotificationConfig({name: name}); | ||
| try { | ||
| const name = client.organizationNotificationConfigPath( | ||
| organizationId, | ||
| configId | ||
| ); | ||
| await client.deleteNotificationConfig({name: name}); | ||
| } catch (err) { | ||
| console.error(`Error deleting config ${configId}:`, err.message); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed |
||
| } | ||
|
|
||
| await deleteNotificationConfig(createConfig); | ||
| await new Promise(resolve => setTimeout(resolve, 1000)); // 1-second delay | ||
| await deleteNotificationConfig(getConfig); | ||
| await new Promise(resolve => setTimeout(resolve, 1000)); // 1-second delay | ||
| await deleteNotificationConfig(listConfig); | ||
| await new Promise(resolve => setTimeout(resolve, 1000)); // 1-second delay | ||
| await deleteNotificationConfig(updateConfig); | ||
|
||
| }); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,82 @@ | ||||||||||||||||
| // Copyright 2024 Google LLC | ||||||||||||||||
| // | ||||||||||||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||
| // you may not use this file except in compliance with the License. | ||||||||||||||||
| // You may obtain a copy of the License at | ||||||||||||||||
| // | ||||||||||||||||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||
| // | ||||||||||||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||||||||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||
| // See the License for the specific language governing permissions and | ||||||||||||||||
| // limitations under the License. | ||||||||||||||||
|
|
||||||||||||||||
| 'use strict'; | ||||||||||||||||
|
|
||||||||||||||||
| const {SecurityCenterClient} = require('@google-cloud/security-center'); | ||||||||||||||||
| const {assert} = require('chai'); | ||||||||||||||||
| const {describe, it, before} = require('mocha'); | ||||||||||||||||
| const {execSync} = require('child_process'); | ||||||||||||||||
|
|
||||||||||||||||
| // TODO(developers): update for your own environment | ||||||||||||||||
| const organizationId = '1081635000895'; | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The organization ID is hardcoded. Consider storing this in an environment variable or a configuration file to make the test more portable and avoid exposing sensitive information directly in the code.
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| describe('client with security marks for assets', async () => { | ||||||||||||||||
| let data; | ||||||||||||||||
| before(async () => { | ||||||||||||||||
| // Creates a new client. | ||||||||||||||||
| const client = new SecurityCenterClient(); | ||||||||||||||||
|
|
||||||||||||||||
| const [assetResults] = await client.listAssets({ | ||||||||||||||||
| parent: client.organizationPath(organizationId), | ||||||||||||||||
| }); | ||||||||||||||||
| const randomAsset = assetResults[0].asset; | ||||||||||||||||
| console.log('random %j', randomAsset); | ||||||||||||||||
| data = { | ||||||||||||||||
| orgId: organizationId, | ||||||||||||||||
| assetName: randomAsset.name, | ||||||||||||||||
| }; | ||||||||||||||||
| console.log('data %j', data); | ||||||||||||||||
| }); | ||||||||||||||||
| it('client can add security marks to asset.', () => { | ||||||||||||||||
| const output = execSync( | ||||||||||||||||
| `node v2/addSecurityMarks.js ${data.assetName}` | ||||||||||||||||
| ).toString(); | ||||||||||||||||
| assert.include(output, data.assetName); | ||||||||||||||||
| assert.match(output, /key_a/); | ||||||||||||||||
| assert.match(output, /value_a/); | ||||||||||||||||
| assert.match(output, /key_b/); | ||||||||||||||||
| assert.match(output, /value_b/); | ||||||||||||||||
| assert.notMatch(output, /undefined/); | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| it('client can add and delete security marks', () => { | ||||||||||||||||
| // Ensure marks are set. | ||||||||||||||||
| execSync(`node v2/addSecurityMarks.js ${data.assetName}`).toString(); | ||||||||||||||||
|
|
||||||||||||||||
| const output = execSync( | ||||||||||||||||
| `node v2/addDeleteSecurityMarks.js ${data.assetName}` | ||||||||||||||||
| ).toString(); | ||||||||||||||||
| assert.match(output, /key_a/); | ||||||||||||||||
| assert.match(output, /new_value_a/); | ||||||||||||||||
| assert.notMatch(output, /key_b/); | ||||||||||||||||
| assert.notMatch(output, /undefined/); | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| it('client can delete security marks', () => { | ||||||||||||||||
| // Ensure marks are set. | ||||||||||||||||
| execSync(`node v2/addSecurityMarks.js ${data.assetName}`).toString(); | ||||||||||||||||
|
|
||||||||||||||||
| const output = execSync( | ||||||||||||||||
| `node v2/deleteAssetsSecurityMarks.js ${data.assetName}` | ||||||||||||||||
| ).toString(); | ||||||||||||||||
| assert.notMatch(output, /key_a/); | ||||||||||||||||
| assert.notMatch(output, /value_a/); | ||||||||||||||||
| assert.notMatch(output, /key_b/); | ||||||||||||||||
| assert.notMatch(output, /value_b/); | ||||||||||||||||
| assert.include(output, data.assetName); | ||||||||||||||||
| assert.include(output, data.assetName); | ||||||||||||||||
| assert.notMatch(output, /undefined/); | ||||||||||||||||
| }); | ||||||||||||||||
| }); | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Copyright 2024 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Demonstrates adding/updating at the same time as deleting security | ||
| * marks from an asset. | ||
| */ | ||
| function main(assetName = 'full asset path to add marks to') { | ||
| // [START securitycenter_add_delete_security_marks_v2] | ||
| // Imports the Google Cloud client library. | ||
| const {SecurityCenterClient} = require('@google-cloud/security-center').v2; | ||
|
|
||
| // Creates a new client. | ||
| const client = new SecurityCenterClient(); | ||
|
|
||
| async function addDeleteSecurityMarks() { | ||
| // assetName is the full resource path for the asset to update. | ||
| // Specify the value of 'assetName' in one of the following formats: | ||
| // `organizations/${org-id}/assets/${asset-id}`; | ||
| // `projects/${project-id}/assets/${asset-id}`; | ||
| // `folders/${folder-id}/assets/${asset-id}`; | ||
| const [newMarks] = await client.updateSecurityMarks({ | ||
| securityMarks: { | ||
| name: `${assetName}/securityMarks`, | ||
| marks: {key_a: 'new_value_a'}, | ||
| }, | ||
| // Only update the enableAssetDiscovery field. | ||
| updateMask: {paths: ['marks.key_a', 'marks.key_b']}, | ||
| }); | ||
|
|
||
| console.log('New marks: %j', newMarks); | ||
| } | ||
| addDeleteSecurityMarks(); | ||
| // [END securitycenter_add_delete_security_marks_v2] | ||
| } | ||
|
|
||
| main(...process.argv.slice(2)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright 2024 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Demostrates adding security marks to an asset. | ||
| */ | ||
| function main(assetName = 'full asset path to add marks to') { | ||
| // [START securitycenter_add_security_marks_v2] | ||
| // Imports the Google Cloud client library. | ||
| const {SecurityCenterClient} = require('@google-cloud/security-center').v2; | ||
|
|
||
| // Creates a new client. | ||
| const client = new SecurityCenterClient(); | ||
|
|
||
| async function addSecurityMarks() { | ||
| // assetName is the full resource path for the asset to update. | ||
| /* | ||
| * TODO(developer): Uncomment the following lines | ||
| */ | ||
| // Specify the value of 'assetName' in one of the following formats: | ||
| // `organizations/${org-id}/assets/${asset-id}`; | ||
| // `projects/${project-id}/assets/${asset-id}`; | ||
| // `folders/${folder-id}/assets/${asset-id}`; | ||
| // const assetName = "organizations/123123342/assets/12312321"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed |
||
| const [newMarks] = await client.updateSecurityMarks({ | ||
| securityMarks: { | ||
| name: `${assetName}/securityMarks`, | ||
| marks: {key_a: 'value_a', key_b: 'value_b'}, | ||
| }, | ||
| // Only update the marks with these keys. | ||
| updateMask: {paths: ['marks.key_a', 'marks.key_b']}, | ||
| }); | ||
|
|
||
| console.log('New marks: %', newMarks); | ||
iennae marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| addSecurityMarks(); | ||
| // [END securitycenter_add_security_marks_v2] | ||
| } | ||
|
|
||
| main(...process.argv.slice(2)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright 2024 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Demostrates deleting security marks on an asset. | ||
| */ | ||
| function main(assetName = 'full asset path to add marks to') { | ||
| // [START securitycenter_delete_security_marks_v2] | ||
| // Imports the Google Cloud client library. | ||
| const {SecurityCenterClient} = require('@google-cloud/security-center').v2; | ||
|
|
||
| // Creates a new client. | ||
| const client = new SecurityCenterClient(); | ||
|
|
||
| async function deleteSecurityMarks() { | ||
| // assetName is the full resource path for the asset to update. | ||
| /* | ||
| * TODO(developer): Uncomment the following lines | ||
| */ | ||
| // Specify the value of 'assetName' in one of the following formats: | ||
| // `organizations/${org-id}/assets/${asset-id}`; | ||
| // `projects/${project-id}/assets/${asset-id}`; | ||
| // `folders/${folder-id}/assets/${asset-id}`; | ||
| // const assetName = "organizations/123123342/assets/12312321"; | ||
iennae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const [newMarks] = await client.updateSecurityMarks({ | ||
| securityMarks: { | ||
| name: `${assetName}/securityMarks`, | ||
| // Intentionally, not setting marks to delete them. | ||
| }, | ||
| // Only delete marks for the following keys. | ||
| updateMask: {paths: ['marks.key_a', 'marks.key_b']}, | ||
| }); | ||
|
|
||
| console.log('Updated marks: %j', newMarks); | ||
| } | ||
| deleteSecurityMarks(); | ||
| // [END securitycenter_delete_security_marks_v2] | ||
| } | ||
|
|
||
| main(...process.argv.slice(2)); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a try-catch block is a good practice for error handling. Consider adding more specific error handling within the try block to catch potential issues like incorrect input or network errors. This would make debugging easier and improve the robustness of the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed