Skip to content

Commit

Permalink
fix(oauth): set loglevel in oauth rh template to use log level set in…
Browse files Browse the repository at this point in the history
… add-on (#1227)

**Issue number:** ADDON-58434

## Summary

### Changes

The OAuth RH template is updated. It now fetches the configured log
level in the add-on and writes logs as per the level set.

### User experience

Using this feature, developers wouldn't have to update their OAuth RH
file in their source code to get the log level set by the user in the
add-on.
## Checklist

If your change doesn't seem to apply, please leave them unchecked.

* [x] I have performed a self-review of this change
* [x] Changes have been tested
* [ ] Changes are documented
* [x] PR title follows [conventional commit
semantics](https://www.conventionalcommits.org/en/v1.0.0/)
  • Loading branch information
hetangmodi-crest authored Jun 10, 2024
1 parent 02e1ab4 commit 3b7b9e1
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 3 deletions.
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",
)

0 comments on commit 3b7b9e1

Please sign in to comment.