跳转到主要内容

IAAC定义的保管策略

项目描述

基础设施代码的保管策略

此软件包允许cloud-custodian直接对基础设施代码源资产评估策略。

它还提供了一个独立的cli,以提供更好的命令行ux进行源资产评估。

安装

我们目前仅在mac和linux上支持python > 3.10,要在windows上运行,我们建议使用我们的docker镜像。

pip install c7n-left

我们还提供已签名的docker镜像。这些镜像基于chainguard的wolfi Linux发行版构建,该发行版旨在最小化、可审计和安全性。

docker pull cloudcustodian/c7n-left:dev

可以使用cosign验证镜像签名

export IMAGE=$(docker image inspect cloudcustodian/c7n-left:dev -f '{{index .RepoDigests 0}}')
cosign verify $IMAGE \
   --certificate-identity 'https://github.com/cloud-custodian/cloud-custodian/.github/workflows/docker.yml@refs/heads/main' \
   --certificate-oidc-issuer 'https://token.actions.githubusercontent.com'

用法

 c7n-left run --help
Usage: c7n-left run [OPTIONS]

  evaluate policies against IaC sources.

  c7n-left -p policy_dir -d terraform_root --filters "severity=HIGH"

  WARNING - CLI interface subject to change.

Options:
  --format TEXT
  --filters TEXT                  Filter policies or resources as k=v pairs
                                  with globbing
  --warn-on TEXT                  Select policies to log instead of fail on
                                  via k=v pairs with globbing								  
  -p, --policy-dir PATH           Directory with policies
  -d, --directory PATH            IaC directory to evaluate
  -o, --output [cli|github|json]  Output format (default cli)
  --output-file FILENAME          Output file (default stdout)
  --var-file FILE                 Load variables from the given file, can be
                                  used more than once
  --output-query TEXT             Use a jmespath expression to filter json
                                  output
  --summary [policy|resource]
  --help                          Show this message and exit.

我们将创建一个包含策略的空目录

policies:
  - name: test
    resource: terraform.aws_s3_bucket
    metadata:
      severity: medium
    filters:
      - server_side_encryption_configuration: absent

现在我们可以使用它来评估一个Terraform根模块

 c7n-left run -p policies -d module
Running 1 policies on 1 resources
test - terraform.aws_s3_bucket
  Failed
  File: s3.tf:1-8
  1 resource "aws_s3_bucket" "example" {                                                                                
  2   bucket = "my-custodian-test-bucket"                                                                               
  3   acl    = "private"                                                                                                
  4                                                                                                                     
  5   tags = {                                                                                                          
  6     original-tag = "original-value"                                                                                 
  7   }                                                                                                                 
  8 }                                                                                                                   

Evaluation complete 0.00 seconds -> 1 Failures
           Summary - By Policy           
┏━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
┃ Severity  Policy  Result            ┃
┡━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩
│ medium    test    1 failed 0 passed │
└──────────┴────────┴───────────────────┘
0 compliant of 1 total, 1 resource has 1 policy violation

在docker中运行时,您需要使用卷挂载来提供对策略目录和Terraform根模块的访问权限。

docker run -ti --rm -v $(pwd)/policies:/policies -v $(pwd)/root-module:/module \
       cloudcustodian/c7n-left:dev run -p /policies -d /module

如果Terraform根模块有其他远程模块依赖项,您需要在运行c7n-left之前使用Terraform先获取这些依赖项。

terraform get -update

CLI过滤器

可以通过命令行中的--filters选项控制哪些策略和哪些资源被评估。

可用的过滤器

  • name - 策略名称
  • category - 策略类别
  • severity - 策略最低严重性(未知、低、中、高、危急)
  • type - 资源类型,例如aws_security_group
  • id - 资源ID,例如aws_vpc.example

可以为给定过滤器指定多个值,以逗号分隔,除了严重性之外的所有过滤器都支持通配符。

示例

# run all encryption policies on ebs volumes and sqs queues
c7n-left run -p policy_dir -d terraform --filters="category=encryption type=aws_ebs_volume,aws_sqs_queue"

# run all medium and higher level policies cost policies
c7n-left run -p policy_dir -d terraform --filters="severity=medium category=cost"

严重性和类别的策略值在它的元数据部分指定。例如

policies:
  - name: check-encryption
    resource: [terraform.aws_ebs_volume, terraform.aws_sqs_queue]
    metadata:
      category: [encryption, security]
      severity: high
    filters:
       - kms_master_key_id: absent

输出

如果你在GitHub动作中使用此功能,我们提供了特殊的输出模式,通过--output github将报告注释直接输入到拉取请求中。

在显示资源匹配后,我们还显示一个总结输出,有两个总结显示可用,默认策略总结和资源总结,可以通过--summary resource启用。

默认情况下,任何策略匹配都会导致运行退出代码1以标记失败,此行为可以通过--warn-on命令行标志来控制。例如,给定一个策略有

policies:
  - name: check-encryption
    resource: [terraform.aws_ebs_volume, terraform.aws_sqs_queue]
    metadata:
      category: [beta, security]
      severity: high

使用--warn-on category=beta运行策略将只会记录匹配,而不是导致退出代码1。

策略语言

标准Custodian过滤器(列表项andornotreduceevent)可用

c7n-left的策略支持一些比Custodian策略更常见的附加功能。

策略可以针对多种资源类型进行指定,可以是数组或通配符。

policies:
  - name: check-encryption
    resource: [aws_ebs_volume, aws_sqs_queue]

可标记过滤器

一个可标记过滤器可用,允许仅过滤支持标记的资源。

结合资源通配符支持,这允许使用单个策略来强制执行组织的标记标准。

policies:
 - name: check-tag-policy
   resource: "terraform.aws*"
   filters:
     - taggable
     - or:
       - tag:Env: absent
       - tag:Owner: absent
       - tag:App: absent

此过滤器支持来自aws、azure、gcp、oci、tencentcloud等几个terraform提供者的资源。

支持默认标记的terraform提供者将在相关资源上自动提供这些值。

遍历过滤器

一个遍历过滤器可用,允许从资源到任何相关资源进行多跳图遍历。

例如,这里有一个针对aws ec2实例的策略,检查该实例附加的任何安全组是否定义了允许从0.0.0.0/0访问的权限。

policies:
 - name: check-security-group-open-cidr
   resource: terraform.aws_instance
   description: "EC2 should not be open to world on ssh"
   filters:
     - type: traverse
       resources:
         - aws_security_group
         - aws_security_ingress_permission
       attrs:
         - Ipv4: 0.0.0.0/0

terraform数据资源

策略也可以针对数据资源编写,请注意数据资源以data.为前缀。

policies:
 - name: check-ami-data
   resource: terraform.data.aws_ami
   filters:
     - type: value
       key: owners
       op: contains
       value: "099720109477"  # Canonical/ubuntu

您还可以从资源遍历到其数据使用。

policies:
 - name: check-owner-specified
   resource: terraform.aws_instance
   filters:
    - type: traverse
      resources: data.aws_ami
      attrs:
       - owners: present

环境变量

c7n-left通常在CI系统中运行,这些系统通过环境变量提供了大量信息。策略可以通过两种不同的方式使用这些环境变量。

它们可以用于在策略执行之前替换策略的内容,这些变量将在策略执行之前被替换。请注意,这使用了Python的格式能力

policies:
 - name: "check-{env[REPO]}-{env[PR_NUMBER]}"
   resource: terraform.aws*

此外,策略可以使用event过滤器来评估这些变量。

policies:
  - name: check-aws
    resource: terraform.aws*
    filters:
      - type: event
        key: env.REPO
        value: "cloud-custodian/cloud-custodian"

策略测试

c7n-left支持编写和运行策略的测试。

要为策略创建测试,在策略文件旁边创建一个测试目录。

在该测试目录中,创建一个与策略名称相同的子目录。

接下来,向该子目录添加terraform文件。通常,您会添加匹配策略的terraform文件以及不匹配的文件。

最后,你需要在 left.plan[.yaml|.json] 文件中添加断言。该文件的格式是一个字典数组。这些字典用于与策略发现进行匹配。其匹配的数据是使用 c7n-left run --output json 找到的。字典中的每个键/值对都与发现进行匹配。

综合上述内容,我们已按以下方式设置测试

 tree policy-dir-a/
policy-dir-a/
├── alb.yaml
└── tests
    └── alb-deletion-protection-disabled
        ├── left.plan.yaml
        ├── negative1.tf
        └── positive1.tf

3 directories, 4 files

❯ cat policy-dir-a/alb.yaml
policies:
  - name: alb-deletion-protection-disabled
    resource: [terraform.aws_lb, terraform.aws_alb]
    description: |
      Application Load Balancer should have deletion protection enabled
    metadata:
      severity: low
      category: "Insecure Configurations"
    filters:
      - enable_deletion_protection: empty

❯ cat policy-dir-a/tests/alb-deletion-protection-disabled/left.plan.yaml
- "resource.__tfmeta.filename": "positive1.tf"

现在我们可以运行测试了

 c7n-left test -p policy-dir-a/
Discovered 1 Tests
Failure alb-deletion-protection-disabled [{'resource.__tfmeta.filename':
'positive1.tf'}] checks not used

1 Test Complete (0.05s) 1 Failure

如果计划文件中的断言与任何策略发现不匹配,或者策略发现没有与断言匹配,则测试失败。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

本发布没有提供源代码分发文件。请参阅有关 生成分发存档 的教程。

构建分发

c7n_left-0.3.27-py3-none-any.whl (41.0 kB 查看哈希值)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面