Search in sources :

Example 6 with SecurityCenterClient

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

the class SecurityMarkSnippets method addToAsset.

/**
 * Add security mark to an asset.
 *
 * @param assetName The asset resource to add the security mark for.
 */
// [START securitycenter_add_security_marks]
static SecurityMarks addToAsset(String assetName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // String assetName = "organizations/123123342/assets/12312321";
        // Start setting up a request to add security marks for an asset.
        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(assetName + "/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)

Example 7 with SecurityCenterClient

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

the class SecurityMarkSnippets method clearFromAsset.

// [END securitycenter_add_security_marks]
/**
 * Clear security marks for an asset.
 *
 * @param assetName The asset resource to clear the security marks for.
 */
// [START securitycenter_delete_security_marks]
static SecurityMarks clearFromAsset(String assetName) {
    // String assetName = "organizations/123123342/assets/12312321";
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to clear security marks for an asset.
        // Create security mark and field mask for clearing security marks.
        SecurityMarks securityMarks = SecurityMarks.newBuilder().setName(assetName + "/securityMarks").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 cleared:");
        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) FieldMask(com.google.protobuf.FieldMask)

Example 8 with SecurityCenterClient

use of com.google.cloud.securitycenter.v1.SecurityCenterClient 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 9 with SecurityCenterClient

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

the class AssetSnippets method listAssetAndStatusChanges.

// [END securitycenter_list_assets_at_time]
/**
 * Returns Assets and metadata about assets activity (e.g. added, removed, no change) between
 * between <code>asOf.minus(timespan)</code> and <code>asOf</code>.
 *
 * @param timeSpan The time-range to compare assets over.
 * @param asOf The instant in time to query for. If null, current time is assumed.
 */
// [START securitycenter_list_assets_and_changes]
static ImmutableList<ListAssetsResult> listAssetAndStatusChanges(OrganizationName organizationName, Duration timeSpan, Instant asOf) {
    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()).setFilter("security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"");
        request.getCompareDurationBuilder().setSeconds(timeSpan.getSeconds()).setNanos(timeSpan.getNano());
        // Set read time to either the instant passed in or now.
        asOf = MoreObjects.firstNonNull(asOf, Instant.now());
        request.getReadTimeBuilder().setSeconds(asOf.getEpochSecond()).setNanos(asOf.getNano());
        // 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("Projects:");
        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 10 with SecurityCenterClient

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

the class AssetSnippets method listAssetsWithFilter.

// [END securitycenter_list_all_assets]
/**
 * Lists all project assets for an organization.
 *
 * @param organizationName The organization to list assets for.
 */
// [START securitycenter_list_assets_with_filter]
static ImmutableList<ListAssetsResult> listAssetsWithFilter(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()).setFilter("security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"");
        // 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("Project assets:");
        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)

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