Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(release): v4.12.0 #346

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ repos:
hooks:
- id: docformatter
args: [--in-place]
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
hooks:
- id: flake8
23 changes: 23 additions & 0 deletions solnlib/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import logging
import logging.handlers
import os.path as op
import traceback
from threading import Lock
from typing import Dict, Any

Expand Down Expand Up @@ -263,3 +264,25 @@ def events_ingested(
"n_events": n_events,
},
)


def log_exception(
logger: logging.Logger,
e: Exception,
full_msg: bool = True,
msg_before: str = None,
msg_after: str = None,
log_level: int = logging.ERROR,
):
"""General function to log exceptions."""
exc_type, exc_value, exc_traceback = type(e), e, e.__traceback__
if full_msg:
error = traceback.format_exception(exc_type, exc_value, exc_traceback)
else:
error = traceback.format_exception_only(exc_type, exc_value)

msg_start = msg_before if msg_before is not None else ""
msg_mid = "".join(error)
msg_end = msg_after if msg_after is not None else ""
msg = f"{msg_start}\n{msg_mid}\n{msg_end}"
logger.log(log_level, msg)
5 changes: 3 additions & 2 deletions tests/integration/_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import context
import os.path as op
import sys
import time

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))
import context
from splunklib import client
from splunklib import results as splunklib_results

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))


def search(session_key, query):
service = client.connect(host=context.host, token=session_key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import uuid

from solnlib import log
from solnlib.modular_input import *
from solnlib.modular_input import ModularInput, Argument

# Set log context
log.Logs.set_context(namespace="solnlib_demo", root_logger_log_file="collector")
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/test__kvstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
#

import context
import json
import os.path as op
import sys
Expand All @@ -22,11 +23,11 @@

import pytest

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))
import context
from splunklib import binding, client
from splunklib.binding import HTTPError

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))


def test_kvstore():
session_key = context.get_session_key()
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/test_acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
# limitations under the License.
#

import context
import os.path as op
import sys
from solnlib import acl

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))
import context

from solnlib import acl


def test_acl_manager():
Expand Down
7 changes: 3 additions & 4 deletions tests/integration/test_conf_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
# limitations under the License.
#

import context
import os.path as op
import sys
import pytest
from solnlib import conf_manager
from unittest import mock

import pytest

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))
import context

from solnlib import conf_manager


def _build_conf_manager(session_key: str) -> conf_manager.ConfManager:
Expand Down
6 changes: 2 additions & 4 deletions tests/integration/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
# limitations under the License.
#

import context
import os.path as op
import sys
from typing import Optional

import pytest
from solnlib import credentials

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))
import context

from solnlib import credentials


def _build_credential_manager(
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_hec_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
# limitations under the License.
#

import context
import os.path as op
import sys

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))
import context

from solnlib import hec_config

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))


def test_hec_config():
session_key = context.get_session_key()
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/test_hec_event_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import context
import os.path as op
import sys
import time

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))
import context
from _search import search

from solnlib.modular_input import event_writer as hew

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))


def test_hec_event_writer():
session_key = context.get_session_key()
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import context
import os.path as op
import sys
import time
from _search import search

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))
import context
from _search import search


def test_CVE_2023_32712():
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_splunkenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import os
import os.path as op
import sys
from solnlib import splunkenv

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))
from solnlib import splunkenv


def test_splunkenv():
Expand Down
6 changes: 2 additions & 4 deletions tests/integration/test_time_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
# limitations under the License.
#

import context
import datetime
import os.path as op
import sys

import pytest
from solnlib import time_parser

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))
import context

from solnlib import time_parser


def test_time_parser():
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_user_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
# limitations under the License.
#

import context
import os.path as op
import sys

import pytest

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))
import context

from solnlib import user_access

sys.path.insert(0, op.dirname(op.dirname(op.abspath(__file__))))


def test_object_acl_manager():
session_key = context.get_session_key()
Expand Down
38 changes: 31 additions & 7 deletions tests/unit/test_acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,37 @@

from solnlib import acl

_old_acl = '{"entry": [{"author": "nobody", "name": "transforms", "acl": {"sharing": "global", "perms": {"read": ["*"], "write": ["*"]}, "app": "unittest", "modifiable": true, "owner": "nobody", "can_change_perms": true, "can_share_global": true, "can_list": true, "can_share_user": false, "can_share_app": true, "removable": false, "can_write": true}}]}'

_new_acl1 = '{"entry": [{"author": "nobody", "name": "transforms", "acl": {"sharing": "global", "perms": {"read": ["admin"], "write": ["admin"]}, "app": "unittest", "modifiable": true, "owner": "nobody", "can_change_perms": true, "can_share_global": true, "can_list": true, "can_share_user": false, "can_share_app": true, "removable": false, "can_write": true}}]}'

_new_acl2 = '{"entry": [{"author": "nobody", "name": "transforms", "acl": {"sharing": "global", "perms": {"read": ["admin"], "write": ["*"]}, "app": "unittest", "modifiable": true, "owner": "nobody", "can_change_perms": true, "can_share_global": true, "can_list": true, "can_share_user": false, "can_share_app": true, "removable": false, "can_write": true}}]}'

_new_acl3 = '{"entry": [{"author": "nobody", "name": "transforms", "acl": {"sharing": "global", "perms": {"read": ["*"], "write": ["admin"]}, "app": "unittest", "modifiable": true, "owner": "nobody", "can_change_perms": true, "can_share_global": true, "can_list": true, "can_share_user": false, "can_share_app": true, "removable": false, "can_write": true}}]}'
_old_acl = (
'{"entry": [{"author": "nobody", "name": "transforms", '
'"acl": {"sharing": "global", "perms": {"read": ["*"], "write": ["*"]}, '
'"app": "unittest", "modifiable": true, "owner": "nobody", "can_change_perms": true, '
'"can_share_global": true, "can_list": true, "can_share_user": false, "can_share_app": true, '
'"removable": false, "can_write": true}}]}'
)

_new_acl1 = (
'{"entry": [{"author": "nobody", "name": "transforms", '
'"acl": {"sharing": "global", "perms": {"read": ["admin"], "write": ["admin"]}, '
'"app": "unittest", "modifiable": true, "owner": "nobody", "can_change_perms": true, '
'"can_share_global": true, "can_list": true, "can_share_user": false, "can_share_app": true, '
'"removable": false, "can_write": true}}]}'
)

_new_acl2 = (
'{"entry": [{"author": "nobody", "name": "transforms", '
'"acl": {"sharing": "global", "perms": {"read": ["admin"], "write": ["*"]}, '
'"app": "unittest", "modifiable": true, "owner": "nobody", "can_change_perms": true, '
'"can_share_global": true, "can_list": true, "can_share_user": false, "can_share_app": true, '
'"removable": false, "can_write": true}}]}'
)

_new_acl3 = (
'{"entry": [{"author": "nobody", "name": "transforms", '
'"acl": {"sharing": "global", "perms": {"read": ["*"], "write": ["admin"]}, '
'"app": "unittest", "modifiable": true, "owner": "nobody", "can_change_perms": true, '
'"can_share_global": true, "can_list": true, "can_share_user": false, "can_share_app": true, '
'"removable": false, "can_write": true}}]}'
)


def _mock_get(self, path_segment, owner=None, app=None, sharing=None, **query):
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
#

import logging
import json
import multiprocessing
import os
import shutil
import threading
import traceback
import time
from unittest import mock

Expand Down Expand Up @@ -197,3 +199,34 @@ def test_events_ingested():
logging.INFO,
"action=events_ingested modular_input_name=modular_input_name sourcetype_ingested=sourcetype n_events=5",
)


def test_log_exceptions_full_msg():
start_msg = "some msg before exception"
with mock.patch("logging.Logger") as mock_logger:
try:
test_jsons = "{'a': 'aa'"
json.loads(test_jsons)
except Exception as e:
log.log_exception(mock_logger, e, msg_before=start_msg)
mock_logger.log.assert_called_with(
logging.ERROR, f"{start_msg}\n{traceback.format_exc()}\n"
)


def test_log_exceptions_partial_msg():
start_msg = "some msg before exception"
end_msg = "some msg after exception"
with mock.patch("logging.Logger") as mock_logger:
try:
test_jsons = "{'a': 'aa'"
json.loads(test_jsons)
except Exception as e:
log.log_exception(
mock_logger, e, full_msg=False, msg_before=start_msg, msg_after=end_msg
)
mock_logger.log.assert_called_with(
logging.ERROR,
"some msg before exception\njson.decoder.JSONDecodeError: Expecting property name enclosed in double "
"quotes: line 1 column 2 (char 1)\n\nsome msg after exception",
)
Loading
Loading