如何将 AWS EC2 安全组中的 Atlassian/Bitbucket IP 列入白名单?

时间:2021-01-21 11:43:07

标签: amazon-ec2 terraform bitbucket

我们希望 Bitbucket webhooks 触发我们的 CI 工具,该工具在 AWS EC2 实例上运行,受入口规则保护,无法进行一般访问。

Bitbucket 在 https://support.atlassian.com/bitbucket-cloud/docs/what-are-the-bitbucket-cloud-ip-addresses-i-should-use-to-configure-my-corporate-firewall/

提供了一个列出其 IP 地址的页面

他们在 https://ip-ranges.atlassian.com/ 处还有一个用于 Atlassian IP 的机器消耗版本。

我想知道在 AWS EC2 安全组中添加和维护此列表的有效方法是什么,例如通过地形。

2 个答案:

答案 0 :(得分:2)

我最终从他们的页面上抓取了机器消耗的 json,让 terraform 管理其余部分。获取json的步骤留作手动任务。

resource "aws_security_group_rule" "bitbucket-ips-sgr" {
  security_group_id = "your-security-group-id"
  type = "ingress"

  from_port = 443
  to_port = 443
  protocol = "TCP"
  cidr_blocks = local.bitbucket_cidrs_ipv4
  ipv6_cidr_blocks = local.bitbucket_cidrs_ipv6
}

locals {
  bitbucket_cidrs_ipv4 = [for item in local.bitbucket_ip_ranges_source.items:
  # see https://stackoverflow.com/q/47243474/1242922
  item.cidr if length(regexall(":", item.cidr)) == 0
  ]
  bitbucket_cidrs_ipv6 = [for item in local.bitbucket_ip_ranges_source.items:
  # see https://stackoverflow.com/q/47243474/1242922
  item.cidr if length(regexall(":", item.cidr)) > 0
  ]
  # the list originates from https://ip-ranges.atlassian.com/
  bitbucket_ip_ranges_source = jsondecode(
<<JSON
the json output from the above URL
JSON
          )
}

答案 1 :(得分:1)

我改进了 Richard 的回答,并想补充一点,TF 的 http 提供程序可以为您获取 JSON,并且对 jsondecode() 调用稍作调整,仍会播放相同的 for 循环。< /p>

provider "http" {}

data "http" "bitbucket_ips" {
  url = "https://ip-ranges.atlassian.com/"

  request_headers = {
    Accept = "application/json"
  }
}

locals {
  bitbucket_ipv4_cidrs = [for c in jsondecode(data.http.bitbucket_ips.body).items : c.cidr if length(regexall(":", c.cidr)) == 0]
  bitbucket_ipv6_cidrs = [for c in jsondecode(data.http.bitbucket_ips.body).items : c.cidr if length(regexall(":", c.cidr)) > 0]
}

output "ipv4_cidrs" {
  value = local.bitbucket_ipv4_cidrs
}

output "ipv6_cidrs" {
  value = local.bitbucket_ipv6_cidrs
}
相关问题