|
@@ -8,15 +8,12 @@ import datetime
|
|
|
import io
|
|
|
import os
|
|
|
import subprocess
|
|
|
-import sys
|
|
|
-import tempfile
|
|
|
+import yaml
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-import six
|
|
|
from six.moves import configparser
|
|
|
|
|
|
-import yaml
|
|
|
+from ansible.module_utils.basic import AnsibleModule
|
|
|
+
|
|
|
try:
|
|
|
|
|
|
|
|
@@ -24,13 +21,14 @@ try:
|
|
|
|
|
|
|
|
|
import OpenSSL.crypto
|
|
|
+ HAS_OPENSSL = True
|
|
|
except ImportError:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- pass
|
|
|
+ HAS_OPENSSL = False
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
---
|
|
@@ -158,6 +156,10 @@ might return: [('O=system', 'nodes'), ('CN=system', 'node:m01.example.com')]
|
|
|
'subjectAltName'"""
|
|
|
return self.extensions[i]
|
|
|
|
|
|
+ def get_extension_count(self):
|
|
|
+ """ get_extension_count """
|
|
|
+ return len(self.extensions)
|
|
|
+
|
|
|
def get_notAfter(self):
|
|
|
"""Returns a date stamp as a string in the form
|
|
|
'20180922170439Z'. strptime the result with format param:
|
|
@@ -268,30 +270,23 @@ A tuple of the form:
|
|
|
|
|
|
|
|
|
|
|
|
- if 'OpenSSL.crypto' in sys.modules:
|
|
|
+ if HAS_OPENSSL:
|
|
|
|
|
|
cert_loaded = OpenSSL.crypto.load_certificate(
|
|
|
OpenSSL.crypto.FILETYPE_PEM, _cert_string)
|
|
|
else:
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
|
|
|
- _, path = tempfile.mkstemp()
|
|
|
- with io.open(path, 'w') as fp:
|
|
|
- fp.write(six.u(_cert_string))
|
|
|
- fp.flush()
|
|
|
-
|
|
|
- cmd = 'openssl x509 -in {} -text'.format(path)
|
|
|
+ cmd = 'openssl x509 -text'
|
|
|
try:
|
|
|
- openssl_decoded = subprocess.Popen(cmd.split(),
|
|
|
- stdout=subprocess.PIPE)
|
|
|
+ openssl_proc = subprocess.Popen(cmd.split(),
|
|
|
+ stdout=subprocess.PIPE,
|
|
|
+ stdin=subprocess.PIPE)
|
|
|
except OSError:
|
|
|
ans_module.fail_json(msg="Error: The 'OpenSSL' python library and CLI command were not found on the target host. Unable to parse any certificates. This host will not be included in generated reports.")
|
|
|
else:
|
|
|
- openssl_decoded = openssl_decoded.communicate()[0]
|
|
|
+ openssl_decoded = openssl_proc.communicate(_cert_string.encode('utf-8'))[0].decode('utf-8')
|
|
|
cert_loaded = FakeOpenSSLCertificate(openssl_decoded)
|
|
|
- finally:
|
|
|
- os.remove(path)
|
|
|
|
|
|
|
|
|
|
|
@@ -301,34 +296,12 @@ A tuple of the form:
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- i = 0
|
|
|
- checked_all_extensions = False
|
|
|
- while not checked_all_extensions:
|
|
|
- try:
|
|
|
-
|
|
|
- ext = cert_loaded.get_extension(i)
|
|
|
- except IndexError:
|
|
|
-
|
|
|
-
|
|
|
- san = None
|
|
|
- checked_all_extensions = True
|
|
|
- else:
|
|
|
-
|
|
|
- if ext.get_short_name() == 'subjectAltName':
|
|
|
- san = ext
|
|
|
- checked_all_extensions = True
|
|
|
- else:
|
|
|
-
|
|
|
- i += 1
|
|
|
+
|
|
|
+ san = None
|
|
|
+ for i in range(cert_loaded.get_extension_count()):
|
|
|
+ ext = cert_loaded.get_extension(i)
|
|
|
+ if ext.get_short_name() == 'subjectAltName':
|
|
|
+ san = ext
|
|
|
|
|
|
if san is not None:
|
|
|
|
|
@@ -341,9 +314,13 @@ A tuple of the form:
|
|
|
|
|
|
|
|
|
|
|
|
+ not_after = cert_loaded.get_notAfter()
|
|
|
+
|
|
|
+ if isinstance(not_after, bytes):
|
|
|
+ not_after = not_after.decode('utf-8')
|
|
|
+
|
|
|
cert_expiry_date = datetime.datetime.strptime(
|
|
|
- cert_loaded.get_notAfter(),
|
|
|
-
|
|
|
+ not_after,
|
|
|
'%Y%m%d%H%M%SZ')
|
|
|
|
|
|
time_remaining = cert_expiry_date - now
|
|
@@ -455,13 +432,11 @@ an OpenShift Container Platform cluster
|
|
|
)
|
|
|
|
|
|
|
|
|
- openshift_base_config_path = module.params['config_base']
|
|
|
- openshift_master_config_path = os.path.normpath(
|
|
|
- os.path.join(openshift_base_config_path, "master/master-config.yaml")
|
|
|
- )
|
|
|
- openshift_node_config_path = os.path.normpath(
|
|
|
- os.path.join(openshift_base_config_path, "node/node-config.yaml")
|
|
|
- )
|
|
|
+ openshift_base_config_path = os.path.realpath(module.params['config_base'])
|
|
|
+ openshift_master_config_path = os.path.join(openshift_base_config_path,
|
|
|
+ "master", "master-config.yaml")
|
|
|
+ openshift_node_config_path = os.path.join(openshift_base_config_path,
|
|
|
+ "node", "node-config.yaml")
|
|
|
openshift_cert_check_paths = [
|
|
|
openshift_master_config_path,
|
|
|
openshift_node_config_path,
|
|
@@ -476,9 +451,7 @@ an OpenShift Container Platform cluster
|
|
|
kubeconfig_paths = []
|
|
|
for m_kube_config in master_kube_configs:
|
|
|
kubeconfig_paths.append(
|
|
|
- os.path.normpath(
|
|
|
- os.path.join(openshift_base_config_path, "master/%s.kubeconfig" % m_kube_config)
|
|
|
- )
|
|
|
+ os.path.join(openshift_base_config_path, "master", m_kube_config + ".kubeconfig")
|
|
|
)
|
|
|
|
|
|
|
|
@@ -527,7 +500,7 @@ an OpenShift Container Platform cluster
|
|
|
|
|
|
for os_cert in filter_paths(openshift_cert_check_paths):
|
|
|
|
|
|
- with open(os_cert, 'r') as fp:
|
|
|
+ with io.open(os_cert, 'r', encoding='utf-8') as fp:
|
|
|
cert_meta = {}
|
|
|
cfg = yaml.load(fp)
|
|
|
|
|
@@ -542,7 +515,7 @@ an OpenShift Container Platform cluster
|
|
|
|
|
|
|
|
|
for _, v in cert_meta.items():
|
|
|
- with open(v, 'r') as fp:
|
|
|
+ with io.open(v, 'r', encoding='utf-8') as fp:
|
|
|
cert = fp.read()
|
|
|
(cert_subject,
|
|
|
cert_expiry_date,
|
|
@@ -575,7 +548,7 @@ an OpenShift Container Platform cluster
|
|
|
try:
|
|
|
|
|
|
|
|
|
- with open(openshift_node_config_path, 'r') as fp:
|
|
|
+ with io.open(openshift_node_config_path, 'r', encoding='utf-8') as fp:
|
|
|
cfg = yaml.load(fp)
|
|
|
|
|
|
|
|
@@ -588,7 +561,7 @@ an OpenShift Container Platform cluster
|
|
|
cfg_path = os.path.dirname(fp.name)
|
|
|
node_kubeconfig = os.path.join(cfg_path, node_masterKubeConfig)
|
|
|
|
|
|
- with open(node_kubeconfig, 'r') as fp:
|
|
|
+ with io.open(node_kubeconfig, 'r', encoding='utf8') as fp:
|
|
|
|
|
|
cfg = yaml.load(fp)
|
|
|
|
|
@@ -613,7 +586,7 @@ an OpenShift Container Platform cluster
|
|
|
pass
|
|
|
|
|
|
for kube in filter_paths(kubeconfig_paths):
|
|
|
- with open(kube, 'r') as fp:
|
|
|
+ with io.open(kube, 'r', encoding='utf-8') as fp:
|
|
|
|
|
|
cfg = yaml.load(fp)
|
|
|
|
|
@@ -656,7 +629,7 @@ an OpenShift Container Platform cluster
|
|
|
etcd_certs = []
|
|
|
etcd_cert_params.append('dne')
|
|
|
try:
|
|
|
- with open('/etc/etcd/etcd.conf', 'r') as fp:
|
|
|
+ with io.open('/etc/etcd/etcd.conf', 'r', encoding='utf-8') as fp:
|
|
|
etcd_config = configparser.ConfigParser()
|
|
|
|
|
|
|
|
@@ -675,7 +648,7 @@ an OpenShift Container Platform cluster
|
|
|
pass
|
|
|
|
|
|
for etcd_cert in filter_paths(etcd_certs_to_check):
|
|
|
- with open(etcd_cert, 'r') as fp:
|
|
|
+ with io.open(etcd_cert, 'r', encoding='utf-8') as fp:
|
|
|
c = fp.read()
|
|
|
(cert_subject,
|
|
|
cert_expiry_date,
|
|
@@ -697,7 +670,7 @@ an OpenShift Container Platform cluster
|
|
|
|
|
|
|
|
|
try:
|
|
|
- with open('/etc/origin/master/master-config.yaml', 'r') as fp:
|
|
|
+ with io.open('/etc/origin/master/master-config.yaml', 'r', encoding='utf-8') as fp:
|
|
|
cfg = yaml.load(fp)
|
|
|
except IOError:
|
|
|
|
|
@@ -864,10 +837,5 @@ an OpenShift Container Platform cluster
|
|
|
)
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-from ansible.module_utils.basic import AnsibleModule
|
|
|
if __name__ == '__main__':
|
|
|
main()
|