Skip to content

Commit 66fabfc

Browse files
feat: add v2 API for muteconfig Go client libraries or samples (#4268)
* Add muteconfig v2 * Add Resource mute config v2 * Address comments * go mod tidy * Remove bulk mute sample due to server side issue --------- Co-authored-by: Deleplace <[email protected]>
1 parent 8486f14 commit 66fabfc

File tree

8 files changed

+707
-0
lines changed

8 files changed

+707
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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_create_mute_config_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+
// createMuteRule: Creates a mute configuration under a given scope that will mute
29+
// all new findings that match a given filter.
30+
// Existing findings will not be muted.
31+
func createMuteRule(w io.Writer, parent string, muteConfigId 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+
// muteConfigId: Set a random id; max of 63 chars.
38+
// muteConfigId := "random-mute-id-" + uuid.New().String()
39+
ctx := context.Background()
40+
client, err := securitycenter.NewClient(ctx)
41+
if err != nil {
42+
return fmt.Errorf("securitycenter.NewClient: %w", err)
43+
}
44+
defer client.Close()
45+
46+
muteConfig := &securitycenterpb.MuteConfig{
47+
Description: "Mute low-medium IAM grants excluding 'compute' ",
48+
// Set mute rule(s).
49+
// To construct mute rules and for supported properties, see:
50+
// https://cloud.google.com/security-command-center/docs/how-to-mute-findings#create_mute_rules
51+
Filter: "severity=\"LOW\" OR severity=\"MEDIUM\" AND " +
52+
"category=\"Persistence: IAM Anomalous Grant\" AND " +
53+
"-resource.type:\"compute\"",
54+
Type: securitycenterpb.MuteConfig_STATIC,
55+
}
56+
57+
req := &securitycenterpb.CreateMuteConfigRequest{
58+
Parent: parent,
59+
MuteConfigId: muteConfigId,
60+
MuteConfig: muteConfig,
61+
}
62+
63+
response, err := client.CreateMuteConfig(ctx, req)
64+
if err != nil {
65+
return fmt.Errorf("failed to create mute rule: %w", err)
66+
}
67+
fmt.Fprintf(w, "Mute rule created successfully: %s", response.Name)
68+
return nil
69+
}
70+
71+
// [END securitycenter_create_mute_config_v2]
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 muteconfigv2
16+
17+
// [START securitycenter_delete_mute_config_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+
// deleteMuteRule deletes a mute configuration given its resource name.
29+
// Note: Previously muted findings are not affected when a mute config is deleted.
30+
func deleteMuteRule(w io.Writer, parent string, muteConfigId string) error {
31+
// parent: Use any one of the following options:
32+
// - organizations/{organization_id}
33+
// - folders/{folder_id}
34+
// - projects/{project_id}
35+
// parent := fmt.Sprintf("projects/%s", "your-google-cloud-project-id")
36+
//
37+
// muteConfigId: Specify the name of the mute config to delete.
38+
// muteConfigId := "mute-config-id"
39+
ctx := context.Background()
40+
client, err := securitycenter.NewClient(ctx)
41+
if err != nil {
42+
return fmt.Errorf("securitycenter.NewClient: %w", err)
43+
}
44+
defer client.Close()
45+
46+
req := &securitycenterpb.DeleteMuteConfigRequest{
47+
Name: fmt.Sprintf("%s/muteConfigs/%s", parent, muteConfigId),
48+
}
49+
50+
if err := client.DeleteMuteConfig(ctx, req); err != nil {
51+
return fmt.Errorf("failed to delete Muteconfig: %w", err)
52+
}
53+
fmt.Fprintf(w, "Mute rule deleted successfully: %s", muteConfigId)
54+
return nil
55+
}
56+
57+
// [END securitycenter_delete_mute_config_v2]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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_get_mute_config_v2]
18+
import (
19+
"context"
20+
"fmt"
21+
"io"
22+
23+
securitycenter "cloud.google.com/go/securitycenter/apiv2"
24+
"cloud.google.com/go/securitycenter/apiv2/securitycenterpb"
25+
)
26+
27+
// getMuteRule retrieves a mute configuration given its resource name.
28+
func getMuteRule(w io.Writer, parent string, muteConfigId string) error {
29+
// Use any one of the following resource paths to get mute configuration:
30+
// - organizations/{organization_id}
31+
// - folders/{folder_id}
32+
// - projects/{project_id}
33+
// parent := fmt.Sprintf("projects/%s", "your-google-cloud-project-id")
34+
//
35+
// Name of the mute config to retrieve.
36+
// muteConfigId := "mute-config-id"
37+
ctx := context.Background()
38+
client, err := securitycenter.NewClient(ctx)
39+
if err != nil {
40+
return fmt.Errorf("securitycenter.NewClient: %w", err)
41+
}
42+
defer client.Close()
43+
44+
req := &securitycenterpb.GetMuteConfigRequest{
45+
Name: fmt.Sprintf("%s/muteConfigs/%s", parent, muteConfigId),
46+
}
47+
48+
muteconfig, err := client.GetMuteConfig(ctx, req)
49+
if err != nil {
50+
return fmt.Errorf("Failed to retrieve Muteconfig: %w", err)
51+
}
52+
fmt.Fprintf(w, "Muteconfig Name: %s ", muteconfig.Name)
53+
return nil
54+
}
55+
56+
// [END securitycenter_get_mute_config_v2]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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_list_mute_configs_v2]
18+
import (
19+
"context"
20+
"fmt"
21+
"io"
22+
23+
securitycenter "cloud.google.com/go/securitycenter/apiv2"
24+
"cloud.google.com/go/securitycenter/apiv2/securitycenterpb"
25+
"google.golang.org/api/iterator"
26+
)
27+
28+
// listMuteRules lists mute configs at the organization level will return all the configs
29+
// at the org, folder, and project levels.
30+
// Similarly, listing configs at folder level will list all the configs
31+
// at the folder and project levels.
32+
func listMuteRules(w io.Writer, parent string) error {
33+
// Use any one of the following resource paths to list mute configurations:
34+
// - organizations/{organization_id}
35+
// - folders/{folder_id}
36+
// - projects/{project_id}
37+
// parent := fmt.Sprintf("projects/%s", "your-google-cloud-project-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.ListMuteConfigsRequest{Parent: parent}
46+
47+
// List all mute configs present in the resource.
48+
it := client.ListMuteConfigs(ctx, req)
49+
for {
50+
muteconfig, err := it.Next()
51+
if err == iterator.Done {
52+
break
53+
}
54+
if err != nil {
55+
return fmt.Errorf("it.Next: %w", err)
56+
}
57+
fmt.Fprintf(w, "Muteconfig Name: %s, ", muteconfig.Name)
58+
}
59+
return nil
60+
}
61+
62+
// [END securitycenter_list_mute_configs_v2]

0 commit comments

Comments
 (0)