Search in sources :

Example 31 with OrganizationName

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

the class SecurityMarkSnippets method listAssetsWithQueryMarks.

// [END securitycenter_add_finding_security_marks]
/**
 * Lists all assets with a filter on security marks.
 *
 * @param organizationName The organization to list assets for.
 */
// [START securitycenter_list_assets_with_security_marks]
static ImmutableList<ListAssetsResult> listAssetsWithQueryMarks(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request for to list all assets filtered by a specific security mark.
        // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
        ListAssetsRequest request = ListAssetsRequest.newBuilder().setParent(organizationName.toString()).setFilter("security_marks.marks.key_a = \"value_a\"").build();
        // Call the API.
        ListAssetsPagedResponse response = client.listAssets(request);
        // 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 batches by returning
        // the Iterable returned response.iterateAll() directly.
        ImmutableList<ListAssetsResult> results = ImmutableList.copyOf(response.iterateAll());
        System.out.println("Assets 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 : ListAssetsRequest(com.google.cloud.securitycenter.v1.ListAssetsRequest) ListAssetsResult(com.google.cloud.securitycenter.v1.ListAssetsResponse.ListAssetsResult) ListAssetsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.ListAssetsPagedResponse) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 32 with OrganizationName

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

the class SourceSnippets method createSource.

/**
 * Create a source under an organization.
 *
 * @param organizationName The organization for the source.
 */
// [START securitycenter_create_source]
static Source createSource(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to create a source in an organization.
        // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
        Source source = Source.newBuilder().setDisplayName("Customized Display Name").setDescription("A new custom source that does X").build();
        CreateSourceRequest.Builder request = CreateSourceRequest.newBuilder().setParent(organizationName.toString()).setSource(source);
        // Call the API.
        Source response = client.createSource(request.build());
        System.out.println("Created Source: " + response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) CreateSourceRequest(com.google.cloud.securitycenter.v1.CreateSourceRequest) Source(com.google.cloud.securitycenter.v1.Source)

Example 33 with OrganizationName

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

the class SourceSnippets method listSources.

// [END securitycenter_create_source]
/**
 * List sources under an organization.
 *
 * @param organizationName The organization for the source.
 */
// [START securitycenter_list_sources]
static ImmutableList<Source> listSources(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to list sources in an organization.
        // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
        ListSourcesRequest.Builder request = ListSourcesRequest.newBuilder().setParent(organizationName.toString());
        // Call the API.
        ListSourcesPagedResponse response = client.listSources(request.build());
        // This creates one list for all sources.  If your organization has a large number of sources
        // this can cause out of memory issues.  You can process them batches by returning
        // the Iterable returned response.iterateAll() directly.
        ImmutableList<Source> results = ImmutableList.copyOf(response.iterateAll());
        System.out.println("Sources:");
        System.out.println(results);
        return results;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : ListSourcesRequest(com.google.cloud.securitycenter.v1.ListSourcesRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) ListSourcesPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.ListSourcesPagedResponse) Source(com.google.cloud.securitycenter.v1.Source)

Example 34 with OrganizationName

use of com.google.cloud.securitycenter.v1.OrganizationName in project java-securitycenter by googleapis.

the class SecurityCenterClientTest method listBigQueryExportsTest2.

@Test
public void listBigQueryExportsTest2() throws Exception {
    BigQueryExport responsesElement = BigQueryExport.newBuilder().build();
    ListBigQueryExportsResponse expectedResponse = ListBigQueryExportsResponse.newBuilder().setNextPageToken("").addAllBigQueryExports(Arrays.asList(responsesElement)).build();
    mockSecurityCenter.addResponse(expectedResponse);
    OrganizationName parent = OrganizationName.of("[ORGANIZATION]");
    ListBigQueryExportsPagedResponse pagedListResponse = client.listBigQueryExports(parent);
    List<BigQueryExport> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getBigQueryExportsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockSecurityCenter.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListBigQueryExportsRequest actualRequest = ((ListBigQueryExportsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListBigQueryExportsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.ListBigQueryExportsPagedResponse) Test(org.junit.Test)

Example 35 with OrganizationName

use of com.google.cloud.securitycenter.v1.OrganizationName in project java-securitycenter by googleapis.

the class SecurityCenterClientTest method listSourcesTest2.

@Test
public void listSourcesTest2() throws Exception {
    Source responsesElement = Source.newBuilder().build();
    ListSourcesResponse expectedResponse = ListSourcesResponse.newBuilder().setNextPageToken("").addAllSources(Arrays.asList(responsesElement)).build();
    mockSecurityCenter.addResponse(expectedResponse);
    OrganizationName parent = OrganizationName.of("[ORGANIZATION]");
    ListSourcesPagedResponse pagedListResponse = client.listSources(parent);
    List<Source> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getSourcesList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockSecurityCenter.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListSourcesRequest actualRequest = ((ListSourcesRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListSourcesPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.ListSourcesPagedResponse) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)66 AbstractMessage (com.google.protobuf.AbstractMessage)35 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)31 SecurityCenterClient (com.google.cloud.securitycenter.v1.SecurityCenterClient)31 StatusRuntimeException (io.grpc.StatusRuntimeException)31 OrganizationName (com.google.monitoring.v3.OrganizationName)30 IOException (java.io.IOException)30 OrganizationName (com.google.logging.v2.OrganizationName)22 OrganizationName (com.google.privacy.dlp.v2.OrganizationName)12 ListAssetsRequest (com.google.cloud.securitycenter.v1.ListAssetsRequest)10 ListAssetsResult (com.google.cloud.securitycenter.v1.ListAssetsResponse.ListAssetsResult)10 ListAssetsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.ListAssetsPagedResponse)10 GroupResult (com.google.cloud.securitycenter.v1.GroupResult)8 LogExclusion (com.google.logging.v2.LogExclusion)7 LogSink (com.google.logging.v2.LogSink)7 ArrayList (java.util.ArrayList)7 GroupAssetsRequest (com.google.cloud.securitycenter.v1.GroupAssetsRequest)6 GroupAssetsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupAssetsPagedResponse)6 MetricDescriptor (com.google.api.MetricDescriptor)3 MockGrpcService (com.google.api.gax.grpc.testing.MockGrpcService)3