test_app_pam.py 4.9 KB

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