Skip to content

Commit c4ce89c

Browse files
authored
feat: add v2 API config for bulk mute finding to the existing mute config Go client library or samples (#4379)
* Add bulk mute finding config
1 parent 746ae5d commit c4ce89c

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 muteconfigv2
16+
17+
// [START securitycenter_bulk_mute_v2]
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"io"
23+
24+
securitycenter "cloud.google.com/go/securitycenter/apiv2"
25+
"cloud.google.com/go/securitycenter/apiv2/securitycenterpb"
26+
)
27+
28+
// bulkMute kicks off a long-running operation (LRO) to bulk mute findings for a parent based on a filter.
29+
// The parent can be either an organization, folder, or project. The findings
30+
// matched by the filter will be muted after the LRO is done.
31+
func bulkMute(w io.Writer, parent string, muteRule string) error {
32+
// parent: Use any one of the following options:
33+
// - organizations/{organization_id}
34+
// - folders/{folder_id}
35+
// - projects/{project_id}
36+
// parent := fmt.Sprintf("projects/%s", "your-google-cloud-project-id")
37+
// muteRule: Expression that identifies findings that should be muted.
38+
// To create mute rules, see:
39+
// https://cloud.google.com/security-command-center/docs/how-to-mute-findings#create_mute_rules
40+
// muteRule := "filter-condition"
41+
ctx := context.Background()
42+
client, err := securitycenter.NewClient(ctx)
43+
if err != nil {
44+
return fmt.Errorf("securitycenter.NewClient: %w", err)
45+
}
46+
defer client.Close()
47+
48+
req := &securitycenterpb.BulkMuteFindingsRequest{
49+
Parent: parent,
50+
Filter: muteRule,
51+
}
52+
53+
op, err := client.BulkMuteFindings(ctx, req)
54+
if err != nil {
55+
return fmt.Errorf("failed to bulk mute findings: %w", err)
56+
}
57+
response, err := op.Wait(ctx)
58+
if err != nil {
59+
return fmt.Errorf("failed to bulk mute findings: %w", err)
60+
}
61+
fmt.Fprintf(w, "Bulk mute findings completed successfully! %s", response)
62+
return nil
63+
}
64+
65+
// [END securitycenter_bulk_mute_v2]

securitycenter/muteconfigv2/mute_config_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,16 @@ func TestSetUnmuteFinding(t *testing.T) {
271271
t.Errorf("setUnmute got %q, expected %q", got, fmt.Sprintf("Mute value for the finding: %s is %s", fixture.finding1Name, "UNMUTE"))
272272
}
273273
}
274+
275+
func TestBulkMuteFinding(t *testing.T) {
276+
testutil.SystemTest(t)
277+
278+
var buf bytes.Buffer
279+
// Bulk mute findings.
280+
if err := bulkMute(&buf, fixture.parent, "severity=\"LOW\""); err != nil {
281+
t.Errorf("bulkMute had error: %v", err)
282+
}
283+
if got := buf.String(); !strings.Contains(got, "Bulk mute findings completed successfully") {
284+
t.Errorf("bulkMute got %q, expected %q", got, "Bulk mute findings completed successfully")
285+
}
286+
}

0 commit comments

Comments
 (0)