test_ssh_auth.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. """
  3. Test the ssh_auth states
  4. """
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. # Import salt libs
  9. import salt.utils.files
  10. # Import Salt Testing libs
  11. from tests.support.case import ModuleCase
  12. from tests.support.helpers import destructiveTest, skip_if_not_root, with_system_user
  13. from tests.support.mixins import SaltReturnAssertsMixin
  14. from tests.support.runtests import RUNTIME_VARS
  15. from tests.support.unit import skipIf
  16. class SSHAuthStateTests(ModuleCase, SaltReturnAssertsMixin):
  17. @destructiveTest
  18. @skip_if_not_root
  19. @with_system_user("issue_7409", on_existing="delete", delete=True)
  20. @skipIf(True, "SLOWTEST skip")
  21. def test_issue_7409_no_linebreaks_between_keys(self, username):
  22. userdetails = self.run_function("user.info", [username])
  23. user_ssh_dir = os.path.join(userdetails["home"], ".ssh")
  24. authorized_keys_file = os.path.join(user_ssh_dir, "authorized_keys")
  25. ret = self.run_state(
  26. "file.managed",
  27. name=authorized_keys_file,
  28. user=username,
  29. makedirs=True,
  30. contents_newline=False,
  31. # Explicit no ending line break
  32. contents="ssh-rsa AAAAB3NzaC1kc3MAAACBAL0sQ9fJ5bYTEyY== root",
  33. )
  34. ret = self.run_state(
  35. "ssh_auth.present",
  36. name="AAAAB3NzaC1kcQ9J5bYTEyZ==",
  37. enc="ssh-rsa",
  38. user=username,
  39. comment=username,
  40. )
  41. self.assertSaltTrueReturn(ret)
  42. self.assertSaltStateChangesEqual(ret, {"AAAAB3NzaC1kcQ9J5bYTEyZ==": "New"})
  43. with salt.utils.files.fopen(authorized_keys_file, "r") as fhr:
  44. self.assertEqual(
  45. fhr.read(),
  46. "ssh-rsa AAAAB3NzaC1kc3MAAACBAL0sQ9fJ5bYTEyY== root\n"
  47. "ssh-rsa AAAAB3NzaC1kcQ9J5bYTEyZ== {0}\n".format(username),
  48. )
  49. @destructiveTest
  50. @skip_if_not_root
  51. @with_system_user("issue_10198", on_existing="delete", delete=True)
  52. @skipIf(True, "SLOWTEST skip")
  53. def test_issue_10198_keyfile_from_another_env(self, username=None):
  54. userdetails = self.run_function("user.info", [username])
  55. user_ssh_dir = os.path.join(userdetails["home"], ".ssh")
  56. authorized_keys_file = os.path.join(user_ssh_dir, "authorized_keys")
  57. key_fname = "issue_10198.id_rsa.pub"
  58. # Create the keyfile that we expect to get back on the state call
  59. with salt.utils.files.fopen(
  60. os.path.join(RUNTIME_VARS.TMP_PRODENV_STATE_TREE, key_fname), "w"
  61. ) as kfh:
  62. kfh.write("ssh-rsa AAAAB3NzaC1kcQ9J5bYTEyZ== {0}\n".format(username))
  63. # Create a bogus key file on base environment
  64. with salt.utils.files.fopen(
  65. os.path.join(RUNTIME_VARS.TMP_STATE_TREE, key_fname), "w"
  66. ) as kfh:
  67. kfh.write("ssh-rsa BAAAB3NzaC1kcQ9J5bYTEyZ== {0}\n".format(username))
  68. ret = self.run_state(
  69. "ssh_auth.present",
  70. name="Setup Keys",
  71. source="salt://{0}?saltenv=prod".format(key_fname),
  72. enc="ssh-rsa",
  73. user=username,
  74. comment=username,
  75. )
  76. self.assertSaltTrueReturn(ret)
  77. with salt.utils.files.fopen(authorized_keys_file, "r") as fhr:
  78. self.assertEqual(
  79. fhr.read(), "ssh-rsa AAAAB3NzaC1kcQ9J5bYTEyZ== {0}\n".format(username)
  80. )
  81. os.unlink(authorized_keys_file)
  82. ret = self.run_state(
  83. "ssh_auth.present",
  84. name="Setup Keys",
  85. source="salt://{0}".format(key_fname),
  86. enc="ssh-rsa",
  87. user=username,
  88. comment=username,
  89. saltenv="prod",
  90. )
  91. self.assertSaltTrueReturn(ret)
  92. with salt.utils.files.fopen(authorized_keys_file, "r") as fhr:
  93. self.assertEqual(
  94. fhr.read(), "ssh-rsa AAAAB3NzaC1kcQ9J5bYTEyZ== {0}\n".format(username)
  95. )