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

How to whitelist Atlassian/Bitbucket IPs in AWS EC2 security group?

我们希望 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 安全组中添加和维护此列表的有效方法是什么,例如通过 terraform.

我最终从他们的页面上抓取了机器消耗品 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 
  item.cidr if length(regexall(":", item.cidr)) == 0
  ]
  bitbucket_cidrs_ipv6 = [for item in local.bitbucket_ip_ranges_source.items:
  # see 
  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
          )
}

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

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
}