-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
129b04d
Add Resource v2 assets security marks
vijaykanthm dbe226b
fix(security center): lint fix
vijaykanthm a948f68
Merge branch 'main' into security-marks-assets-v2
vijaykanthm 5d8a033
refactor notifications test to fix quota limit issue
vijaykanthm 91e531e
Merge branch 'security-marks-assets-v2' of github.com:GoogleCloudPlat…
vijaykanthm 0201997
fix lint issue
vijaykanthm f29640b
refactor the test file to address comments
vijaykanthm a2eba15
refactor the test to fix lint issue
vijaykanthm 9a85b6b
pick the first asset as random
vijaykanthm 35e7fb7
Address comments by bot:
vijaykanthm 2fac912
Merge branch 'main' into security-marks-assets-v2
vijaykanthm 00edc7f
fix test in notification
vijaykanthm 5094475
Merge branch 'main' into security-marks-assets-v2
feywind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
security-center/snippets/system-test/v2/assetSecurityMarks.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; | ||
|
|
||
| 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/); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // 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. | ||
| // 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"; | ||
| 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: %j', newMarks); | ||
| } | ||
| addSecurityMarks(); | ||
| // [END securitycenter_add_security_marks_v2] | ||
| } | ||
|
|
||
| main(...process.argv.slice(2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // 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. | ||
| // 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"; | ||
| 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)); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.