oc_secret.py 62 KB

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