配置审计 CONFIG-修正配置快速入门:基于FunctionGraph函数执行修正

时间:2024-11-15 17:55:38

基于FunctionGraph函数执行修正

  1. 使用预设策略“VPC启用流日志(vpc-flow-logs-enabled)”添加预定义合规规则

    由于当前账号存在未开启流日志的VPC资源,该合规规则的合规评估结果为“不合规”。

    图5 合规评估结果

  2. 创建FunctionGraph函数,并填入以下函数代码。

    该函数代码表示为指定的VPC资源开启流日志。

    使用以下函数示例时,您需要手动添加依赖包huaweicloudsdkvpc,详见配置依赖包

    # -*- coding:utf-8 -*-
    import time
    import requests
    import random, string
    import http.client
    from huaweicloudsdkcore.exceptions.exceptions import ConnectionException
    from huaweicloudsdkcore.exceptions.exceptions import RequestTimeoutException
    from huaweicloudsdkcore.exceptions.exceptions import ServiceResponseException
    from huaweicloudsdkvpc.v2.region.vpc_region import VpcRegion
    from huaweicloudsdkvpc.v2.vpc_client import VpcClient
    from huaweicloudsdkvpc.v2.model import CreateFlowLogReq
    from huaweicloudsdkvpc.v2.model import CreateFlowLogReqBody
    from huaweicloudsdkvpc.v2.model import CreateFlowLogRequest
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    
    requests.packages.urllib3.disable_warnings()
    
    def get_random_string(length=6):
      letters_and_digits = string.ascii_letters + string.digits
      return ''.join(random.choice(letters_and_digits) for _ in range(length))
    
    def createSlowLog(context, project_id, request):
      auth = BasicCredentials(ak=context.getAccessKey(), sk=context.getSecretKey())\
        .with_project_id(project_id)
      client = VpcClient.new_builder() \
        .with_credentials(credentials=auth) \
        .with_region(region=VpcRegion.value_of(region_id="cn-north-4")) \
        .build()
    
      try:
        response = client.create_flow_log(request)
        return 200
      except ConnectionException as e:
        print("A connect timeout exception occurs while the vpc performs some operations, exception: ", e.error_msg)
        return e.status_code
      except RequestTimeoutException as e:
        print("A request timeout exception occurs while the vpc 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):
      project_id = event.get("project_id")
      request_body = CreateFlowLogRequest(CreateFlowLogReqBody(
          CreateFlowLogReq(
            name="auto" + get_random_string(4),
            description="to remediate vpc",
            resource_type="vpc",
            resource_id=event.get("noncompliant_resource_id", {}),
            traffic_type="all",
            log_group_id=event.get("log_group_id", {}),
            log_topic_id=event.get("log_topic_id", {}),
            index_enabled=False
          )
        ))
      for retry in range(5):
        status_code = createSlowLog(context, project_id, request_body)
        if status_code == http.client.TOO_MANY_REQUESTS:
          time.sleep(1)
        else:
          break

  3. 创建该合规规则的修正配置,具体如下图所示。

    部分示例参数的说明:

    • 修正方法选择“自动修正”。
    • 修正模板选择上一步中创建的FunctionGraph函数。
    • 配置资源ID参数,Config会将该参数赋值为不合规资源的ID。在当前场景下“noncompliant_resource_id”将会被赋值为不合规资源的ID,FunctionGraph函数中使用resource_id=event.get("noncompliant_resource_id", {})语句获取不合规资源ID,每个不合规资源均会传递一次资源ID。
    • 参数,指定您期望由Config传递给FunctionGraph函数的静态参数,该参数为不合规资源修正的具体规则参数值。例如在当前场景下此处配置日志组的ID(键:log_group_id;值:具体日志组ID值),在FGS函数中使用log_group_id=event.get("log_group_id", {})语句获取日志组ID,执行修正时为不合规的VPC资源创建的流日志均将在此日志组内。
    图6 创建修正配置

  4. 修正配置创建完成后,重新触发规则评估

    当该合规规则评估完成且合规评估结果仍然为不合规时,Config将自动执行修正,调用FunctionGraph函数为每个不合规的VPC资源创建流日志。

    如下图所示,不合规的VPC资源在修正成功后已全部创建流日志。

    图7 VPC资源已创建流日志

support.huaweicloud.com/usermanual-rms/rms_05_1007.html