Skip to content

Commit 8486f14

Browse files
docs: Create set_mute_undefined_finding sample (#4309)
* docs: Create set_mute_undefined_finding sample Create set_mute_undefined_finding sample for use case: resetting an individual finding's mute state to UNDEFINED. * docs: Create set_mute_undefined sample Add code sample for resetting finding mute state to UNDEFINED. * Update function name to match sample context * Update set_unmute_finding.go * docs: Update set_mute_finding.go * fix undefined type * Update mute_config_test.go * Update mute_config_test.go * chore: skip test possibly related to #3793 --------- Co-authored-by: Tony Pujals <[email protected]> Co-authored-by: Tony Pujals <[email protected]>
1 parent 96ceea1 commit 8486f14

File tree

4 files changed

+84
-7
lines changed

4 files changed

+84
-7
lines changed

securitycenter/muteconfig/mute_config_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ func TestSetMuteFinding(t *testing.T) {
252252

253253
func TestSetUnmuteFinding(t *testing.T) {
254254
t.Skip("see https://github.com/GoogleCloudPlatform/golang-samples/issues/3793")
255+
// Needs more investigation (doesn't match on missing `locations/global`)
256+
// got: Mute value for the finding: organizations/688851828130/sources/14743348522722609714/locations/global/findings/updated is UNDEFINED
257+
// expected: Mute value for the finding: organizations/688851828130/sources/14743348522722609714/findings/updated is UNDEFINED
255258
testutil.SystemTest(t)
256259

257260
var buf bytes.Buffer
@@ -265,8 +268,25 @@ func TestSetUnmuteFinding(t *testing.T) {
265268
if err := setUnmute(&buf, fixture.finding2Name); err != nil {
266269
t.Errorf("setUnmute had error: %v", err)
267270
}
268-
if got := buf.String(); !strings.Contains(got, fmt.Sprintf("Mute value for the finding: %s is %s", fixture.finding1Name, "UNMUTE")) {
269-
t.Errorf("setUnmute got %q, expected %q", got, fmt.Sprintf("Mute value for the finding: %s is %s", fixture.finding1Name, "UNMUTE"))
271+
if got := buf.String(); !strings.Contains(got, fmt.Sprintf("Mute value for the finding: %s is %s", fixture.finding2Name, "UNMUTE")) {
272+
t.Errorf("setUnmute got %q, expected %q", got, fmt.Sprintf("Mute value for the finding: %s is %s", fixture.finding2Name, "UNMUTE"))
273+
}
274+
}
275+
276+
func TestSetMuteUndefinedFinding(t *testing.T) {
277+
t.Skip("see https://github.com/GoogleCloudPlatform/golang-samples/issues/3793")
278+
// Needs more investigation (doesn't match on missing `locations/global`)
279+
// got: Mute value for the finding: organizations/688851828130/sources/14743348522722609714/locations/global/findings/updated is UNDEFINED
280+
// expected: Mute value for the finding: organizations/688851828130/sources/14743348522722609714/findings/updated is UNDEFINED
281+
testutil.SystemTest(t)
282+
283+
var buf bytes.Buffer
284+
// Reset an individual finding mute state to UNDEFINED.
285+
if err := setMuteUndefined(&buf, fixture.finding1Name); err != nil {
286+
t.Errorf("setMuteUndefined had error: %v", err)
287+
}
288+
if got := buf.String(); !strings.Contains(got, fmt.Sprintf("Mute value for the finding: %s is %s", fixture.finding1Name, "UNDEFINED")) {
289+
t.Errorf("setMuteUndefined got %q, expected %q", got, fmt.Sprintf("Mute value for the finding: %s is %s", fixture.finding1Name, "UNDEFINED"))
270290
}
271291
}
272292

securitycenter/muteconfig/set_mute_finding.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424
"cloud.google.com/go/securitycenter/apiv1/securitycenterpb"
2525
)
2626

27-
// setMute mutes an individual finding.
27+
// setMute mutes an individual finding, can also unmute or reset the mute state of a finding.
2828
// If a finding is already muted, muting it again has no effect.
29-
// Various mute states are: MUTE_UNSPECIFIED/MUTE/UNMUTE.
29+
// Various mute states are: UNDEFINED/MUTE/UNMUTE.
3030
func setMute(w io.Writer, findingPath string) error {
3131
// findingPath: The relative resource name of the finding. See:
3232
// https://cloud.google.com/apis/design/resource_names#relative_resource_name
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package muteconfig
16+
17+
// [START securitycenter_set_mute_undefined]
18+
import (
19+
"context"
20+
"fmt"
21+
"io"
22+
23+
securitycenter "cloud.google.com/go/securitycenter/apiv1"
24+
"cloud.google.com/go/securitycenter/apiv1/securitycenterpb"
25+
)
26+
27+
// setMute mutes an individual finding, can also unmute or reset the mute state of a finding.
28+
// If a finding is already muted, muting it again has no effect.
29+
// Various mute states are: UNDEFINED/MUTE/UNMUTE.
30+
func setMuteUndefined(w io.Writer, findingPath string) error {
31+
// findingPath: The relative resource name of the finding. See:
32+
// https://cloud.google.com/apis/design/resource_names#relative_resource_name
33+
// Use any one of the following formats:
34+
// - organizations/{organization_id}/sources/{source_id}/finding/{finding_id}
35+
// - folders/{folder_id}/sources/{source_id}/finding/{finding_id}
36+
// - projects/{project_id}/sources/{source_id}/finding/{finding_id}
37+
// findingPath := fmt.Sprintf("projects/%s/sources/%s/finding/%s", "your-google-cloud-project-id", "source", "finding-id")
38+
ctx := context.Background()
39+
client, err := securitycenter.NewClient(ctx)
40+
if err != nil {
41+
return fmt.Errorf("securitycenter.NewClient: %w", err)
42+
}
43+
defer client.Close()
44+
45+
req := &securitycenterpb.SetMuteRequest{
46+
Name: findingPath,
47+
Mute: securitycenterpb.Finding_UNDEFINED}
48+
49+
finding, err := client.SetMute(ctx, req)
50+
if err != nil {
51+
return fmt.Errorf("failed to set the specified mute value: %w", err)
52+
}
53+
fmt.Fprintf(w, "Mute value for the finding: %s is %s", finding.Name, finding.Mute)
54+
return nil
55+
}
56+
57+
// [END securitycenter_set_mute_undefined]

securitycenter/muteconfig/set_unmute_finding.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import (
2525
"cloud.google.com/go/securitycenter/apiv1/securitycenterpb"
2626
)
2727

28-
// setUnmute unmutes an individual finding.
29-
// Unmuting a finding that isn't muted has no effect.
30-
// Various mute states are: MUTE_UNSPECIFIED/MUTE/UNMUTE.
28+
// setMute mutes an individual finding, can also unmute or reset the mute state of a finding.
29+
// If a finding is already muted, muting it again has no effect.
30+
// Various mute states are: UNDEFINED/MUTE/UNMUTE.
3131
func setUnmute(w io.Writer, findingPath string) error {
3232
// findingPath: The relative resource name of the finding. See:
3333
// https://cloud.google.com/apis/design/resource_names#relative_resource_name

0 commit comments

Comments
 (0)