test_app_pam.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. """
  2. Integration Tests for restcherry salt-api with pam eauth
  3. """
  4. import salt.utils.platform
  5. import tests.support.cherrypy_testclasses as cptc
  6. from salt.ext.six.moves.urllib.parse import ( # pylint: disable=no-name-in-module,import-error
  7. urlencode,
  8. )
  9. from tests.support.case import ModuleCase
  10. from tests.support.helpers import destructiveTest, skip_if_not_root, slowTest
  11. from tests.support.unit import skipIf
  12. if cptc.HAS_CHERRYPY:
  13. import cherrypy
  14. USERA = "saltdev-netapi"
  15. USERA_PWD = "saltdev"
  16. HASHED_USERA_PWD = "$6$SALTsalt$ZZFD90fKFWq8AGmmX0L3uBtS9fXL62SrTk5zcnQ6EkD6zoiM3kB88G1Zvs0xm/gZ7WXJRs5nsTBybUvGSqZkT."
  17. AUTH_CREDS = {"username": USERA, "password": USERA_PWD, "eauth": "pam"}
  18. @skipIf(cptc.HAS_CHERRYPY is False, "CherryPy not installed")
  19. class TestAuthPAM(cptc.BaseRestCherryPyTest, ModuleCase):
  20. """
  21. Test auth with pam using salt-api
  22. """
  23. @destructiveTest
  24. @skip_if_not_root
  25. def setUp(self):
  26. super().setUp()
  27. try:
  28. add_user = self.run_function("user.add", [USERA], createhome=False)
  29. add_pwd = self.run_function(
  30. "shadow.set_password",
  31. [
  32. USERA,
  33. USERA_PWD if salt.utils.platform.is_darwin() else HASHED_USERA_PWD,
  34. ],
  35. )
  36. self.assertTrue(add_user)
  37. self.assertTrue(add_pwd)
  38. user_list = self.run_function("user.list_users")
  39. self.assertIn(USERA, str(user_list))
  40. except AssertionError:
  41. self.run_function("user.delete", [USERA], remove=True)
  42. self.skipTest("Could not add user or password, skipping test")
  43. @slowTest
  44. def test_bad_pwd_pam_chsh_service(self):
  45. """
  46. Test login while specifying chsh service with bad passwd
  47. This test ensures this PR is working correctly:
  48. https://github.com/saltstack/salt/pull/31826
  49. """
  50. copyauth_creds = AUTH_CREDS.copy()
  51. copyauth_creds["service"] = "chsh"
  52. copyauth_creds["password"] = "wrong_password"
  53. body = urlencode(copyauth_creds)
  54. request, response = self.request(
  55. "/login",
  56. method="POST",
  57. body=body,
  58. headers={"content-type": "application/x-www-form-urlencoded"},
  59. )
  60. self.assertEqual(response.status, "401 Unauthorized")
  61. @slowTest
  62. def test_bad_pwd_pam_login_service(self):
  63. """
  64. Test login while specifying login service with bad passwd
  65. This test ensures this PR is working correctly:
  66. https://github.com/saltstack/salt/pull/31826
  67. """
  68. copyauth_creds = AUTH_CREDS.copy()
  69. copyauth_creds["service"] = "login"
  70. copyauth_creds["password"] = "wrong_password"
  71. body = urlencode(copyauth_creds)
  72. request, response = self.request(
  73. "/login",
  74. method="POST",
  75. body=body,
  76. headers={"content-type": "application/x-www-form-urlencoded"},
  77. )
  78. self.assertEqual(response.status, "401 Unauthorized")
  79. @slowTest
  80. def test_good_pwd_pam_chsh_service(self):
  81. """
  82. Test login while specifying chsh service with good passwd
  83. This test ensures this PR is working correctly:
  84. https://github.com/saltstack/salt/pull/31826
  85. """
  86. copyauth_creds = AUTH_CREDS.copy()
  87. copyauth_creds["service"] = "chsh"
  88. body = urlencode(copyauth_creds)
  89. request, response = self.request(
  90. "/login",
  91. method="POST",
  92. body=body,
  93. headers={"content-type": "application/x-www-form-urlencoded"},
  94. )
  95. self.assertEqual(response.status, "200 OK")
  96. @slowTest
  97. def test_good_pwd_pam_login_service(self):
  98. """
  99. Test login while specifying login service with good passwd
  100. This test ensures this PR is working correctly:
  101. https://github.com/saltstack/salt/pull/31826
  102. """
  103. copyauth_creds = AUTH_CREDS.copy()
  104. copyauth_creds["service"] = "login"
  105. body = urlencode(copyauth_creds)
  106. request, response = self.request(
  107. "/login",
  108. method="POST",
  109. body=body,
  110. headers={"content-type": "application/x-www-form-urlencoded"},
  111. )
  112. self.assertEqual(response.status, "200 OK")
  113. @destructiveTest
  114. @skip_if_not_root
  115. def tearDown(self):
  116. """
  117. Clean up after tests. Delete user
  118. """
  119. super().tearDown()
  120. user_list = self.run_function("user.list_users")
  121. # Remove saltdev user
  122. if USERA in user_list:
  123. self.run_function("user.delete", [USERA], remove=True)
  124. # need to exit cherypy engine
  125. cherrypy.engine.exit()