-
Notifications
You must be signed in to change notification settings - Fork 50
/
cf_checks.py
executable file
·29 lines (27 loc) · 1.06 KB
/
cf_checks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- coding: utf-8 -*-
try:
import urllib2 as urllib_req
from urllib2 import HTTPError, URLError
except ImportError:
import urllib.request as urllib_req
from urllib.error import HTTPError, URLError
from xml.dom.minidom import parse
def check_cf(cf_endpoints):
"""Check if a CloudFront URL points to an S3 bucket"""
if not cf_endpoints:
return None
bucket_names = []
for endpoint in cf_endpoints:
if not endpoint.startswith('https://'):
endpoint = 'https://' + endpoint
request = urllib_req.Request(endpoint)
try:
response = urllib_req.urlopen(request, timeout=20)
if 'AmazonS3' in dict(response.info())['server']:
bucket_name = parse(response).documentElement.getElementsByTagName('Name')[0]
bucket_names.append((endpoint,
bucket_name.firstChild.nodeValue.encode('utf-8'))
)
except (AttributeError, HTTPError, URLError):
continue
return bucket_names