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
@@ -0,0 +1,63 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package vtwo.muteconfig;
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: rename to v2.muteconfig.

question: I see that there is a muteconfig package already in the parent java/ folder. How do we plan to disambiguate between the two packages?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Every existing file under security-command-center/snippets/src/main/java/vtwo follows the naming convention that I've used here. The existing files all use a package name that starts with vtwo, not v2. Also, the existing files often have package names that overlap with the parent java/ folder.

Can I ask that you allow me to add this sample with the package name vtwo.muteconfig, and that you work with the sample owners (the Security Command Center product team) to improve the package names as a follow-on task?

@owenhuyn FYI.


// [START securitycenter_set_mute_undefined_v2]

import com.google.cloud.securitycenter.v2.Finding;
import com.google.cloud.securitycenter.v2.Finding.Mute;
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
import com.google.cloud.securitycenter.v2.SetMuteRequest;
import java.io.IOException;

public class SetMuteUndefinedFinding {

public static void main(String[] args) throws IOException {
// TODO: Replace the variables within {}

// findingPath: The relative resource name of the finding. See:
// https://cloud.google.com/apis/design/resource_names#relative_resource_name
// Use any one of the following formats:
// - organizations/{organization_id}/sources/{source_id}/finding/{finding_id}
// - folders/{folder_id}/sources/{source_id}/finding/{finding_id}
// - projects/{project_id}/sources/{source_id}/finding/{finding_id}
String findingPath = "{path-to-the-finding}";
setMuteUndefined(findingPath);
}

// Reset mute state of an individual finding.
// If a finding is already reset, resetting it again has no effect.
// Various mute states are: MUTE_UNSPECIFIED/MUTE/UNMUTE/UNDEFINED.
public static Finding setMuteUndefined(String findingPath) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: don't return an object, instead process the result in the sample.

See https://googlecloudplatform.github.io/samples-style-guide/#result.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this repo's SAMPLE_FORMAT.md file, it says the opposite:

Snippet methods should return data that can be used in the calling method to show the user how to interact with a returned object programmatically.

In #9547, I tried to log information instead of returning a result, as you're suggesting, and I was told to return a result instead.

I'd like to keep this code as-is, given that it's consistent with the instructions in this repo.

Copy link
Contributor

Choose a reason for hiding this comment

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

There is a discrepancy between the Samples Style Guide and the guidance in this repo. We're working behind the scenes to sort it out. This is okay for now.

// Initialize client that will be used to send requests. This client only needs
// to be created once, and can be reused for multiple requests.
try (SecurityCenterClient client = SecurityCenterClient.create()) {

SetMuteRequest setMuteRequest =
SetMuteRequest.newBuilder()
.setName(findingPath)
.setMute(Mute.UNDEFINED)
.build();

Finding finding = client.setMute(setMuteRequest);
System.out.println(
"Mute value for the finding " + finding.getName() + " is: " + finding.getMute());
return finding;
}
}
}
// [END securitycenter_set_mute_undefined_v2]
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import vtwo.muteconfig.GetMuteRule;
import vtwo.muteconfig.ListMuteRules;
import vtwo.muteconfig.SetMuteFinding;
import vtwo.muteconfig.SetMuteUndefinedFinding;
import vtwo.muteconfig.SetUnmuteFinding;
import vtwo.muteconfig.UpdateMuteRule;
import vtwo.source.CreateSource;
Expand All @@ -74,9 +75,8 @@ public class MuteFindingIT {
private static ByteArrayOutputStream stdOut;

@Rule
public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(
MAX_ATTEMPT_COUNT,
INITIAL_BACKOFF_MILLIS);
public final MultipleAttemptsRule multipleAttemptsRule =
new MultipleAttemptsRule(MAX_ATTEMPT_COUNT, INITIAL_BACKOFF_MILLIS);

// Check if the required environment variables are set.
public static void requireEnvVar(String envVarName) {
Expand Down Expand Up @@ -104,12 +104,22 @@ public static void setUp() throws IOException, InterruptedException {

// Create findings within the source.
String uuid = UUID.randomUUID().toString().split("-")[0];
FINDING_1 = CreateFindings.createFinding(ORGANIZATION_ID, LOCATION, "testfindingv2" + uuid,
SOURCE.getName().split("/")[3], Optional.of("MEDIUM_RISK_ONE"));
FINDING_1 =
CreateFindings.createFinding(
ORGANIZATION_ID,
LOCATION,
"testfindingv2" + uuid,
SOURCE.getName().split("/")[3],
Optional.of("MEDIUM_RISK_ONE"));

uuid = UUID.randomUUID().toString().split("-")[0];
FINDING_2 = CreateFindings.createFinding(ORGANIZATION_ID, LOCATION, "testfindingv2" + uuid,
SOURCE.getName().split("/")[3], Optional.empty());
FINDING_2 =
CreateFindings.createFinding(
ORGANIZATION_ID,
LOCATION,
"testfindingv2" + uuid,
SOURCE.getName().split("/")[3],
Optional.empty());

stdOut = null;
System.setOut(out);
Expand All @@ -132,9 +142,7 @@ public static void cleanUp() throws IOException {
public static ListFindingsPagedResponse getAllFindings(String sourceName) throws IOException {
try (SecurityCenterClient client = SecurityCenterClient.create()) {

ListFindingsRequest request = ListFindingsRequest.newBuilder()
.setParent(sourceName)
.build();
ListFindingsRequest request = ListFindingsRequest.newBuilder().setParent(sourceName).build();

return client.listFindings(request);
}
Expand Down Expand Up @@ -173,18 +181,20 @@ public void testUpdateMuteRules() throws IOException {
}

@Test
public void testMuteUnmuteFinding() throws IOException {
public void testSetMuteFinding() throws IOException {
Finding finding = SetMuteFinding.setMute(FINDING_1.getName());
assertThat(finding.getMute()).isEqualTo(Mute.MUTED);
finding = SetUnmuteFinding.setUnmute(FINDING_1.getName());
assertThat(finding.getMute()).isEqualTo(Mute.UNMUTED);
finding = SetMuteUndefinedFinding.setMuteUndefined(FINDING_1.getName());
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: don't check for a result; instead parse stdout to ensure that something was printed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd like to keep this code as-is, given that it's consistent with the instructions in this repo.

assertThat(finding.getMute()).isEqualTo(Mute.UNDEFINED);
}

@Test
public void testBulkMuteFindings() throws IOException, ExecutionException, InterruptedException {
// Mute findings that belong to this project.
BulkMuteFindings.bulkMute(PROJECT_ID, LOCATION,
String.format("resource.project_display_name=\"%s\"", PROJECT_ID));
BulkMuteFindings.bulkMute(
PROJECT_ID, LOCATION, String.format("resource.project_display_name=\"%s\"", PROJECT_ID));

// Get all findings in the source to check if they are muted.
ListFindingsPagedResponse response =
Expand Down