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
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.
*/

package vtwo.assets;

import com.google.cloud.securitycenter.v2.SecurityCenterClient;
import com.google.cloud.securitycenter.v2.SecurityMarks;
import com.google.cloud.securitycenter.v2.UpdateSecurityMarksRequest;
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.


// 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.


// 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.


addDeleteSecurityMarks(organizationId, location, assetId);
}

// 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.

public static SecurityMarks addDeleteSecurityMarks(String organizationId,
Copy link
Contributor

Choose a reason for hiding this comment

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

the name "add" "delete" "security marks" is confusing. please refactor.

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 location, String assetId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
SecurityCenterClient client = SecurityCenterClient.create();
Copy link
Contributor

Choose a reason for hiding this comment

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

please follow try-resource Java pattern for resource allocations

Copy link
Author

Choose a reason for hiding this comment

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

Addressed.


// Specify the value of 'assetName' in one of the following formats:
// String assetName = "organizations/{org-id}/assets/{asset-id}";
// String assetName = "projects/{project-id}/assets/{asset-id}";
// String assetName = "folders/{folder-id}/assets/{asset-id}";
String assetName = String.format("organizations/%s/assets/%s", organizationId, assetId);
Copy link
Contributor

Choose a reason for hiding this comment

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

if the code sample can work with a project, please use project instead of organization to simplify testing.
provide a link to documentation instead of the detailed self-written comment like this

Copy link
Author

Choose a reason for hiding this comment

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

Addressed.


// Start setting up a request to clear and update security marks for an asset.
// Create security mark and field mask for clearing security marks.
SecurityMarks securityMarks = SecurityMarks.newBuilder()
.setName(assetName + "/securityMarks")
.putMarks("key_a", "new_value_for_a")
.build();

FieldMask updateMask = FieldMask.newBuilder()
.addPaths("marks.key_a")
.addPaths("marks.key_b")
.build();

UpdateSecurityMarksRequest request = UpdateSecurityMarksRequest.newBuilder()
.setSecurityMarks(securityMarks)
.setUpdateMask(updateMask)
.build();
Copy link
Contributor

Choose a reason for hiding this comment

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

the code sample is unclear. how marks and mask paths correlate. It looks like the example demonstrates two disconnected things. Can a user set security marks without adding paths? Please, refactor 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.


// Call the API.
SecurityMarks response = client.updateSecurityMarks(request);

System.out.println("Security Marks updated and cleared::" + response);
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 stdout printing. delete this line

Copy link
Author

Choose a reason for hiding this comment

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

Addressed.

return response;
}
}

//[END 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.

please, self review the code following feedback in AddDeleteSecurityMarks.java

Copy link
Author

Choose a reason for hiding this comment

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

Addressed.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.
*/

package vtwo.assets;

// [START securitycenter_add_security_marks_assets_v2]

import autovalue.shaded.com.google.common.collect.ImmutableMap;
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
import com.google.cloud.securitycenter.v2.SecurityMarks;
import com.google.cloud.securitycenter.v2.UpdateSecurityMarksRequest;
import com.google.protobuf.FieldMask;
import java.io.IOException;

public class AddSecurityMarksToAssets {

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

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

// Specify the location.
String location = "global";

addToAsset(organizationId, location, assetId);
}

// Demonstrates adding security marks to findings.
// To add or change security marks, you must have an IAM role that includes permission:
public static SecurityMarks addToAsset(String organizationId, String location, String assetId)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
SecurityCenterClient client = SecurityCenterClient.create();

// Specify the value of 'assetName' in one of the following formats:
// String assetName = "organizations/{org-id}/assets/{asset-id}";
// String assetName = "projects/{project-id}/assets/{asset-id}";
// String assetName = "folders/{folder-id}/assets/{asset-id}";
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");

// Add security marks and field mask for security marks.
SecurityMarks securityMarks =
SecurityMarks.newBuilder()
.setName(assetName + "/securityMarks")
.putAllMarks(markMap)
.build();

// Set the update mask to specify which properties should be updated.
// If empty, all mutable fields will be updated.
// For more info on constructing field mask path, see the proto or:
// https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask
FieldMask updateMask =
FieldMask.newBuilder().addPaths("marks.key_a").addPaths("marks.key_b").build();

UpdateSecurityMarksRequest request =
UpdateSecurityMarksRequest.newBuilder()
.setSecurityMarks(securityMarks)
.setUpdateMask(updateMask)
.build();

// Call the API.
SecurityMarks response = client.updateSecurityMarks(request);

System.out.println("Security Marks:" + response);
return response;
}
}

// [END securitycenter_add_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.

please, self review the code following feedback in AddDeleteSecurityMarks.java

Copy link
Author

Choose a reason for hiding this comment

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

Addressed.

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.
*/

package vtwo.assets;

import com.google.cloud.securitycenter.v2.SecurityCenterClient;
import com.google.cloud.securitycenter.v2.SecurityMarks;
import com.google.cloud.securitycenter.v2.UpdateSecurityMarksRequest;
import com.google.protobuf.FieldMask;
import java.io.IOException;

//[START securitycenter_delete_security_marks_assets_v2]

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

// Specify the asset-id.
String assetId = "{asset-id}";

// Specify the location.
String location = "global";

deleteSecurityMarks(organizationId, location, assetId);
}

// Demonstrates deleting security marks on an asset.
// To add or change security marks, you must have an IAM role that includes permission:
public static SecurityMarks deleteSecurityMarks(String organizationId,
String location, String assetId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
SecurityCenterClient client = SecurityCenterClient.create();

// Specify the value of 'assetName' in one of the following formats:
// String assetName = "organizations/{org-id}/assets/{asset-id}";
// String assetName = "projects/{project-id}/assets/{asset-id}";
// String assetName = "folders/{folder-id}/assets/{asset-id}";
String assetName = String.format("organizations/%s/assets/%s", organizationId, assetId);

// Start setting up a request to clear and update security marks for an asset.
// Create security mark and field mask for clearing security marks.
SecurityMarks securityMarks = SecurityMarks.newBuilder()
.setName(assetName + "/securityMarks")
.build();

FieldMask updateMask = FieldMask.newBuilder()
.addPaths("marks.key_a")
.addPaths("marks.key_b")
.build();

UpdateSecurityMarksRequest request = UpdateSecurityMarksRequest.newBuilder()
.setSecurityMarks(securityMarks)
.setUpdateMask(updateMask)
.build();

// Call the API.
SecurityMarks response = client.updateSecurityMarks(request);

System.out.println("Security Marks cleared::" + response);
return response;
}
}

//[END securitycenter_delete_security_marks_assets_v2]

Loading