mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-24 13:50:22 -07:00
Switch etcd and url lookup plugins to verify ssl certificates
This commit is contained in:
parent
4161d78a94
commit
77c76e632e
2 changed files with 27 additions and 17 deletions
|
@ -18,23 +18,25 @@ from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import urllib2
|
|
||||||
try:
|
try:
|
||||||
import json
|
import json
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
|
|
||||||
from ansible.plugins.lookup import LookupBase
|
from ansible.plugins.lookup import LookupBase
|
||||||
|
from ansible.module_utils.urls import open_url
|
||||||
|
|
||||||
# this can be made configurable, not should not use ansible.cfg
|
# this can be made configurable, not should not use ansible.cfg
|
||||||
ANSIBLE_ETCD_URL = 'http://127.0.0.1:4001'
|
ANSIBLE_ETCD_URL = 'http://127.0.0.1:4001'
|
||||||
if os.getenv('ANSIBLE_ETCD_URL') is not None:
|
if os.getenv('ANSIBLE_ETCD_URL') is not None:
|
||||||
ANSIBLE_ETCD_URL = os.environ['ANSIBLE_ETCD_URL']
|
ANSIBLE_ETCD_URL = os.environ['ANSIBLE_ETCD_URL']
|
||||||
|
|
||||||
class etcd():
|
class Etcd:
|
||||||
def __init__(self, url=ANSIBLE_ETCD_URL):
|
def __init__(self, url=ANSIBLE_ETCD_URL, validate_certs):
|
||||||
self.url = url
|
self.url = url
|
||||||
self.baseurl = '%s/v1/keys' % (self.url)
|
self.baseurl = '%s/v1/keys' % (self.url)
|
||||||
|
self.validate_certs = validate_certs
|
||||||
|
|
||||||
def get(self, key):
|
def get(self, key):
|
||||||
url = "%s/%s" % (self.baseurl, key)
|
url = "%s/%s" % (self.baseurl, key)
|
||||||
|
@ -42,7 +44,7 @@ class etcd():
|
||||||
data = None
|
data = None
|
||||||
value = ""
|
value = ""
|
||||||
try:
|
try:
|
||||||
r = urllib2.urlopen(url)
|
r = open_url(url, validate_certs=self.validate_certs)
|
||||||
data = r.read()
|
data = r.read()
|
||||||
except:
|
except:
|
||||||
return value
|
return value
|
||||||
|
@ -67,7 +69,9 @@ class LookupModule(LookupBase):
|
||||||
if isinstance(terms, basestring):
|
if isinstance(terms, basestring):
|
||||||
terms = [ terms ]
|
terms = [ terms ]
|
||||||
|
|
||||||
etcd = etcd()
|
validate_certs = kwargs.get('validate_certs', True)
|
||||||
|
|
||||||
|
etcd = Etcd(validate_certs=validate_certs)
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
for term in terms:
|
for term in terms:
|
||||||
|
|
|
@ -17,30 +17,36 @@
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
from ansible.plugins.lookup import LookupBase
|
|
||||||
import urllib2
|
import urllib2
|
||||||
|
|
||||||
|
from ansible.errors import AnsibleError
|
||||||
|
from ansible.plugins.lookup import LookupBase
|
||||||
|
from ansible.module_utils.urls import open_url, ConnectionError, SSLValidationError
|
||||||
|
from ansible.utils.unicode import to_unicode
|
||||||
|
|
||||||
class LookupModule(LookupBase):
|
class LookupModule(LookupBase):
|
||||||
|
|
||||||
|
|
||||||
def run(self, terms, inject=None, **kwargs):
|
def run(self, terms, variables=None, **kwargs):
|
||||||
|
|
||||||
if isinstance(terms, basestring):
|
if isinstance(terms, basestring):
|
||||||
terms = [ terms ]
|
terms = [ terms ]
|
||||||
|
|
||||||
|
validate_certs = kwargs.get('validate_certs', True)
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
for term in terms:
|
for term in terms:
|
||||||
try:
|
try:
|
||||||
r = urllib2.Request(term)
|
response = open_url(term, validate_certs=validate_certs)
|
||||||
response = urllib2.urlopen(r)
|
except urllib2.URLError as e:
|
||||||
except URLError as e:
|
raise AnsibleError("Failed lookup url for %s : %s" % (term, str(e)))
|
||||||
utils.warnings("Failed lookup url for %s : %s" % (term, str(e)))
|
except urllib2.HTTPError as e:
|
||||||
continue
|
raise AnsibleError("Received HTTP error for %s : %s" % (term, str(e)))
|
||||||
except HTTPError as e:
|
except SSLValidationError as e:
|
||||||
utils.warnings("Received HTTP error for %s : %s" % (term, str(e)))
|
raise AnsibleError("Error validating the server's certificate for %s: %s" % (term, str(e)))
|
||||||
continue
|
except ConnectionError as e:
|
||||||
|
raise AnsibleError("Error connecting to %s: %s" % (term, str(e)))
|
||||||
|
|
||||||
for line in response.read().splitlines():
|
for line in response.read().splitlines():
|
||||||
ret.append(line)
|
ret.append(to_unicode(line))
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue