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

fix(oauth): set loglevel in oauth rh template to use log level set in add-on #1227

Merged
merged 4 commits into from
Jun 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import List
from typing import List, Any

from splunk_add_on_ucc_framework import utils
from splunk_add_on_ucc_framework.commands.rest_builder.endpoint.base import (
Expand All @@ -22,9 +22,11 @@


class OAuthModelEndpointBuilder(RestEndpointBuilder):
def __init__(self, name: str, namespace: str, app_name: str) -> None:
def __init__(self, name: str, namespace: str, app_name: str, **kwargs: Any) -> None:
super().__init__(name, namespace)
self._app_name = app_name
self._log_stanza = kwargs.get("log_stanza") or "logging"
self._log_level_field = kwargs.get("log_level_field") or "loglevel"

def actions(self) -> List[str]:
return ["edit"]
Expand All @@ -35,5 +37,7 @@ def generate_rh(self) -> str:
.get_template("oauth.template")
.render(
app_name=self._app_name,
log_stanza=self._log_stanza,
log_level_field=self._log_level_field,
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,13 @@ def _builder_configs(self) -> None:
# If we have given oauth support then we have to add endpoint for accesstoken
for entity_element in config["entity"]:
if entity_element["type"] == "oauth":
log_details = self.global_config.logging_tab
oauth_endpoint = OAuthModelEndpointBuilder(
name="oauth",
namespace=self.global_config.namespace,
app_name=self.global_config.product,
log_stanza=log_details.get("name"),
log_level_field=log_details.get("entity", [{}])[0].get("field"),
)
self._endpoints["oauth"] = oauth_endpoint
self._oauth_conf_file_names.add(oauth_endpoint.conf_name)
Expand Down
9 changes: 8 additions & 1 deletion splunk_add_on_ucc_framework/global_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from splunk_add_on_ucc_framework import utils
from splunk_add_on_ucc_framework.entity import expand_entity
from splunk_add_on_ucc_framework.tabs import resolve_tab
from splunk_add_on_ucc_framework.tabs import resolve_tab, LoggingTab

Loader = getattr(yaml, "CSafeLoader", yaml.SafeLoader)
yaml_load = functools.partial(yaml.load, Loader=Loader)
Expand Down Expand Up @@ -123,6 +123,13 @@ def settings(self) -> List[Any]:
settings.append(tab)
return settings

@property
def logging_tab(self) -> Dict[str, Any]:
for tab in self.tabs:
if LoggingTab.from_definition(tab) is not None:
return tab
return {}

@property
def configs(self) -> List[Any]:
configs = []
Expand Down
13 changes: 13 additions & 0 deletions splunk_add_on_ucc_framework/templates/oauth.template
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ class {{app_name | lower}}_rh_oauth2_token(admin.MConfigHandler):
This method checks which action is getting called and what parameters are required for the request.
"""

def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
session_key = self.getSessionKey()
log_level = conf_manager.get_log_level(
logger=logger,
session_key=session_key,
app_name="{{app_name}}",
conf_name="{{app_name | lower}}_settings",
log_stanza="{{log_stanza}}",
log_level_field="{{log_level_field}}"
)
logger.set_level(log_level)

def setup(self):
if self.requestedAction == admin.ACTION_EDIT:
# Add required args in supported args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ class splunk_ta_uccexample_rh_oauth2_token(admin.MConfigHandler):
This method checks which action is getting called and what parameters are required for the request.
"""

def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
session_key = self.getSessionKey()
log_level = conf_manager.get_log_level(
logger=logger,
session_key=session_key,
app_name="Splunk_TA_UCCExample",
conf_name="splunk_ta_uccexample_settings",
log_stanza="logging",
log_level_field="loglevel"
)
logger.set_level(log_level)

def setup(self):
if self.requestedAction == admin.ACTION_EDIT:
# Add required args in supported args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ class splunk_ta_uccexample_rh_oauth2_token(admin.MConfigHandler):
This method checks which action is getting called and what parameters are required for the request.
"""

def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
session_key = self.getSessionKey()
log_level = conf_manager.get_log_level(
logger=logger,
session_key=session_key,
app_name="Splunk_TA_UCCExample",
conf_name="splunk_ta_uccexample_settings",
log_stanza="logging",
log_level_field="loglevel"
)
logger.set_level(log_level)

def setup(self):
if self.requestedAction == admin.ACTION_EDIT:
# Add required args in supported args
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import patch

from splunk_add_on_ucc_framework.commands.rest_builder.global_config_builder_schema import (
GlobalConfigBuilderSchema,
)
Expand Down Expand Up @@ -45,3 +47,19 @@ def test_global_config_builder_schema_custom_rh_config(global_config_all_json):
).rh_class
== "CustomAccountValidator"
)


@patch(
"splunk_add_on_ucc_framework.commands.rest_builder.global_config_builder_schema.OAuthModelEndpointBuilder",
autospec=True,
)
def test__builder_configs_for_oauth(mock_oauth_model, global_config_all_json):
GlobalConfigBuilderSchema(global_config_all_json)

mock_oauth_model.assert_called_once_with(
app_name="Splunk_TA_UCCExample",
log_level_field="loglevel",
log_stanza="logging",
name="oauth",
namespace="splunk_ta_uccexample",
)
Loading