Search in sources :

Example 6 with SourceName

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

the class SecurityMarkSnippets method listFindingsWithQueryMarks.

// [END securitycenter_list_assets_with_security_marks]
/**
 * List all findings with a filter on security marks.
 *
 * @param sourceName The source to list filtered findings for.
 */
// [START securitycenter_list_findings_with_security_marks]
static ImmutableList<ListFindingsResult> listFindingsWithQueryMarks(SourceName sourceName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request for to list all findings filtered by a specific security mark.
        // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
        // "423432321");
        String filter = "NOT security_marks.marks.key_a=\"value_a\"";
        ListFindingsRequest.Builder request = ListFindingsRequest.newBuilder().setParent(sourceName.toString()).setFilter(filter);
        // Call the API.
        ListFindingsPagedResponse response = client.listFindings(request.build());
        // This creates one list for all findings in the filter.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<ListFindingsResult> results = ImmutableList.copyOf(response.iterateAll());
        System.out.println("Findings with security mark - key_a=value_a:");
        System.out.println(results);
        return results;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : ListFindingsRequest(com.google.cloud.securitycenter.v1.ListFindingsRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) ListFindingsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.ListFindingsPagedResponse) ListFindingsResult(com.google.cloud.securitycenter.v1.ListFindingsResponse.ListFindingsResult)

Example 7 with SourceName

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

the class SourceSnippets method setIamPolicySource.

// [END securitycenter_get_source]
/**
 * Set IAM policy for a source.
 *
 * @param sourceName The source to set IAM Policy for.
 */
// [START securitycenter_set_source_iam]
static Policy setIamPolicySource(SourceName sourceName, String userEmail) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // userEmail = "someuser@domain.com"
        // Set up IAM Policy for the user userMail to use the role findingsEditor.
        // The user must be a valid google account.
        Policy oldPolicy = client.getIamPolicy(sourceName.toString());
        Binding bindings = Binding.newBuilder().setRole("roles/securitycenter.findingsEditor").addMembers("user:" + userEmail).build();
        Policy policy = oldPolicy.toBuilder().addBindings(bindings).build();
        // Start setting up a request to set IAM policy for a source.
        // SourceName sourceName = SourceName.of("123234324", "423432321");
        SetIamPolicyRequest.Builder request = SetIamPolicyRequest.newBuilder().setPolicy(policy).setResource(sourceName.toString());
        // Call the API.
        Policy response = client.setIamPolicy(request.build());
        System.out.println("Policy: " + response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) SetIamPolicyRequest(com.google.iam.v1.SetIamPolicyRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 8 with SourceName

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

the class SourceSnippets method updateSource.

// [END securitycenter_list_sources]
/**
 * Update a source under an organization.
 *
 * @param sourceName The source to update.
 */
// [START securitycenter_update_source]
static Source updateSource(SourceName sourceName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to update a source.
        // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
        // "423432321");
        Source source = Source.newBuilder().setDisplayName("Updated Display Name").setName(sourceName.toString()).build();
        FieldMask updateMask = FieldMask.newBuilder().addPaths("display_name").build();
        UpdateSourceRequest.Builder request = UpdateSourceRequest.newBuilder().setSource(source).setUpdateMask(updateMask);
        // Call the API.
        Source response = client.updateSource(request.build());
        System.out.println("Updated Source: " + response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : UpdateSourceRequest(com.google.cloud.securitycenter.v1.UpdateSourceRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) Source(com.google.cloud.securitycenter.v1.Source) FieldMask(com.google.protobuf.FieldMask)

Example 9 with SourceName

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

the class SourceSnippets method getIamPolicySource.

// [END securitycenter_set_source_iam]
/**
 * Get IAM policy for a source.
 *
 * @param sourceName The source to set IAM Policy for.
 */
// [START securitycenter_get_source_iam]
static Policy getIamPolicySource(SourceName sourceName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to get IAM policy for a source.
        // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
        // "423432321");
        GetIamPolicyRequest request = GetIamPolicyRequest.newBuilder().setResource(sourceName.toString()).build();
        // Call the API.
        Policy response = client.getIamPolicy(request);
        System.out.println("Policy: " + response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : Policy(com.google.iam.v1.Policy) IOException(java.io.IOException) GetIamPolicyRequest(com.google.iam.v1.GetIamPolicyRequest) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 10 with SourceName

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

the class SourceSnippets method getSource.

// [END securitycenter_update_source]
/**
 * Get a source under an organization.
 *
 * @param sourceName The source to get.
 */
// [START securitycenter_get_source]
static Source getSource(SourceName sourceName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to get a source.
        // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
        // "423432321");
        GetSourceRequest.Builder request = GetSourceRequest.newBuilder().setName(sourceName.toString());
        // Call the API.
        Source response = client.getSource(request.build());
        System.out.println("Source: " + response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : GetSourceRequest(com.google.cloud.securitycenter.v1.GetSourceRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) Source(com.google.cloud.securitycenter.v1.Source)

Aggregations

SecurityCenterClient (com.google.cloud.securitycenter.v1.SecurityCenterClient)16 IOException (java.io.IOException)16 GroupFindingsRequest (com.google.cloud.securitycenter.v1.GroupFindingsRequest)5 GroupResult (com.google.cloud.securitycenter.v1.GroupResult)5 GroupFindingsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupFindingsPagedResponse)5 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 SourceName (org.orcid.jaxb.model.common_v2.SourceName)4 Test (org.junit.Test)3 Source (org.orcid.jaxb.model.common_v2.Source)3 Instant (org.threeten.bp.Instant)3 Finding (com.google.cloud.securitycenter.v1.Finding)2 Source (com.google.cloud.securitycenter.v1.Source)2 SourceName (com.google.cloud.securitycenter.v1.SourceName)2 Policy (com.google.iam.v1.Policy)2 ArrayList (java.util.ArrayList)2 SourceClientId (org.orcid.jaxb.model.common_v2.SourceClientId)2 ExternalIDs (org.orcid.jaxb.model.record_v2.ExternalIDs)2 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)2