pluginpath_validation_test.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2016 Red Hat, Inc.
  3. # All rights reserved.
  4. #
  5. # License: GPL (version 3 or any later version).
  6. # See LICENSE for details.
  7. # --- END COPYRIGHT BLOCK ---
  8. #
  9. import pytest
  10. from lib389.tasks import *
  11. from lib389.utils import *
  12. from lib389.topologies import topology_st
  13. from lib389.plugins import WhoamiPlugin
  14. pytestmark = pytest.mark.tier1
  15. logging.getLogger(__name__).setLevel(logging.DEBUG)
  16. log = logging.getLogger(__name__)
  17. @pytest.mark.ds47384
  18. @pytest.mark.ds47601
  19. def test_pluginpath_validation(topology_st):
  20. """Test pluginpath validation: relative and absolute paths
  21. With the inclusion of ticket 47601 - we do allow plugin paths
  22. outside the default location
  23. :id: 99f1fb2f-051d-4fd9-93d0-592dcd9b4c22
  24. :setup: Standalone instance
  25. :steps:
  26. 1. Copy the library to a temporary directory
  27. 2. Add valid plugin paths
  28. * using the absolute path to the current library
  29. * using new remote location
  30. 3. Set plugin path back to the default
  31. 4. Check invalid path (no library present)
  32. 5. Check invalid relative path (no library present)
  33. :expectedresults:
  34. 1. This should pass
  35. 2. This should pass
  36. 3. This should pass
  37. 4. This should fail
  38. 5. This should fail
  39. """
  40. inst = topology_st.standalone
  41. whoami = WhoamiPlugin(inst)
  42. # /tmp nowadays comes with noexec bit set on some systems
  43. # so instead let's write somewhere where dirsrv user has access
  44. tmp_dir = inst.get_bak_dir()
  45. plugin_dir = inst.get_plugin_dir()
  46. # Copy the library to our tmp directory
  47. try:
  48. shutil.copy('%s/libwhoami-plugin.so' % plugin_dir, tmp_dir)
  49. except IOError as e:
  50. log.fatal('Failed to copy %s/libwhoami-plugin.so to the tmp directory %s, error: %s' % (
  51. plugin_dir, tmp_dir, e.strerror))
  52. assert False
  53. #
  54. # Test adding valid plugin paths
  55. #
  56. # Try using the absolute path to the current library
  57. whoami.replace('nsslapd-pluginPath', '%s/libwhoami-plugin' % plugin_dir)
  58. # Try using new remote location
  59. # If SELinux is enabled, plugin can't be loaded as it's not labeled properly
  60. if selinux_present:
  61. import selinux
  62. if selinux.is_selinux_enabled():
  63. with pytest.raises(ldap.UNWILLING_TO_PERFORM):
  64. whoami.replace('nsslapd-pluginPath', '%s/libwhoami-plugin' % tmp_dir)
  65. # Label it with lib_t, so it can be executed
  66. # We can't use selinux.setfilecon() here, because py.test needs to have mac_admin capability
  67. # Instead we can call chcon directly:
  68. subprocess.check_call(['/usr/bin/chcon', '-t', 'lib_t', '%s/libwhoami-plugin.so' % tmp_dir])
  69. # And try to change the path again
  70. whoami.replace('nsslapd-pluginPath', '%s/libwhoami-plugin' % tmp_dir)
  71. else:
  72. whoami.replace('nsslapd-pluginPath', '%s/libwhoami-plugin' % tmp_dir)
  73. # Set plugin path back to the default
  74. whoami.replace('nsslapd-pluginPath', 'libwhoami-plugin')
  75. #
  76. # Test invalid path (no library present)
  77. #
  78. with pytest.raises(ldap.UNWILLING_TO_PERFORM):
  79. whoami.replace('nsslapd-pluginPath', '/bin/libwhoami-plugin')
  80. # No exception?! This is an error
  81. log.error('Invalid plugin path was incorrectly accepted by the server!')
  82. #
  83. # Test invalid relative path (no library present)
  84. #
  85. with pytest.raises(ldap.UNWILLING_TO_PERFORM):
  86. whoami.replace('nsslapd-pluginPath', '../libwhoami-plugin')
  87. # No exception?! This is an error
  88. log.error('Invalid plugin path was incorrectly accepted by the server!')
  89. log.info('Test complete')
  90. if __name__ == '__main__':
  91. # Run isolated
  92. # -s for DEBUG mode
  93. CURRENT_FILE = os.path.realpath(__file__)
  94. pytest.main("-s %s" % CURRENT_FILE)