|
16 | 16 |
|
17 | 17 | """This module contains Splunk credential related interfaces.""" |
18 | 18 |
|
19 | | -import json |
20 | 19 | import re |
21 | 20 | import warnings |
22 | 21 | from typing import Dict, List |
23 | 22 |
|
24 | 23 | from splunklib import binding, client |
25 | 24 |
|
26 | 25 | from . import splunk_rest_client as rest_client |
27 | | -from .net_utils import validate_scheme_host_port |
28 | | -from .splunkenv import get_splunkd_access_info |
29 | 26 | from .utils import retry |
30 | 27 |
|
31 | 28 | __all__ = [ |
32 | 29 | "CredentialException", |
33 | 30 | "CredentialNotExistException", |
34 | 31 | "CredentialManager", |
35 | | - "get_session_key", |
36 | 32 | ] |
37 | 33 |
|
38 | 34 |
|
@@ -339,56 +335,3 @@ def _get_all_passwords(self) -> List[Dict[str, str]]: |
339 | 335 | ) |
340 | 336 | passwords = self._storage_passwords.list(count=-1) |
341 | 337 | return self._get_clear_passwords(passwords) |
342 | | - |
343 | | - |
344 | | -@retry(exceptions=[binding.HTTPError]) |
345 | | -def get_session_key( |
346 | | - username: str, |
347 | | - password: str, |
348 | | - scheme: str = None, |
349 | | - host: str = None, |
350 | | - port: int = None, |
351 | | - **context: dict, |
352 | | -) -> str: |
353 | | - """Get splunkd access token. |
354 | | -
|
355 | | - Arguments: |
356 | | - username: The Splunk account username, which is used to authenticate the Splunk instance. |
357 | | - password: The Splunk account password. |
358 | | - scheme: (optional) The access scheme, default is None. |
359 | | - host: (optional) The host name, default is None. |
360 | | - port: (optional) The port number, default is None. |
361 | | - context: Other configurations for Splunk rest client. |
362 | | -
|
363 | | - Returns: |
364 | | - Splunk session key. |
365 | | -
|
366 | | - Raises: |
367 | | - CredentialException: If username/password are invalid. |
368 | | - ValueError: if scheme, host or port are invalid. |
369 | | -
|
370 | | - Examples: |
371 | | - >>> get_session_key('user', 'password') |
372 | | - """ |
373 | | - validate_scheme_host_port(scheme, host, port) |
374 | | - |
375 | | - if any([scheme is None, host is None, port is None]): |
376 | | - scheme, host, port = get_splunkd_access_info() |
377 | | - |
378 | | - uri = "{scheme}://{host}:{port}/{endpoint}".format( |
379 | | - scheme=scheme, host=host, port=port, endpoint="services/auth/login" |
380 | | - ) |
381 | | - _rest_client = rest_client.SplunkRestClient( |
382 | | - None, "-", "nobody", scheme, host, port, **context |
383 | | - ) |
384 | | - try: |
385 | | - response = _rest_client.http.post( |
386 | | - uri, username=username, password=password, output_mode="json" |
387 | | - ) |
388 | | - except binding.HTTPError as e: |
389 | | - if e.status != 401: |
390 | | - raise |
391 | | - |
392 | | - raise CredentialException("Invalid username/password.") |
393 | | - |
394 | | - return json.loads(response.body.read())["sessionKey"] |
0 commit comments