oc_secret.py 61 KB

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