配置审计 Config
配置审计 Config
- 最新动态
- 产品介绍
- 快速入门
-
用户指南
- 资源清单
- 资源记录器
-
资源合规
- 资源合规概述
- 资源合规规则
- 组织合规规则
- 查看不合规资源
- 合规规则修正配置
- 合规规则概念详解
-
系统内置预设策略
- 预设策略列表
- 公共可用预设策略
- API网关 APIG
- 部署 CodeArts Deploy
- MapReduce服务 MRS
- NAT网关 NAT
- VPC终端节点 VPCEP
- Web应用防火墙 WAF
- 弹性负载均衡 ELB
- 弹性公网IP EIP
- 弹性伸缩 AS
- 高性能弹性文件服务 SFS Turbo
- 弹性云服务器 ECS
- 分布式缓存服务 DCS
- 函数工作流 FunctionGraph
- 内容分发网络 CDN
- 配置审计 Config
- 数据仓库服务 DWS
- 数据复制服务 DRS
- 数据加密服务 DEW
-
统一身份认证服务 IAM
- IAM用户的AccessKey在指定时间内轮换
- IAM策略中不授权KMS的禁止的action
- IAM用户组添加了IAM用户
- IAM用户密码策略符合要求
- IAM策略黑名单检查
- IAM策略不具备Admin权限
- IAM自定义策略具备所有权限
- 根用户存在可使用的访问密钥
- IAM用户访问模式
- IAM用户创建时设置AccessKey
- IAM用户归属指定用户组
- IAM用户在指定时间内有登录行为
- IAM用户开启MFA
- IAM用户单访问密钥
- Console侧密码登录的IAM用户开启MFA认证
- 根用户开启MFA认证
- IAM策略使用中
- IAM权限使用中
- IAM用户开启登录保护
- IAM委托绑定策略检查
- IAM用户admin权限检查
- IAM用户不直接附加策略或权限
- 文档数据库服务 DDS
- 消息通知服务 SMN
- 虚拟私有云 VPC
- 虚拟专用网络 VPN
- 云监控服务 CES
- 云容器引擎 CCE
- 云审计服务 CTS
- 云数据库 RDS
- 云数据库 GaussDB
- 云数据库 TaurusDB
- 云数据库 GeminiDB
- 云搜索服务 CSS
- 云硬盘 EVS
- 云证书管理服务 CCM
- 分布式消息服务Kafka版
- 分布式消息服务RabbitMQ版
- 分布式消息服务RocketMQ版
- 组织 Organizations
- 云防火墙 CFW
- 云备份 CBR
- 对象存储服务 OBS
- 镜像服务 IMS
- 裸金属服务器 BMS
- 图引擎服务 GES
- 资源合规事件监控
-
合规规则包
- 合规规则包概述
- 合规规则包
- 组织合规规则包
- 自定义合规规则包
-
合规规则包示例模板
- 示例模板概述
- 等保三级2.0规范检查的标准合规包
- 适用于金融行业的合规实践
- 华为云网络安全合规实践
- 适用于统一身份认证服务(IAM)的最佳实践
- 适用于云监控服务(CES)的最佳实践
- 适用于计算服务的最佳实践
- 适用于弹性云服务器(ECS)的最佳实践
- 适用于弹性负载均衡(ELB)的最佳实践
- 适用于管理与监管服务的最佳实践
- 适用于云数据库(RDS)的最佳实践
- 适用于弹性伸缩(AS)的最佳实践
- 适用于云审计服务(CTS)的最佳实践
- 适用于人工智能与机器学习场景的合规实践
- 适用于自动驾驶场景的合规实践
- 资源开启公网访问最佳实践
- 适用于日志和监控的最佳实践
- 华为云架构可靠性最佳实践
- 适用于中国香港金融管理局的标准合规包
- 适用于中小企业的ENISA的标准合规包
- 适用于SWIFT CSP的标准合规包
- 适用于德国云计算合规标准目录的标准合规包
- 适用于PCI-DSS的标准合规包
- 适用于医疗行业的合规实践
- 网络及数据安全最佳实践
- 适用于Landing Zone基础场景的最佳实践
- 架构安全支柱运营最佳实践
- 网络和内容交付服务运营最佳实践
- 适用于空闲资产管理的最佳实践
- 多可用区架构最佳实践
- 资源稳定性最佳实践
- 适用于API网关(APIG)的最佳实践
- 适用于云容器引擎(CCE)的最佳实践
- 适用于内容分发网络(CDN)的最佳实践
- 适用于函数工作流(FunctionGraph)的最佳实践
- 适用于云数据库(GaussDB)的最佳实践
- 适用于云数据库(GeminiDB)的最佳实践
- 适用于MapReduce服务(MRS)的最佳实践
- NIST审计标准最佳实践
- 新加坡金融行业的最佳实践
- 安全身份和合规性运营最佳实践
- 华为云安全配置基线指南的标准合规包(level 1)
- 华为云安全配置基线指南的标准合规包(level 2)
- 静态数据加密最佳实践
- 数据传输加密最佳实践
- 适用于云备份(CBR)的最佳实践
- 适用于云搜索服务(CSS)的最佳实践
- 适用于分布式缓存服务(DCS)的最佳实践
- 适用于分布式消息服务(DMS)的最佳实践
- 适用于数据仓库服务(DWS)的最佳实践
- 适用于云数据库(TaurusDB)的最佳实践
- 适用于对象存储服务(OBS)的最佳实践
- 适用于VPC安全组的最佳实践
- 适用于Web应用防火墙(WAF)的最佳实践
- 高级查询
- 资源聚合器
- 云审计-记录配置审计
- 附录
- 最佳实践
- API参考
- SDK参考
- 常见问题
- 文档下载
- 通用参考
本文导读
链接复制成功!
示例函数(Python)
评估由配置变更触发的示例函数
Config服务检测到自定义组织合规规则范围内的资源发生更改时,会调用函数的示例如下:
import time import http.client from huaweicloudsdkcore.auth.credentials import GlobalCredentials from huaweicloudsdkcore.exceptions.exceptions import ConnectionException from huaweicloudsdkcore.exceptions.exceptions import RequestTimeoutException from huaweicloudsdkcore.exceptions.exceptions import ServiceResponseException from huaweicloudsdkconfig.v1.region.config_region import ConfigRegion from huaweicloudsdkconfig.v1.config_client import ConfigClient from huaweicloudsdkconfig.v1 import PolicyResource, PolicyStateRequestBody from huaweicloudsdkconfig.v1 import UpdatePolicyStateRequest ''' 合规规则评估逻辑: 返回“Compliant” 或 “NonCompliant” 本示例中, 当资源类型为ecs.cloudservers, 且该ecs的vpcId字段不是合规规则参数所指定的vpcId时, 会返回不合规, 否则返回合规。 ''' def evaluate_compliance(resource, parameter): if resource.get("provider") != "ecs" or resource.get("type") != "cloudservers": return "Compliant" vpc_id = resource.get("properties", {}).get("metadata", {}).get("vpcId") return "Compliant" if vpc_id == parameter.get("vpcId") else "NonCompliant" def update_policy_state(context, domain_id, evaluation): auth = GlobalCredentials(ak=context.getAccessKey(), sk=context.getSecretKey(), domain_id=domain_id) client = ConfigClient.new_builder() \ .with_credentials(credentials=auth) \ .with_region(region=ConfigRegion.value_of(region_id="cn-north-4")) \ .build() try: response = client.update_policy_state(evaluation) return 200 except ConnectionException as e: print("A connect timeout exception occurs while the Config performs some operations, exception: ", e.error_msg) return e.status_code except RequestTimeoutException as e: print("A request timeout exception occurs while the Config performs some operations, exception: ", e.error_msg) return e.status_code except ServiceResponseException as e: print("There is service error, exception: ", e.status_code, e.error_msg) return e.status_code def handler(event, context): domain_id = "<manager_domain_id>" resource = event.get("invoking_event", {}) parameters = event.get("rule_parameter") compliance_state = evaluate_compliance(resource, parameters) request_body = UpdatePolicyStateRequest(PolicyStateRequestBody( policy_resource = PolicyResource( resource_id = resource.get("id"), resource_name = resource.get("name"), resource_provider = resource.get("provider"), resource_type = resource.get("type"), region_id = resource.get("region_id"), domain_id = event.get("domain_id") ), trigger_type = event.get("trigger_type"), compliance_state = compliance_state, policy_assignment_id = event.get("policy_assignment_id"), policy_assignment_name = event.get("policy_assignment_name"), evaluation_time = event.get("evaluation_time"), evaluation_hash = event.get("evaluation_hash") )) for retry in range(5): status_code = update_policy_state(context, domain_id, request_body) if status_code == http.client.TOO_MANY_REQUESTS: print("TOO_MANY_REQUESTS: retry again") time.sleep(1) elif status_code == http.client.OK: print("Update policyState successfully.") break else: print("Failed to update policyState.") break
评估由周期执行触发的示例函数
Config针对周期执行的自定义组织合规规则,会调用函数的示例如下:
import time import http.client from huaweicloudsdkcore.auth.credentials import GlobalCredentials from huaweicloudsdkcore.exceptions.exceptions import ConnectionException from huaweicloudsdkcore.exceptions.exceptions import RequestTimeoutException from huaweicloudsdkcore.exceptions.exceptions import ServiceResponseException from huaweicloudsdkconfig.v1.region.config_region import ConfigRegion from huaweicloudsdkconfig.v1.config_client import ConfigClient from huaweicloudsdkconfig.v1 import PolicyResource, PolicyStateRequestBody from huaweicloudsdkconfig.v1 import UpdatePolicyStateRequest from huaweicloudsdkiam.v3.region.iam_region import IamRegion from huaweicloudsdkiam.v3 import IamClient, ShowDomainLoginPolicyRequest """ 合规规则评估逻辑:返回“Compliant”或“NonCompliant”。 本示例中, 当账号设置的登录会话失效时间大于30分钟, 会返回不合规, 否则返回合规。 实现方式是调用IAM服务的接口ShowDomainLoginPolicy。 该场景下,可能需要适当增加函数的执行超时时间和内存限制。 """ def evaluate_compliance(ak, sk, domain_id): credentials = GlobalCredentials(ak, sk) client = IamClient.new_builder() \ .with_credentials(credentials) \ .with_region(IamRegion.value_of("cn-north-4")) \ .build() try: request = ShowDomainLoginPolicyRequest() request.domain_id = domain_id response = client.show_domain_login_policy(request) session_timeout = response.login_policy.session_timeout print("session_timeout", session_timeout) if not session_timeout: return "NonCompliant" return "NonCompliant" if session_timeout > 30 else "Compliant" except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) def update_policy_state(context, domain_id, evaluation): auth = GlobalCredentials(ak=context.getAccessKey(), sk=context.getSecretKey(), domain_id=domain_id) client = ConfigClient.new_builder() \ .with_credentials(credentials=auth) \ .with_region(region=ConfigRegion.value_of(region_id="cn-north-4")) \ .build() try: response = client.update_policy_state(evaluation) return 200 except ConnectionException as e: print("A connect timeout exception occurs while the Config performs some operations, exception: ", e.error_msg) return e.status_code except RequestTimeoutException as e: print("A request timeout exception occurs while the Config performs some operations, exception: ", e.error_msg) return e.status_code except ServiceResponseException as e: print("There is service error, exception: ", e.status_code, e.error_msg) return e.status_code def handler(event, context): domain_id = "<manager_domain_id>" ak = "<user_ak"> sk = "<user_sk> resource = event.get("invoking_event", {}) if resource.get("name") != "Account": return compliance_state = evaluate_compliance(ak, sk, event.get("domain_id")) request_body = UpdatePolicyStateRequest(PolicyStateRequestBody( policy_resource = PolicyResource( resource_id = resource.get("id"), resource_name = resource.get("name"), resource_provider = resource.get("provider"), resource_type = resource.get("type"), region_id = resource.get("region_id"), domain_id = event.get("domain_id") ), trigger_type = event.get("trigger_type"), compliance_state = compliance_state, policy_assignment_id = event.get("policy_assignment_id"), policy_assignment_name = event.get("policy_assignment_name"), evaluation_time = event.get("evaluation_time"), evaluation_hash = event.get("evaluation_hash") )) for retry in range(5): status_code = update_policy_state(context, domain_id, request_body) if status_code == http.client.TOO_MANY_REQUESTS: print("TOO_MANY_REQUESTS: retry again") time.sleep(1) elif status_code == http.client.OK: print("Update policyState successfully.") break else: print("Failed to update policyState.") break
依赖包
如果依赖包缺失,则需要手动导入依赖包,详见配置依赖包。在上述示例中,使用到的依赖包为huaweicloudsdkiam和huaweicloudsdkconfig。
父主题: 自定义组织合规规则样例