Search in sources :

Example 31 with SecurityCenterClient

use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.

the class FindingSnippets method groupFindingsWithSource.

// [END securitycenter_group_all_findings]
/**
 * Group 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_findings_with_source]
static ImmutableList<GroupResult> groupFindingsWithSource(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");
        // 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);
    }
}
Also used : GroupFindingsRequest(com.google.cloud.securitycenter.v1.GroupFindingsRequest) GroupResult(com.google.cloud.securitycenter.v1.GroupResult) GroupFindingsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupFindingsPagedResponse) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 32 with SecurityCenterClient

use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.

the class FindingSnippets method createFinding.

/**
 * Create a finding under a source.
 *
 * @param sourceName The source for the finding.
 */
// [START securitycenter_create_finding]
static Finding createFinding(SourceName sourceName, String findingId) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
        // "423432321");
        // String findingId = "samplefindingid";
        // Use the current time as the finding "event time".
        Instant eventTime = Instant.now();
        // The resource this finding applies to.  The CSCC UI can link
        // the findings for a resource to the corresponding Asset of a resource
        // if there are matches.
        String resourceName = "//cloudresourcemanager.googleapis.com/organizations/11232";
        // Start setting up a request to create a finding in a source.
        Finding finding = Finding.newBuilder().setParent(sourceName.toString()).setState(State.ACTIVE).setResourceName(resourceName).setEventTime(Timestamp.newBuilder().setSeconds(eventTime.getEpochSecond()).setNanos(eventTime.getNano())).setCategory("MEDIUM_RISK_ONE").build();
        // Call the API.
        Finding response = client.createFinding(sourceName, findingId, finding);
        System.out.println("Created Finding: " + response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : Instant(org.threeten.bp.Instant) Finding(com.google.cloud.securitycenter.v1.Finding) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 33 with SecurityCenterClient

use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.

the class FindingSnippets method createFindingWithSourceProperties.

// [END securitycenter_create_finding]
/**
 * Create a finding with source properties under a source.
 *
 * @param sourceName The source for the finding.
 */
// [START securitycenter_create_finding_with_source_properties]
static Finding createFindingWithSourceProperties(SourceName sourceName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
        // "423432321");
        // Use the current time as the finding "event time".
        Instant eventTime = Instant.now();
        // Controlled by caller.
        String findingId = "samplefindingid2";
        // The resource this finding applies to.  The CSCC UI can link
        // the findings for a resource to the corresponding Asset of a resource
        // if there are matches.
        String resourceName = "//cloudresourcemanager.googleapis.com/organizations/11232";
        // Define source properties values as protobuf "Value" objects.
        Value stringValue = Value.newBuilder().setStringValue("stringExample").build();
        Value numValue = Value.newBuilder().setNumberValue(1234).build();
        ImmutableMap<String, Value> sourceProperties = ImmutableMap.of("stringKey", stringValue, "numKey", numValue);
        // Start setting up a request to create a finding in a source.
        Finding finding = Finding.newBuilder().setParent(sourceName.toString()).setState(State.ACTIVE).setResourceName(resourceName).setEventTime(Timestamp.newBuilder().setSeconds(eventTime.getEpochSecond()).setNanos(eventTime.getNano())).putAllSourceProperties(sourceProperties).build();
        // Call the API.
        Finding response = client.createFinding(sourceName, findingId, finding);
        System.out.println("Created Finding with Source Properties: " + response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : Instant(org.threeten.bp.Instant) Finding(com.google.cloud.securitycenter.v1.Finding) Value(com.google.protobuf.Value) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 34 with SecurityCenterClient

use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.

the class FindingSnippets method groupFindings.

// [END securitycenter_test_iam]
/**
 * Group all findings under an organization across all sources by their specified properties (e.g.
 * category).
 *
 * @param organizationName The organizatoin to group all findings for.
 */
// [START securitycenter_group_all_findings]
static ImmutableList<GroupResult> groupFindings(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // OrganizationName organizationName = OrganizationName.of("123234324");
        SourceName sourceName = SourceName.of(organizationName.getOrganization(), "-");
        GroupFindingsRequest.Builder request = GroupFindingsRequest.newBuilder().setParent(sourceName.toString()).setGroupBy("category");
        // 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);
    }
}
Also used : GroupFindingsRequest(com.google.cloud.securitycenter.v1.GroupFindingsRequest) GroupResult(com.google.cloud.securitycenter.v1.GroupResult) SourceName(com.google.cloud.securitycenter.v1.SourceName) GroupFindingsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupFindingsPagedResponse) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 35 with SecurityCenterClient

use of com.google.cloud.securitycenter.v1.SecurityCenterClient in project google-cloud-java by GoogleCloudPlatform.

the class SecurityMarkSnippets method addToFinding.

// [END securitycenter_add_delete_security_marks]
/**
 * Add security mark to a finding.
 *
 * @param findingName The finding resource path to add the security mark for.
 */
// [START securitycenter_add_finding_security_marks]
static SecurityMarks addToFinding(FindingName findingName) {
    // /*source=*/"423432321", /*findingId=*/"samplefindingid2");
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to add security marks for a finding.
        ImmutableMap markMap = ImmutableMap.of("key_a", "value_a", "key_b", "value_b");
        // Add security marks and field mask for security marks.
        SecurityMarks securityMarks = SecurityMarks.newBuilder().setName(findingName + "/securityMarks").putAllMarks(markMap).build();
        FieldMask updateMask = FieldMask.newBuilder().addPaths("marks.key_a").addPaths("marks.key_b").build();
        UpdateSecurityMarksRequest request = UpdateSecurityMarksRequest.newBuilder().setSecurityMarks(securityMarks).setUpdateMask(updateMask).build();
        // Call the API.
        SecurityMarks response = client.updateSecurityMarks(request);
        System.out.println("Security Marks:");
        System.out.println(response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : SecurityMarks(com.google.cloud.securitycenter.v1.SecurityMarks) UpdateSecurityMarksRequest(com.google.cloud.securitycenter.v1.UpdateSecurityMarksRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) ImmutableMap(com.google.common.collect.ImmutableMap) FieldMask(com.google.protobuf.FieldMask)

Aggregations

SecurityCenterClient (com.google.cloud.securitycenter.v1.SecurityCenterClient)35 IOException (java.io.IOException)35 GroupResult (com.google.cloud.securitycenter.v1.GroupResult)8 FieldMask (com.google.protobuf.FieldMask)7 Instant (org.threeten.bp.Instant)6 GroupFindingsRequest (com.google.cloud.securitycenter.v1.GroupFindingsRequest)5 ListAssetsRequest (com.google.cloud.securitycenter.v1.ListAssetsRequest)5 ListAssetsResult (com.google.cloud.securitycenter.v1.ListAssetsResponse.ListAssetsResult)5 GroupFindingsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupFindingsPagedResponse)5 ListAssetsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.ListAssetsPagedResponse)5 Finding (com.google.cloud.securitycenter.v1.Finding)4 ListFindingsRequest (com.google.cloud.securitycenter.v1.ListFindingsRequest)4 ListFindingsResult (com.google.cloud.securitycenter.v1.ListFindingsResponse.ListFindingsResult)4 ListFindingsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.ListFindingsPagedResponse)4 SecurityMarks (com.google.cloud.securitycenter.v1.SecurityMarks)4 Source (com.google.cloud.securitycenter.v1.Source)4 UpdateSecurityMarksRequest (com.google.cloud.securitycenter.v1.UpdateSecurityMarksRequest)4 GroupAssetsRequest (com.google.cloud.securitycenter.v1.GroupAssetsRequest)3 GroupAssetsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupAssetsPagedResponse)3 OrganizationSettings (com.google.cloud.securitycenter.v1.OrganizationSettings)2