Skip to content

Commit c64abfb

Browse files
committed
Add Resource v2 Assets Security Marks
1 parent f89f6fa commit c64abfb

File tree

4 files changed

+408
-0
lines changed

4 files changed

+408
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package vtwo.assets;
18+
19+
import java.io.IOException;
20+
21+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
22+
import com.google.cloud.securitycenter.v2.SecurityMarks;
23+
import com.google.cloud.securitycenter.v2.UpdateSecurityMarksRequest;
24+
import com.google.protobuf.FieldMask;
25+
26+
//[START securitycenter_add_delete_security_marks_assets_v2]
27+
28+
29+
public class AddDeleteSecurityMarks {
30+
public static void main(String[] args) throws IOException {
31+
// organizationId: Google Cloud Organization id.
32+
String organizationId = "{google-cloud-organization-id}";
33+
34+
// Specify the finding-id.
35+
String assetId = "{asset-id}";
36+
37+
// Specify the location.
38+
String location = "global";
39+
40+
addDeleteSecurityMarks(organizationId, location, assetId);
41+
}
42+
43+
// Demonstrates adding/updating at the same time as deleting security
44+
// marks from an asset.
45+
// To add or change security marks, you must have an IAM role that includes permission:
46+
public static SecurityMarks addDeleteSecurityMarks(String organizationId,
47+
String location, String assetId) throws IOException {
48+
// Initialize client that will be used to send requests. This client only needs to be created
49+
// once, and can be reused for multiple requests.
50+
SecurityCenterClient client = SecurityCenterClient.create();
51+
52+
// Specify the value of 'assetName' in one of the following formats:
53+
// String assetName = "organizations/{org-id}/assets/{asset-id}";
54+
// String assetName = "projects/{project-id}/assets/{asset-id}";
55+
// String assetName = "folders/{folder-id}/assets/{asset-id}";
56+
String assetName = String.format("organizations/%s/assets/%s", organizationId, assetId);
57+
58+
// Start setting up a request to clear and update security marks for an asset.
59+
// Create security mark and field mask for clearing security marks.
60+
SecurityMarks securityMarks = SecurityMarks.newBuilder()
61+
.setName(assetName + "/securityMarks")
62+
.putMarks("key_a", "new_value_for_a")
63+
.build();
64+
65+
FieldMask updateMask = FieldMask.newBuilder()
66+
.addPaths("marks.key_a")
67+
.addPaths("marks.key_b")
68+
.build();
69+
70+
UpdateSecurityMarksRequest request = UpdateSecurityMarksRequest.newBuilder()
71+
.setSecurityMarks(securityMarks)
72+
.setUpdateMask(updateMask)
73+
.build();
74+
75+
// Call the API.
76+
SecurityMarks response = client.updateSecurityMarks(request);
77+
78+
System.out.println("Security Marks updated and cleared::" + response);
79+
return response;
80+
}
81+
}
82+
83+
//[END securitycenter_add_delete_security_marks_assets_v2]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package vtwo.assets;
18+
19+
// [START securitycenter_add_security_marks_assets_v2]
20+
21+
import autovalue.shaded.com.google.common.collect.ImmutableMap;
22+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
23+
import com.google.cloud.securitycenter.v2.SecurityMarks;
24+
import com.google.cloud.securitycenter.v2.UpdateSecurityMarksRequest;
25+
import com.google.protobuf.FieldMask;
26+
import java.io.IOException;
27+
28+
public class AddSecurityMarksToAssets {
29+
30+
public static void main(String[] args) throws IOException {
31+
// organizationId: Google Cloud Organization id.
32+
String organizationId = "{google-cloud-organization-id}";
33+
34+
// Specify the finding-id.
35+
String assetId = "{asset-id}";
36+
37+
// Specify the location.
38+
String location = "global";
39+
40+
addToAsset(organizationId, location, assetId);
41+
}
42+
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,
46+
String location, String assetId) throws IOException {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// 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 = SecurityMarks.newBuilder()
62+
.setName(assetName + "/securityMarks")
63+
.putAllMarks(markMap)
64+
.build();
65+
66+
// Set the update mask to specify which properties should be updated.
67+
// If empty, all mutable fields will be updated.
68+
// For more info on constructing field mask path, see the proto or:
69+
// https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask
70+
FieldMask updateMask = FieldMask.newBuilder()
71+
.addPaths("marks.key_a")
72+
.addPaths("marks.key_b")
73+
.build();
74+
75+
UpdateSecurityMarksRequest request = 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;
85+
}
86+
}
87+
88+
89+
// [END securitycenter_add_security_marks_assets_v2]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package vtwo.assets;
18+
19+
import java.io.IOException;
20+
21+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
22+
import com.google.cloud.securitycenter.v2.SecurityMarks;
23+
import com.google.cloud.securitycenter.v2.UpdateSecurityMarksRequest;
24+
import com.google.protobuf.FieldMask;
25+
26+
//[START securitycenter_delete_security_marks_assets_v2]
27+
28+
public class DeleteAssetsSecurityMarks {
29+
public static void main(String[] args) throws IOException {
30+
// organizationId: Google Cloud Organization id.
31+
String organizationId = "{google-cloud-organization-id}";
32+
33+
// Specify the asset-id.
34+
String assetId = "{asset-id}";
35+
36+
// Specify the location.
37+
String location = "global";
38+
39+
deleteSecurityMarks(organizationId, location, assetId);
40+
}
41+
42+
// Demonstrates deleting security marks on an asset.
43+
// To add or change security marks, you must have an IAM role that includes permission:
44+
public static SecurityMarks deleteSecurityMarks(String organizationId,
45+
String location, String assetId) throws IOException {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests.
48+
SecurityCenterClient client = SecurityCenterClient.create();
49+
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);
55+
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+
.build();
61+
62+
FieldMask updateMask = FieldMask.newBuilder()
63+
.addPaths("marks.key_a")
64+
.addPaths("marks.key_b")
65+
.build();
66+
67+
UpdateSecurityMarksRequest request = UpdateSecurityMarksRequest.newBuilder()
68+
.setSecurityMarks(securityMarks)
69+
.setUpdateMask(updateMask)
70+
.build();
71+
72+
// Call the API.
73+
SecurityMarks response = client.updateSecurityMarks(request);
74+
75+
System.out.println("Security Marks cleared::" + response);
76+
return response;
77+
}
78+
}
79+
80+
//[END securitycenter_delete_security_marks_assets_v2]
81+

0 commit comments

Comments
 (0)