1919import os
2020import os .path as op
2121import socket
22- import subprocess
23- from configparser import ConfigParser
24- from io import StringIO
2522from typing import List , Optional , Tuple , Union
23+ from splunk .clilib .cli_common import getAppConf
24+ from splunk .clilib .bundle_paths import make_splunkhome_path
2625
2726from .utils import is_true
2827
@@ -112,41 +111,7 @@ def make_splunkhome_path(parts: Union[List, Tuple]) -> str:
112111 ValueError: Escape from intended parent directories.
113112 """
114113
115- relpath = os .path .normpath (os .path .join (* parts ))
116-
117- basepath = None
118- shared_storage = _get_shared_storage ()
119- if shared_storage :
120- for candidate in on_shared_storage :
121- # SPL-100508 On windows if the path is missing the drive letter,
122- # construct fullpath manually and call relpath
123- if os .name == "nt" and not _verify_path_prefix (relpath , candidate ):
124- break
125-
126- if os .path .relpath (relpath , candidate )[0 :2 ] != ".." :
127- basepath = shared_storage
128- break
129-
130- if basepath is None :
131- etc_with_trailing_sep = os .path .join (ETC_LEAF , "" )
132- if relpath == ETC_LEAF or relpath .startswith (etc_with_trailing_sep ):
133- # Redirect $SPLUNK_HOME/etc to $SPLUNK_ETC.
134- basepath = _splunk_etc ()
135- # Remove leading etc (and path separator, if present). Note: when
136- # emitting $SPLUNK_ETC exactly, with no additional path parts, we
137- # set <relpath> to the empty string.
138- relpath = relpath [4 :]
139- else :
140- basepath = _splunk_home ()
141-
142- fullpath = os .path .normpath (os .path .join (basepath , relpath ))
143-
144- # Check that we haven't escaped from intended parent directories.
145- if os .path .relpath (fullpath , basepath )[0 :2 ] == ".." :
146- raise ValueError (
147- f'Illegal escape from parent directory "{ basepath } ": { fullpath } '
148- )
149- return fullpath
114+ make_splunkhome_path (parts )
150115
151116
152117def get_splunk_host_info () -> Tuple :
@@ -284,7 +249,7 @@ def get_conf_stanza(
284249 return stanzas [stanza ]
285250
286251
287- def get_conf_stanzas (conf_name : str , app_name : Optional [ str ] = None ) -> dict :
252+ def get_conf_stanzas (conf_name : str , app_name : str , logger = None ) -> dict :
288253 """Get stanzas of `conf_name`
289254
290255 Arguments:
@@ -299,34 +264,13 @@ def get_conf_stanzas(conf_name: str, app_name: Optional[str] = None) -> dict:
299264 >>> return: {'serverName': 'testServer', 'sessionTimeout': '1h', ...}
300265 """
301266
302- if conf_name .endswith (".conf" ):
303- conf_name = conf_name [:- 5 ]
304-
305- # TODO: dynamically calculate SPLUNK_HOME
306- btool_cli = [
307- op .join (os .environ ["SPLUNK_HOME" ], "bin" , "splunk" ),
308- "cmd" ,
309- "btool" ,
310- conf_name ,
311- "list" ,
312- ]
313-
314- if app_name :
315- btool_cli .append (f"--app={ app_name } " )
316-
317- p = subprocess .Popen ( # nosemgrep: python.lang.security.audit.dangerous-subprocess-use.dangerous-subprocess-use
318- btool_cli , stdout = subprocess .PIPE , stderr = subprocess .PIPE
319- )
320- out , _ = p .communicate ()
321-
322- if isinstance (out , bytes ):
323- out = out .decode ()
324-
325- parser = ConfigParser (** {"strict" : False })
326- parser .optionxform = str
327- parser .read_file (StringIO (out ))
328-
329- out = {}
330- for section in parser .sections ():
331- out [section ] = {item [0 ]: item [1 ] for item in parser .items (section , raw = True )}
332- return out
267+ path = make_splunkhome_path (["etc" , "apps" , app_name ])
268+ app_conf = getAppConf (confName = conf_name , app = app_name , use_btool = False , app_path = path )
269+
270+ if logger :
271+ logger .info (f"akakakakakkakakakaka 21 path: { path } " )
272+
273+ if logger :
274+ logger .info (f"akakakakakkakakakaka 22 app_conf={ type (app_conf )} app_conf: { app_conf } " )
275+
276+ return app_conf
0 commit comments