Skip to content

Commit e101a79

Browse files
committed
Introduce util for locating the plugins dir
This commit adds a method to `gssapi.tests._utils` to locate the `krb5` plugin directory. The logic works roughly as such: If there is a LD_LIBRARY_PATH set, start searching there. First, assume that LD_LIBRARY_PATH refers to a normal installation root (like '/usr/lib'), and so search for a 'krb5/plugins' directory contain .so files somewhere amongst its subdirectories. If that doesn't work, assume LD_LIBRARY_PATH points right into a krb5 source directory's lib folder, so search for a 'plugins' directory which contains .so files, in the parent directory of LD_LIBRARY_PATH. If no LD_LIBRARY_PATH is set, or the above fails, use the '$(krb5-config --prefix)/lib' instead of LD_LIBRARY_PATH in the first case. If multiple results are found, use the shortest path. Also-Authored-By: Robbie Harwood (frozencemetery) <rharwood@redhat.com>
1 parent af11adf commit e101a79

File tree

2 files changed

+88
-11
lines changed

2 files changed

+88
-11
lines changed

gssapi/tests/_utils.py

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
from gssapi._utils import import_gssapi_extension
1+
import os
2+
import subprocess
23

3-
try:
4-
import commands
5-
get_output = commands.getoutput
6-
except ImportError:
7-
import subprocess
4+
from gssapi._utils import import_gssapi_extension
85

9-
def _get_output(*args, **kwargs):
10-
res = subprocess.check_output(*args, shell=True, **kwargs)
11-
decoded = res.decode('utf-8')
12-
return decoded.strip()
136

14-
get_output = _get_output
7+
def get_output(*args, **kwargs):
8+
res = subprocess.check_output(*args, shell=True, **kwargs)
9+
decoded = res.decode('utf-8')
10+
return decoded.strip()
1511

1612

1713
def _extension_test(extension_name, extension_text):
@@ -47,3 +43,80 @@ def ext_test(self, *args, **kwargs):
4743
return ext_test
4844

4945
return make_ext_test
46+
47+
48+
_PLUGIN_DIR = None
49+
50+
51+
def _find_plugin_dir():
52+
global _PLUGIN_DIR
53+
if _PLUGIN_DIR is not None:
54+
return _PLUGIN_DIR
55+
56+
# if we've set a LD_LIBRARY_PATH, use that first
57+
ld_path_raw = os.environ.get('LD_LIBRARY_PATH')
58+
if ld_path_raw is not None:
59+
# first, try assuming it's just a normal install
60+
61+
ld_paths = [path for path in ld_path_raw.split(':') if path]
62+
63+
for ld_path in ld_paths:
64+
if not os.path.exists(ld_path):
65+
continue
66+
67+
_PLUGIN_DIR = _decide_plugin_dir(
68+
_find_plugin_dirs_installed(ld_path))
69+
if _PLUGIN_DIR is None:
70+
_PLUGIN_DIR = _decide_plugin_dir(
71+
_find_plugin_dirs_src(ld_path))
72+
73+
if _PLUGIN_DIR is not None:
74+
break
75+
76+
# if there was no LD_LIBRARY_PATH, or the above failed
77+
if _PLUGIN_DIR is None:
78+
# if we don't have a LD_LIBRARY_PATH, just search in
79+
# $prefix/lib
80+
81+
lib_dir = os.path.join(get_output('krb5-config --prefix'), 'lib')
82+
_PLUGIN_DIR = _decide_plugin_dir(_find_plugin_dirs_installed(lib_dir))
83+
84+
if _PLUGIN_DIR is not None:
85+
_PLUGIN_DIR = os.path.normpath(_PLUGIN_DIR)
86+
return _PLUGIN_DIR
87+
else:
88+
return None
89+
90+
91+
def _decide_plugin_dir(dirs):
92+
if dirs is None:
93+
return None
94+
95+
# the shortest path is probably more correct
96+
shortest_first = sorted(dirs, key=len)
97+
98+
for path in shortest_first:
99+
# check to see if it actually contains .so files
100+
if get_output('find %s -name "*.so"' % path):
101+
return path
102+
103+
return None
104+
105+
106+
def _find_plugin_dirs_installed(search_path):
107+
options_raw = get_output('find %s/ -type d '
108+
'-path "*/krb5/plugins"' % search_path)
109+
110+
if options_raw:
111+
return options_raw.split('\n')
112+
else:
113+
return None
114+
115+
116+
def _find_plugin_dirs_src(search_path):
117+
options_raw = get_output('find %s/../ -type d -name plugins' % search_path)
118+
119+
if options_raw:
120+
return options_raw.split('\n')
121+
else:
122+
return None

gssapi/tests/k5test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
import six
3939

40+
from gssapi.tests import _utils
41+
4042

4143
def _cfg_merge(cfg1, cfg2):
4244
if not cfg2:
@@ -85,6 +87,8 @@ def _cfg_merge(cfg1, cfg2):
8587
'kdc_tcp_ports': '$port0',
8688
'database_name': '$tmpdir/db'}},
8789
'dbmodules': {
90+
'db_module_dir': os.path.join(_utils._find_plugin_dir(),
91+
'kdb'),
8892
'db': {'db_library': 'db2', 'database_name': '$tmpdir/db'}},
8993
'logging': {
9094
'admin_server': 'FILE:$tmpdir/kadmind5.log',

0 commit comments

Comments
 (0)