
# 通过Cloud Custodian自动增加镜像老化策略
![](https://support.huaweicloud.com/bestpractice2-swr/public_sys-resources/note_3.0-zh-cn.png)
该实践仅适用于容器镜像服务企业版。
当前很多企业会同时使用多个不同的云厂商提供的云服务。在这样的混合云环境中，除了各个云厂商提供的安全机制外，云基础设施还需要严格的云治理。容器镜像服务 SWR是容器域中一个重要的服务，其安全性至关重要。必须对其进行监控以防存在任何访问和权限漏洞。
Cloud Custodian 提供了一套开源规则引擎，能够根据预定义的安全策略和合规要求，对云资源进行自动化检查和治理。可用于华为云容器镜像服务 SWR的资源的访问限制以及资源治理。借助 Cloud Custodian，您可设置规则，根据定义的安全和合规标准对环境进行验证。您可使用 Cloud Custodian 来管理您的云环境，帮助确保遵守安全政策、标签政策、未使用资源的垃圾回收和成本管理。借助 Cloud Custodian，其统一的治理界面使企业在混合云环境中能够轻松实施一致的安全策略和运营规范。
图1使用Cloud Custodian对SWR服务进行监管的架构   
![](https://support.huaweicloud.com/bestpractice2-swr/zh-cn_image_0000002443433872.png "点击放大")
架构图中的服务是我们使用Cloud Custodian必须依赖的服务：
- CTS审计：即云审计服务，是华为云安全解决方案中专业的日志审计服务，提供对各种云资源操作记录的收集、存储和查询功能，可用于支撑安全分析、合规审计、资源跟踪和问题定位等常见应用场景。
- FunctionGraph：函数工作流FunctionGraph是华为云提供的一款无服务器（Serverless）计算服务，作为事件驱动型的函数托管平台，用户只需编写业务函数代码并设置运行的条件，无需配置和管理服务器等基础设施，函数以弹性、免运维、高可靠的方式运行。
- SWR企业版：SWR企业版作为容器镜像服务的企业版，为用户提供企业级专属安全托管服务，支持托管容器镜像、Helm Chart等符合OCI标准的云原生制品。
#### 操作步骤
1. 安装Python运行环境。此为Cloud Custodian依赖的运行环境。 
   ![](https://support.huaweicloud.com/bestpractice2-swr/public_sys-resources/note_3.0-zh-cn.png)
   python建议使用Python 3.11版本，建议使用虚拟环境进行开发。
   如果使用原生Python 3.11，使用以下命令创建和激活虚拟环境。其他版本Python请根据Python官方文档进行安装。
   ```
   # 创建虚拟环境
   python -m venv custodian
   # 激活虚拟环境（Linux）
   source custodian/bin/activate
   # 激活虚拟环境（Windows）
   custodian\Scripts\activate.bat
   如果使用Conda或者Miniconda，也可以使用以下命令创建和激活虚拟环境：
   # 创建虚拟环境
   conda create -n custodian python=3.11
   # 激活虚拟环境
   conda activate custodian
   ```
   
   
2. 安装Cloud Custodian。 
   1. 下载Cloud Custodian
      ```
      git clone https://github.com/huaweicloud/cloud-custodian.git
      cd cloud-custodian
      ```
      
   
   
   
   2. 安装python依赖
      ```
      pip install -e .
      pip install -e tools/c7n_huaweicloud/.
      ```
      
   
   
   更详细的安装指导请参考 [Cloud Custodian 官方文档](https://cloudcustodian.io/docs/quickstart/index.html#install-cc)。
   
   
3. 执行下述命令检查cloud custodian是否安装正常。 
   ```
   custodian schema huaweicloud.swr-ee
   ```
   回显如下表示安装成功
   ![](https://support.huaweicloud.com/bestpractice2-swr/zh-cn_image_0000002476953653.png "点击放大")
   
   
4. 定义Cloud Custodian策略，策略如下： 
   ```
   policies:
     - name: swr-ee-event
       resource: huaweicloud.swr-ee-namespace
       mode:
         type: cloudtrace
         xrole: fgs_default_agency
         events:
           - source: 'SWR.namespace'
             event: 'createNamespace'
             ids: 'resource_name'
       actions:
         - type: set-lifecycle
           rules:
             - template: latestPushedK
               params:
                 latestPushedK: 50
               scope_selectors:
                 repository:
                   - kind: doublestar
                     pattern: '**'
               tag_selectors:
                 - kind: doublestar
                   decoration: matches
                   pattern: '**'
   ```
   ![](https://support.huaweicloud.com/bestpractice2-swr/public_sys-resources/note_3.0-zh-cn.png)
   当你在SWR企业版上新建一个命名空间的时候，该策略会自动在该命名空间上设置老化策略，老化策略为保留最近推送的50个制品版本。
   下面给出如下两种场景的策略参考以供您参考：
   策略1：查询超过创建时间90的镜像：
   ```
   policies:
     - name: swr-ee-repos
       resource: huaweicloud.swr-ee
       filters:
         - type: age
           days: 90
           op: gt
   ```
   策略2：制品Tag匹配release\*的设置镜像不可变规则:
   ```
   policies:
     - name: swr-ee-set-immutability-rules
       resource: huaweicloud.swr-ee-namespace
       actions:
         - type: set-immutability
           state: True
           scope_selectors:
             repository:
               - kind: doublestar
                 pattern: '**'
           tag_selectors:
             - kind: doublestar
               decoration: matches
               pattern: '{release*}'
   ```
   如果想要了解更多的策略配置请参考 [Cloud Custodian 官方文档](https://cloudcustodian.io/docs/quickstart/index.html#install-cc)。
   
   
5. 执行Custodian命令启用策略。 
   1. 执行策略之前请先在Terminal中配置环境变量，以下为linux环境下的配置命令，windows环境下的配置只需将export改为set即可。
      ```
      # 配置华为云账号ak/sk
      export HUAWEI_ACCESS_KEY_ID={your-ak}
      export HUAWEI_SECRET_ACCESS_KEY={your-sk}
      # 配置资源默认所在region，例如ap-southeast-1，可从网页URL中获取
      export HUAWEI_DEFAULT_REGION={your-region}
      ```
      
   
   
   
   2. 执行custodian run命令启用策略。
      ```
      custodian run --output-dir=<output_directory> <policy_name>.yaml
      ```
      
   
   
   
   
   
 
