openshift_cert_expiry.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # pylint: disable=line-too-long,invalid-name
  4. """For details on this module see DOCUMENTATION (below)"""
  5. import datetime
  6. import io
  7. import os
  8. import subprocess
  9. import yaml
  10. # pylint import-error disabled because pylint cannot find the package
  11. # when installed in a virtualenv
  12. from ansible.module_utils.six.moves import configparser # pylint: disable=import-error
  13. from ansible.module_utils.basic import AnsibleModule
  14. try:
  15. # You can comment this import out and include a 'pass' in this
  16. # block if you're manually testing this module on a NON-ATOMIC
  17. # HOST (or any host that just doesn't have PyOpenSSL
  18. # available). That will force the `load_and_handle_cert` function
  19. # to use the Fake OpenSSL classes.
  20. import OpenSSL.crypto
  21. HAS_OPENSSL = True
  22. except ImportError:
  23. # Some platforms (such as RHEL Atomic) may not have the Python
  24. # OpenSSL library installed. In this case we will use a manual
  25. # work-around to parse each certificate.
  26. #
  27. # Check for 'OpenSSL.crypto' in `sys.modules` later.
  28. HAS_OPENSSL = False
  29. DOCUMENTATION = '''
  30. ---
  31. module: openshift_cert_expiry
  32. short_description: Check OpenShift Container Platform (OCP) and Kube certificate expirations on a cluster
  33. description:
  34. - The M(openshift_cert_expiry) module has two basic functions: to flag certificates which will expire in a set window of time from now, and to notify you about certificates which have already expired.
  35. - When the module finishes, a summary of the examination is returned. Each certificate in the summary has a C(health) key with a value of one of the following:
  36. - C(ok) - not expired, and outside of the expiration C(warning_days) window.
  37. - C(warning) - not expired, but will expire between now and the C(warning_days) window.
  38. - C(expired) - an expired certificate.
  39. - Certificate flagging follow this logic:
  40. - If the expiration date is before now then the certificate is classified as C(expired).
  41. - The certificates time to live (expiration date - now) is calculated, if that time window is less than C(warning_days) the certificate is classified as C(warning).
  42. - All other conditions are classified as C(ok).
  43. - The following keys are ALSO present in the certificate summary:
  44. - C(cert_cn) - The common name of the certificate (additional CNs present in SAN extensions are omitted)
  45. - C(days_remaining) - The number of days until the certificate expires.
  46. - C(expiry) - The date the certificate expires on.
  47. - C(path) - The full path to the certificate on the examined host.
  48. version_added: "1.0"
  49. options:
  50. config_base:
  51. description:
  52. - Base path to OCP system settings.
  53. required: false
  54. default: /etc/origin
  55. warning_days:
  56. description:
  57. - Flag certificates which will expire in C(warning_days) days from now.
  58. required: false
  59. default: 30
  60. show_all:
  61. description:
  62. - Enable this option to show analysis of ALL certificates examined by this module.
  63. - By default only certificates which have expired, or will expire within the C(warning_days) window will be reported.
  64. required: false
  65. default: false
  66. author: "Tim Bielawa (@tbielawa) <tbielawa@redhat.com>"
  67. '''
  68. EXAMPLES = '''
  69. # Default invocation, only notify about expired certificates or certificates which will expire within 30 days from now
  70. - openshift_cert_expiry:
  71. # Expand the warning window to show certificates expiring within a year from now
  72. - openshift_cert_expiry: warning_days=365
  73. # Show expired, soon to expire (now + 30 days), and all other certificates examined
  74. - openshift_cert_expiry: show_all=true
  75. '''
  76. class FakeOpenSSLCertificate(object):
  77. """This provides a rough mock of what you get from
  78. `OpenSSL.crypto.load_certificate()`. This is a work-around for
  79. platforms missing the Python OpenSSL library.
  80. """
  81. def __init__(self, cert_string):
  82. """`cert_string` is a certificate in the form you get from running a
  83. .crt through 'openssl x509 -in CERT.cert -text'"""
  84. self.cert_string = cert_string
  85. self.serial = None
  86. self.subject = None
  87. self.extensions = []
  88. self.not_after = None
  89. self._parse_cert()
  90. def _parse_cert(self):
  91. """Manually parse the certificate line by line"""
  92. self.extensions = []
  93. PARSING_ALT_NAMES = False
  94. for line in self.cert_string.split('\n'):
  95. l = line.strip()
  96. if PARSING_ALT_NAMES:
  97. # We're parsing a 'Subject Alternative Name' line
  98. self.extensions.append(
  99. FakeOpenSSLCertificateSANExtension(l))
  100. PARSING_ALT_NAMES = False
  101. continue
  102. # parse out the bits that we can
  103. if l.startswith('Serial Number:'):
  104. # Serial Number: 11 (0xb)
  105. # => 11
  106. self.serial = int(l.split()[-2])
  107. elif l.startswith('Not After :'):
  108. # Not After : Feb 7 18:19:35 2019 GMT
  109. # => strptime(str, '%b %d %H:%M:%S %Y %Z')
  110. # => strftime('%Y%m%d%H%M%SZ')
  111. # => 20190207181935Z
  112. not_after_raw = l.partition(' : ')[-1]
  113. # Last item: ('Not After', ' : ', 'Feb 7 18:19:35 2019 GMT')
  114. not_after_parsed = datetime.datetime.strptime(not_after_raw, '%b %d %H:%M:%S %Y %Z')
  115. self.not_after = not_after_parsed.strftime('%Y%m%d%H%M%SZ')
  116. elif l.startswith('X509v3 Subject Alternative Name:'):
  117. PARSING_ALT_NAMES = True
  118. continue
  119. elif l.startswith('Subject:'):
  120. # O=system:nodes, CN=system:node:m01.example.com
  121. self.subject = FakeOpenSSLCertificateSubjects(l.partition(': ')[-1])
  122. def get_serial_number(self):
  123. """Return the serial number of the cert"""
  124. return self.serial
  125. def get_subject(self):
  126. """Subjects must implement get_components() and return dicts or
  127. tuples. An 'openssl x509 -in CERT.cert -text' with 'Subject':
  128. Subject: Subject: O=system:nodes, CN=system:node:m01.example.com
  129. might return: [('O=system', 'nodes'), ('CN=system', 'node:m01.example.com')]
  130. """
  131. return self.subject
  132. def get_extension(self, i):
  133. """Extensions must implement get_short_name() and return the string
  134. 'subjectAltName'"""
  135. return self.extensions[i]
  136. def get_extension_count(self):
  137. """ get_extension_count """
  138. return len(self.extensions)
  139. def get_notAfter(self):
  140. """Returns a date stamp as a string in the form
  141. '20180922170439Z'. strptime the result with format param:
  142. '%Y%m%d%H%M%SZ'."""
  143. return self.not_after
  144. class FakeOpenSSLCertificateSANExtension(object): # pylint: disable=too-few-public-methods
  145. """Mocks what happens when `get_extension` is called on a certificate
  146. object"""
  147. def __init__(self, san_string):
  148. """With `san_string` as you get from:
  149. $ openssl x509 -in certificate.crt -text
  150. """
  151. self.san_string = san_string
  152. self.short_name = 'subjectAltName'
  153. def get_short_name(self):
  154. """Return the 'type' of this extension. It's always the same though
  155. because we only care about subjectAltName's"""
  156. return self.short_name
  157. def __str__(self):
  158. """Return this extension and the value as a simple string"""
  159. return self.san_string
  160. # pylint: disable=too-few-public-methods
  161. class FakeOpenSSLCertificateSubjects(object):
  162. """Mocks what happens when `get_subject` is called on a certificate
  163. object"""
  164. def __init__(self, subject_string):
  165. """With `subject_string` as you get from:
  166. $ openssl x509 -in certificate.crt -text
  167. """
  168. self.subjects = []
  169. for s in subject_string.split(', '):
  170. name, _, value = s.partition('=')
  171. self.subjects.append((name, value))
  172. def get_components(self):
  173. """Returns a list of tuples"""
  174. return self.subjects
  175. # We only need this for one thing, we don't care if it doesn't have
  176. # that many public methods
  177. #
  178. # pylint: disable=too-few-public-methods
  179. class FakeSecHead(object):
  180. """etcd does not begin their config file with an opening [section] as
  181. required by the Python ConfigParser module. We hack around it by
  182. slipping one in ourselves prior to parsing.
  183. Source: Alex Martelli - http://stackoverflow.com/a/2819788/6490583
  184. """
  185. def __init__(self, fp):
  186. self.fp = fp
  187. self.sechead = '[ETCD]\n'
  188. def readline(self):
  189. """Make this look like a file-type object"""
  190. if self.sechead:
  191. try:
  192. return self.sechead
  193. finally:
  194. self.sechead = None
  195. else:
  196. return self.fp.readline()
  197. ######################################################################
  198. def filter_paths(path_list):
  199. """`path_list` - A list of file paths to check. Only files which exist
  200. will be returned
  201. """
  202. return [p for p in path_list if os.path.exists(os.path.realpath(p))]
  203. # pylint: disable=too-many-locals,too-many-branches
  204. #
  205. # TODO: Break this function down into smaller chunks
  206. def load_and_handle_cert(cert_string, now, base64decode=False, ans_module=None):
  207. """Load a certificate, split off the good parts, and return some
  208. useful data
  209. Params:
  210. - `cert_string` (string) - a certificate loaded into a string object
  211. - `now` (datetime) - a datetime object of the time to calculate the certificate 'time_remaining' against
  212. - `base64decode` (bool) - run .decode('base64') on the input?
  213. - `ans_module` (AnsibleModule) - The AnsibleModule object for this module (so we can raise errors)
  214. Returns:
  215. A tuple of the form:
  216. (cert_subject, cert_expiry_date, time_remaining, cert_serial_number)
  217. """
  218. if base64decode:
  219. _cert_string = cert_string.decode('base-64')
  220. else:
  221. _cert_string = cert_string
  222. # Disable this. We 'redefine' the type because we are working
  223. # around a missing library on the target host.
  224. #
  225. # pylint: disable=redefined-variable-type
  226. if HAS_OPENSSL:
  227. # No work-around required
  228. cert_loaded = OpenSSL.crypto.load_certificate(
  229. OpenSSL.crypto.FILETYPE_PEM, _cert_string)
  230. else:
  231. # Missing library, work-around required. Run the 'openssl'
  232. # command on it to decode it
  233. cmd = 'openssl x509 -text'
  234. try:
  235. openssl_proc = subprocess.Popen(cmd.split(),
  236. stdout=subprocess.PIPE,
  237. stdin=subprocess.PIPE)
  238. except OSError:
  239. 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.")
  240. else:
  241. openssl_decoded = openssl_proc.communicate(_cert_string.encode('utf-8'))[0].decode('utf-8')
  242. cert_loaded = FakeOpenSSLCertificate(openssl_decoded)
  243. ######################################################################
  244. # Read all possible names from the cert
  245. cert_subjects = []
  246. for name, value in cert_loaded.get_subject().get_components():
  247. cert_subjects.append('{}:{}'.format(name, value))
  248. # To read SANs from a cert we must read the subjectAltName
  249. # extension from the X509 Object. What makes this more difficult
  250. # is that pyOpenSSL does not give extensions as an iterable
  251. san = None
  252. for i in range(cert_loaded.get_extension_count()):
  253. ext = cert_loaded.get_extension(i)
  254. if ext.get_short_name() == 'subjectAltName':
  255. san = ext
  256. if san is not None:
  257. # The X509Extension object for subjectAltName prints as a
  258. # string with the alt names separated by a comma and a
  259. # space. Split the string by ', ' and then add our new names
  260. # to the list of existing names
  261. cert_subjects.extend(str(san).split(', '))
  262. cert_subject = ', '.join(cert_subjects)
  263. ######################################################################
  264. # Grab the expiration date
  265. not_after = cert_loaded.get_notAfter()
  266. # example get_notAfter() => 20180922170439Z
  267. if isinstance(not_after, bytes):
  268. not_after = not_after.decode('utf-8')
  269. cert_expiry_date = datetime.datetime.strptime(
  270. not_after,
  271. '%Y%m%d%H%M%SZ')
  272. time_remaining = cert_expiry_date - now
  273. return (cert_subject, cert_expiry_date, time_remaining, cert_loaded.get_serial_number())
  274. def classify_cert(cert_meta, now, time_remaining, expire_window, cert_list):
  275. """Given metadata about a certificate under examination, classify it
  276. into one of three categories, 'ok', 'warning', and 'expired'.
  277. Params:
  278. - `cert_meta` dict - A dict with certificate metadata. Required fields
  279. include: 'cert_cn', 'path', 'expiry', 'days_remaining', 'health'.
  280. - `now` (datetime) - a datetime object of the time to calculate the certificate 'time_remaining' against
  281. - `time_remaining` (datetime.timedelta) - a timedelta for how long until the cert expires
  282. - `expire_window` (datetime.timedelta) - a timedelta for how long the warning window is
  283. - `cert_list` list - A list to shove the classified cert into
  284. Return:
  285. - `cert_list` - The updated list of classified certificates
  286. """
  287. expiry_str = str(cert_meta['expiry'])
  288. # Categorization
  289. if cert_meta['expiry'] < now:
  290. # This already expired, must NOTIFY
  291. cert_meta['health'] = 'expired'
  292. elif time_remaining < expire_window:
  293. # WARN about this upcoming expirations
  294. cert_meta['health'] = 'warning'
  295. else:
  296. # Not expired or about to expire
  297. cert_meta['health'] = 'ok'
  298. cert_meta['expiry'] = expiry_str
  299. cert_meta['serial_hex'] = hex(int(cert_meta['serial']))
  300. cert_list.append(cert_meta)
  301. return cert_list
  302. def tabulate_summary(certificates, kubeconfigs, etcd_certs, router_certs, registry_certs):
  303. """Calculate the summary text for when the module finishes
  304. running. This includes counts of each classification and what have
  305. you.
  306. Params:
  307. - `certificates` (list of dicts) - Processed `expire_check_result`
  308. dicts with filled in `health` keys for system certificates.
  309. - `kubeconfigs` - as above for kubeconfigs
  310. - `etcd_certs` - as above for etcd certs
  311. Return:
  312. - `summary_results` (dict) - Counts of each cert type classification
  313. and total items examined.
  314. """
  315. items = certificates + kubeconfigs + etcd_certs + router_certs + registry_certs
  316. summary_results = {
  317. 'system_certificates': len(certificates),
  318. 'kubeconfig_certificates': len(kubeconfigs),
  319. 'etcd_certificates': len(etcd_certs),
  320. 'router_certs': len(router_certs),
  321. 'registry_certs': len(registry_certs),
  322. 'total': len(items),
  323. 'ok': 0,
  324. 'warning': 0,
  325. 'expired': 0
  326. }
  327. summary_results['expired'] = len([c for c in items if c['health'] == 'expired'])
  328. summary_results['warning'] = len([c for c in items if c['health'] == 'warning'])
  329. summary_results['ok'] = len([c for c in items if c['health'] == 'ok'])
  330. return summary_results
  331. ######################################################################
  332. # This is our module MAIN function after all, so there's bound to be a
  333. # lot of code bundled up into one block
  334. #
  335. # Reason: These checks are disabled because the issue was introduced
  336. # during a period where the pylint checks weren't enabled for this file
  337. # Status: temporarily disabled pending future refactoring
  338. # pylint: disable=too-many-locals,too-many-statements,too-many-branches
  339. def main():
  340. """This module examines certificates (in various forms) which compose
  341. an OpenShift Container Platform cluster
  342. """
  343. module = AnsibleModule(
  344. argument_spec=dict(
  345. config_base=dict(
  346. required=False,
  347. default="/etc/origin",
  348. type='str'),
  349. warning_days=dict(
  350. required=False,
  351. default=30,
  352. type='int'),
  353. show_all=dict(
  354. required=False,
  355. default=False,
  356. type='bool')
  357. ),
  358. supports_check_mode=True,
  359. )
  360. # Basic scaffolding for OpenShift specific certs
  361. openshift_base_config_path = os.path.realpath(module.params['config_base'])
  362. openshift_master_config_path = os.path.join(openshift_base_config_path,
  363. "master", "master-config.yaml")
  364. openshift_node_config_path = os.path.join(openshift_base_config_path,
  365. "node", "node-config.yaml")
  366. openshift_cert_check_paths = [
  367. openshift_master_config_path,
  368. openshift_node_config_path,
  369. ]
  370. # Paths for Kubeconfigs. Additional kubeconfigs are conditionally
  371. # checked later in the code
  372. master_kube_configs = ['admin', 'openshift-master',
  373. 'openshift-node', 'openshift-router',
  374. 'openshift-registry']
  375. kubeconfig_paths = []
  376. for m_kube_config in master_kube_configs:
  377. kubeconfig_paths.append(
  378. os.path.join(openshift_base_config_path, "master", m_kube_config + ".kubeconfig")
  379. )
  380. # Validate some paths we have the ability to do ahead of time
  381. openshift_cert_check_paths = filter_paths(openshift_cert_check_paths)
  382. kubeconfig_paths = filter_paths(kubeconfig_paths)
  383. # etcd, where do you hide your certs? Used when parsing etcd.conf
  384. etcd_cert_params = [
  385. "ETCD_CA_FILE",
  386. "ETCD_CERT_FILE",
  387. "ETCD_PEER_CA_FILE",
  388. "ETCD_PEER_CERT_FILE",
  389. ]
  390. # Expiry checking stuff
  391. now = datetime.datetime.now()
  392. # todo, catch exception for invalid input and return a fail_json
  393. warning_days = int(module.params['warning_days'])
  394. expire_window = datetime.timedelta(days=warning_days)
  395. # Module stuff
  396. #
  397. # The results of our cert checking to return from the task call
  398. check_results = {}
  399. check_results['meta'] = {}
  400. check_results['meta']['warning_days'] = warning_days
  401. check_results['meta']['checked_at_time'] = str(now)
  402. check_results['meta']['warn_before_date'] = str(now + expire_window)
  403. check_results['meta']['show_all'] = str(module.params['show_all'])
  404. # All the analyzed certs accumulate here
  405. ocp_certs = []
  406. ######################################################################
  407. # Sure, why not? Let's enable check mode.
  408. if module.check_mode:
  409. check_results['ocp_certs'] = []
  410. module.exit_json(
  411. check_results=check_results,
  412. msg="Checked 0 total certificates. Expired/Warning/OK: 0/0/0. Warning window: %s days" % module.params['warning_days'],
  413. rc=0,
  414. changed=False
  415. )
  416. ######################################################################
  417. # Check for OpenShift Container Platform specific certs
  418. ######################################################################
  419. for os_cert in filter_paths(openshift_cert_check_paths):
  420. # Open up that config file and locate the cert and CA
  421. with io.open(os_cert, 'r', encoding='utf-8') as fp:
  422. cert_meta = {}
  423. cfg = yaml.load(fp)
  424. # cert files are specified in parsed `fp` as relative to the path
  425. # of the original config file. 'master-config.yaml' with certFile
  426. # = 'foo.crt' implies that 'foo.crt' is in the same
  427. # directory. certFile = '../foo.crt' is in the parent directory.
  428. cfg_path = os.path.dirname(fp.name)
  429. cert_meta['certFile'] = os.path.join(cfg_path, cfg['servingInfo']['certFile'])
  430. cert_meta['clientCA'] = os.path.join(cfg_path, cfg['servingInfo']['clientCA'])
  431. ######################################################################
  432. # Load the certificate and the CA, parse their expiration dates into
  433. # datetime objects so we can manipulate them later
  434. for _, v in cert_meta.items():
  435. with io.open(v, 'r', encoding='utf-8') as fp:
  436. cert = fp.read()
  437. (cert_subject,
  438. cert_expiry_date,
  439. time_remaining,
  440. cert_serial) = load_and_handle_cert(cert, now, ans_module=module)
  441. expire_check_result = {
  442. 'cert_cn': cert_subject,
  443. 'path': fp.name,
  444. 'expiry': cert_expiry_date,
  445. 'days_remaining': time_remaining.days,
  446. 'health': None,
  447. 'serial': cert_serial
  448. }
  449. classify_cert(expire_check_result, now, time_remaining, expire_window, ocp_certs)
  450. ######################################################################
  451. # /Check for OpenShift Container Platform specific certs
  452. ######################################################################
  453. ######################################################################
  454. # Check service Kubeconfigs
  455. ######################################################################
  456. kubeconfigs = []
  457. # There may be additional kubeconfigs to check, but their naming
  458. # is less predictable than the ones we've already assembled.
  459. try:
  460. # Try to read the standard 'node-config.yaml' file to check if
  461. # this host is a node.
  462. with io.open(openshift_node_config_path, 'r', encoding='utf-8') as fp:
  463. cfg = yaml.load(fp)
  464. # OK, the config file exists, therefore this is a
  465. # node. Nodes have their own kubeconfig files to
  466. # communicate with the master API. Let's read the relative
  467. # path to that file from the node config.
  468. node_masterKubeConfig = cfg['masterKubeConfig']
  469. # As before, the path to the 'masterKubeConfig' file is
  470. # relative to `fp`
  471. cfg_path = os.path.dirname(fp.name)
  472. node_kubeconfig = os.path.join(cfg_path, node_masterKubeConfig)
  473. with io.open(node_kubeconfig, 'r', encoding='utf8') as fp:
  474. # Read in the nodes kubeconfig file and grab the good stuff
  475. cfg = yaml.load(fp)
  476. c = cfg['users'][0]['user']['client-certificate-data']
  477. (cert_subject,
  478. cert_expiry_date,
  479. time_remaining,
  480. cert_serial) = load_and_handle_cert(c, now, base64decode=True, ans_module=module)
  481. expire_check_result = {
  482. 'cert_cn': cert_subject,
  483. 'path': fp.name,
  484. 'expiry': cert_expiry_date,
  485. 'days_remaining': time_remaining.days,
  486. 'health': None,
  487. 'serial': cert_serial
  488. }
  489. classify_cert(expire_check_result, now, time_remaining, expire_window, kubeconfigs)
  490. except IOError:
  491. # This is not a node
  492. pass
  493. for kube in filter_paths(kubeconfig_paths):
  494. with io.open(kube, 'r', encoding='utf-8') as fp:
  495. # TODO: Maybe consider catching exceptions here?
  496. cfg = yaml.load(fp)
  497. # Per conversation, "the kubeconfigs you care about:
  498. # admin, router, registry should all be single
  499. # value". Following that advice we only grab the data for
  500. # the user at index 0 in the 'users' list. There should
  501. # not be more than one user.
  502. c = cfg['users'][0]['user']['client-certificate-data']
  503. (cert_subject,
  504. cert_expiry_date,
  505. time_remaining,
  506. cert_serial) = load_and_handle_cert(c, now, base64decode=True, ans_module=module)
  507. expire_check_result = {
  508. 'cert_cn': cert_subject,
  509. 'path': fp.name,
  510. 'expiry': cert_expiry_date,
  511. 'days_remaining': time_remaining.days,
  512. 'health': None,
  513. 'serial': cert_serial
  514. }
  515. classify_cert(expire_check_result, now, time_remaining, expire_window, kubeconfigs)
  516. ######################################################################
  517. # /Check service Kubeconfigs
  518. ######################################################################
  519. ######################################################################
  520. # Check etcd certs
  521. #
  522. # Two things to check: 'external' etcd, and embedded etcd.
  523. ######################################################################
  524. # FIRST: The 'external' etcd
  525. #
  526. # Some values may be duplicated, make this a set for now so we
  527. # unique them all
  528. etcd_certs_to_check = set([])
  529. etcd_certs = []
  530. etcd_cert_params.append('dne')
  531. try:
  532. with io.open('/etc/etcd/etcd.conf', 'r', encoding='utf-8') as fp:
  533. etcd_config = configparser.ConfigParser()
  534. # Reason: This check is disabled because the issue was introduced
  535. # during a period where the pylint checks weren't enabled for this file
  536. # Status: temporarily disabled pending future refactoring
  537. # pylint: disable=deprecated-method
  538. etcd_config.readfp(FakeSecHead(fp))
  539. for param in etcd_cert_params:
  540. try:
  541. etcd_certs_to_check.add(etcd_config.get('ETCD', param))
  542. except configparser.NoOptionError:
  543. # That parameter does not exist, oh well...
  544. pass
  545. except IOError:
  546. # No etcd to see here, move along
  547. pass
  548. for etcd_cert in filter_paths(etcd_certs_to_check):
  549. with io.open(etcd_cert, 'r', encoding='utf-8') as fp:
  550. c = fp.read()
  551. (cert_subject,
  552. cert_expiry_date,
  553. time_remaining,
  554. cert_serial) = load_and_handle_cert(c, now, ans_module=module)
  555. expire_check_result = {
  556. 'cert_cn': cert_subject,
  557. 'path': fp.name,
  558. 'expiry': cert_expiry_date,
  559. 'days_remaining': time_remaining.days,
  560. 'health': None,
  561. 'serial': cert_serial
  562. }
  563. classify_cert(expire_check_result, now, time_remaining, expire_window, etcd_certs)
  564. ######################################################################
  565. # Now the embedded etcd
  566. ######################################################################
  567. try:
  568. with io.open('/etc/origin/master/master-config.yaml', 'r', encoding='utf-8') as fp:
  569. cfg = yaml.load(fp)
  570. except IOError:
  571. # Not present
  572. pass
  573. else:
  574. if cfg.get('etcdConfig', {}).get('servingInfo', {}).get('certFile', None) is not None:
  575. # This is embedded
  576. etcd_crt_name = cfg['etcdConfig']['servingInfo']['certFile']
  577. else:
  578. # Not embedded
  579. etcd_crt_name = None
  580. if etcd_crt_name is not None:
  581. # etcd_crt_name is relative to the location of the
  582. # master-config.yaml file
  583. cfg_path = os.path.dirname(fp.name)
  584. etcd_cert = os.path.join(cfg_path, etcd_crt_name)
  585. with open(etcd_cert, 'r') as etcd_fp:
  586. (cert_subject,
  587. cert_expiry_date,
  588. time_remaining,
  589. cert_serial) = load_and_handle_cert(etcd_fp.read(), now, ans_module=module)
  590. expire_check_result = {
  591. 'cert_cn': cert_subject,
  592. 'path': etcd_fp.name,
  593. 'expiry': cert_expiry_date,
  594. 'days_remaining': time_remaining.days,
  595. 'health': None,
  596. 'serial': cert_serial
  597. }
  598. classify_cert(expire_check_result, now, time_remaining, expire_window, etcd_certs)
  599. ######################################################################
  600. # /Check etcd certs
  601. ######################################################################
  602. ######################################################################
  603. # Check router/registry certs
  604. #
  605. # These are saved as secrets in etcd. That means that we can not
  606. # simply read a file to grab the data. Instead we're going to
  607. # subprocess out to the 'oc get' command. On non-masters this
  608. # command will fail, that is expected so we catch that exception.
  609. ######################################################################
  610. router_certs = []
  611. registry_certs = []
  612. ######################################################################
  613. # First the router certs
  614. try:
  615. router_secrets_raw = subprocess.Popen('oc get -n default secret router-certs -o yaml'.split(),
  616. stdout=subprocess.PIPE)
  617. router_ds = yaml.load(router_secrets_raw.communicate()[0])
  618. router_c = router_ds['data']['tls.crt']
  619. router_path = router_ds['metadata']['selfLink']
  620. except TypeError:
  621. # YAML couldn't load the result, this is not a master
  622. pass
  623. except OSError:
  624. # The OC command doesn't exist here. Move along.
  625. pass
  626. else:
  627. (cert_subject,
  628. cert_expiry_date,
  629. time_remaining,
  630. cert_serial) = load_and_handle_cert(router_c, now, base64decode=True, ans_module=module)
  631. expire_check_result = {
  632. 'cert_cn': cert_subject,
  633. 'path': router_path,
  634. 'expiry': cert_expiry_date,
  635. 'days_remaining': time_remaining.days,
  636. 'health': None,
  637. 'serial': cert_serial
  638. }
  639. classify_cert(expire_check_result, now, time_remaining, expire_window, router_certs)
  640. ######################################################################
  641. # Now for registry
  642. try:
  643. registry_secrets_raw = subprocess.Popen('oc get -n default secret registry-certificates -o yaml'.split(),
  644. stdout=subprocess.PIPE)
  645. registry_ds = yaml.load(registry_secrets_raw.communicate()[0])
  646. registry_c = registry_ds['data']['registry.crt']
  647. registry_path = registry_ds['metadata']['selfLink']
  648. except TypeError:
  649. # YAML couldn't load the result, this is not a master
  650. pass
  651. except OSError:
  652. # The OC command doesn't exist here. Move along.
  653. pass
  654. else:
  655. (cert_subject,
  656. cert_expiry_date,
  657. time_remaining,
  658. cert_serial) = load_and_handle_cert(registry_c, now, base64decode=True, ans_module=module)
  659. expire_check_result = {
  660. 'cert_cn': cert_subject,
  661. 'path': registry_path,
  662. 'expiry': cert_expiry_date,
  663. 'days_remaining': time_remaining.days,
  664. 'health': None,
  665. 'serial': cert_serial
  666. }
  667. classify_cert(expire_check_result, now, time_remaining, expire_window, registry_certs)
  668. ######################################################################
  669. # /Check router/registry certs
  670. ######################################################################
  671. res = tabulate_summary(ocp_certs, kubeconfigs, etcd_certs, router_certs, registry_certs)
  672. msg = "Checked {count} total certificates. Expired/Warning/OK: {exp}/{warn}/{ok}. Warning window: {window} days".format(
  673. count=res['total'],
  674. exp=res['expired'],
  675. warn=res['warning'],
  676. ok=res['ok'],
  677. window=int(module.params['warning_days']),
  678. )
  679. # By default we only return detailed information about expired or
  680. # warning certificates. If show_all is true then we will print all
  681. # the certificates examined.
  682. if not module.params['show_all']:
  683. check_results['ocp_certs'] = [crt for crt in ocp_certs if crt['health'] in ['expired', 'warning']]
  684. check_results['kubeconfigs'] = [crt for crt in kubeconfigs if crt['health'] in ['expired', 'warning']]
  685. check_results['etcd'] = [crt for crt in etcd_certs if crt['health'] in ['expired', 'warning']]
  686. check_results['registry'] = [crt for crt in registry_certs if crt['health'] in ['expired', 'warning']]
  687. check_results['router'] = [crt for crt in router_certs if crt['health'] in ['expired', 'warning']]
  688. else:
  689. check_results['ocp_certs'] = ocp_certs
  690. check_results['kubeconfigs'] = kubeconfigs
  691. check_results['etcd'] = etcd_certs
  692. check_results['registry'] = registry_certs
  693. check_results['router'] = router_certs
  694. # Sort the final results to report in order of ascending safety
  695. # time. That is to say, the certificates which will expire sooner
  696. # will be at the front of the list and certificates which will
  697. # expire later are at the end. Router and registry certs should be
  698. # limited to just 1 result, so don't bother sorting those.
  699. def cert_key(item):
  700. ''' return the days_remaining key '''
  701. return item['days_remaining']
  702. check_results['ocp_certs'] = sorted(check_results['ocp_certs'], key=cert_key)
  703. check_results['kubeconfigs'] = sorted(check_results['kubeconfigs'], key=cert_key)
  704. check_results['etcd'] = sorted(check_results['etcd'], key=cert_key)
  705. # This module will never change anything, but we might want to
  706. # change the return code parameter if there is some catastrophic
  707. # error we noticed earlier
  708. module.exit_json(
  709. check_results=check_results,
  710. summary=res,
  711. msg=msg,
  712. rc=0,
  713. changed=False
  714. )
  715. if __name__ == '__main__':
  716. main()