演示如何删除忽略规则
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
Go
如需向 Security Command Center 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
import (
"context"
"fmt"
"io"
securitycenter "cloud.google.com/go/securitycenter/apiv1"
"cloud.google.com/go/securitycenter/apiv1/securitycenterpb"
)
// deleteMuteRule deletes a mute configuration given its resource name.
// Note: Previously muted findings are not affected when a mute config is deleted.
func deleteMuteRule(w io.Writer, parent string, muteConfigId string) error {
// parent: Use any one of the following options:
// - organizations/{organization_id}
// - folders/{folder_id}
// - projects/{project_id}
// parent := fmt.Sprintf("projects/%s", "your-google-cloud-project-id")
//
// muteConfigId: Specify the name of the mute config to delete.
// muteConfigId := "mute-config-id"
ctx := context.Background()
client, err := securitycenter.NewClient(ctx)
if err != nil {
return fmt.Errorf("securitycenter.NewClient: %w", err)
}
defer client.Close()
req := &securitycenterpb.DeleteMuteConfigRequest{
Name: fmt.Sprintf("%s/muteConfigs/%s", parent, muteConfigId),
}
if err := client.DeleteMuteConfig(ctx, req); err != nil {
return fmt.Errorf("failed to delete Muteconfig: %w", err)
}
fmt.Fprintf(w, "Mute rule deleted successfully: %s", muteConfigId)
return nil
}
Java
如需向 Security Command Center 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
import com.google.cloud.securitycenter.v1.MuteConfigName;
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
import java.io.IOException;
public class DeleteMuteRule {
public static void main(String[] args) {
// TODO(Developer): Replace the following variables
// parentPath: Use any one of the following options:
// - organizations/{organization_id}
// - folders/{folder_id}
// - projects/{project_id}
String parentPath = String.format("projects/%s", "your-google-cloud-project-id");
// muteConfigId: Specify the name of the mute config to delete.
String muteConfigId = "mute-config-id";
deleteMuteRule(parentPath, muteConfigId);
}
// Deletes a mute configuration given its resource name.
// Note: Previously muted findings are not affected when a mute config is deleted.
public static void deleteMuteRule(String projectId, String muteConfigId) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// Use appropriate MuteConfigName methods depending on the type of parent.
// org -> MuteConfigName.ofOrganizationMuteConfigName()
// folder -> MuteConfigName.ofFolderMuteConfigName()
// project -> MuteConfigName.ofProjectMuteConfigName)
client.deleteMuteConfig(MuteConfigName.ofProjectMuteConfigName(projectId, muteConfigId));
System.out.println("Mute rule deleted successfully: " + muteConfigId);
} catch (IOException e) {
System.out.println("Mute rule deletion failed! \n Exception: " + e);
}
}
}
Python
如需向 Security Command Center 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
def delete_mute_rule(mute_config_name: str) -> None:
"""
Deletes a mute configuration given its resource name.
Note: Previously muted findings are not affected when a mute config is deleted.
Args:
mute_config_name: Specify the name of the mute config to delete.
Use any one of the following formats:
- organizations/{organization}/muteConfigs/{config_id}
- folders/{folder}/muteConfigs/{config_id} or
- projects/{project}/muteConfigs/{config_id}
"""
from google.cloud import securitycenter
client = securitycenter.SecurityCenterClient()
request = securitycenter.DeleteMuteConfigRequest()
request.name = mute_config_name
client.delete_mute_config(request)
print(f"Mute rule deleted successfully: {mute_config_name}")
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。