|
| 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] |
0 commit comments