use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.
the class FindingSnippets method listAllFindings.
// [END securitycenter_update_finding_state]
/**
* List all findings under an organization.
*
* @param organizationName The source to list all findings for.
*/
// [START securitycenter_list_all_findings]
static ImmutableList<ListFindingsResult> listAllFindings(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
// "-" Indicates listing across all sources.
SourceName sourceName = SourceName.of(organizationName.getOrganization(), "-");
ListFindingsRequest.Builder request = ListFindingsRequest.newBuilder().setParent(sourceName.toString());
// Call the API.
ListFindingsPagedResponse response = client.listFindings(request.build());
// This creates one list for all findings. If your organization has a large number of
// findings this can cause out of memory issues. You can process them in incrementally
// by returning the Iterable returned response.iterateAll() directly.
ImmutableList<ListFindingsResult> results = ImmutableList.copyOf(response.iterateAll());
System.out.println("Findings:");
System.out.println(results);
return results;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.
the class FindingSnippets method groupActiveFindingsWithSource.
// [END securitycenter_group_findings_with_source]
/**
* Group active findings under an organization and a source by their specified properties (e.g.
* category).
*
* @param sourceName The source to limit the findings to.
*/
// [START securitycenter_group_active_findings_with_source]
static ImmutableList<GroupResult> groupActiveFindingsWithSource(SourceName sourceName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
// "423432321");
GroupFindingsRequest.Builder request = GroupFindingsRequest.newBuilder().setParent(sourceName.toString()).setGroupBy("category").setFilter("state=\"ACTIVE\"");
// Call the API.
GroupFindingsPagedResponse response = client.groupFindings(request.build());
// This creates one list for all findings. If your organization has a large number of
// findings
// this can cause out of memory issues. You can process them batches by returning
// the Iterable returned response.iterateAll() directly.
ImmutableList<GroupResult> results = ImmutableList.copyOf(response.iterateAll());
System.out.println("Findings:");
System.out.println(results);
return results;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.
the class FindingSnippets method updateFinding.
// [END securitycenter_create_finding_with_source_properties]
/**
* Update a finding's source properties.
*
* @param findingName The finding to update.
*/
// [START securitycenter_update_finding_source_properties]
static Finding updateFinding(FindingName findingName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// FindingName findingName = FindingName.of(/*organization=*/"123234324",
// /*source=*/"423432321", /*findingId=*/"samplefindingid2");
// 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();
FieldMask updateMask = FieldMask.newBuilder().addPaths("event_time").addPaths("source_properties.stringKey").build();
Finding finding = Finding.newBuilder().setName(findingName.toString()).setEventTime(Timestamp.newBuilder().setSeconds(eventTime.getEpochSecond()).setNanos(eventTime.getNano())).putSourceProperties("stringKey", stringValue).build();
UpdateFindingRequest.Builder request = UpdateFindingRequest.newBuilder().setFinding(finding).setUpdateMask(updateMask);
// Call the API.
Finding response = client.updateFinding(request.build());
System.out.println("Updated Finding: " + response);
return response;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.
the class AssetSnippets method listAssets.
/**
* Lists all assets for an organization.
*
* @param organizationName The organization to list assets for.
*/
// [START securitycenter_list_all_assets]
static ImmutableList<ListAssetsResult> listAssets(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Start setting up a request for to search for all assets in an organization.
// OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
ListAssetsRequest.Builder request = ListAssetsRequest.newBuilder().setParent(organizationName.toString());
// Call the API.
ListAssetsPagedResponse response = client.listAssets(request.build());
// This creates one list for all assets. If your organization has a large number of assets
// this can cause out of memory issues. You can process them incrementally by returning
// the Iterable returned response.iterateAll() directly.
ImmutableList<ListAssetsResult> results = ImmutableList.copyOf(response.iterateAll());
System.out.println("All assets:");
System.out.println(results);
return results;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.
the class AssetSnippets method runAssetDiscovery.
// [END securitycenter_group_all_assets_with_compare_duration]
// [START securitycenter_run_asset_discovery]
static void runAssetDiscovery(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Call the API. Note calls to runAssetDiscovery are throttled if too many requests
// are made.
OperationFuture<RunAssetDiscoveryResponse, Empty> result = client.runAssetDiscoveryAsync(organizationName);
// Uncomment this line to wait for a certain amount of time for the asset discovery run
// to complete.
// result.get(130, TimeUnit.SECONDS);
System.out.println("Asset discovery runs asynchronously.");
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
} catch (ResourceExhaustedException e) {
System.out.println("Asset discovery run already in progress.");
}
}
Aggregations