test_app_pam.py 4.7 KB

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