Skip to content

Commit

Permalink
rest-publish: Cleanup internal output handling
Browse files Browse the repository at this point in the history
- Stop using 'print()' in rest-publish. Whoops.
- Cleanup namespace arguments with better defaults. Remove incorrect reference
  to '$SPLUNK_APP', which never was implemented and doesn't follow a pattern
  used anywhere in ksconf.
  • Loading branch information
lowell80 committed Oct 12, 2023
1 parent f63963a commit 32d0162
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
6 changes: 5 additions & 1 deletion docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ Renames:
Ksconf v0.13.3 (DRAFT)
~~~~~~~~~~~~~~~~~~~~~~~~~~~

* Changes for :ref:`ksconf_cmd_rest-publish`:
* Changes for :ref:`ksconf_cmd_rest-publish`:

* Implement ``--insecure`` parameter.
Previously, SSL/TLS validation was not enforced as expected.
* Fix bug with ``acl`` endpoint URL.
* Fix issue where sharing namespace settings could be ignored.
More work may be needed. (Unit testing desperately needed.)
* Fix some incorrect/misleading CLI argument help.
* Internal improvements for long-term maintenance.


Ksconf v0.13.2 (2023-10-10)
Expand Down
9 changes: 5 additions & 4 deletions ksconf/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,13 +533,14 @@ def add_splunkd_access_args(parser: ArgumentParser) -> ArgumentParser:


def add_splunkd_namespace(parser: ArgumentParser) -> ArgumentParser:
parser.add_argument("--app", default="$SPLUNK_APP",
help="Set the namespace (app name) for the endpoint")
parser.add_argument("--app", default="search",
help="Set the namespace (app name) for the endpoint. "
"Default %(default)s")
parser.add_argument("--owner", default="nobody",
help="Set the user who owns the content. "
"The default of 'nobody' works well for app-level sharing.")
parser.add_argument("--sharing", default="global", choices=["user", "app", "global"],
help="Set the sharing mode.")
parser.add_argument("--sharing", default="app", choices=["user", "app", "global"],
help="Set the sharing mode. Defaults to 'app'.")
return parser


Expand Down
17 changes: 9 additions & 8 deletions ksconf/commands/restpublish.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def handle_conf_file(self, args: Namespace, conf_proxy: ConfFileProxy):
stanza_data = conf[stanza_name]

if not stanza_data:
print(f"Skipping empty stanza [{stanza_name}]")
sys.stderr.write(f"Skipping empty stanza [{stanza_name}]\n")
continue

if stanza_name is GLOBAL_STANZA or stanza_name == "":
Expand All @@ -180,7 +180,7 @@ def handle_conf_file(self, args: Namespace, conf_proxy: ConfFileProxy):
else:
action, info = self.publish_conf(stanza_name, stanza_data, config_file)

print(f"{stanza_name:50} {action:8} (delta size: { len(info.get('delta', [])) })")
self.stdout.write(f"{stanza_name:50} {action:8} (delta size: { len(info.get('delta', [])) })\n")

update_time = info.get("updated", 0)
# headers = (conf_proxy.name, f"{args.url}/{config_file.path}")
Expand All @@ -190,7 +190,7 @@ def handle_conf_file(self, args: Namespace, conf_proxy: ConfFileProxy):
show_diff(self.stdout, info["delta"], headers=(conf_proxy.name, rest_header))

if "meta" in info:
print(info["meta"])
self.stdout.write(f'{info["meta"]}\n')

if "acl_delta" in info:
show_diff(self.stdout, info["acl_delta"])
Expand All @@ -200,15 +200,16 @@ def publish_conf(self,
stanza_data: ConfType,
config_file):
if self.meta:
namespace = self._service.namespace
metadata = self.meta.get(config_file.name, stanza_name)
owner = metadata.get("owner", None)
app = config_file.service.namespace.app
owner = metadata.get("owner", namespace.owner)
app = namespace.app
if metadata.get("export", None) == "system":
sharing = "global"
else:
# Could still be "user" technically; but it seems unlikely that '--meta' would be given
# in that case. Still, there's possible room for improvement.
sharing = "app"
sharing = namespace.sharing
else:
metadata = {}
owner = None
Expand Down Expand Up @@ -328,7 +329,7 @@ def publish_conf(self,
res["meta_response"] = response
except Exception:
# Don't die on exceptions for ACLs... print the error and move on (too many things to go wrong here)
print(f"Failed hitting: {resource} ARGS={final_meta}")
sys.stderr.write(f"Failed hitting: {resource} ARGS={final_meta}\n")
import traceback
traceback.print_exc()
# XXX: Do better
Expand Down Expand Up @@ -364,7 +365,7 @@ def run(self, args: Namespace):
if args.meta:
self.meta = MetaData()
for meta_file in args.meta:
print(f"Loading metadata from {meta_file}")
self.stdout.write(f"Loading metadata from {meta_file}\n")
self.meta.feed_file(meta_file)

self.connect_splunkd(args)
Expand Down

0 comments on commit 32d0162

Please sign in to comment.