test_salt_auth.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """
  2. tests.integration.shell.auth
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. """
  5. import logging
  6. import pytest
  7. import salt.utils.platform
  8. import salt.utils.pycrypto
  9. from tests.support.helpers import slowTest
  10. log = logging.getLogger(__name__)
  11. pytestmark = [
  12. pytest.mark.skip_if_not_root,
  13. pytest.mark.destructive_test,
  14. pytest.mark.skip_on_windows,
  15. ]
  16. USERA = "saltdev-auth"
  17. USERA_PWD = "saltdev"
  18. @pytest.fixture(scope="module")
  19. def saltdev_account(sminion):
  20. try:
  21. assert sminion.functions.user.add(USERA, createhome=False)
  22. assert sminion.functions.shadow.set_password(
  23. USERA,
  24. USERA_PWD
  25. if salt.utils.platform.is_darwin()
  26. else salt.utils.pycrypto.gen_hash(password=USERA_PWD),
  27. )
  28. assert USERA in sminion.functions.user.list_users()
  29. # Run tests
  30. yield
  31. finally:
  32. sminion.functions.user.delete(USERA, remove=True)
  33. SALTOPS = "saltops"
  34. @pytest.fixture(scope="module")
  35. def saltops_group(sminion):
  36. try:
  37. assert sminion.functions.group.add(SALTOPS)
  38. # Run tests
  39. yield
  40. finally:
  41. sminion.functions.group.delete(SALTOPS)
  42. USERB = "saltdev-adm"
  43. USERB_PWD = USERA_PWD
  44. @pytest.fixture(scope="module")
  45. def saltadm_account(sminion, saltops_group):
  46. try:
  47. assert sminion.functions.user.add(USERB, groups=[SALTOPS], createhome=False)
  48. assert sminion.functions.shadow.set_password(
  49. USERB,
  50. USERB_PWD
  51. if salt.utils.platform.is_darwin()
  52. else salt.utils.pycrypto.gen_hash(password=USERB_PWD),
  53. )
  54. assert USERB in sminion.functions.user.list_users()
  55. # Run tests
  56. yield
  57. finally:
  58. sminion.functions.user.delete(USERB, remove=True)
  59. @slowTest
  60. def test_pam_auth_valid_user(salt_minion, salt_cli, saltdev_account):
  61. """
  62. test that pam auth mechanism works with a valid user
  63. """
  64. # test user auth against pam
  65. ret = salt_cli.run(
  66. "-a",
  67. "pam",
  68. "--username",
  69. USERA,
  70. "--password",
  71. USERA_PWD,
  72. "test.ping",
  73. minion_tgt=salt_minion.id,
  74. )
  75. assert ret.exitcode == 0
  76. assert ret.json is True
  77. @slowTest
  78. def test_pam_auth_invalid_user(salt_minion, salt_cli, saltdev_account):
  79. """
  80. test pam auth mechanism errors for an invalid user
  81. """
  82. ret = salt_cli.run(
  83. "-a",
  84. "pam",
  85. "--username",
  86. "nouser",
  87. "--password",
  88. "1234",
  89. "test.ping",
  90. minion_tgt=salt_minion.id,
  91. )
  92. assert ret.stdout == "Authentication error occurred."
  93. @slowTest
  94. def test_pam_auth_valid_group(salt_minion, salt_cli, saltadm_account):
  95. """
  96. test that pam auth mechanism works for a valid group
  97. """
  98. # test group auth against pam: saltadm is not configured in
  99. # external_auth, but saltops is and saldadm is a member of saltops
  100. ret = salt_cli.run(
  101. "-a",
  102. "pam",
  103. "--username",
  104. USERB,
  105. "--password",
  106. USERB_PWD,
  107. "test.ping",
  108. minion_tgt=salt_minion.id,
  109. )
  110. assert ret.exitcode == 0
  111. assert ret.json is True