有没有办法检索 GCP 存储服务使用的 IP 范围?当我
dig storage.googleapis.com
时,我得到了一个未包含在此处范围内的 IP 列表。
我的场景: 我正在从 AWS 网络访问 Google Cloud Storage 存储桶。我在 AWS 安全组中有一个出站规则,允许连接到特定的 IP 范围。我需要在其中包含 GCP 存储的 IP 范围。 GCP 中是否有与 S3 托管前缀列表类似的概念?
您共享的 IP 范围适用于本文档
的计算服务我不记得(目前也找不到)GCP 上是否存在此类存储服务列表。
但是也许您可以尝试在 GCP Bucket 前面添加一个负载均衡器?在这种情况下,您可以将负载均衡器的 IP 列入白名单。 此文档可能会有所帮助
此处描述了一种方法。此列表适用于 Google API 端点,不限于存储服务。您可以从 goog.json 中获取 IP,并从 cloud.json 中减去 IP,以获得 google API IP。它们可能会发生变化,因此您需要定期检索它们。
import requests
import ipaddress
def get_ipv4_ranges(url):
"""Fetches IPv4 ranges from a given URL."""
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
return {prefix['ipv4Prefix'] for prefix in data['prefixes'] if 'ipv4Prefix' in prefix}
except requests.RequestException as e:
print(f"Error fetching data from {url}: {e}")
return set()
def sort_ipv4_ranges(ranges):
"""Sorts IPv4 ranges."""
return sorted(ranges, key=lambda ip: ipaddress.IPv4Network(ip))
def main():
# URLs containing the IPv4 ranges
goog_url = "https://www.gstatic.com/ipranges/goog.json"
cloud_url = "https://www.gstatic.com/ipranges/cloud.json"
# Fetching the IPv4 ranges
goog_ipv4_ranges = get_ipv4_ranges(goog_url)
cloud_ipv4_ranges = get_ipv4_ranges(cloud_url)
# Removing the cloud ranges from the goog ranges and sorting
remaining_ranges = sort_ipv4_ranges(goog_ipv4_ranges - cloud_ipv4_ranges)
# Output the sorted remaining ranges
for range in remaining_ranges:
print(range)
if __name__ == "__main__":
main()