Skip to content

Commit 2b43cb4

Browse files
author
Rafael Murata
authored
feat: Adding API v2 Source samples (GoogleCloudPlatform#9299)
* add sources samples * feat: sources samples v2 * improve readability * improve comments * adjustments for pull request
1 parent 4182434 commit 2b43cb4

5 files changed

Lines changed: 452 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.source;
18+
19+
// [START securitycenter_get_source_v2]
20+
21+
import com.google.cloud.securitycenter.v2.GetSourceRequest;
22+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
23+
import com.google.cloud.securitycenter.v2.Source;
24+
import com.google.cloud.securitycenter.v2.SourceName;
25+
import java.io.IOException;
26+
27+
public class GetSource {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO: Replace the below variables.
31+
// organizationId: Google Cloud Organization id.
32+
String organizationId = "{google-cloud-organization-id}";
33+
34+
// Specify the source-id.
35+
String sourceId = "{source-id}";
36+
37+
getSource(organizationId, sourceId);
38+
}
39+
40+
// Demonstrates how to retrieve a specific source.
41+
public static Source getSource(String organizationId, String sourceId) throws IOException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests.
44+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
45+
46+
// Start setting up a request to get a source.
47+
SourceName sourceName = SourceName.ofOrganizationSourceName(organizationId, sourceId);
48+
49+
GetSourceRequest request = GetSourceRequest.newBuilder()
50+
.setName(sourceName.toString())
51+
.build();
52+
53+
// Call the API.
54+
Source response = client.getSource(request);
55+
56+
System.out.println("Source: " + response);
57+
return response;
58+
}
59+
}
60+
}
61+
// [END securitycenter_get_source_v2]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.source;
18+
19+
// [START securitycenter_list_sources_v2]
20+
21+
import com.google.cloud.securitycenter.v2.OrganizationLocationName;
22+
import com.google.cloud.securitycenter.v2.OrganizationName;
23+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
24+
import com.google.cloud.securitycenter.v2.SecurityCenterClient.ListSourcesPagedResponse;
25+
import com.google.cloud.securitycenter.v2.Source;
26+
import java.io.IOException;
27+
import java.util.ArrayList;
28+
import java.util.List;
29+
30+
public class ListSources {
31+
32+
public static void main(String[] args) throws IOException {
33+
// TODO: Replace the below variables.
34+
// organizationId: Google Cloud Organization id.
35+
String organizationId = "{google-cloud-organization-id}";
36+
37+
listSources(organizationId);
38+
}
39+
40+
// Demonstrates how to list all security sources in an organization.
41+
public static List<Source> listSources(String organizationId) throws IOException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests.
44+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
45+
46+
// Start setting up a request to get a source.
47+
OrganizationName parent = OrganizationName.of(organizationId);
48+
49+
// Call the API.
50+
List<Source> sourcesList = new ArrayList<>();
51+
ListSourcesPagedResponse response = client.listSources(parent);
52+
response.iterateAll().forEach(sourcesList::add);
53+
54+
for (Source source : sourcesList) {
55+
System.out.println("List sources: " + source);
56+
}
57+
return sourcesList;
58+
}
59+
}
60+
}
61+
// [END securitycenter_list_sources_v2]
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.source;
18+
19+
// [START securitycenter_update_finding_source_properties_v2]
20+
21+
import com.google.cloud.securitycenter.v2.Finding;
22+
import com.google.cloud.securitycenter.v2.FindingName;
23+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
24+
import com.google.cloud.securitycenter.v2.UpdateFindingRequest;
25+
import com.google.protobuf.FieldMask;
26+
import com.google.protobuf.Timestamp;
27+
import com.google.protobuf.Value;
28+
import java.io.IOException;
29+
import java.time.Instant;
30+
31+
public class UpdateFindingSource {
32+
33+
public static void main(String[] args) throws IOException {
34+
// TODO: Replace the below variables.
35+
// organizationId: Google Cloud Organization id.
36+
String organizationId = "{google-cloud-organization-id}";
37+
38+
// Specify the location to list the findings.
39+
String location = "global";
40+
41+
// Specify the source-id.
42+
String sourceId = "{source-id}";
43+
44+
// Specify the finding-id.
45+
String findingId = "{finding-id}";
46+
47+
updateFinding(organizationId, location, sourceId, findingId);
48+
}
49+
50+
// Creates or updates a finding.
51+
public static Finding updateFinding(String organizationId,
52+
String location, String sourceId, String findingId) throws IOException {
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests.
55+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
56+
57+
// Instead of using the FindingName, a plain String can also be used. E.g.:
58+
// String findingName = String.format("organizations/%s/sources/%s/locations/%s/findings/%s",
59+
// organizationId, sourceId, location, findingId);
60+
FindingName findingName = FindingName
61+
.ofOrganizationSourceLocationFindingName(organizationId, sourceId, location, findingId);
62+
63+
// Use the current time as the finding "event time".
64+
Instant eventTime = Instant.now();
65+
66+
// Define source properties values as protobuf "Value" objects.
67+
Value stringValue = Value.newBuilder().setStringValue("value").build();
68+
69+
// Set the update mask to specify which properties should be updated.
70+
// If empty, all mutable fields will be updated.
71+
// For more info on constructing field mask path, see the proto or:
72+
// https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask
73+
FieldMask updateMask =
74+
FieldMask.newBuilder()
75+
.addPaths("event_time")
76+
.addPaths("source_properties.stringKey")
77+
.build();
78+
79+
Finding finding =
80+
Finding.newBuilder()
81+
.setName(findingName.toString())
82+
.setDescription("Updated finding source")
83+
.setEventTime(
84+
Timestamp.newBuilder()
85+
.setSeconds(eventTime.getEpochSecond())
86+
.setNanos(eventTime.getNano()))
87+
.putSourceProperties("stringKey", stringValue)
88+
.build();
89+
90+
UpdateFindingRequest request =
91+
UpdateFindingRequest.newBuilder()
92+
.setFinding(finding)
93+
.setUpdateMask(updateMask)
94+
.build();
95+
96+
// Call the API.
97+
Finding response = client.updateFinding(request);
98+
99+
System.out.println("Updated finding source: " + response);
100+
return response;
101+
}
102+
}
103+
}
104+
// [END securitycenter_update_finding_source_properties_v2]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.source;
18+
19+
// [START securitycenter_update_source_v2]
20+
21+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
22+
import com.google.cloud.securitycenter.v2.Source;
23+
import com.google.cloud.securitycenter.v2.SourceName;
24+
import com.google.cloud.securitycenter.v2.UpdateSourceRequest;
25+
import com.google.protobuf.FieldMask;
26+
import java.io.IOException;
27+
28+
public class UpdateSource {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO: Replace the below variables.
32+
// organizationId: Google Cloud Organization id.
33+
String organizationId = "{google-cloud-organization-id}";
34+
35+
// Specify the source-id.
36+
String sourceId = "{source-id}";
37+
38+
updateSource(organizationId, sourceId);
39+
}
40+
41+
// Demonstrates how to update a source.
42+
public static Source updateSource(String organizationId, String sourceId) throws IOException {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests.
45+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
46+
47+
// Start setting up a request to get a source.
48+
SourceName sourceName = SourceName.ofOrganizationSourceName(organizationId, sourceId);
49+
Source source = Source.newBuilder()
50+
.setDisplayName("Updated Display Name")
51+
.setName(sourceName.toString())
52+
.build();
53+
54+
// Set the update mask to specify which properties should be updated.
55+
// If empty, all mutable fields will be updated.
56+
// For more info on constructing field mask path, see the proto or:
57+
// https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask
58+
FieldMask updateMask = FieldMask.newBuilder()
59+
.addPaths("display_name")
60+
.build();
61+
62+
UpdateSourceRequest request = UpdateSourceRequest.newBuilder()
63+
.setSource(source)
64+
.setUpdateMask(updateMask)
65+
.build();
66+
67+
// Call the API.
68+
Source response = client.updateSource(request);
69+
70+
System.out.println("Updated Source: " + response);
71+
return response;
72+
}
73+
}
74+
}
75+
// [END securitycenter_update_source_v2]

0 commit comments

Comments
 (0)