Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a heads up that the SCC Assets API is deprecated and will be replaced by integrating Cloud Asset Inventory's API in SCC. The CAI replacement is expected to GA at the beginning of August. Existing users have a year to move off of the SCC API. SCC's Assets API is disabled for all new users who activate SCC after last week's deprecation notice. New users can only use CAI.

Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@
import org.threeten.bp.Duration;
import org.threeten.bp.Instant;

/** Snippets for how to work with Assets in Cloud Security Command Center. */
/**
* Snippets for how to work with Assets in Cloud Security Command Center.
*/
public class AssetSnippets {
private AssetSnippets() {}

private AssetSnippets() {
}

/**
* Lists all assets for an organization.
Expand All @@ -46,8 +50,12 @@ private AssetSnippets() {}
// [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");
// Start setting up a request for to search for all assets in an organization/ project/ folder.
Comment thread
Sita04 marked this conversation as resolved.
Outdated
//
// Parent must be in one of the following formats:
// OrganizationName organizationName = OrganizationName.of("organization-id");
// ProjectName projectName = ProjectName.of("project-id");
// FolderName folderName = FolderName.of("folder-id");
ListAssetsRequest.Builder request =
ListAssetsRequest.newBuilder().setParent(organizationName.toString());

Expand Down Expand Up @@ -75,8 +83,12 @@ static ImmutableList<ListAssetsResult> listAssets(OrganizationName organizationN
// [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");
// Start setting up a request for to search for all assets in an organization/ project/ folder.
//
// Parent must be in one of the following formats:
// OrganizationName organizationName = OrganizationName.of("organization-id");
// ProjectName projectName = ProjectName.of("project-id");
// FolderName folderName = FolderName.of("folder-id");
ListAssetsRequest.Builder request =
ListAssetsRequest.newBuilder()
.setParent(organizationName.toString())
Expand Down Expand Up @@ -109,10 +121,13 @@ static ImmutableList<ListAssetsResult> listAssetsWithFilter(OrganizationName org
static ImmutableList<ListAssetsResult> listAssetsAsOfYesterday(
OrganizationName organizationName, 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");

// Initialize the builder with the organization and filter
// Start setting up a request for to search for all assets in an organization/ project/ folder.
//
// Parent must be in one of the following formats:
// OrganizationName organizationName = OrganizationName.of("organization-id");
// ProjectName projectName = ProjectName.of("project-id");
// FolderName folderName = FolderName.of("folder-id");
// Initialize the builder with the parent and filter
ListAssetsRequest.Builder request =
ListAssetsRequest.newBuilder()
.setParent(organizationName.toString())
Expand Down Expand Up @@ -151,8 +166,12 @@ 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");
// Start setting up a request for to search for all assets in an organization/ project/ folder.
//
// Parent must be in one of the following formats:
// OrganizationName organizationName = OrganizationName.of("organization-id");
// ProjectName projectName = ProjectName.of("project-id");
// FolderName folderName = FolderName.of("folder-id");
ListAssetsRequest.Builder request =
ListAssetsRequest.newBuilder()
.setParent(organizationName.toString())
Expand Down Expand Up @@ -191,8 +210,12 @@ static ImmutableList<ListAssetsResult> listAssetAndStatusChanges(
// [START securitycenter_group_all_assets]
static ImmutableList<GroupResult> groupAssets(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Start setting up a request for to group all assets by type in an organization.
// OrganizationName organizationName = OrganizationName.of("123234324");
// Start setting up a request for to group all assets by type in an organization/ project/ folder.
//
// Parent must be in one of the following formats:
// OrganizationName organizationName = OrganizationName.of("organization-id");
// ProjectName projectName = ProjectName.of("project-id");
// FolderName folderName = FolderName.of("folder-id");
GroupAssetsRequest.Builder request =
GroupAssetsRequest.newBuilder()
.setGroupBy("security_center_properties.resource_type")
Expand Down Expand Up @@ -225,7 +248,8 @@ static ImmutableList<GroupResult> groupAssetsWithFilter(OrganizationName organiz
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Start setting up a request for to filter all assets by type and group them by project in an
// organization.
// OrganizationName organizationName = OrganizationName.of("123234324");
// You can also use a project/ folder as a parent resource and filter assests in them
// respectively.
GroupAssetsRequest.Builder request =
GroupAssetsRequest.newBuilder()
.setFilter(
Expand Down Expand Up @@ -260,8 +284,12 @@ static ImmutableList<GroupResult> groupAssetsWithCompareDuration(
OrganizationName organizationName, Duration duration) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Start setting up a request for to group all assets during a period of time in an
// organization.
// OrganizationName organizationName = OrganizationName.of("123234324");
// organization/ project/ folder.
//
// Parent must be in one of the following formats:
// OrganizationName organizationName = OrganizationName.of("organization-id");
// ProjectName projectName = ProjectName.of("project-id");
// FolderName folderName = FolderName.of("folder-id");
GroupAssetsRequest.Builder request =
GroupAssetsRequest.newBuilder()
.setGroupBy("state_change")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@
import org.threeten.bp.Duration;
import org.threeten.bp.Instant;

/** Snippets for how to work with Findings in Cloud Security Command Center. */
/**
* Snippets for how to work with Findings in Cloud Security Command Center.
*/
public class FindingSnippets {
private FindingSnippets() {}

private FindingSnippets() {
}

/**
* Create a finding under a source.
Expand Down Expand Up @@ -226,7 +230,14 @@ static Finding setFindingState(FindingName findingName) {
// [START securitycenter_list_all_findings]
static ImmutableList<ListFindingsResult> listAllFindings(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
// SourceName must be in one of the following formats:
Comment thread
Sita04 marked this conversation as resolved.
Outdated
// * OrganizationName organizationName = OrganizationName.of("organization-id");
// organizationName.getOrganization();
// * ProjectName projectName = ProjectName.of("project-id");
// projectName.getProject();
// * FolderName folderName = FolderName.of("folder-id");
// folderName.getFolder();
//
// "-" Indicates listing across all sources.
SourceName sourceName = SourceName.of(organizationName.getOrganization(), "-");

Expand Down Expand Up @@ -257,8 +268,11 @@ static ImmutableList<ListFindingsResult> listAllFindings(OrganizationName organi
// [START securitycenter_list_filtered_findings]
static ImmutableList<ListFindingsResult> listFilteredFindings(SourceName sourceName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// SourceName sourceName = SourceName.of(/*organizationId=*/"123234324",
// /*sourceId=*/"423432321");
// parentId: must be one of the following:
// "organization-id"
// "project-id"
// "folder-id"
// SourceName sourceName = SourceName.of(parentId, sourceId);

// Create filter to category of MEDIUM_RISK_ONE
String filter = "category=\"MEDIUM_RISK_ONE\"";
Expand Down Expand Up @@ -290,8 +304,11 @@ static ImmutableList<ListFindingsResult> listFilteredFindings(SourceName sourceN
// [START securitycenter_list_findings_at_time]
static ImmutableList<ListFindingsResult> listFindingsAtTime(SourceName sourceName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// SourceName sourceName = SourceName.of(/*organizationId=*/"123234324",
// /*sourceId=*/"423432321");
// parentId: must be one of the following:
// "organization-id"
// "project-id"
// "folder-id"
// SourceName sourceName = SourceName.of(parentId, sourceId);

// 5 days ago
Instant fiveDaysAgo = Instant.now().minus(Duration.ofDays(5));
Expand Down Expand Up @@ -353,12 +370,18 @@ static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) {
* 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.
* @param organizationName The organization to group all findings for.
Comment thread
Sita04 marked this conversation as resolved.
Outdated
*/
// [START securitycenter_group_all_findings]
static ImmutableList<GroupResult> groupFindings(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// OrganizationName organizationName = OrganizationName.of("123234324");
// SourceName must be in one of the following formats:
// * OrganizationName organizationName = OrganizationName.of("organization-id");
// organizationName.getOrganization();
// * ProjectName projectName = ProjectName.of("project-id");
// projectName.getProject();
// * FolderName folderName = FolderName.of("folder-id");
// folderName.getFolder();
SourceName sourceName = SourceName.of(organizationName.getOrganization(), "-");

GroupFindingsRequest.Builder request =
Expand Down Expand Up @@ -390,8 +413,11 @@ static ImmutableList<GroupResult> groupFindings(OrganizationName organizationNam
// [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");
// parentId: must be one of the following:
// "organization-id"
// "project-id"
// "folder-id"
// SourceName sourceName = SourceName.of(parentId, sourceId);

GroupFindingsRequest.Builder request =
GroupFindingsRequest.newBuilder().setParent(sourceName.toString()).setGroupBy("category");
Expand Down Expand Up @@ -422,8 +448,11 @@ static ImmutableList<GroupResult> groupFindingsWithSource(SourceName sourceName)
// [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");
// parentId: must be one of the following:
// "organization-id"
// "project-id"
// "folder-id"
// SourceName sourceName = SourceName.of(parentId, sourceId);

GroupFindingsRequest.Builder request =
GroupFindingsRequest.newBuilder()
Expand Down Expand Up @@ -457,8 +486,11 @@ static ImmutableList<GroupResult> groupActiveFindingsWithSource(SourceName sourc
// [START securitycenter_group_active_findings_with_source_at_time]
static ImmutableList<GroupResult> groupActiveFindingsWithSourceAtTime(SourceName sourceName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
// "423432321");
// parentId: must be one of the following:
// "organization-id"
// "project-id"
// "folder-id"
// SourceName sourceName = SourceName.of(parentId, sourceId);

// 1 day ago
Instant oneDayAgo = Instant.now().minusSeconds(60 * 60 * 24);
Expand Down Expand Up @@ -500,8 +532,11 @@ static ImmutableList<GroupResult> groupActiveFindingsWithSourceAtTime(SourceName
static ImmutableList<GroupResult> groupActiveFindingsWithSourceAndCompareDuration(
SourceName sourceName, Duration duration) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
// "423432321");
// parentId: must be one of the following:
// "organization-id"
// "project-id"
// "folder-id"
// SourceName sourceName = SourceName.of(parentId, sourceId);

GroupFindingsRequest.Builder request =
GroupFindingsRequest.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@

public class SecurityMarkSnippets {

private SecurityMarkSnippets() {}
private SecurityMarkSnippets() {
}

/**
* Add security mark to an asset.
Expand All @@ -44,7 +45,11 @@ private SecurityMarkSnippets() {}
// [START securitycenter_add_security_marks]
static SecurityMarks addToAsset(String assetName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// String assetName = "organizations/123123342/assets/12312321";
// assetName: must be in one of the following formats:
Comment thread
Sita04 marked this conversation as resolved.
Outdated
// String assetName = "organizations/{org-id}/assets/{asset-id}";
// String assetName = "projects/{project-id}/assets/{asset-id}";
// String assetName = "folders/{folder-id}/assets/{asset-id}";
//
// Start setting up a request to add security marks for an asset.
ImmutableMap markMap = ImmutableMap.of("key_a", "value_a", "key_b", "value_b");

Expand Down Expand Up @@ -82,8 +87,11 @@ static SecurityMarks addToAsset(String assetName) {
*/
// [START securitycenter_delete_security_marks]
static SecurityMarks clearFromAsset(String assetName) {
// String assetName = "organizations/123123342/assets/12312321";
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// assetName: must be in one of the following formats:
// String assetName = "organizations/{org-id}/assets/{asset-id}";
// String assetName = "projects/{project-id}/assets/{asset-id}";
// String assetName = "folders/{folder-id}/assets/{asset-id}";
// Start setting up a request to clear security marks for an asset.
// Create security mark and field mask for clearing security marks.
SecurityMarks securityMarks =
Expand Down Expand Up @@ -116,8 +124,11 @@ static SecurityMarks clearFromAsset(String assetName) {
*/
// [START securitycenter_add_delete_security_marks]
static SecurityMarks deleteAndUpdateMarks(String assetName) {
// String assetName = "organizations/123123342/assets/12312321";
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// assetName: must be in one of the following formats:
// String assetName = "organizations/{org-id}/assets/{asset-id}";
// String assetName = "projects/{project-id}/assets/{asset-id}";
// String assetName = "folders/{folder-id}/assets/{asset-id}";
// Start setting up a request to clear and update security marks for an asset.
// Create security mark and field mask for clearing security marks.
SecurityMarks securityMarks =
Expand Down Expand Up @@ -153,12 +164,14 @@ static SecurityMarks deleteAndUpdateMarks(String assetName) {
*/
// [START securitycenter_add_finding_security_marks]
static SecurityMarks addToFinding(FindingName findingName) {
// FindingName findingName = FindingName.of(/*organization=*/"123234324",
// /*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");

// findingName: must in one of the following formats:
// FindingName.ofOrganizationSourceFindingName("org-id", "source", "finding-id");
// FindingName.ofProjectSourceFindingName("project-id", "source", "finding-id");
// FindingName.ofFolderSourceFindingName("folder-id", "source", "finding-id");
// Add security marks and field mask for security marks.
SecurityMarks securityMarks =
SecurityMarks.newBuilder()
Expand Down Expand Up @@ -196,7 +209,10 @@ 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");
// Parent must be in one of the following formats:
// OrganizationName organizationName = OrganizationName.of("organization-id");
// ProjectName projectName = ProjectName.of("project-id");
// FolderName folderName = FolderName.of("folder-id");
ListAssetsRequest request =
ListAssetsRequest.newBuilder()
.setParent(organizationName.toString())
Expand Down Expand Up @@ -228,8 +244,14 @@ static ImmutableList<ListAssetsResult> listAssetsWithQueryMarks(
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");
// SourceName must be in one of the following formats:
Comment thread
Sita04 marked this conversation as resolved.
Outdated
// * OrganizationName organizationName = OrganizationName.of("organization-id");
// String parent = organizationName.getOrganization();
// * ProjectName projectName = ProjectName.of("project-id");
// String parent = projectName.getProject();
// * FolderName folderName = FolderName.of("folder-id");
// String parent = folderName.getFolder();
// SourceName sourceName = SourceName.of(parent, {source-id});

String filter = "NOT security_marks.marks.key_a=\"value_a\"";

Expand Down
Loading