Skip to content

Commit 5ecd916

Browse files
committed
Change macros endpoint from configs/conf-macros to data/macros
1 parent f83af55 commit 5ecd916

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

splunklib/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
PATH_JOBS = "search/jobs/"
109109
PATH_JOBS_V2 = "search/v2/jobs/"
110110
PATH_LOGGER = "/services/server/logger/"
111-
PATH_MACROS = "configs/conf-macros/"
111+
PATH_MACROS = "data/macros/"
112112
PATH_MESSAGES = "messages/"
113113
PATH_MODULAR_INPUTS = "data/modular-inputs"
114114
PATH_ROLES = "authorization/roles/"

tests/integration/test_macro.py

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# under the License.
1616

1717
from __future__ import absolute_import
18-
from splunklib.binding import HTTPError
18+
from splunklib.binding import HTTPError, namespace
1919
from tests import testlib
2020
import logging
2121

@@ -264,7 +264,7 @@ def query():
264264
self.assertRaisesRegex(HTTPError, "value must be smaller that value2", query)
265265

266266

267-
# This test makes sure that the endpoint we use for macros (configs/conf-macros)
267+
# This test makes sure that the endpoint we use for macros (data/macros/)
268268
# does not require admin privileges and can be used by normal users.
269269
class TestPrivileges(testlib.SDKTestCase):
270270
macro_name = "SDKTestMacro"
@@ -318,6 +318,70 @@ def test_create_macro_no_admin(self):
318318
self.assertEqual(out[0]["test"], "123")
319319

320320

321+
# This test makes sure that the endpoint we use for macros (data/macros/)
322+
# does not require admin privileges and can be used by normal users.
323+
class TestPrivilegesWithNamespace(testlib.SDKTestCase):
324+
macro_name = "SDKTestMacro"
325+
username = "SDKTestMacroUser".lower()
326+
password = "SDKTestMacroUserPassword!"
327+
328+
# TODO: Would be nice to create app on-demand here and control the
329+
# permissions, so that we are not dependent on default settings.
330+
# namespace = namespace(owner="nobody", app="launcher")
331+
namespace = namespace(owner="nobody", app="search")
332+
333+
def setUp(self):
334+
testlib.SDKTestCase.setUp(self)
335+
self.cleanUsers()
336+
337+
self.service.users.create(
338+
username=self.username, password=self.password, roles=["power"]
339+
)
340+
341+
self.service.logout()
342+
kwargs = self.opts.kwargs.copy()
343+
kwargs["username"] = self.username
344+
kwargs["password"] = self.password
345+
self.service = client.connect(**kwargs)
346+
347+
self.cleanMacros()
348+
349+
def tearDown(self):
350+
testlib.SDKTestCase.tearDown(self)
351+
self.cleanMacros()
352+
self.service = client.connect(**self.opts.kwargs)
353+
self.cleanUsers()
354+
355+
def cleanUsers(self):
356+
for user in self.service.users:
357+
if user.name == self.username:
358+
self.service.users.delete(self.username)
359+
360+
def cleanMacros(self):
361+
try:
362+
m = self.service.macros[self.macro_name, self.namespace]
363+
except KeyError:
364+
return # macro does not exist
365+
m.delete()
366+
367+
def test_create_macro_no_admin(self):
368+
self.service.macros.create(
369+
self.macro_name, 'eval test="123"', namespace=self.namespace
370+
)
371+
372+
stream = self.service.jobs.oneshot(
373+
f"| makeresults count=1 | `{self.macro_name}`",
374+
output_mode="json",
375+
namespace=self.namespace,
376+
)
377+
378+
result = results.JSONResultsReader(stream)
379+
out = list(result)
380+
381+
self.assertTrue(len(out) == 1)
382+
self.assertEqual(out[0]["test"], "123")
383+
384+
321385
if __name__ == "__main__":
322386
try:
323387
import unittest2 as unittest

0 commit comments

Comments
 (0)