Skip to content
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

feat(security-center): Add Resource v2 API Assets Security Marks Samples #9680

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

vijaykanthm
Copy link

Description

This PR adds v2 API Assets Security Marks Java 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 Sample Format Guide
  • pom.xml parent set to latest shared-configuration
  • Appropriate changes to README are included in PR
  • 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)
  • Tests pass: mvn clean verify required
  • Lint passes: mvn -P lint checkstyle:check required
  • Static Analysis: mvn -P lint clean compile pmd:cpd-check spotbugs:check advisory only
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners 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 yoshi-approver and a team as code owners November 12, 2024 20:33
Copy link

snippet-bot bot commented Nov 12, 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

@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 12, 2024
@vijaykanthm vijaykanthm added kokoro:run Add this label to force Kokoro to re-run the tests. kokoro:force-run Add this label to force Kokoro to re-run the tests. and removed kokoro:run Add this label to force Kokoro to re-run the tests. labels Nov 14, 2024
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 14, 2024
@owenhuyn owenhuyn added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 26, 2024
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 26, 2024
@vijaykanthm vijaykanthm added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 3, 2024
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 3, 2024
import com.google.protobuf.FieldMask;
import java.io.IOException;

//[START securitycenter_add_delete_security_marks_assets_v2]
Copy link
Contributor

Choose a reason for hiding this comment

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

place region tags to enclose necessary imports and the code sample method. no need to leave space lines between the region tags and the code

Copy link
Author

Choose a reason for hiding this comment

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

Addressed.

public class AddDeleteSecurityMarks {
public static void main(String[] args) throws IOException {
// organizationId: Google Cloud Organization id.
String organizationId = "{google-cloud-organization-id}";
Copy link
Contributor

Choose a reason for hiding this comment

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

please align the style of the ID with other code samples. No need for curly brackets. Most of code samples use capitalized expression like PROJECT_ID or ORGANIZATION_ID

Copy link
Author

Choose a reason for hiding this comment

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

Addressed.

String organizationId = "{google-cloud-organization-id}";

// Specify the finding-id.
String assetId = "{asset-id}";
Copy link
Contributor

Choose a reason for hiding this comment

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

same here. note that "finding-id" is not used anywhere. consider to refactor the comment

Copy link
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 35 to 36
// Specify the location.
String location = "global";
Copy link
Contributor

Choose a reason for hiding this comment

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

does your code sample support other locations? if not, please use this literal inside the code sample method instead of using it as parameter. if a user can use different locations, provide a link to documentation that enumerates these locations in the comment for this argument.

Copy link
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 41 to 43
// Demonstrates adding/updating at the same time as deleting security
// marks from an asset.
// To add or change security marks, you must have an IAM role that includes permission:
Copy link
Contributor

Choose a reason for hiding this comment

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

no need for comments here.

Copy link
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 80 to 82
final PrintStream out = System.out;
stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));
Copy link
Contributor

Choose a reason for hiding this comment

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

please use return values instead of capturing and parsing stdout.

Copy link
Author

Choose a reason for hiding this comment

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

Addressed.

stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));

requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you can skip this check

Copy link
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 86 to 98
// Fetch a valid asset ID dynamically
try (SecurityCenterClient client = SecurityCenterClient.create()) {
OrganizationName orgName = OrganizationName.of(ORGANIZATION_ID);
ListAssetsRequest request =
ListAssetsRequest.newBuilder().setParent(orgName.toString()).setPageSize(1).build();

Asset asset = client.listAssets(request).iterateAll().iterator().next().getAsset();
assetName = asset.getName(); // Get the full resource name for the asset
assetId = extractAssetId(assetName);
} catch (InvalidArgumentException e) {
System.err.println("Error retrieving asset ID: " + e.getMessage());
throw e;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

setup method is used to create resources. you cannot assume that a resource exists before the tests are executed. in rare occasions when tests use pre-provisioned resources (e.g. because provisioning a resource takes too long), all required attributes of the resource are hardcoded and expressed via environment variables in the testing environment.

Copy link
Author

Choose a reason for hiding this comment

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

Addressed.


stdOut = null;
System.setOut(out);
TimeUnit.MINUTES.sleep(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

remove this delay

Copy link
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 107 to 108
stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));
Copy link
Contributor

Choose a reason for hiding this comment

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

do not capture stdout stream

Copy link
Author

Choose a reason for hiding this comment

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

Addressed.

@vijaykanthm vijaykanthm requested review from a team as code owners February 2, 2025 21:27
@vijaykanthm vijaykanthm requested review from a team as code owners February 2, 2025 21:27
@vijaykanthm vijaykanthm force-pushed the security-marks-assets-v2 branch from af64a50 to e4b8d95 Compare February 2, 2025 22:56
@vijaykanthm vijaykanthm removed request for a team February 2, 2025 22:59
@lovenishs04 lovenishs04 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 3, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 3, 2025
@vijaykanthm vijaykanthm requested a review from minherz February 3, 2025 18:14
@telpirion telpirion self-assigned this Feb 18, 2025
Copy link
Contributor

@minherz minherz left a comment

Choose a reason for hiding this comment

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

Please review and address the comments.
Please provide explanation about differences between AddDeleteSecurityMarks and AddSecurityMarksToAssets code samples.

Comment on lines +29 to +32
// organizationId: Google Cloud Organization id.
String organizationId = "ORGANIZATION_ID";

// Specify the asset id.
Copy link
Contributor

Choose a reason for hiding this comment

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

TODO (actionable comments) are required here to instruct a reader how to create a working example.
See this main() method for an example.

import com.google.protobuf.FieldMask;
import java.io.IOException;

public class AddDeleteSecurityMarks {
Copy link
Contributor

Choose a reason for hiding this comment

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

The example name is confusing. Adding and deleting marks in the same action is contradicting. Consider changing the name to UpdateSecurityMarks. If you want to demonstrate something else, change the name accordingly.

public class AddSecurityMarksToAssets {

public static void main(String[] args) throws IOException {
// organizationId: Google Cloud Organization id.
Copy link
Contributor

Choose a reason for hiding this comment

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

TODO (actionable comments) are required here to instruct a reader how to create a working example.

String assetName = String.format("organizations/%s/assets/%s", organizationId, assetId);

// Start setting up a request to add security marks for a finding.
ImmutableMap markMap = ImmutableMap.of("key_a", "value_a", "key_b", "value_b");
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: consider passing the collection of key/value strings as an argument in order to make the example more usable.


@Rule
public final MultipleAttemptsRule multipleAttemptsRule =
new MultipleAttemptsRule(3, 120000); // 2 minutes
Copy link
Contributor

Choose a reason for hiding this comment

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

2 minutes per test is too long. please consider to reduce the maximal time of execution.

Comment on lines +103 to +117
@Before
public void beforeEach() {
stdOut = new ByteArrayOutputStream();
}

@After
public void afterEach() {
stdOut = null;
System.setOut(null);
}

@AfterClass
public static void cleanUp() {
System.setOut(System.out);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

remove this code since code samples do not print to stdout.

Copy link
Contributor

Choose a reason for hiding this comment

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

why to store so much information in order to retrieve a (supposedly) fixed asset ID?

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.

8 participants