-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
c64abfb
8c76982
1d4693b
406c411
38f9e26
99eacfc
d06981b
e4b8d95
885be93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] | ||
|
||
public class AddDeleteSecurityMarks { | ||
public static void main(String[] args) throws IOException { | ||
// organizationId: Google Cloud Organization id. | ||
String organizationId = "{google-cloud-organization-id}"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed. |
||
|
||
// Specify the finding-id. | ||
String assetId = "{asset-id}"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed. |
||
|
||
// Specify the location. | ||
String location = "global"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for comments here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed. |
||
public static SecurityMarks addDeleteSecurityMarks(String organizationId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the name "add" "delete" "security marks" is confusing. please refactor. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please follow try-resource Java pattern for resource allocations There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for stdout printing. delete this line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed. |
||
return response; | ||
} | ||
} | ||
|
||
//[END securitycenter_add_delete_security_marks_assets_v2] |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please, self review the code following feedback in AddDeleteSecurityMarks.java There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please, self review the code following feedback in AddDeleteSecurityMarks.java There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
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.
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
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.
Addressed.