Skip to content

Conversation

@vijaykanthm
Copy link
Contributor

Description

This PR adds v2 API Assets Security Marks Node js client samples to Add Security Marks, Delete Security Marks, Add Delete Security Marks.

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

Checklist

  • I have followed guidelines from CONTRIBUTING.MD and Samples Style Guide
  • Tests pass: npm test (see Testing)
  • Lint pass: npm run lint (see Style)
  • These samples need a new API enabled in testing projects to pass (let us know which ones)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • This pull request is from a branch created directly off of GoogleCloudPlatform/nodejs-docs-samples. Not a fork.
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new sample directory, and I created GitHub Actions workflow for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

@vijaykanthm vijaykanthm requested review from a team as code owners November 4, 2024 20:13
@conventional-commit-lint-gcf
Copy link

conventional-commit-lint-gcf bot commented Nov 4, 2024

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@product-auto-label product-auto-label bot added api: securitycenter Issues related to the Security Command Center API. samples Issues that are directly related to samples. labels Nov 4, 2024
@snippet-bot
Copy link

snippet-bot bot commented Nov 4, 2024

Here is the summary of changes.

You are about to add 3 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

Copy link

@feywind feywind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generally looks fine, maybe just check on that exec() vs execSync() thing. Let's see if we can get someone closer to docs/samples to approve the rest of the way.

parent: client.organizationPath(organizationId),
});
const randomAsset =
assetResults[Math.floor(Math.random() * assetResults.length)].asset;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing random() in a test makes me worry that it's going to end up being flaky, but I guess if all the items are more or less equivalent, should be okay.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored it.


it('client can add and delete security marks', () => {
// Ensure marks are set.
exec(`node v2/addSecurityMarks.js ${data.assetName}`);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be execSync()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed. Other tests were using the exec() method so I have used the same but I have updated it.

@vijaykanthm vijaykanthm requested a review from feywind November 14, 2024 17:56
@iennae
Copy link
Contributor

iennae commented Nov 16, 2024

@gemini review

Copy link
Contributor

@code-review-assist-experimental code-review-assist-experimental bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pull request adds samples for the Security Center v2 API's Asset Security Marks in Node.js. The new samples demonstrate adding, deleting, and adding/deleting security marks. The changes also include updates to the system tests to incorporate these new functionalities. The implementation looks good overall, but I have a few suggestions to improve clarity, consistency, and robustness.

One improvement could be to add more comprehensive documentation in the README file for the security-center/snippets/v2 directory. This documentation should explain the purpose of each sample, how to run them, and the expected outcomes. It should also clearly outline the prerequisites, such as setting up a Security Center project and enabling the necessary APIs.

Additionally, the system tests could be enhanced by adding more assertions to validate the state of the security marks after each operation. This would ensure that the samples are functioning correctly and provide more confidence in the code's reliability.

Comment on lines 39 to 52
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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

Comment on lines 55 to 61
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Adding delays using setTimeout can make tests flaky and increase execution time. Consider using a more robust approach like polling or a dedicated testing library that provides utilities for waiting for asynchronous operations to complete.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

Comment on lines 66 to 75
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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the creation process, adding a try-catch block is good practice for error handling during deletion. Consider adding more specific error handling within the try block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

Comment on lines 78 to 84
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using setTimeout for delays can lead to flaky tests. Consider using a more robust approach like polling or a dedicated testing library for waiting for asynchronous operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

const {execSync} = require('child_process');

// TODO(developers): update for your own environment
const organizationId = '1081635000895';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
const organizationId = '1081635000895';
// Retrieve organization ID from environment variable
const organizationId = process.env.ORGANIZATION_ID || 'YOUR_ORGANIZATION_ID';
if (!organizationId) {
throw new Error('Environment variable ORGANIZATION_ID is not set.');
}

Comment on lines 29 to 37
// 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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

The TODO comments suggest uncommenting lines to specify the asset name. It's better to provide clear instructions or examples on how to obtain and use the asset name rather than relying on commented-out code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

@iennae iennae merged commit 0469e1f into main Nov 27, 2024
136 checks passed
@iennae iennae deleted the security-marks-assets-v2 branch November 27, 2024 06:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: securitycenter Issues related to the Security Command Center API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants