oc_adm_csr.py 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. #!/usr/bin/env python
  2. # pylint: disable=missing-docstring
  3. # flake8: noqa: T001
  4. # ___ ___ _ _ ___ ___ _ _____ ___ ___
  5. # / __| __| \| | __| _ \ /_\_ _| __| \
  6. # | (_ | _|| .` | _|| / / _ \| | | _|| |) |
  7. # \___|___|_|\_|___|_|_\/_/_\_\_|_|___|___/_ _____
  8. # | \ / _ \ | \| |/ _ \_ _| | __| \_ _|_ _|
  9. # | |) | (_) | | .` | (_) || | | _|| |) | | | |
  10. # |___/ \___/ |_|\_|\___/ |_| |___|___/___| |_|
  11. #
  12. # Copyright 2016 Red Hat, Inc. and/or its affiliates
  13. # and other contributors as indicated by the @author tags.
  14. #
  15. # Licensed under the Apache License, Version 2.0 (the "License");
  16. # you may not use this file except in compliance with the License.
  17. # You may obtain a copy of the License at
  18. #
  19. # http://www.apache.org/licenses/LICENSE-2.0
  20. #
  21. # Unless required by applicable law or agreed to in writing, software
  22. # distributed under the License is distributed on an "AS IS" BASIS,
  23. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. # See the License for the specific language governing permissions and
  25. # limitations under the License.
  26. #
  27. # -*- -*- -*- Begin included fragment: lib/import.py -*- -*- -*-
  28. '''
  29. OpenShiftCLI class that wraps the oc commands in a subprocess
  30. '''
  31. # pylint: disable=too-many-lines
  32. from __future__ import print_function
  33. import atexit
  34. import copy
  35. import fcntl
  36. import json
  37. import time
  38. import os
  39. import re
  40. import shutil
  41. import subprocess
  42. import tempfile
  43. # pylint: disable=import-error
  44. try:
  45. import ruamel.yaml as yaml
  46. except ImportError:
  47. import yaml
  48. from ansible.module_utils.basic import AnsibleModule
  49. # -*- -*- -*- End included fragment: lib/import.py -*- -*- -*-
  50. # -*- -*- -*- Begin included fragment: doc/csr -*- -*- -*-
  51. DOCUMENTATION = '''
  52. ---
  53. module: oc_adm_csr
  54. short_description: Module to approve or deny openshift certificate signing requests
  55. description:
  56. - Wrapper around the openshift `oc adm certificate approve|deny <csr>` command.
  57. options:
  58. state:
  59. description:
  60. - State controls the action that will be taken with resource
  61. - approve - create the certificate
  62. - deny - remove the certificate
  63. - list - return the current representation of a certificate
  64. required: false
  65. default: approve
  66. choices: ["approve", "deny", "list"]
  67. aliases: []
  68. kubeconfig:
  69. description:
  70. - The path for the kubeconfig file to use for authentication
  71. required: false
  72. default: /etc/origin/master/admin.kubeconfig
  73. aliases: []
  74. debug:
  75. description:
  76. - Turn on debug output.
  77. required: false
  78. default: False
  79. aliases: []
  80. nodes:
  81. description:
  82. - A list of the names of the nodes in which to accept the certificates
  83. required: false
  84. default: None
  85. aliases: []
  86. timeout:
  87. description:
  88. - This flag allows for a timeout value when approving nodes.
  89. required: false
  90. default: 30
  91. aliases: []
  92. timeout:
  93. description:
  94. - This flag allows for a timeout value when doing node approvals.
  95. - A zero value for the timeout will block until the nodes have been accepted
  96. required: false
  97. default: 30
  98. aliases: []
  99. approve_all:
  100. description:
  101. - This flag allows for the module to approve all CSRs that are found.
  102. - This facilitates testing.
  103. required: false
  104. default: False
  105. aliases: []
  106. service_account:
  107. description:
  108. - This parameter tells the approval process which service account is being used for the requests
  109. required: false
  110. default: node-bootstrapper
  111. aliases: []
  112. author:
  113. - "Kenny Woodson <kwoodson@redhat.com>"
  114. extends_documentation_fragment: []
  115. '''
  116. EXAMPLES = '''
  117. - name: Approve certificates for node xyz
  118. oc_adm_scr:
  119. nodes:
  120. - xyz
  121. timeout: 300
  122. - name: Approve certificates for node xyz
  123. oc_adm_scr:
  124. nodes:
  125. - xyz
  126. timeout: 0
  127. '''
  128. # -*- -*- -*- End included fragment: doc/csr -*- -*- -*-
  129. # -*- -*- -*- Begin included fragment: ../../lib_utils/src/class/yedit.py -*- -*- -*-
  130. class YeditException(Exception): # pragma: no cover
  131. ''' Exception class for Yedit '''
  132. pass
  133. # pylint: disable=too-many-public-methods,too-many-instance-attributes
  134. class Yedit(object): # pragma: no cover
  135. ''' Class to modify yaml files '''
  136. re_valid_key = r"(((\[-?\d+\])|([0-9a-zA-Z%s/_-]+)).?)+$"
  137. re_key = r"(?:\[(-?\d+)\])|([0-9a-zA-Z{}/_-]+)"
  138. com_sep = set(['.', '#', '|', ':'])
  139. # pylint: disable=too-many-arguments
  140. def __init__(self,
  141. filename=None,
  142. content=None,
  143. content_type='yaml',
  144. separator='.',
  145. backup_ext=None,
  146. backup=False):
  147. self.content = content
  148. self._separator = separator
  149. self.filename = filename
  150. self.__yaml_dict = content
  151. self.content_type = content_type
  152. self.backup = backup
  153. if backup_ext is None:
  154. self.backup_ext = ".{}".format(time.strftime("%Y%m%dT%H%M%S"))
  155. else:
  156. self.backup_ext = backup_ext
  157. self.load(content_type=self.content_type)
  158. if self.__yaml_dict is None:
  159. self.__yaml_dict = {}
  160. @property
  161. def separator(self):
  162. ''' getter method for separator '''
  163. return self._separator
  164. @separator.setter
  165. def separator(self, inc_sep):
  166. ''' setter method for separator '''
  167. self._separator = inc_sep
  168. @property
  169. def yaml_dict(self):
  170. ''' getter method for yaml_dict '''
  171. return self.__yaml_dict
  172. @yaml_dict.setter
  173. def yaml_dict(self, value):
  174. ''' setter method for yaml_dict '''
  175. self.__yaml_dict = value
  176. @staticmethod
  177. def parse_key(key, sep='.'):
  178. '''parse the key allowing the appropriate separator'''
  179. common_separators = list(Yedit.com_sep - set([sep]))
  180. return re.findall(Yedit.re_key.format(''.join(common_separators)), key)
  181. @staticmethod
  182. def valid_key(key, sep='.'):
  183. '''validate the incoming key'''
  184. common_separators = list(Yedit.com_sep - set([sep]))
  185. if not re.match(Yedit.re_valid_key.format(''.join(common_separators)), key):
  186. return False
  187. return True
  188. # pylint: disable=too-many-return-statements,too-many-branches
  189. @staticmethod
  190. def remove_entry(data, key, index=None, value=None, sep='.'):
  191. ''' remove data at location key '''
  192. if key == '' and isinstance(data, dict):
  193. if value is not None:
  194. data.pop(value)
  195. elif index is not None:
  196. raise YeditException("remove_entry for a dictionary does not have an index {}".format(index))
  197. else:
  198. data.clear()
  199. return True
  200. elif key == '' and isinstance(data, list):
  201. ind = None
  202. if value is not None:
  203. try:
  204. ind = data.index(value)
  205. except ValueError:
  206. return False
  207. elif index is not None:
  208. ind = index
  209. else:
  210. del data[:]
  211. if ind is not None:
  212. data.pop(ind)
  213. return True
  214. if not (key and Yedit.valid_key(key, sep)) and \
  215. isinstance(data, (list, dict)):
  216. return None
  217. key_indexes = Yedit.parse_key(key, sep)
  218. for arr_ind, dict_key in key_indexes[:-1]:
  219. if dict_key and isinstance(data, dict):
  220. data = data.get(dict_key)
  221. elif (arr_ind and isinstance(data, list) and
  222. int(arr_ind) <= len(data) - 1):
  223. data = data[int(arr_ind)]
  224. else:
  225. return None
  226. # process last index for remove
  227. # expected list entry
  228. if key_indexes[-1][0]:
  229. if isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1: # noqa: E501
  230. del data[int(key_indexes[-1][0])]
  231. return True
  232. # expected dict entry
  233. elif key_indexes[-1][1]:
  234. if isinstance(data, dict):
  235. del data[key_indexes[-1][1]]
  236. return True
  237. @staticmethod
  238. def add_entry(data, key, item=None, sep='.'):
  239. ''' Get an item from a dictionary with key notation a.b.c
  240. d = {'a': {'b': 'c'}}}
  241. key = a#b
  242. return c
  243. '''
  244. if key == '':
  245. pass
  246. elif (not (key and Yedit.valid_key(key, sep)) and
  247. isinstance(data, (list, dict))):
  248. return None
  249. key_indexes = Yedit.parse_key(key, sep)
  250. for arr_ind, dict_key in key_indexes[:-1]:
  251. if dict_key:
  252. if isinstance(data, dict) and dict_key in data and data[dict_key]: # noqa: E501
  253. data = data[dict_key]
  254. continue
  255. elif data and not isinstance(data, dict):
  256. raise YeditException("Unexpected item type found while going through key " +
  257. "path: {} (at key: {})".format(key, dict_key))
  258. data[dict_key] = {}
  259. data = data[dict_key]
  260. elif (arr_ind and isinstance(data, list) and
  261. int(arr_ind) <= len(data) - 1):
  262. data = data[int(arr_ind)]
  263. else:
  264. raise YeditException("Unexpected item type found while going through key path: {}".format(key))
  265. if key == '':
  266. data = item
  267. # process last index for add
  268. # expected list entry
  269. elif key_indexes[-1][0] and isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1: # noqa: E501
  270. data[int(key_indexes[-1][0])] = item
  271. # expected dict entry
  272. elif key_indexes[-1][1] and isinstance(data, dict):
  273. data[key_indexes[-1][1]] = item
  274. # didn't add/update to an existing list, nor add/update key to a dict
  275. # so we must have been provided some syntax like a.b.c[<int>] = "data" for a
  276. # non-existent array
  277. else:
  278. raise YeditException("Error adding to object at path: {}".format(key))
  279. return data
  280. @staticmethod
  281. def get_entry(data, key, sep='.'):
  282. ''' Get an item from a dictionary with key notation a.b.c
  283. d = {'a': {'b': 'c'}}}
  284. key = a.b
  285. return c
  286. '''
  287. if key == '':
  288. pass
  289. elif (not (key and Yedit.valid_key(key, sep)) and
  290. isinstance(data, (list, dict))):
  291. return None
  292. key_indexes = Yedit.parse_key(key, sep)
  293. for arr_ind, dict_key in key_indexes:
  294. if dict_key and isinstance(data, dict):
  295. data = data.get(dict_key)
  296. elif (arr_ind and isinstance(data, list) and
  297. int(arr_ind) <= len(data) - 1):
  298. data = data[int(arr_ind)]
  299. else:
  300. return None
  301. return data
  302. @staticmethod
  303. def _write(filename, contents):
  304. ''' Actually write the file contents to disk. This helps with mocking. '''
  305. tmp_filename = filename + '.yedit'
  306. with open(tmp_filename, 'w') as yfd:
  307. fcntl.flock(yfd, fcntl.LOCK_EX | fcntl.LOCK_NB)
  308. yfd.write(contents)
  309. fcntl.flock(yfd, fcntl.LOCK_UN)
  310. os.rename(tmp_filename, filename)
  311. def write(self):
  312. ''' write to file '''
  313. if not self.filename:
  314. raise YeditException('Please specify a filename.')
  315. if self.backup and self.file_exists():
  316. shutil.copy(self.filename, '{}{}'.format(self.filename, self.backup_ext))
  317. # Try to set format attributes if supported
  318. try:
  319. self.yaml_dict.fa.set_block_style()
  320. except AttributeError:
  321. pass
  322. # Try to use RoundTripDumper if supported.
  323. if self.content_type == 'yaml':
  324. try:
  325. Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
  326. except AttributeError:
  327. Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))
  328. elif self.content_type == 'json':
  329. Yedit._write(self.filename, json.dumps(self.yaml_dict, indent=4, sort_keys=True))
  330. else:
  331. raise YeditException('Unsupported content_type: {}.'.format(self.content_type) +
  332. 'Please specify a content_type of yaml or json.')
  333. return (True, self.yaml_dict)
  334. def read(self):
  335. ''' read from file '''
  336. # check if it exists
  337. if self.filename is None or not self.file_exists():
  338. return None
  339. contents = None
  340. with open(self.filename) as yfd:
  341. contents = yfd.read()
  342. return contents
  343. def file_exists(self):
  344. ''' return whether file exists '''
  345. if os.path.exists(self.filename):
  346. return True
  347. return False
  348. def load(self, content_type='yaml'):
  349. ''' return yaml file '''
  350. contents = self.read()
  351. if not contents and not self.content:
  352. return None
  353. if self.content:
  354. if isinstance(self.content, dict):
  355. self.yaml_dict = self.content
  356. return self.yaml_dict
  357. elif isinstance(self.content, str):
  358. contents = self.content
  359. # check if it is yaml
  360. try:
  361. if content_type == 'yaml' and contents:
  362. # Try to set format attributes if supported
  363. try:
  364. self.yaml_dict.fa.set_block_style()
  365. except AttributeError:
  366. pass
  367. # Try to use RoundTripLoader if supported.
  368. try:
  369. self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
  370. except AttributeError:
  371. self.yaml_dict = yaml.safe_load(contents)
  372. # Try to set format attributes if supported
  373. try:
  374. self.yaml_dict.fa.set_block_style()
  375. except AttributeError:
  376. pass
  377. elif content_type == 'json' and contents:
  378. self.yaml_dict = json.loads(contents)
  379. except yaml.YAMLError as err:
  380. # Error loading yaml or json
  381. raise YeditException('Problem with loading yaml file. {}'.format(err))
  382. return self.yaml_dict
  383. def get(self, key):
  384. ''' get a specified key'''
  385. try:
  386. entry = Yedit.get_entry(self.yaml_dict, key, self.separator)
  387. except KeyError:
  388. entry = None
  389. return entry
  390. def pop(self, path, key_or_item):
  391. ''' remove a key, value pair from a dict or an item for a list'''
  392. try:
  393. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  394. except KeyError:
  395. entry = None
  396. if entry is None:
  397. return (False, self.yaml_dict)
  398. if isinstance(entry, dict):
  399. # AUDIT:maybe-no-member makes sense due to fuzzy types
  400. # pylint: disable=maybe-no-member
  401. if key_or_item in entry:
  402. entry.pop(key_or_item)
  403. return (True, self.yaml_dict)
  404. return (False, self.yaml_dict)
  405. elif isinstance(entry, list):
  406. # AUDIT:maybe-no-member makes sense due to fuzzy types
  407. # pylint: disable=maybe-no-member
  408. ind = None
  409. try:
  410. ind = entry.index(key_or_item)
  411. except ValueError:
  412. return (False, self.yaml_dict)
  413. entry.pop(ind)
  414. return (True, self.yaml_dict)
  415. return (False, self.yaml_dict)
  416. def delete(self, path, index=None, value=None):
  417. ''' remove path from a dict'''
  418. try:
  419. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  420. except KeyError:
  421. entry = None
  422. if entry is None:
  423. return (False, self.yaml_dict)
  424. result = Yedit.remove_entry(self.yaml_dict, path, index, value, self.separator)
  425. if not result:
  426. return (False, self.yaml_dict)
  427. return (True, self.yaml_dict)
  428. def exists(self, path, value):
  429. ''' check if value exists at path'''
  430. try:
  431. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  432. except KeyError:
  433. entry = None
  434. if isinstance(entry, list):
  435. if value in entry:
  436. return True
  437. return False
  438. elif isinstance(entry, dict):
  439. if isinstance(value, dict):
  440. rval = False
  441. for key, val in value.items():
  442. if entry[key] != val:
  443. rval = False
  444. break
  445. else:
  446. rval = True
  447. return rval
  448. return value in entry
  449. return entry == value
  450. def append(self, path, value):
  451. '''append value to a list'''
  452. try:
  453. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  454. except KeyError:
  455. entry = None
  456. if entry is None:
  457. self.put(path, [])
  458. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  459. if not isinstance(entry, list):
  460. return (False, self.yaml_dict)
  461. # AUDIT:maybe-no-member makes sense due to loading data from
  462. # a serialized format.
  463. # pylint: disable=maybe-no-member
  464. entry.append(value)
  465. return (True, self.yaml_dict)
  466. # pylint: disable=too-many-arguments
  467. def update(self, path, value, index=None, curr_value=None):
  468. ''' put path, value into a dict '''
  469. try:
  470. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  471. except KeyError:
  472. entry = None
  473. if isinstance(entry, dict):
  474. # AUDIT:maybe-no-member makes sense due to fuzzy types
  475. # pylint: disable=maybe-no-member
  476. if not isinstance(value, dict):
  477. raise YeditException('Cannot replace key, value entry in dict with non-dict type. ' +
  478. 'value=[{}] type=[{}]'.format(value, type(value)))
  479. entry.update(value)
  480. return (True, self.yaml_dict)
  481. elif isinstance(entry, list):
  482. # AUDIT:maybe-no-member makes sense due to fuzzy types
  483. # pylint: disable=maybe-no-member
  484. ind = None
  485. if curr_value:
  486. try:
  487. ind = entry.index(curr_value)
  488. except ValueError:
  489. return (False, self.yaml_dict)
  490. elif index is not None:
  491. ind = index
  492. if ind is not None and entry[ind] != value:
  493. entry[ind] = value
  494. return (True, self.yaml_dict)
  495. # see if it exists in the list
  496. try:
  497. ind = entry.index(value)
  498. except ValueError:
  499. # doesn't exist, append it
  500. entry.append(value)
  501. return (True, self.yaml_dict)
  502. # already exists, return
  503. if ind is not None:
  504. return (False, self.yaml_dict)
  505. return (False, self.yaml_dict)
  506. def put(self, path, value):
  507. ''' put path, value into a dict '''
  508. try:
  509. entry = Yedit.get_entry(self.yaml_dict, path, self.separator)
  510. except KeyError:
  511. entry = None
  512. if entry == value:
  513. return (False, self.yaml_dict)
  514. # deepcopy didn't work
  515. # Try to use ruamel.yaml and fallback to pyyaml
  516. try:
  517. tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict,
  518. default_flow_style=False),
  519. yaml.RoundTripLoader)
  520. except AttributeError:
  521. tmp_copy = copy.deepcopy(self.yaml_dict)
  522. # set the format attributes if available
  523. try:
  524. tmp_copy.fa.set_block_style()
  525. except AttributeError:
  526. pass
  527. result = Yedit.add_entry(tmp_copy, path, value, self.separator)
  528. if result is None:
  529. return (False, self.yaml_dict)
  530. # When path equals "" it is a special case.
  531. # "" refers to the root of the document
  532. # Only update the root path (entire document) when its a list or dict
  533. if path == '':
  534. if isinstance(result, list) or isinstance(result, dict):
  535. self.yaml_dict = result
  536. return (True, self.yaml_dict)
  537. return (False, self.yaml_dict)
  538. self.yaml_dict = tmp_copy
  539. return (True, self.yaml_dict)
  540. def create(self, path, value):
  541. ''' create a yaml file '''
  542. if not self.file_exists():
  543. # deepcopy didn't work
  544. # Try to use ruamel.yaml and fallback to pyyaml
  545. try:
  546. tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict,
  547. default_flow_style=False),
  548. yaml.RoundTripLoader)
  549. except AttributeError:
  550. tmp_copy = copy.deepcopy(self.yaml_dict)
  551. # set the format attributes if available
  552. try:
  553. tmp_copy.fa.set_block_style()
  554. except AttributeError:
  555. pass
  556. result = Yedit.add_entry(tmp_copy, path, value, self.separator)
  557. if result is not None:
  558. self.yaml_dict = tmp_copy
  559. return (True, self.yaml_dict)
  560. return (False, self.yaml_dict)
  561. @staticmethod
  562. def get_curr_value(invalue, val_type):
  563. '''return the current value'''
  564. if invalue is None:
  565. return None
  566. curr_value = invalue
  567. if val_type == 'yaml':
  568. curr_value = yaml.safe_load(str(invalue))
  569. elif val_type == 'json':
  570. curr_value = json.loads(invalue)
  571. return curr_value
  572. @staticmethod
  573. def parse_value(inc_value, vtype=''):
  574. '''determine value type passed'''
  575. true_bools = ['y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE',
  576. 'on', 'On', 'ON', ]
  577. false_bools = ['n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE',
  578. 'off', 'Off', 'OFF']
  579. # It came in as a string but you didn't specify value_type as string
  580. # we will convert to bool if it matches any of the above cases
  581. if isinstance(inc_value, str) and 'bool' in vtype:
  582. if inc_value not in true_bools and inc_value not in false_bools:
  583. raise YeditException('Not a boolean type. str=[{}] vtype=[{}]'.format(inc_value, vtype))
  584. elif isinstance(inc_value, bool) and 'str' in vtype:
  585. inc_value = str(inc_value)
  586. # There is a special case where '' will turn into None after yaml loading it so skip
  587. if isinstance(inc_value, str) and inc_value == '':
  588. pass
  589. # If vtype is not str then go ahead and attempt to yaml load it.
  590. elif isinstance(inc_value, str) and 'str' not in vtype:
  591. try:
  592. inc_value = yaml.safe_load(inc_value)
  593. except Exception:
  594. raise YeditException('Could not determine type of incoming value. ' +
  595. 'value=[{}] vtype=[{}]'.format(type(inc_value), vtype))
  596. return inc_value
  597. @staticmethod
  598. def process_edits(edits, yamlfile):
  599. '''run through a list of edits and process them one-by-one'''
  600. results = []
  601. for edit in edits:
  602. value = Yedit.parse_value(edit['value'], edit.get('value_type', ''))
  603. if edit.get('action') == 'update':
  604. # pylint: disable=line-too-long
  605. curr_value = Yedit.get_curr_value(
  606. Yedit.parse_value(edit.get('curr_value')),
  607. edit.get('curr_value_format'))
  608. rval = yamlfile.update(edit['key'],
  609. value,
  610. edit.get('index'),
  611. curr_value)
  612. elif edit.get('action') == 'append':
  613. rval = yamlfile.append(edit['key'], value)
  614. else:
  615. rval = yamlfile.put(edit['key'], value)
  616. if rval[0]:
  617. results.append({'key': edit['key'], 'edit': rval[1]})
  618. return {'changed': len(results) > 0, 'results': results}
  619. # pylint: disable=too-many-return-statements,too-many-branches
  620. @staticmethod
  621. def run_ansible(params):
  622. '''perform the idempotent crud operations'''
  623. yamlfile = Yedit(filename=params['src'],
  624. backup=params['backup'],
  625. content_type=params['content_type'],
  626. backup_ext=params['backup_ext'],
  627. separator=params['separator'])
  628. state = params['state']
  629. if params['src']:
  630. rval = yamlfile.load()
  631. if yamlfile.yaml_dict is None and state != 'present':
  632. return {'failed': True,
  633. 'msg': 'Error opening file [{}]. Verify that the '.format(params['src']) +
  634. 'file exists, that it is has correct permissions, and is valid yaml.'}
  635. if state == 'list':
  636. if params['content']:
  637. content = Yedit.parse_value(params['content'], params['content_type'])
  638. yamlfile.yaml_dict = content
  639. if params['key']:
  640. rval = yamlfile.get(params['key'])
  641. return {'changed': False, 'result': rval, 'state': state}
  642. elif state == 'absent':
  643. if params['content']:
  644. content = Yedit.parse_value(params['content'], params['content_type'])
  645. yamlfile.yaml_dict = content
  646. if params['update']:
  647. rval = yamlfile.pop(params['key'], params['value'])
  648. else:
  649. rval = yamlfile.delete(params['key'], params['index'], params['value'])
  650. if rval[0] and params['src']:
  651. yamlfile.write()
  652. return {'changed': rval[0], 'result': rval[1], 'state': state}
  653. elif state == 'present':
  654. # check if content is different than what is in the file
  655. if params['content']:
  656. content = Yedit.parse_value(params['content'], params['content_type'])
  657. # We had no edits to make and the contents are the same
  658. if yamlfile.yaml_dict == content and \
  659. params['value'] is None:
  660. return {'changed': False, 'result': yamlfile.yaml_dict, 'state': state}
  661. yamlfile.yaml_dict = content
  662. # If we were passed a key, value then
  663. # we enapsulate it in a list and process it
  664. # Key, Value passed to the module : Converted to Edits list #
  665. edits = []
  666. _edit = {}
  667. if params['value'] is not None:
  668. _edit['value'] = params['value']
  669. _edit['value_type'] = params['value_type']
  670. _edit['key'] = params['key']
  671. if params['update']:
  672. _edit['action'] = 'update'
  673. _edit['curr_value'] = params['curr_value']
  674. _edit['curr_value_format'] = params['curr_value_format']
  675. _edit['index'] = params['index']
  676. elif params['append']:
  677. _edit['action'] = 'append'
  678. edits.append(_edit)
  679. elif params['edits'] is not None:
  680. edits = params['edits']
  681. if edits:
  682. results = Yedit.process_edits(edits, yamlfile)
  683. # if there were changes and a src provided to us we need to write
  684. if results['changed'] and params['src']:
  685. yamlfile.write()
  686. return {'changed': results['changed'], 'result': results['results'], 'state': state}
  687. # no edits to make
  688. if params['src']:
  689. # pylint: disable=redefined-variable-type
  690. rval = yamlfile.write()
  691. return {'changed': rval[0],
  692. 'result': rval[1],
  693. 'state': state}
  694. # We were passed content but no src, key or value, or edits. Return contents in memory
  695. return {'changed': False, 'result': yamlfile.yaml_dict, 'state': state}
  696. return {'failed': True, 'msg': 'Unkown state passed'}
  697. # -*- -*- -*- End included fragment: ../../lib_utils/src/class/yedit.py -*- -*- -*-
  698. # -*- -*- -*- Begin included fragment: lib/base.py -*- -*- -*-
  699. # pylint: disable=too-many-lines
  700. # noqa: E301,E302,E303,T001
  701. class OpenShiftCLIError(Exception):
  702. '''Exception class for openshiftcli'''
  703. pass
  704. ADDITIONAL_PATH_LOOKUPS = ['/usr/local/bin', os.path.expanduser('~/bin')]
  705. def locate_oc_binary():
  706. ''' Find and return oc binary file '''
  707. # https://github.com/openshift/openshift-ansible/issues/3410
  708. # oc can be in /usr/local/bin in some cases, but that may not
  709. # be in $PATH due to ansible/sudo
  710. paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS
  711. oc_binary = 'oc'
  712. # Use shutil.which if it is available, otherwise fallback to a naive path search
  713. try:
  714. which_result = shutil.which(oc_binary, path=os.pathsep.join(paths))
  715. if which_result is not None:
  716. oc_binary = which_result
  717. except AttributeError:
  718. for path in paths:
  719. if os.path.exists(os.path.join(path, oc_binary)):
  720. oc_binary = os.path.join(path, oc_binary)
  721. break
  722. return oc_binary
  723. # pylint: disable=too-few-public-methods
  724. class OpenShiftCLI(object):
  725. ''' Class to wrap the command line tools '''
  726. def __init__(self,
  727. namespace,
  728. kubeconfig='/etc/origin/master/admin.kubeconfig',
  729. verbose=False,
  730. all_namespaces=False):
  731. ''' Constructor for OpenshiftCLI '''
  732. self.namespace = namespace
  733. self.verbose = verbose
  734. self.kubeconfig = Utils.create_tmpfile_copy(kubeconfig)
  735. self.all_namespaces = all_namespaces
  736. self.oc_binary = locate_oc_binary()
  737. # Pylint allows only 5 arguments to be passed.
  738. # pylint: disable=too-many-arguments
  739. def _replace_content(self, resource, rname, content, edits=None, force=False, sep='.'):
  740. ''' replace the current object with the content '''
  741. res = self._get(resource, rname)
  742. if not res['results']:
  743. return res
  744. fname = Utils.create_tmpfile(rname + '-')
  745. yed = Yedit(fname, res['results'][0], separator=sep)
  746. updated = False
  747. if content is not None:
  748. changes = []
  749. for key, value in content.items():
  750. changes.append(yed.put(key, value))
  751. if any([change[0] for change in changes]):
  752. updated = True
  753. elif edits is not None:
  754. results = Yedit.process_edits(edits, yed)
  755. if results['changed']:
  756. updated = True
  757. if updated:
  758. yed.write()
  759. atexit.register(Utils.cleanup, [fname])
  760. return self._replace(fname, force)
  761. return {'returncode': 0, 'updated': False}
  762. def _replace(self, fname, force=False):
  763. '''replace the current object with oc replace'''
  764. # We are removing the 'resourceVersion' to handle
  765. # a race condition when modifying oc objects
  766. yed = Yedit(fname)
  767. results = yed.delete('metadata.resourceVersion')
  768. if results[0]:
  769. yed.write()
  770. cmd = ['replace', '-f', fname]
  771. if force:
  772. cmd.append('--force')
  773. return self.openshift_cmd(cmd)
  774. def _create_from_content(self, rname, content):
  775. '''create a temporary file and then call oc create on it'''
  776. fname = Utils.create_tmpfile(rname + '-')
  777. yed = Yedit(fname, content=content)
  778. yed.write()
  779. atexit.register(Utils.cleanup, [fname])
  780. return self._create(fname)
  781. def _create(self, fname):
  782. '''call oc create on a filename'''
  783. return self.openshift_cmd(['create', '-f', fname])
  784. def _delete(self, resource, name=None, selector=None):
  785. '''call oc delete on a resource'''
  786. cmd = ['delete', resource]
  787. if selector is not None:
  788. cmd.append('--selector={}'.format(selector))
  789. elif name is not None:
  790. cmd.append(name)
  791. else:
  792. raise OpenShiftCLIError('Either name or selector is required when calling delete.')
  793. return self.openshift_cmd(cmd)
  794. def _process(self, template_name, create=False, params=None, template_data=None): # noqa: E501
  795. '''process a template
  796. template_name: the name of the template to process
  797. create: whether to send to oc create after processing
  798. params: the parameters for the template
  799. template_data: the incoming template's data; instead of a file
  800. '''
  801. cmd = ['process']
  802. if template_data:
  803. cmd.extend(['-f', '-'])
  804. else:
  805. cmd.append(template_name)
  806. if params:
  807. param_str = ["{}={}".format(key, str(value).replace("'", r'"')) for key, value in params.items()]
  808. cmd.append('-p')
  809. cmd.extend(param_str)
  810. results = self.openshift_cmd(cmd, output=True, input_data=template_data)
  811. if results['returncode'] != 0 or not create:
  812. return results
  813. fname = Utils.create_tmpfile(template_name + '-')
  814. yed = Yedit(fname, results['results'])
  815. yed.write()
  816. atexit.register(Utils.cleanup, [fname])
  817. return self.openshift_cmd(['create', '-f', fname])
  818. def _get(self, resource, name=None, selector=None, field_selector=None):
  819. '''return a resource by name '''
  820. cmd = ['get', resource]
  821. if selector is not None:
  822. cmd.append('--selector={}'.format(selector))
  823. if field_selector is not None:
  824. cmd.append('--field-selector={}'.format(field_selector))
  825. # Name cannot be used with selector or field_selector.
  826. if selector is None and field_selector is None and name is not None:
  827. cmd.append(name)
  828. cmd.extend(['-o', 'json'])
  829. rval = self.openshift_cmd(cmd, output=True)
  830. # Ensure results are retuned in an array
  831. if 'items' in rval:
  832. rval['results'] = rval['items']
  833. elif not isinstance(rval['results'], list):
  834. rval['results'] = [rval['results']]
  835. return rval
  836. def _schedulable(self, node=None, selector=None, schedulable=True):
  837. ''' perform oadm manage-node scheduable '''
  838. cmd = ['manage-node']
  839. if node:
  840. cmd.extend(node)
  841. else:
  842. cmd.append('--selector={}'.format(selector))
  843. cmd.append('--schedulable={}'.format(schedulable))
  844. return self.openshift_cmd(cmd, oadm=True, output=True, output_type='raw') # noqa: E501
  845. def _list_pods(self, node=None, selector=None, pod_selector=None):
  846. ''' perform oadm list pods
  847. node: the node in which to list pods
  848. selector: the label selector filter if provided
  849. pod_selector: the pod selector filter if provided
  850. '''
  851. cmd = ['manage-node']
  852. if node:
  853. cmd.extend(node)
  854. else:
  855. cmd.append('--selector={}'.format(selector))
  856. if pod_selector:
  857. cmd.append('--pod-selector={}'.format(pod_selector))
  858. cmd.extend(['--list-pods', '-o', 'json'])
  859. return self.openshift_cmd(cmd, oadm=True, output=True, output_type='raw')
  860. # pylint: disable=too-many-arguments
  861. def _evacuate(self, node=None, selector=None, pod_selector=None, dry_run=False, grace_period=None, force=False):
  862. ''' perform oadm manage-node evacuate '''
  863. cmd = ['manage-node']
  864. if node:
  865. cmd.extend(node)
  866. else:
  867. cmd.append('--selector={}'.format(selector))
  868. if dry_run:
  869. cmd.append('--dry-run')
  870. if pod_selector:
  871. cmd.append('--pod-selector={}'.format(pod_selector))
  872. if grace_period:
  873. cmd.append('--grace-period={}'.format(int(grace_period)))
  874. if force:
  875. cmd.append('--force')
  876. cmd.append('--evacuate')
  877. return self.openshift_cmd(cmd, oadm=True, output=True, output_type='raw')
  878. def _version(self):
  879. ''' return the openshift version'''
  880. return self.openshift_cmd(['version'], output=True, output_type='raw')
  881. def _import_image(self, url=None, name=None, tag=None):
  882. ''' perform image import '''
  883. cmd = ['import-image']
  884. image = '{0}'.format(name)
  885. if tag:
  886. image += ':{0}'.format(tag)
  887. cmd.append(image)
  888. if url:
  889. cmd.append('--from={0}/{1}'.format(url, image))
  890. cmd.append('-n{0}'.format(self.namespace))
  891. cmd.append('--confirm')
  892. return self.openshift_cmd(cmd)
  893. def _run(self, cmds, input_data):
  894. ''' Actually executes the command. This makes mocking easier. '''
  895. curr_env = os.environ.copy()
  896. curr_env.update({'KUBECONFIG': self.kubeconfig})
  897. proc = subprocess.Popen(cmds,
  898. stdin=subprocess.PIPE,
  899. stdout=subprocess.PIPE,
  900. stderr=subprocess.PIPE,
  901. env=curr_env)
  902. stdout, stderr = proc.communicate(input_data)
  903. return proc.returncode, stdout.decode('utf-8'), stderr.decode('utf-8')
  904. # pylint: disable=too-many-arguments,too-many-branches
  905. def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None):
  906. '''Base command for oc '''
  907. cmds = [self.oc_binary]
  908. if oadm:
  909. cmds.append('adm')
  910. cmds.extend(cmd)
  911. if self.all_namespaces:
  912. cmds.extend(['--all-namespaces'])
  913. elif self.namespace is not None and self.namespace.lower() not in ['none', 'emtpy']: # E501
  914. cmds.extend(['-n', self.namespace])
  915. if self.verbose:
  916. print(' '.join(cmds))
  917. try:
  918. returncode, stdout, stderr = self._run(cmds, input_data)
  919. except OSError as ex:
  920. returncode, stdout, stderr = 1, '', 'Failed to execute {}: {}'.format(subprocess.list2cmdline(cmds), ex)
  921. rval = {"returncode": returncode,
  922. "cmd": ' '.join(cmds)}
  923. if output_type == 'json':
  924. rval['results'] = {}
  925. if output and stdout:
  926. try:
  927. rval['results'] = json.loads(stdout)
  928. except ValueError as verr:
  929. if "No JSON object could be decoded" in verr.args:
  930. rval['err'] = verr.args
  931. elif output_type == 'raw':
  932. rval['results'] = stdout if output else ''
  933. if self.verbose:
  934. print("STDOUT: {0}".format(stdout))
  935. print("STDERR: {0}".format(stderr))
  936. if 'err' in rval or returncode != 0:
  937. rval.update({"stderr": stderr,
  938. "stdout": stdout})
  939. return rval
  940. class Utils(object): # pragma: no cover
  941. ''' utilities for openshiftcli modules '''
  942. @staticmethod
  943. def _write(filename, contents):
  944. ''' Actually write the file contents to disk. This helps with mocking. '''
  945. with open(filename, 'w') as sfd:
  946. sfd.write(str(contents))
  947. @staticmethod
  948. def create_tmp_file_from_contents(rname, data, ftype='yaml'):
  949. ''' create a file in tmp with name and contents'''
  950. tmp = Utils.create_tmpfile(prefix=rname)
  951. if ftype == 'yaml':
  952. # AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage
  953. # pylint: disable=no-member
  954. if hasattr(yaml, 'RoundTripDumper'):
  955. Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
  956. else:
  957. Utils._write(tmp, yaml.safe_dump(data, default_flow_style=False))
  958. elif ftype == 'json':
  959. Utils._write(tmp, json.dumps(data))
  960. else:
  961. Utils._write(tmp, data)
  962. # Register cleanup when module is done
  963. atexit.register(Utils.cleanup, [tmp])
  964. return tmp
  965. @staticmethod
  966. def create_tmpfile_copy(inc_file):
  967. '''create a temporary copy of a file'''
  968. tmpfile = Utils.create_tmpfile('lib_openshift-')
  969. Utils._write(tmpfile, open(inc_file).read())
  970. # Cleanup the tmpfile
  971. atexit.register(Utils.cleanup, [tmpfile])
  972. return tmpfile
  973. @staticmethod
  974. def create_tmpfile(prefix='tmp'):
  975. ''' Generates and returns a temporary file name '''
  976. with tempfile.NamedTemporaryFile(prefix=prefix, delete=False) as tmp:
  977. return tmp.name
  978. @staticmethod
  979. def create_tmp_files_from_contents(content, content_type=None):
  980. '''Turn an array of dict: filename, content into a files array'''
  981. if not isinstance(content, list):
  982. content = [content]
  983. files = []
  984. for item in content:
  985. path = Utils.create_tmp_file_from_contents(item['path'] + '-',
  986. item['data'],
  987. ftype=content_type)
  988. files.append({'name': os.path.basename(item['path']),
  989. 'path': path})
  990. return files
  991. @staticmethod
  992. def cleanup(files):
  993. '''Clean up on exit '''
  994. for sfile in files:
  995. if os.path.exists(sfile):
  996. if os.path.isdir(sfile):
  997. shutil.rmtree(sfile)
  998. elif os.path.isfile(sfile):
  999. os.remove(sfile)
  1000. @staticmethod
  1001. def exists(results, _name):
  1002. ''' Check to see if the results include the name '''
  1003. if not results:
  1004. return False
  1005. if Utils.find_result(results, _name):
  1006. return True
  1007. return False
  1008. @staticmethod
  1009. def find_result(results, _name):
  1010. ''' Find the specified result by name'''
  1011. rval = None
  1012. for result in results:
  1013. if 'metadata' in result and result['metadata']['name'] == _name:
  1014. rval = result
  1015. break
  1016. return rval
  1017. @staticmethod
  1018. def get_resource_file(sfile, sfile_type='yaml'):
  1019. ''' return the service file '''
  1020. contents = None
  1021. with open(sfile) as sfd:
  1022. contents = sfd.read()
  1023. if sfile_type == 'yaml':
  1024. # AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage
  1025. # pylint: disable=no-member
  1026. if hasattr(yaml, 'RoundTripLoader'):
  1027. contents = yaml.load(contents, yaml.RoundTripLoader)
  1028. else:
  1029. contents = yaml.safe_load(contents)
  1030. elif sfile_type == 'json':
  1031. contents = json.loads(contents)
  1032. return contents
  1033. @staticmethod
  1034. def filter_versions(stdout):
  1035. ''' filter the oc version output '''
  1036. version_dict = {}
  1037. version_search = ['oc', 'openshift', 'kubernetes']
  1038. for line in stdout.strip().split('\n'):
  1039. for term in version_search:
  1040. if not line:
  1041. continue
  1042. if line.startswith(term):
  1043. version_dict[term] = line.split()[-1]
  1044. # horrible hack to get openshift version in Openshift 3.2
  1045. # By default "oc version in 3.2 does not return an "openshift" version
  1046. if "openshift" not in version_dict:
  1047. version_dict["openshift"] = version_dict["oc"]
  1048. return version_dict
  1049. @staticmethod
  1050. def add_custom_versions(versions):
  1051. ''' create custom versions strings '''
  1052. versions_dict = {}
  1053. for tech, version in versions.items():
  1054. # clean up "-" from version
  1055. if "-" in version:
  1056. version = version.split("-")[0]
  1057. if version.startswith('v'):
  1058. version = version[1:] # Remove the 'v' prefix
  1059. versions_dict[tech + '_numeric'] = version.split('+')[0]
  1060. # "3.3.0.33" is what we have, we want "3.3"
  1061. versions_dict[tech + '_short'] = "{}.{}".format(*version.split('.'))
  1062. return versions_dict
  1063. @staticmethod
  1064. def openshift_installed():
  1065. ''' check if openshift is installed '''
  1066. import rpm
  1067. transaction_set = rpm.TransactionSet()
  1068. rpmquery = transaction_set.dbMatch("name", "atomic-openshift")
  1069. return rpmquery.count() > 0
  1070. # Disabling too-many-branches. This is a yaml dictionary comparison function
  1071. # pylint: disable=too-many-branches,too-many-return-statements,too-many-statements
  1072. @staticmethod
  1073. def check_def_equal(user_def, result_def, skip_keys=None, debug=False):
  1074. ''' Given a user defined definition, compare it with the results given back by our query. '''
  1075. if user_def is None:
  1076. user_def = {}
  1077. # Currently these values are autogenerated and we do not need to check them
  1078. skip = ['metadata', 'status']
  1079. if skip_keys:
  1080. skip.extend(skip_keys)
  1081. for key, value in result_def.items():
  1082. if key in skip:
  1083. continue
  1084. # Both are lists
  1085. if isinstance(value, list):
  1086. if key not in user_def:
  1087. if debug:
  1088. print('User data does not have key [%s]' % key)
  1089. print('User data: %s' % user_def)
  1090. return False
  1091. if not isinstance(user_def[key], list):
  1092. if debug:
  1093. print('user_def[key] is not a list key=[%s] user_def[key]=%s' % (key, user_def[key]))
  1094. return False
  1095. if len(user_def[key]) != len(value):
  1096. if debug:
  1097. print("List lengths are not equal.")
  1098. print("key=[%s]: user_def[%s] != value[%s]" % (key, len(user_def[key]), len(value)))
  1099. print("user_def: %s" % user_def[key])
  1100. print("value: %s" % value)
  1101. return False
  1102. for values in zip(user_def[key], value):
  1103. if isinstance(values[0], dict) and isinstance(values[1], dict):
  1104. if debug:
  1105. print('sending list - list')
  1106. print(type(values[0]))
  1107. print(type(values[1]))
  1108. result = Utils.check_def_equal(values[0], values[1], skip_keys=skip_keys, debug=debug)
  1109. if not result:
  1110. print('list compare returned false')
  1111. return False
  1112. elif value != user_def[key]:
  1113. if debug:
  1114. print('value should be identical')
  1115. print(user_def[key])
  1116. print(value)
  1117. return False
  1118. # recurse on a dictionary
  1119. elif isinstance(value, dict):
  1120. if key not in user_def:
  1121. if debug:
  1122. print("user_def does not have key [%s]" % key)
  1123. return False
  1124. if not isinstance(user_def[key], dict):
  1125. if debug:
  1126. print("dict returned false: not instance of dict")
  1127. return False
  1128. # before passing ensure keys match
  1129. api_values = set(value.keys()) - set(skip)
  1130. user_values = set(user_def[key].keys()) - set(skip)
  1131. if api_values != user_values:
  1132. if debug:
  1133. print("keys are not equal in dict")
  1134. print(user_values)
  1135. print(api_values)
  1136. return False
  1137. result = Utils.check_def_equal(user_def[key], value, skip_keys=skip_keys, debug=debug)
  1138. if not result:
  1139. if debug:
  1140. print("dict returned false")
  1141. print(result)
  1142. return False
  1143. # Verify each key, value pair is the same
  1144. else:
  1145. if key not in user_def or value != user_def[key]:
  1146. if debug:
  1147. print("value not equal; user_def does not have key")
  1148. print(key)
  1149. print(value)
  1150. if key in user_def:
  1151. print(user_def[key])
  1152. return False
  1153. if debug:
  1154. print('returning true')
  1155. return True
  1156. class OpenShiftCLIConfig(object):
  1157. '''Generic Config'''
  1158. def __init__(self, rname, namespace, kubeconfig, options):
  1159. self.kubeconfig = kubeconfig
  1160. self.name = rname
  1161. self.namespace = namespace
  1162. self._options = options
  1163. @property
  1164. def config_options(self):
  1165. ''' return config options '''
  1166. return self._options
  1167. def to_option_list(self, ascommalist=''):
  1168. '''return all options as a string
  1169. if ascommalist is set to the name of a key, and
  1170. the value of that key is a dict, format the dict
  1171. as a list of comma delimited key=value pairs'''
  1172. return self.stringify(ascommalist)
  1173. def stringify(self, ascommalist=''):
  1174. ''' return the options hash as cli params in a string
  1175. if ascommalist is set to the name of a key, and
  1176. the value of that key is a dict, format the dict
  1177. as a list of comma delimited key=value pairs '''
  1178. rval = []
  1179. for key in sorted(self.config_options.keys()):
  1180. data = self.config_options[key]
  1181. if data['include'] \
  1182. and (data['value'] is not None or isinstance(data['value'], int)):
  1183. if key == ascommalist:
  1184. val = ','.join(['{}={}'.format(kk, vv) for kk, vv in sorted(data['value'].items())])
  1185. else:
  1186. val = data['value']
  1187. rval.append('--{}={}'.format(key.replace('_', '-'), val))
  1188. return rval
  1189. # -*- -*- -*- End included fragment: lib/base.py -*- -*- -*-
  1190. # -*- -*- -*- Begin included fragment: class/oc_adm_csr.py -*- -*- -*-
  1191. class OCcsr(OpenShiftCLI):
  1192. ''' Class to wrap the oc adm certificate command line'''
  1193. kind = 'csr'
  1194. # pylint: disable=too-many-arguments
  1195. def __init__(self,
  1196. nodes=None,
  1197. approve_all=False,
  1198. service_account=None,
  1199. kubeconfig='/etc/origin/master/admin.kubeconfig',
  1200. verbose=False):
  1201. ''' Constructor for oc adm certificate '''
  1202. super(OCcsr, self).__init__(None, kubeconfig, verbose)
  1203. self.service_account = service_account
  1204. self.nodes = self.create_nodes(nodes)
  1205. self._csrs = []
  1206. self.approve_all = approve_all
  1207. self.verbose = verbose
  1208. @property
  1209. def csrs(self):
  1210. '''property for managing csrs'''
  1211. # any processing needed??
  1212. self._csrs = self._get(resource=self.kind)['results'][0]['items']
  1213. return self._csrs
  1214. def create_nodes(self, nodes):
  1215. '''create a node object to track csr signing status'''
  1216. nodes_list = []
  1217. if nodes is None:
  1218. return nodes_list
  1219. results = self._get(resource='nodes')['results'][0]['items']
  1220. for node in nodes:
  1221. nodes_list.append(dict(name=node, csrs={}, server_accepted=False, client_accepted=False, denied=False))
  1222. # Ready nodes have already been accepted. Mark client and server as accepted.
  1223. for ocnode in results:
  1224. if ocnode['metadata']['name'] == node:
  1225. for condition in ocnode['status']['conditions']:
  1226. if condition['type'] == 'Ready' and condition['status'] == 'True':
  1227. nodes_list[-1]['server_accepted'] = True
  1228. nodes_list[-1]['client_accepted'] = True
  1229. return nodes_list
  1230. def get(self):
  1231. '''get the current certificate signing requests'''
  1232. return self.csrs
  1233. @staticmethod
  1234. def action_needed(csr, action):
  1235. '''check to see if csr is in desired state'''
  1236. if csr['status'] == {}:
  1237. return True
  1238. state = csr['status']['conditions'][0]['type']
  1239. if action == 'approve' and state != 'Approved':
  1240. return True
  1241. elif action == 'deny' and state != 'Denied':
  1242. return True
  1243. return False
  1244. def get_csr_request(self, request):
  1245. '''base64 decode the request object and call openssl to determine the
  1246. subject and specifically the CN: from the request
  1247. Output:
  1248. (0, '...
  1249. Subject: O=system:nodes, CN=system:node:ip-172-31-54-54.ec2.internal
  1250. ...')
  1251. '''
  1252. import base64
  1253. return self._run(['openssl', 'req', '-noout', '-text'], base64.b64decode(request))[1]
  1254. def match_node(self, csr):
  1255. '''match an inc csr to a node in self.nodes'''
  1256. for node in self.nodes:
  1257. # we need to match based upon the csr's request certificate's CN
  1258. if node['name'] in self.get_csr_request(csr['spec']['request']):
  1259. node['csrs'][csr['metadata']['name']] = csr
  1260. # client certs may come in as either the service_account or as the node during upgrade
  1261. # server certs always come in as the node
  1262. if ((node['name'] in csr['spec']['username'] or
  1263. csr['spec']['username'] in [self.service_account, 'system:admin']) and
  1264. csr['status'] and csr['status']['conditions'][0]['type'] == 'Approved'):
  1265. if 'server auth' in csr['spec']['usages']:
  1266. node['server_accepted'] = True
  1267. if 'client auth' in csr['spec']['usages']:
  1268. node['client_accepted'] = True
  1269. # check type is 'Denied' and mark node as such
  1270. if csr['status'] and csr['status']['conditions'][0]['type'] == 'Denied':
  1271. node['denied'] = True
  1272. return node
  1273. return None
  1274. def finished(self):
  1275. '''determine if there are more csrs to sign'''
  1276. # if nodes is set and we have nodes then return if all nodes are 'accepted'
  1277. if self.nodes is not None and len(self.nodes) > 0:
  1278. return all([(node['server_accepted'] and node['client_accepted']) or node['denied'] for node in self.nodes])
  1279. # we are approving everything or we still have nodes outstanding
  1280. return False
  1281. def manage(self, action):
  1282. '''run openshift oc adm ca create-server-cert cmd and store results into self.nodes
  1283. we attempt to verify if the node is one that was given to us to accept.
  1284. action - (allow | deny)
  1285. '''
  1286. results = []
  1287. # There are 2 types of requests:
  1288. # - node-bootstrapper-client-ip-172-31-51-246-ec2-internal
  1289. # The client request allows the client to talk to the api/controller
  1290. # - node-bootstrapper-server-ip-172-31-51-246-ec2-internal
  1291. # The server request allows the server to join the cluster
  1292. # Here we need to determine how to approve/deny
  1293. # we should query the csrs and verify they are from the nodes we thought
  1294. for csr in self.csrs:
  1295. node = self.match_node(csr)
  1296. # oc adm certificate <approve|deny> csr
  1297. # there are 3 known states: Denied, Approved, {}
  1298. # verify something is needed by OCcsr.action_needed
  1299. # if approve_all, then do it
  1300. # if you passed in nodes, you must have a node that matches
  1301. if self.approve_all or (node and OCcsr.action_needed(csr, action)):
  1302. result = self.openshift_cmd(['certificate', action, csr['metadata']['name']], oadm=True)
  1303. # if we successfully approved
  1304. if result['returncode'] == 0:
  1305. # client should have service account name in username field
  1306. # server should have node name in username field
  1307. if node and csr['metadata']['name'] not in node['csrs']:
  1308. node['csrs'][csr['metadata']['name']] = csr
  1309. # mark node as accepted in our list of nodes
  1310. # we will use {client,server}_accepted fields to determine if we're finished
  1311. if (node['name'] in csr['spec']['username'] or
  1312. csr['spec']['username'] in [self.service_account, 'system:admin']):
  1313. if 'server auth' in csr['spec']['usages']:
  1314. node['server_accepted'] = True
  1315. if 'client auth' in csr['spec']['usages']:
  1316. node['client_accepted'] = True
  1317. results.append(result)
  1318. return results
  1319. @staticmethod
  1320. def run_ansible(params, check_mode=False):
  1321. '''run the oc_adm_csr module'''
  1322. client = OCcsr(params['nodes'],
  1323. params['approve_all'],
  1324. params['service_account'],
  1325. params['kubeconfig'],
  1326. params['debug'])
  1327. state = params['state']
  1328. api_rval = client.get()
  1329. if state == 'list':
  1330. return {'changed': False, 'results': api_rval, 'state': state}
  1331. if state in ['approve', 'deny']:
  1332. if check_mode:
  1333. return {'changed': True,
  1334. 'msg': "CHECK_MODE: Would have {} the certificate.".format(params['state']),
  1335. 'state': state}
  1336. all_results = []
  1337. finished = False
  1338. timeout = False
  1339. # loop for timeout or block until all nodes pass
  1340. ctr = 0
  1341. while True:
  1342. all_results.extend(client.manage(params['state']))
  1343. if client.finished():
  1344. finished = True
  1345. break
  1346. if params['timeout'] == 0:
  1347. if not params['approve_all']:
  1348. ctr = 0
  1349. if ctr * 2 > params['timeout']:
  1350. timeout = True
  1351. break
  1352. # This provides time for the nodes to send their csr requests between approvals
  1353. time.sleep(2)
  1354. ctr += 1
  1355. for result in all_results:
  1356. if result['returncode'] != 0:
  1357. return {'failed': True, 'msg': all_results, 'timeout': timeout}
  1358. return dict(changed=len(all_results) > 0,
  1359. results=all_results,
  1360. nodes=client.nodes,
  1361. state=state,
  1362. finished=finished,
  1363. timeout=timeout)
  1364. return {'failed': True,
  1365. 'msg': 'Unknown state passed. %s' % state}
  1366. # -*- -*- -*- End included fragment: class/oc_adm_csr.py -*- -*- -*-
  1367. # -*- -*- -*- Begin included fragment: ansible/oc_adm_csr.py -*- -*- -*-
  1368. def main():
  1369. '''
  1370. ansible oc module for approving certificate signing requests
  1371. '''
  1372. module = AnsibleModule(
  1373. argument_spec=dict(
  1374. kubeconfig=dict(default='/etc/origin/master/admin.kubeconfig', type='str'),
  1375. state=dict(default='approve', type='str',
  1376. choices=['approve', 'deny', 'list']),
  1377. debug=dict(default=False, type='bool'),
  1378. nodes=dict(default=None, type='list'),
  1379. timeout=dict(default=30, type='int'),
  1380. approve_all=dict(default=False, type='bool'),
  1381. service_account=dict(default='system:serviceaccount:openshift-infra:node-bootstrapper', type='str'),
  1382. fail_on_timeout=dict(default=False, type='bool'),
  1383. ),
  1384. supports_check_mode=True,
  1385. mutually_exclusive=[['approve_all', 'nodes']],
  1386. )
  1387. if module.params['nodes'] == []:
  1388. module.fail_json(**dict(failed=True, msg='Please specify hosts.'))
  1389. rval = OCcsr.run_ansible(module.params, module.check_mode)
  1390. # If we timed out then we weren't finished. Fail if user requested to fail.
  1391. if (module.params['timeout'] > 0 and
  1392. module.params['fail_on_timeout'] and
  1393. rval['timeout']):
  1394. return module.fail_json(msg='Timed out accepting certificate signing requests. Failing as requested.', **rval)
  1395. if 'failed' in rval:
  1396. return module.fail_json(**rval)
  1397. return module.exit_json(**rval)
  1398. if __name__ == '__main__':
  1399. main()
  1400. # -*- -*- -*- End included fragment: ansible/oc_adm_csr.py -*- -*- -*-