Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private FindingSnippets() {
*
* @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=*/
Expand Down Expand Up @@ -89,12 +90,14 @@ static Finding createFinding(SourceName sourceName, String findingId) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [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=*/
Expand Down Expand Up @@ -139,12 +142,14 @@ static Finding createFindingWithSourceProperties(SourceName sourceName) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END securitycenter_create_finding_with_source_properties]

/**
* Update a finding's source properties.
*
* @param findingName The finding to update.
*/
// [START securitycenter_update_finding_source_properties]
static Finding updateFinding(FindingName findingName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// FindingName findingName = FindingName.of(/*organization=*/"123234324",
Expand Down Expand Up @@ -184,12 +189,14 @@ static Finding updateFinding(FindingName findingName) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END securitycenter_update_finding_source_properties]

/**
* Updates a finding's state to INACTIVE.
*
* @param findingName The finding to update.
*/
// [START securitycenter_update_finding_state]
static Finding setFindingState(FindingName findingName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// FindingName findingName = FindingName.of(/*organization=*/"123234324",
Expand All @@ -213,12 +220,14 @@ static Finding setFindingState(FindingName findingName) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END securitycenter_update_finding_state]

/**
* List all findings under an organization.
*
* @param organizationName The source to list all findings for.
*/
// [START securitycenter_list_all_findings]
static ImmutableList<ListFindingsResult> listAllFindings(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Input parameters for SourceName must be in one of the following formats:
Expand Down Expand Up @@ -249,12 +258,14 @@ static ImmutableList<ListFindingsResult> listAllFindings(OrganizationName organi
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END securitycenter_list_all_findings]

/**
* List filtered findings under a source.
*
* @param sourceName The source to list filtered findings for.
*/
// [START securitycenter_list_filtered_findings]
static ImmutableList<ListFindingsResult> listFilteredFindings(SourceName sourceName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// parentId: must be one of the following:
Expand Down Expand Up @@ -283,6 +294,7 @@ static ImmutableList<ListFindingsResult> listFilteredFindings(SourceName sourceN
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END securitycenter_list_filtered_findings]

/**
* List findings at a specific time under a source.
Expand Down Expand Up @@ -329,6 +341,7 @@ static ImmutableList<ListFindingsResult> listFindingsAtTime(SourceName sourceNam
*
* @param sourceName The source to create a finding for.
*/
// [START securitycenter_test_iam]
static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// SourceName sourceName = SourceName.of(/*organizationId=*/"123234324",
Expand All @@ -349,13 +362,15 @@ static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END securitycenter_test_iam]

/**
* Group all findings under an organization across all sources by their specified properties (e.g.
* category).
*
* @param organizationName The organization to group all findings for.
*/
// [START securitycenter_group_all_findings]
static ImmutableList<GroupResult> groupFindings(OrganizationName organizationName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Input parameters for 'SourceName' must be in one of the following formats:
Expand Down Expand Up @@ -385,13 +400,15 @@ static ImmutableList<GroupResult> groupFindings(OrganizationName organizationNam
throw new RuntimeException("Couldn't create client.", e);
}
}
// [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()) {
// parentId: must be one of the following:
Expand All @@ -418,13 +435,15 @@ static ImmutableList<GroupResult> groupFindingsWithSource(SourceName sourceName)
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END securitycenter_group_findings_with_source]

/**
* Group active 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_active_findings_with_source]
static ImmutableList<GroupResult> groupActiveFindingsWithSource(SourceName sourceName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// parentId: must be one of the following:
Expand Down Expand Up @@ -454,13 +473,15 @@ static ImmutableList<GroupResult> groupActiveFindingsWithSource(SourceName sourc
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END securitycenter_group_active_findings_with_source]

/**
* Group active findings under an organization and a source by their specified properties (e.g.
* category) at a specified time.
*
* @param sourceName The source to limit the findings to.
*/
// [START securitycenter_group_active_findings_with_source_at_time]
static ImmutableList<GroupResult> groupActiveFindingsWithSourceAtTime(SourceName sourceName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// parentId: must be one of the following:
Expand Down Expand Up @@ -497,13 +518,15 @@ static ImmutableList<GroupResult> groupActiveFindingsWithSourceAtTime(SourceName
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END securitycenter_group_active_findings_with_source_at_time]

/**
* Group active findings under an organization and a source by their state_changes
* (ADDED/CHANGED/UNCHANGED) during a period.
*
* @param sourceName The source to limit the findings to.
*/
// [START securitycenter_group_active_findings_with_source_and_compare_duration]
static ImmutableList<GroupResult> groupActiveFindingsWithSourceAndCompareDuration(
SourceName sourceName, Duration duration) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
Expand Down Expand Up @@ -538,4 +561,5 @@ static ImmutableList<GroupResult> groupActiveFindingsWithSourceAndCompareDuratio
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END securitycenter_group_active_findings_with_source_and_compare_duration]
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private SourceSnippets() {
*
* @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.
Expand All @@ -68,12 +69,14 @@ static Source createSource(OrganizationName organizationName) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [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, project, or folder.
Expand All @@ -98,12 +101,14 @@ static ImmutableList<Source> listSources(OrganizationName organizationName) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [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.
Expand All @@ -128,12 +133,14 @@ static Source updateSource(SourceName sourceName) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [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.
Expand All @@ -151,12 +158,14 @@ static Source getSource(SourceName sourceName) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [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"
Expand Down Expand Up @@ -184,12 +193,14 @@ static Policy setIamPolicySource(SourceName sourceName, String userEmail) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [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.
Expand All @@ -207,5 +218,6 @@ static Policy getIamPolicySource(SourceName sourceName) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END securitycenter_get_source_iam]

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.google.cloud.securitycenter.v1.Finding.State;
import com.google.cloud.securitycenter.v1.ListFindingsRequest;
import com.google.cloud.securitycenter.v1.ListFindingsResponse.ListFindingsResult;
import com.google.cloud.securitycenter.v1.ListMuteConfigsRequest;
import com.google.cloud.securitycenter.v1.MuteConfig;
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
import com.google.cloud.securitycenter.v1.SecurityCenterClient.ListFindingsPagedResponse;
import com.google.cloud.securitycenter.v1.Source;
Expand Down Expand Up @@ -76,16 +78,41 @@ public static void requireEnvVar(String envVarName) {
.isNotEmpty();
}

private static void cleanUpExistingMuteRules() {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
ListMuteConfigsRequest request =
ListMuteConfigsRequest.newBuilder()
.setParent(String.format("projects/%s", PROJECT_ID))
.build();
for (MuteConfig muteConfig : client.listMuteConfigs(request).iterateAll()) {
String name = muteConfig.getName();
String muteConfigId = name.substring(name.lastIndexOf('/') + 1);
if (muteConfigId.startsWith("random-mute-id-")) {
try {
client.deleteMuteConfig(name);
} catch (Exception e) {
System.err.println("Failed to delete mute rule: " + name);
e.printStackTrace();
}
}
}
} catch (Exception e) {
System.err.println("Warning: Pre-test cleanup of mute rules failed: " + e.getMessage());
}
}

@BeforeClass
public static void setUp() throws IOException {
final PrintStream out = System.out;
stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));

requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
requireEnvVar("SCC_PROJECT_ID");
requireEnvVar("SCC_PROJECT_ORG_ID");

cleanUpExistingMuteRules();

final PrintStream out = System.out;
stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));

// Create mute rules.
CreateMuteRule.createMuteRule(String.format("projects/%s", PROJECT_ID), MUTE_RULE_CREATE);
CreateMuteRule.createMuteRule(String.format("projects/%s", PROJECT_ID), MUTE_RULE_UPDATE);
Expand All @@ -106,10 +133,19 @@ public static void cleanUp() {
final PrintStream out = System.out;
stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));
DeleteMuteRule.deleteMuteRule(PROJECT_ID, MUTE_RULE_CREATE);
assertThat(stdOut.toString()).contains("Mute rule deleted successfully: " + MUTE_RULE_CREATE);
DeleteMuteRule.deleteMuteRule(PROJECT_ID, MUTE_RULE_UPDATE);
assertThat(stdOut.toString()).contains("Mute rule deleted successfully: " + MUTE_RULE_UPDATE);
try {
DeleteMuteRule.deleteMuteRule(PROJECT_ID, MUTE_RULE_CREATE);
} catch (Exception e) {
System.err.println("Failed to delete mute rule: " + MUTE_RULE_CREATE);
e.printStackTrace();
}
try {
DeleteMuteRule.deleteMuteRule(PROJECT_ID, MUTE_RULE_UPDATE);
} catch (Exception e) {
System.err.println("Failed to delete mute rule: " + MUTE_RULE_UPDATE);
e.printStackTrace();
}
cleanUpExistingMuteRules();
stdOut = null;
System.setOut(out);
}
Expand Down
Loading