-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding API v2 Source samples (#9299)
* add sources samples * feat: sources samples v2 * improve readability * improve comments * adjustments for pull request
- Loading branch information
Rafael Murata
committed
May 23, 2024
1 parent
4182434
commit 2b43cb4
Showing
5 changed files
with
452 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
security-command-center/snippets/src/main/java/vtwo/source/GetSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.source; | ||
|
||
// [START securitycenter_get_source_v2] | ||
|
||
import com.google.cloud.securitycenter.v2.GetSourceRequest; | ||
import com.google.cloud.securitycenter.v2.SecurityCenterClient; | ||
import com.google.cloud.securitycenter.v2.Source; | ||
import com.google.cloud.securitycenter.v2.SourceName; | ||
import java.io.IOException; | ||
|
||
public class GetSource { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO: Replace the below variables. | ||
// organizationId: Google Cloud Organization id. | ||
String organizationId = "{google-cloud-organization-id}"; | ||
|
||
// Specify the source-id. | ||
String sourceId = "{source-id}"; | ||
|
||
getSource(organizationId, sourceId); | ||
} | ||
|
||
// Demonstrates how to retrieve a specific source. | ||
public static Source getSource(String organizationId, String sourceId) 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. | ||
try (SecurityCenterClient client = SecurityCenterClient.create()) { | ||
|
||
// Start setting up a request to get a source. | ||
SourceName sourceName = SourceName.ofOrganizationSourceName(organizationId, sourceId); | ||
|
||
GetSourceRequest request = GetSourceRequest.newBuilder() | ||
.setName(sourceName.toString()) | ||
.build(); | ||
|
||
// Call the API. | ||
Source response = client.getSource(request); | ||
|
||
System.out.println("Source: " + response); | ||
return response; | ||
} | ||
} | ||
} | ||
// [END securitycenter_get_source_v2] |
61 changes: 61 additions & 0 deletions
61
security-command-center/snippets/src/main/java/vtwo/source/ListSources.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.source; | ||
|
||
// [START securitycenter_list_sources_v2] | ||
|
||
import com.google.cloud.securitycenter.v2.OrganizationLocationName; | ||
import com.google.cloud.securitycenter.v2.OrganizationName; | ||
import com.google.cloud.securitycenter.v2.SecurityCenterClient; | ||
import com.google.cloud.securitycenter.v2.SecurityCenterClient.ListSourcesPagedResponse; | ||
import com.google.cloud.securitycenter.v2.Source; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ListSources { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO: Replace the below variables. | ||
// organizationId: Google Cloud Organization id. | ||
String organizationId = "{google-cloud-organization-id}"; | ||
|
||
listSources(organizationId); | ||
} | ||
|
||
// Demonstrates how to list all security sources in an organization. | ||
public static List<Source> listSources(String organizationId) 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. | ||
try (SecurityCenterClient client = SecurityCenterClient.create()) { | ||
|
||
// Start setting up a request to get a source. | ||
OrganizationName parent = OrganizationName.of(organizationId); | ||
|
||
// Call the API. | ||
List<Source> sourcesList = new ArrayList<>(); | ||
ListSourcesPagedResponse response = client.listSources(parent); | ||
response.iterateAll().forEach(sourcesList::add); | ||
|
||
for (Source source : sourcesList) { | ||
System.out.println("List sources: " + source); | ||
} | ||
return sourcesList; | ||
} | ||
} | ||
} | ||
// [END securitycenter_list_sources_v2] |
104 changes: 104 additions & 0 deletions
104
security-command-center/snippets/src/main/java/vtwo/source/UpdateFindingSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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.source; | ||
|
||
// [START securitycenter_update_finding_source_properties_v2] | ||
|
||
import com.google.cloud.securitycenter.v2.Finding; | ||
import com.google.cloud.securitycenter.v2.FindingName; | ||
import com.google.cloud.securitycenter.v2.SecurityCenterClient; | ||
import com.google.cloud.securitycenter.v2.UpdateFindingRequest; | ||
import com.google.protobuf.FieldMask; | ||
import com.google.protobuf.Timestamp; | ||
import com.google.protobuf.Value; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
|
||
public class UpdateFindingSource { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO: Replace the below variables. | ||
// organizationId: Google Cloud Organization id. | ||
String organizationId = "{google-cloud-organization-id}"; | ||
|
||
// Specify the location to list the findings. | ||
String location = "global"; | ||
|
||
// Specify the source-id. | ||
String sourceId = "{source-id}"; | ||
|
||
// Specify the finding-id. | ||
String findingId = "{finding-id}"; | ||
|
||
updateFinding(organizationId, location, sourceId, findingId); | ||
} | ||
|
||
// Creates or updates a finding. | ||
public static Finding updateFinding(String organizationId, | ||
String location, String sourceId, String findingId) 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. | ||
try (SecurityCenterClient client = SecurityCenterClient.create()) { | ||
|
||
// Instead of using the FindingName, a plain String can also be used. E.g.: | ||
// String findingName = String.format("organizations/%s/sources/%s/locations/%s/findings/%s", | ||
// organizationId, sourceId, location, findingId); | ||
FindingName findingName = FindingName | ||
.ofOrganizationSourceLocationFindingName(organizationId, sourceId, location, findingId); | ||
|
||
// Use the current time as the finding "event time". | ||
Instant eventTime = Instant.now(); | ||
|
||
// Define source properties values as protobuf "Value" objects. | ||
Value stringValue = Value.newBuilder().setStringValue("value").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("event_time") | ||
.addPaths("source_properties.stringKey") | ||
.build(); | ||
|
||
Finding finding = | ||
Finding.newBuilder() | ||
.setName(findingName.toString()) | ||
.setDescription("Updated finding source") | ||
.setEventTime( | ||
Timestamp.newBuilder() | ||
.setSeconds(eventTime.getEpochSecond()) | ||
.setNanos(eventTime.getNano())) | ||
.putSourceProperties("stringKey", stringValue) | ||
.build(); | ||
|
||
UpdateFindingRequest request = | ||
UpdateFindingRequest.newBuilder() | ||
.setFinding(finding) | ||
.setUpdateMask(updateMask) | ||
.build(); | ||
|
||
// Call the API. | ||
Finding response = client.updateFinding(request); | ||
|
||
System.out.println("Updated finding source: " + response); | ||
return response; | ||
} | ||
} | ||
} | ||
// [END securitycenter_update_finding_source_properties_v2] |
75 changes: 75 additions & 0 deletions
75
security-command-center/snippets/src/main/java/vtwo/source/UpdateSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* 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.source; | ||
|
||
// [START securitycenter_update_source_v2] | ||
|
||
import com.google.cloud.securitycenter.v2.SecurityCenterClient; | ||
import com.google.cloud.securitycenter.v2.Source; | ||
import com.google.cloud.securitycenter.v2.SourceName; | ||
import com.google.cloud.securitycenter.v2.UpdateSourceRequest; | ||
import com.google.protobuf.FieldMask; | ||
import java.io.IOException; | ||
|
||
public class UpdateSource { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO: Replace the below variables. | ||
// organizationId: Google Cloud Organization id. | ||
String organizationId = "{google-cloud-organization-id}"; | ||
|
||
// Specify the source-id. | ||
String sourceId = "{source-id}"; | ||
|
||
updateSource(organizationId, sourceId); | ||
} | ||
|
||
// Demonstrates how to update a source. | ||
public static Source updateSource(String organizationId, String sourceId) 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. | ||
try (SecurityCenterClient client = SecurityCenterClient.create()) { | ||
|
||
// Start setting up a request to get a source. | ||
SourceName sourceName = SourceName.ofOrganizationSourceName(organizationId, sourceId); | ||
Source source = Source.newBuilder() | ||
.setDisplayName("Updated Display Name") | ||
.setName(sourceName.toString()) | ||
.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("display_name") | ||
.build(); | ||
|
||
UpdateSourceRequest request = UpdateSourceRequest.newBuilder() | ||
.setSource(source) | ||
.setUpdateMask(updateMask) | ||
.build(); | ||
|
||
// Call the API. | ||
Source response = client.updateSource(request); | ||
|
||
System.out.println("Updated Source: " + response); | ||
return response; | ||
} | ||
} | ||
} | ||
// [END securitycenter_update_source_v2] |
Oops, something went wrong.