Skip to content

Commit 38f9e26

Browse files
committed
Address comments
1 parent 406c411 commit 38f9e26

File tree

5 files changed

+177
-178
lines changed

5 files changed

+177
-178
lines changed

security-command-center/snippets/src/main/java/vtwo/assets/AddDeleteSecurityMarks.java

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
// [START securitycenter_add_delete_security_marks_assets_v2]
1718
package vtwo.assets;
1819

1920
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
@@ -22,60 +23,52 @@
2223
import com.google.protobuf.FieldMask;
2324
import java.io.IOException;
2425

25-
//[START securitycenter_add_delete_security_marks_assets_v2]
26-
2726
public class AddDeleteSecurityMarks {
2827
public static void main(String[] args) throws IOException {
2928
// organizationId: Google Cloud Organization id.
30-
String organizationId = "{google-cloud-organization-id}";
31-
32-
// Specify the finding-id.
33-
String assetId = "{asset-id}";
29+
String organizationId = "ORGANIZATION_ID";
3430

35-
// Specify the location.
36-
String location = "global";
31+
// Specify the asset id.
32+
String assetId = "ASSET_ID";
3733

38-
addDeleteSecurityMarks(organizationId, location, assetId);
34+
addAndDeleteSecurityMarks(organizationId, assetId);
3935
}
4036

41-
// Demonstrates adding/updating at the same time as deleting security
42-
// marks from an asset.
43-
// To add or change security marks, you must have an IAM role that includes permission:
44-
public static SecurityMarks addDeleteSecurityMarks(String organizationId,
45-
String location, String assetId) throws IOException {
37+
public static SecurityMarks addAndDeleteSecurityMarks(String organizationId, String assetId)
38+
throws IOException {
4639
// Initialize client that will be used to send requests. This client only needs to be created
4740
// once, and can be reused for multiple requests.
48-
SecurityCenterClient client = SecurityCenterClient.create();
41+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
4942

50-
// Specify the value of 'assetName' in one of the following formats:
51-
// String assetName = "organizations/{org-id}/assets/{asset-id}";
52-
// String assetName = "projects/{project-id}/assets/{asset-id}";
53-
// String assetName = "folders/{folder-id}/assets/{asset-id}";
54-
String assetName = String.format("organizations/%s/assets/%s", organizationId, assetId);
43+
// Specify the value of 'assetName' in one of the following formats:
44+
// String assetName = "organizations/{org-id}/assets/{asset-id}";
45+
String assetName = String.format("organizations/%s/assets/%s", organizationId, assetId);
5546

56-
// Start setting up a request to clear and update security marks for an asset.
57-
// Create security mark and field mask for clearing security marks.
58-
SecurityMarks securityMarks = SecurityMarks.newBuilder()
59-
.setName(assetName + "/securityMarks")
60-
.putMarks("key_a", "new_value_for_a")
61-
.build();
47+
// Start setting up a request to clear and update security marks for an asset.
48+
// Create security mark and field mask for clearing security marks.
49+
SecurityMarks securityMarks =
50+
SecurityMarks.newBuilder()
51+
.setName(assetName + "/securityMarks")
52+
.putMarks("key_a", "new_value_for_a")
53+
.putMarks("key_b", "new_value_for_b")
54+
.build();
6255

63-
FieldMask updateMask = FieldMask.newBuilder()
64-
.addPaths("marks.key_a")
65-
.addPaths("marks.key_b")
66-
.build();
56+
// Define the paths in the updateMask that correspond to the keys being updated in
57+
// securityMarks.
58+
FieldMask updateMask =
59+
FieldMask.newBuilder().addPaths("marks.key_a").addPaths("marks.key_b").build();
6760

68-
UpdateSecurityMarksRequest request = UpdateSecurityMarksRequest.newBuilder()
69-
.setSecurityMarks(securityMarks)
70-
.setUpdateMask(updateMask)
71-
.build();
61+
// Create the request to update security marks.
62+
UpdateSecurityMarksRequest request =
63+
UpdateSecurityMarksRequest.newBuilder()
64+
.setSecurityMarks(securityMarks)
65+
.setUpdateMask(updateMask)
66+
.build();
7267

73-
// Call the API.
74-
SecurityMarks response = client.updateSecurityMarks(request);
75-
76-
System.out.println("Security Marks updated and cleared::" + response);
77-
return response;
68+
// Call the API and return the response.
69+
SecurityMarks response = client.updateSecurityMarks(request);
70+
return response;
71+
}
7872
}
7973
}
80-
81-
//[END securitycenter_add_delete_security_marks_assets_v2]
74+
// [END securitycenter_add_delete_security_marks_assets_v2]

security-command-center/snippets/src/main/java/vtwo/assets/AddSecurityMarksToAssets.java

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
package vtwo.assets;
18-
1917
// [START securitycenter_add_security_marks_assets_v2]
18+
package vtwo.assets;
2019

2120
import autovalue.shaded.com.google.common.collect.ImmutableMap;
2221
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
@@ -29,59 +28,50 @@ public class AddSecurityMarksToAssets {
2928

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

34-
// Specify the finding-id.
35-
String assetId = "{asset-id}";
33+
// Specify the asset id.
34+
String assetId = "ASSET_ID";
3635

37-
// Specify the location.
38-
String location = "global";
39-
40-
addToAsset(organizationId, location, assetId);
36+
addToAsset(organizationId, assetId);
4137
}
4238

43-
// Demonstrates adding security marks to findings.
44-
// To add or change security marks, you must have an IAM role that includes permission:
45-
public static SecurityMarks addToAsset(String organizationId, String location, String assetId)
46-
throws IOException {
39+
public static SecurityMarks addToAsset(String organizationId, String assetId) throws IOException {
4740
// Initialize client that will be used to send requests. This client only needs to be created
4841
// once, and can be reused for multiple requests.
49-
SecurityCenterClient client = SecurityCenterClient.create();
50-
51-
// Specify the value of 'assetName' in one of the following formats:
52-
// String assetName = "organizations/{org-id}/assets/{asset-id}";
53-
// String assetName = "projects/{project-id}/assets/{asset-id}";
54-
// String assetName = "folders/{folder-id}/assets/{asset-id}";
55-
String assetName = String.format("organizations/%s/assets/%s", organizationId, assetId);
56-
57-
// Start setting up a request to add security marks for a finding.
58-
ImmutableMap markMap = ImmutableMap.of("key_a", "value_a", "key_b", "value_b");
59-
60-
// Add security marks and field mask for security marks.
61-
SecurityMarks securityMarks =
62-
SecurityMarks.newBuilder()
63-
.setName(assetName + "/securityMarks")
64-
.putAllMarks(markMap)
65-
.build();
66-
67-
// Set the update mask to specify which properties should be updated.
68-
// If empty, all mutable fields will be updated.
69-
// For more info on constructing field mask path, see the proto or:
70-
// https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask
71-
FieldMask updateMask =
72-
FieldMask.newBuilder().addPaths("marks.key_a").addPaths("marks.key_b").build();
73-
74-
UpdateSecurityMarksRequest request =
75-
UpdateSecurityMarksRequest.newBuilder()
76-
.setSecurityMarks(securityMarks)
77-
.setUpdateMask(updateMask)
78-
.build();
79-
80-
// Call the API.
81-
SecurityMarks response = client.updateSecurityMarks(request);
82-
83-
System.out.println("Security Marks:" + response);
84-
return response;
42+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
43+
44+
// Specify the value of 'assetName' in one of the following formats:
45+
// String assetName = "organizations/{org-id}/assets/{asset-id}";
46+
String assetName = String.format("organizations/%s/assets/%s", organizationId, assetId);
47+
48+
// Start setting up a request to add security marks for a finding.
49+
ImmutableMap markMap = ImmutableMap.of("key_a", "value_a", "key_b", "value_b");
50+
51+
// Add security marks and field mask for security marks.
52+
SecurityMarks securityMarks =
53+
SecurityMarks.newBuilder()
54+
.setName(assetName + "/securityMarks")
55+
.putAllMarks(markMap)
56+
.build();
57+
58+
// Set the update mask to specify which properties should be updated.
59+
// If empty, all mutable fields will be updated.
60+
// For more info on constructing field mask path, see the proto or:
61+
// https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask
62+
FieldMask updateMask =
63+
FieldMask.newBuilder().addPaths("marks.key_a").addPaths("marks.key_b").build();
64+
65+
UpdateSecurityMarksRequest request =
66+
UpdateSecurityMarksRequest.newBuilder()
67+
.setSecurityMarks(securityMarks)
68+
.setUpdateMask(updateMask)
69+
.build();
70+
71+
// Call the API and return the response.
72+
SecurityMarks response = client.updateSecurityMarks(request);
73+
return response;
74+
}
8575
}
8676
}
8777

security-command-center/snippets/src/main/java/vtwo/assets/DeleteAssetsSecurityMarks.java

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
// [START securitycenter_delete_security_marks_assets_v2]
1718
package vtwo.assets;
1819

1920
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
@@ -22,59 +23,46 @@
2223
import com.google.protobuf.FieldMask;
2324
import java.io.IOException;
2425

25-
//[START securitycenter_delete_security_marks_assets_v2]
26-
2726
public class DeleteAssetsSecurityMarks {
2827
public static void main(String[] args) throws IOException {
2928
// organizationId: Google Cloud Organization id.
30-
String organizationId = "{google-cloud-organization-id}";
29+
String organizationId = "ORGANIZATION_ID";
3130

3231
// Specify the asset-id.
33-
String assetId = "{asset-id}";
34-
35-
// Specify the location.
36-
String location = "global";
32+
String assetId = "ASSET_ID";
3733

38-
deleteSecurityMarks(organizationId, location, assetId);
34+
deleteSecurityMarks(organizationId, assetId);
3935
}
4036

41-
// Demonstrates deleting security marks on an asset.
42-
// To add or change security marks, you must have an IAM role that includes permission:
43-
public static SecurityMarks deleteSecurityMarks(String organizationId,
44-
String location, String assetId) throws IOException {
37+
public static SecurityMarks deleteSecurityMarks(String organizationId, String assetId)
38+
throws IOException {
4539
// Initialize client that will be used to send requests. This client only needs to be created
4640
// once, and can be reused for multiple requests.
47-
SecurityCenterClient client = SecurityCenterClient.create();
48-
49-
// Specify the value of 'assetName' in one of the following formats:
50-
// String assetName = "organizations/{org-id}/assets/{asset-id}";
51-
// String assetName = "projects/{project-id}/assets/{asset-id}";
52-
// String assetName = "folders/{folder-id}/assets/{asset-id}";
53-
String assetName = String.format("organizations/%s/assets/%s", organizationId, assetId);
54-
55-
// Start setting up a request to clear and update security marks for an asset.
56-
// Create security mark and field mask for clearing security marks.
57-
SecurityMarks securityMarks = SecurityMarks.newBuilder()
58-
.setName(assetName + "/securityMarks")
59-
.build();
60-
61-
FieldMask updateMask = FieldMask.newBuilder()
62-
.addPaths("marks.key_a")
63-
.addPaths("marks.key_b")
64-
.build();
65-
66-
UpdateSecurityMarksRequest request = UpdateSecurityMarksRequest.newBuilder()
67-
.setSecurityMarks(securityMarks)
68-
.setUpdateMask(updateMask)
69-
.build();
70-
71-
// Call the API.
72-
SecurityMarks response = client.updateSecurityMarks(request);
73-
74-
System.out.println("Security Marks cleared::" + response);
75-
return response;
41+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
42+
43+
// Specify the value of 'assetName' in one of the following formats:
44+
// String assetName = "organizations/{org-id}/assets/{asset-id}";
45+
String assetName = String.format("organizations/%s/assets/%s", organizationId, assetId);
46+
47+
// Start setting up a request to clear and update security marks for an asset.
48+
// Create security mark and field mask for clearing security marks.
49+
SecurityMarks securityMarks =
50+
SecurityMarks.newBuilder().setName(assetName + "/securityMarks").build();
51+
52+
FieldMask updateMask =
53+
FieldMask.newBuilder().addPaths("marks.key_a").addPaths("marks.key_b").build();
54+
55+
UpdateSecurityMarksRequest request =
56+
UpdateSecurityMarksRequest.newBuilder()
57+
.setSecurityMarks(securityMarks)
58+
.setUpdateMask(updateMask)
59+
.build();
60+
61+
// Call the API.
62+
SecurityMarks response = client.updateSecurityMarks(request);
63+
return response;
64+
}
7665
}
7766
}
7867

79-
//[END securitycenter_delete_security_marks_assets_v2]
80-
68+
// [END securitycenter_delete_security_marks_assets_v2]

0 commit comments

Comments
 (0)