test_ssh.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: :email:`Daniel Wallace <dwallace@saltstack.com`
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. import shutil
  9. import tempfile
  10. # Import Salt Testing libs
  11. from tests.support.runtime import RUNTIME_VARS
  12. from tests.support.unit import skipIf, TestCase
  13. from tests.support.case import ShellCase
  14. from tests.support.mock import patch, MagicMock
  15. # Import Salt libs
  16. import salt.config
  17. import salt.roster
  18. import salt.utils.files
  19. import salt.utils.path
  20. import salt.utils.thin
  21. import salt.utils.yaml
  22. from salt.client import ssh
  23. ROSTER = '''
  24. localhost:
  25. host: 127.0.0.1
  26. port: 2827
  27. self:
  28. host: 0.0.0.0
  29. port: 42
  30. '''
  31. @skipIf(not salt.utils.path.which('ssh'), "No ssh binary found in path")
  32. class SSHPasswordTests(ShellCase):
  33. def test_password_failure(self):
  34. '''
  35. Check password failures when trying to deploy keys
  36. '''
  37. opts = salt.config.client_config(self.get_config_file_path('master'))
  38. opts['list_hosts'] = False
  39. opts['argv'] = ['test.ping']
  40. opts['selected_target_option'] = 'glob'
  41. opts['tgt'] = 'localhost'
  42. opts['arg'] = []
  43. roster = os.path.join(self.config_dir, 'roster')
  44. handle_ssh_ret = [
  45. {'localhost': {'retcode': 255, 'stderr': u'Permission denied (publickey).\r\n', 'stdout': ''}},
  46. ]
  47. expected = {'localhost': 'Permission denied (publickey)'}
  48. display_output = MagicMock()
  49. with patch('salt.roster.get_roster_file', MagicMock(return_value=roster)), \
  50. patch('salt.client.ssh.SSH.handle_ssh', MagicMock(return_value=handle_ssh_ret)), \
  51. patch('salt.client.ssh.SSH.key_deploy', MagicMock(return_value=expected)), \
  52. patch('salt.output.display_output', display_output):
  53. client = ssh.SSH(opts)
  54. ret = next(client.run_iter())
  55. with self.assertRaises(SystemExit):
  56. client.run()
  57. display_output.assert_called_once_with(expected, 'nested', opts)
  58. self.assertIs(ret, handle_ssh_ret[0])
  59. class SSHRosterDefaults(TestCase):
  60. def test_roster_defaults_flat(self):
  61. '''
  62. Test Roster Defaults on the flat roster
  63. '''
  64. tempdir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
  65. expected = {
  66. 'self': {
  67. 'host': '0.0.0.0',
  68. 'user': 'daniel',
  69. 'port': 42,
  70. },
  71. 'localhost': {
  72. 'host': '127.0.0.1',
  73. 'user': 'daniel',
  74. 'port': 2827,
  75. },
  76. }
  77. try:
  78. root_dir = os.path.join(tempdir, 'foo', 'bar')
  79. os.makedirs(root_dir)
  80. fpath = os.path.join(root_dir, 'config')
  81. with salt.utils.files.fopen(fpath, 'w') as fp_:
  82. fp_.write(
  83. '''
  84. roster_defaults:
  85. user: daniel
  86. '''
  87. )
  88. opts = salt.config.master_config(fpath)
  89. with patch('salt.roster.get_roster_file', MagicMock(return_value=ROSTER)):
  90. with patch('salt.template.compile_template', MagicMock(return_value=salt.utils.yaml.safe_load(ROSTER))):
  91. roster = salt.roster.Roster(opts=opts)
  92. self.assertEqual(roster.targets('*', 'glob'), expected)
  93. finally:
  94. if os.path.isdir(tempdir):
  95. shutil.rmtree(tempdir)
  96. class SSHSingleTests(TestCase):
  97. def setUp(self):
  98. self.tmp_cachedir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
  99. def test_single_opts(self):
  100. ''' Sanity check for ssh.Single options
  101. '''
  102. argv = ['ssh.set_auth_key', 'root', 'hobn+amNAXSBTiOXEqlBjGB...rsa root@master']
  103. opts = {
  104. 'argv': argv,
  105. '__role': 'master',
  106. 'cachedir': self.tmp_cachedir,
  107. 'extension_modules': os.path.join(self.tmp_cachedir, 'extmods'),
  108. }
  109. target = {
  110. 'passwd': 'abc123',
  111. 'ssh_options': None,
  112. 'sudo': False,
  113. 'identities_only': False,
  114. 'host': 'login1',
  115. 'user': 'root',
  116. 'timeout': 65,
  117. 'remote_port_forwards': None,
  118. 'sudo_user': '',
  119. 'port': '22',
  120. 'priv': '/etc/salt/pki/master/ssh/salt-ssh.rsa'
  121. }
  122. single = ssh.Single(
  123. opts,
  124. opts['argv'],
  125. 'localhost',
  126. mods={},
  127. fsclient=None,
  128. thin=salt.utils.thin.thin_path(opts['cachedir']),
  129. mine=False,
  130. **target)
  131. self.assertEqual(single.shell._ssh_opts(), '')
  132. self.assertEqual(single.shell._cmd_str('date +%s'), 'ssh login1 '
  133. '-o KbdInteractiveAuthentication=no -o '
  134. 'PasswordAuthentication=yes -o ConnectTimeout=65 -o Port=22 '
  135. '-o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa '
  136. '-o User=root date +%s')