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 4 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

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

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

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.

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.

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.

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

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.


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

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

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.

5 participants