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

Amazon ec2 如何在AWS EC2安全组中将Atlassian/Bitbucket IP列入白名单?,amazon-ec2,terraform,bitbucket,Amazon Ec2,Terraform,Bitbucket,我们希望Bitbucket webhooks触发我们的CI工具,该工具在AWS EC2实例上运行,受一般访问的入口规则保护 Bitbucket提供了一个页面,其中列出了他们的IP地址 一般来说,他们在Atlassian IPs上也有一个机器耗材版本 我想知道,在AWS EC2安全组中添加和维护此列表的有效方法是什么,例如通过terraform。我最终从他们的页面中删除了机器可消费的json,并让terraform管理其余部分。获取json的步骤留作手动任务 resource "aws_

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

Bitbucket提供了一个页面,其中列出了他们的IP地址

一般来说,他们在Atlassian IPs上也有一个机器耗材版本


我想知道,在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 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
          )
}
资源“aws\u安全性\u组\u规则”“比特桶ips sgr”{
安全组\u id=“您的安全组id”
type=“入口”
从_端口=443
至_端口=443
协议=“TCP”
cidr_块=local.bitbucket_cidr_ipv4
ipv6\u cidr\u块=local.bitbucket\u cidr\u ipv6
}
当地人{
bitbucket\u cidrs\u ipv4=[对于local.bitbucket\u ip\u ranges\u source.items中的项:
#看https://stackoverflow.com/q/47243474/1242922
如果长度(regexall(“:”,item.cidr))=0,则为item.cidr
]
bitbucket\u cidrs\u ipv6=[对于local.bitbucket\u ip\u ranges\u source.items中的项:
#看https://stackoverflow.com/q/47243474/1242922
如果长度(regexall(“:”,item.cidr))>0,则为item.cidr
]
#该列表源于https://ip-ranges.atlassian.com/
bitbucket\u ip\u ranges\u source=jsondecode(

最后,我从他们的页面中删除了机器可消费的json,并让terraform管理其余部分

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
          )
}
资源“aws\u安全性\u组\u规则”“比特桶ips sgr”{
安全组\u id=“您的安全组id”
type=“入口”
从_端口=443
至_端口=443
协议=“TCP”
cidr_块=local.bitbucket_cidr_ipv4
ipv6\u cidr\u块=local.bitbucket\u cidr\u ipv6
}
当地人{
bitbucket\u cidrs\u ipv4=[对于local.bitbucket\u ip\u ranges\u source.items中的项:
#看https://stackoverflow.com/q/47243474/1242922
如果长度(regexall(“:”,item.cidr))=0,则为item.cidr
]
bitbucket\u cidrs\u ipv6=[对于local.bitbucket\u ip\u ranges\u source.items中的项:
#看https://stackoverflow.com/q/47243474/1242922
如果长度(regexall(“:”,item.cidr))>0,则为item.cidr
]
#该列表源于https://ip-ranges.atlassian.com/
bitbucket\u ip\u ranges\u source=jsondecode(

我改进了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
}

我改进了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
}

很高兴听到:)太棒了,谢谢你的改进,非常清晰。是否可以控制何时重新获取列表?我想在每个tf申请时都会这样做。我冒昧地编辑了你的帖子,给它更多的是一种独立回答的感觉,而不是一个线程讨论。:)假设正确!在此实现中,每次都会运行数据查找。跳过它(即使用
数据
查找上的控制变量和三元
计数
)引用资源调用时会导致空值,Terraform会将所有Bitbucket CIDR视为要删除的漂移。如果您的用例要求严格控制何时引入更改,那么手动刮取/硬编码JSON方法将是更好的选择。很高兴听到:)太棒了,感谢您的改进,v非常清楚。是否可以控制何时重新获取列表?我假设每次tf申请时都会这样做。我自由编辑了您的帖子,使其更像是一个独立的答案,而不是一个线程讨论。:)您假设正确!在此实现中,每次都会运行数据查找。跳过它(即,在
数据
查找上使用控制变量和三元
计数
)引用资源调用时会导致空值,Terraform会将所有Bitbucket CIDR视为要删除的漂移。如果您的用例要求严格控制何时引入更改,那么手动刮取/硬编码JSON方法将是更好的选择。