1
0

test_ssh.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. tests.unit.config.schemas.test_ssh
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. """
  7. # Import python libs
  8. from __future__ import absolute_import, print_function, unicode_literals
  9. import salt.utils.stringutils
  10. # Import Salt Libs
  11. from salt.config.schemas import ssh as ssh_schemas
  12. from salt.config.schemas.minion import MinionConfiguration
  13. from salt.utils.versions import LooseVersion as _LooseVersion
  14. # Import Salt Testing Libs
  15. from tests.support.unit import TestCase, skipIf
  16. # Import 3rd-party libs
  17. try:
  18. import jsonschema
  19. import jsonschema.exceptions
  20. HAS_JSONSCHEMA = True
  21. JSONSCHEMA_VERSION = _LooseVersion(jsonschema.__version__)
  22. except ImportError:
  23. HAS_JSONSCHEMA = False
  24. JSONSCHEMA_VERSION = _LooseVersion("0")
  25. class RosterEntryConfigTest(TestCase):
  26. def test_config(self):
  27. config = ssh_schemas.RosterEntryConfig()
  28. expected = {
  29. "$schema": "http://json-schema.org/draft-04/schema#",
  30. "title": "Roster Entry",
  31. "description": "Salt SSH roster entry definition",
  32. "type": "object",
  33. "properties": {
  34. "host": {
  35. "title": "Host",
  36. "description": "The IP address or DNS name of the remote host",
  37. "type": "string",
  38. "pattern": r"^((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|([A-Za-z0-9][A-Za-z0-9\.\-]{1,255}))$",
  39. "minLength": 1,
  40. },
  41. "port": {
  42. "description": "The target system's ssh port number",
  43. "title": "Port",
  44. "default": 22,
  45. "maximum": 65535,
  46. "minimum": 0,
  47. "type": "integer",
  48. },
  49. "user": {
  50. "default": "root",
  51. "type": "string",
  52. "description": "The user to log in as. Defaults to root",
  53. "title": "User",
  54. "minLength": 1,
  55. },
  56. "passwd": {
  57. "title": "Password",
  58. "type": "string",
  59. "description": "The password to log in with",
  60. "format": "secret",
  61. "minLength": 1,
  62. },
  63. "priv": {
  64. "type": "string",
  65. "description": "File path to ssh private key, defaults to salt-ssh.rsa",
  66. "title": "Private Key",
  67. "minLength": 1,
  68. },
  69. "priv_passwd": {
  70. "type": "string",
  71. "description": "Passphrase for private key file",
  72. "title": "Private Key passphrase",
  73. "format": "secret",
  74. "minLength": 1,
  75. },
  76. "sudo": {
  77. "default": False,
  78. "type": "boolean",
  79. "description": "run command via sudo. Defaults to False",
  80. "title": "Sudo",
  81. },
  82. "timeout": {
  83. "type": "integer",
  84. "description": "Number of seconds to wait for response when establishing an SSH connection",
  85. "title": "Timeout",
  86. },
  87. "thin_dir": {
  88. "type": "string",
  89. "description": "The target system's storage directory for Salt components. Defaults to /tmp/salt-<hash>.",
  90. "title": "Thin Directory",
  91. },
  92. # The actuall representation of the minion options would make this HUGE!
  93. "minion_opts": ssh_schemas.DictItem(
  94. title="Minion Options",
  95. description="Dictionary of minion options",
  96. properties=MinionConfiguration(),
  97. ).serialize(),
  98. },
  99. "anyOf": [{"required": ["passwd"]}, {"required": ["priv"]}],
  100. "required": ["host", "user"],
  101. "x-ordering": [
  102. "host",
  103. "port",
  104. "user",
  105. "passwd",
  106. "priv",
  107. "priv_passwd",
  108. "sudo",
  109. "timeout",
  110. "thin_dir",
  111. "minion_opts",
  112. ],
  113. "additionalProperties": False,
  114. }
  115. try:
  116. self.assertDictContainsSubset(
  117. expected["properties"], config.serialize()["properties"]
  118. )
  119. self.assertDictContainsSubset(expected, config.serialize())
  120. except AssertionError:
  121. import salt.utils.json
  122. print(salt.utils.json.dumps(config.serialize(), indent=4))
  123. raise
  124. @skipIf(HAS_JSONSCHEMA is False, "The 'jsonschema' library is missing")
  125. def test_config_validate(self):
  126. try:
  127. jsonschema.validate(
  128. {"host": "localhost", "user": "root", "passwd": "foo"},
  129. ssh_schemas.RosterEntryConfig.serialize(),
  130. format_checker=jsonschema.FormatChecker(),
  131. )
  132. except jsonschema.exceptions.ValidationError as exc:
  133. self.fail("ValidationError raised: {0}".format(exc))
  134. try:
  135. jsonschema.validate(
  136. {"host": "127.0.0.1", "user": "root", "passwd": "foo"},
  137. ssh_schemas.RosterEntryConfig.serialize(),
  138. format_checker=jsonschema.FormatChecker(),
  139. )
  140. except jsonschema.exceptions.ValidationError as exc:
  141. self.fail("ValidationError raised: {0}".format(exc))
  142. try:
  143. jsonschema.validate(
  144. {"host": "127.1.0.1", "user": "root", "priv": "foo", "passwd": "foo"},
  145. ssh_schemas.RosterEntryConfig.serialize(),
  146. format_checker=jsonschema.FormatChecker(),
  147. )
  148. except jsonschema.exceptions.ValidationError as exc:
  149. self.fail("ValidationError raised: {0}".format(exc))
  150. try:
  151. jsonschema.validate(
  152. {"host": "127.1.0.1", "user": "root", "passwd": "foo", "sudo": False},
  153. ssh_schemas.RosterEntryConfig.serialize(),
  154. format_checker=jsonschema.FormatChecker(),
  155. )
  156. except jsonschema.exceptions.ValidationError as exc:
  157. self.fail("ValidationError raised: {0}".format(exc))
  158. try:
  159. jsonschema.validate(
  160. {
  161. "host": "127.1.0.1",
  162. "user": "root",
  163. "priv": "foo",
  164. "passwd": "foo",
  165. "thin_dir": "/foo/bar",
  166. },
  167. ssh_schemas.RosterEntryConfig.serialize(),
  168. format_checker=jsonschema.FormatChecker(),
  169. )
  170. except jsonschema.exceptions.ValidationError as exc:
  171. self.fail("ValidationError raised: {0}".format(exc))
  172. try:
  173. jsonschema.validate(
  174. {
  175. "host": "127.1.0.1",
  176. "user": "root",
  177. "passwd": "foo",
  178. "minion_opts": {"interface": "0.0.0.0"},
  179. },
  180. ssh_schemas.RosterEntryConfig.serialize(),
  181. format_checker=jsonschema.FormatChecker(),
  182. )
  183. except jsonschema.exceptions.ValidationError as exc:
  184. self.fail("ValidationError raised: {0}".format(exc))
  185. with self.assertRaises(jsonschema.exceptions.ValidationError) as excinfo:
  186. jsonschema.validate(
  187. {"host": "127.1.0.1", "user": "", "passwd": "foo"},
  188. ssh_schemas.RosterEntryConfig.serialize(),
  189. format_checker=jsonschema.FormatChecker(),
  190. )
  191. self.assertIn("is too short", excinfo.exception.message)
  192. with self.assertRaises(jsonschema.exceptions.ValidationError) as excinfo:
  193. jsonschema.validate(
  194. {
  195. "host": "127.1.0.1",
  196. "user": "root",
  197. "passwd": "foo",
  198. "minion_opts": {"interface": 0},
  199. },
  200. ssh_schemas.RosterEntryConfig.serialize(),
  201. format_checker=jsonschema.FormatChecker(),
  202. )
  203. self.assertIn("is not of type", excinfo.exception.message)
  204. class RosterItemTest(TestCase):
  205. def test_roster_config(self):
  206. try:
  207. self.assertDictContainsSubset(
  208. {
  209. "$schema": "http://json-schema.org/draft-04/schema#",
  210. "title": "Roster Configuration",
  211. "description": "Roster entries definition",
  212. "type": "object",
  213. "patternProperties": {
  214. r"^([^:]+)$": ssh_schemas.RosterEntryConfig.serialize()
  215. },
  216. "additionalProperties": False,
  217. },
  218. ssh_schemas.RosterItem.serialize(),
  219. )
  220. except AssertionError:
  221. import salt.utils.json
  222. print(salt.utils.json.dumps(ssh_schemas.RosterItem.serialize(), indent=4))
  223. raise
  224. @skipIf(HAS_JSONSCHEMA is False, "The 'jsonschema' library is missing")
  225. def test_roster_config_validate(self):
  226. try:
  227. jsonschema.validate(
  228. {"target-1": {"host": "localhost", "user": "root", "passwd": "foo"}},
  229. ssh_schemas.RosterItem.serialize(),
  230. format_checker=jsonschema.FormatChecker(),
  231. )
  232. except jsonschema.exceptions.ValidationError as exc:
  233. self.fail("ValidationError raised: {0}".format(exc))
  234. with self.assertRaises(jsonschema.exceptions.ValidationError) as excinfo:
  235. jsonschema.validate(
  236. {
  237. salt.utils.stringutils.to_str("target-1:1"): {
  238. "host": "localhost",
  239. "user": "root",
  240. "passwd": "foo",
  241. }
  242. },
  243. ssh_schemas.RosterItem.serialize(),
  244. format_checker=jsonschema.FormatChecker(),
  245. )
  246. if JSONSCHEMA_VERSION < _LooseVersion("2.6.0"):
  247. self.assertIn(
  248. "Additional properties are not allowed ('target-1:1' was unexpected)",
  249. excinfo.exception.message,
  250. )
  251. else:
  252. self.assertIn(
  253. "'target-1:1' does not match any of the regexes",
  254. excinfo.exception.message,
  255. )