【问题标题】:How to download a file from GitHub Enterprise using Terraform?如何使用 Terraform 从 GitHub Enterprise 下载文件?
【发布时间】:2018-01-01 05:49:09
【问题描述】:

这是我的 s3_policy.json

{
  "Version":"2012-10-17",
  "Statement":[
    {
      "Sid":"mybucket",
      "Effect":"Allow",
      "Principal": "*",
      "Action":["s3:GetObject"],
      "Resource":[
        "arn:aws:s3:::${bucket_name}/*"
      ],
      "Condition": {
          "IpAddress": {
              "aws:SourceIp": [
              "10.xx.xxx.x",
              "172.168.xx.x",
              ........,
              .........,
              ..........,
              ...........,
              ]
          }
      }
    }
  ]
}

我有共同的回购协议,我将其用于不同的项目。这个通用 repo 有一个 yaml 格式的 CIDR IP 列表。

我想把它放到我的 Terraform 项目中,这样我就可以重新使用同一个文件,而不是硬编码 IP 地址。

我无法找到一种方法来自动执行此操作,而不是在此 repo 中硬编码 IP 地址。

【问题讨论】:

    标签: amazon-s3 terraform


    【解决方案1】:

    您可以将 IP 地址用作数据源并改为使用它。

    您的政策文件将如下所示:

    resource "aws_iam_policy" "whitelist_ips" {
      name        = "whitelist_ips"
      description = "${var.policy_description}"
    
      policy = <<EOF
    {
      "Version":"2012-10-17",
      "Statement":[
        {
          "Sid":"mybucket",
          "Effect":"Allow",
          "Principal": "*",
          "Action":["s3:GetObject"],
          "Resource":[
            "arn:aws:s3:::${bucket_name}/*"
          ],
          "Condition": {
              "IpAddress": {
                  "aws:SourceIp": ["${data.external.ip_addresses.result}"]
              }
          }
        }
      ]
    }
    EOF
    }
    

    您需要创建一个可以运行的external data source,它会从某个位置获取 IP 地址并将 IP 地址作为逗号分隔的字符串返回。

    data "external" "ip_addresses" {
      program = ["python", "${path.module}/get_ips.py"]
    }
    

    get_ips.py 可能看起来像这样:

    #!/usr/bin/env python
    from __future__ import print_function
    import json
    import re
    
    yaml_string = """ - 1.2.3.4/32
     - 1.2.3.5/32
     - 1.3.0.0/16
    """
    
    result = []
    lines = yaml_string.split("\n")
    
    for line in lines:
        # Remove empty lines
        if line != "":
            result.append(re.sub('\s*-\s*', '', line))
    
    print(json.dumps(','.join(result)))
    

    但显然您需要从 Github 获取 YAML 列表,而不是在此数据源中毫无意义地硬编码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-20
      • 2021-12-17
      • 2016-01-09
      • 2013-04-16
      • 2020-09-30
      • 1970-01-01
      • 2020-09-11
      • 2012-12-16
      相关资源
      最近更新 更多