Skip to content

Commit 62cec69

Browse files
committed
Add region scoping and system tenant checks
1 parent 70d0cb8 commit 62cec69

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

splunk_sdk/auth/auth_manager.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,28 @@ def _post(self, url, auth=None, headers=None, data=None, cookies=None):
193193
return response
194194

195195
def _url(self, path):
196-
if self._tenant_scoped is True and self._tenant is not None and self._tenant != "system":
197-
self._host = self._tenant + "." + self._host
198-
# generate tenant scoped token
199-
os.path.join("/", self._tenant, path)
200-
# ToDo:Region+system scoped tokens will be added once implementation details are finalized in multi-cell
201-
# else:
196+
if self._tenant_scoped is True:
197+
if self._tenant is None:
198+
raise ValueError("Tenant is empty.")
199+
elif self._region is None:
200+
raise ValueError("Region is empty.")
201+
elif "token" in path and self._tenant != "system":
202+
self._host = self._tenant + "." + self._host
203+
# generate tenant scoped token
204+
path = "/" + self._tenant + path
205+
elif "token" in path and self._tenant == "system":
206+
self._host = "region-" + self._region + "." + self._host
207+
# generate system scoped token
208+
path = "/system" + path
209+
elif "authorize" in path and self._tenant != "system":
210+
self._host = self._tenant + "." + self._host
211+
elif "authorize" in path and self._tenant == "system":
212+
self._host = "region-" + self._region + "." + self._host
213+
elif "csrfToken" in path:
214+
self._host = "region-" + self._region + "." + self._host
215+
elif "authn" in path:
216+
self._host = "region-" + self._region + "." + self._host
217+
202218
print("https://%s%s" % (self._host, path))
203219
return "https://%s%s" % (self._host, path)
204220

splunk_sdk/base_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ def build_url(self, route: str, **kwargs) -> str:
240240
"""
241241
if self.context.tenant_scoped is True and self.context.tenant != "system" and route.startswith("/system") is False:
242242
url = self.context.scheme + "://" + self.context.tenant + "." + self.context.host
243+
elif self.context.tenant_scoped is True and route.startswith("/system/") and self.context.region is None:
244+
raise ValueError("Invalid value for `region`, must not be `None`")
243245
elif self.context.tenant_scoped is True and route.startswith("/system/") and self.context.region is not None:
244246
url = self.context.scheme + "://region-" + self.context.region + "." + self.context.host
245247
else:

test/auth/test_auth_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_client_credentials_authenticate(client_auth_manager):
6767
auth_context = client_auth_manager.authenticate()
6868
_assert_client_credentials_auth_context(auth_context)
6969

70-
@pytest.mark.usefixtures('client_auth_manager') # NOQA
70+
@pytest.mark.usefixtures('client_auth_manager_scoped') # NOQA
7171
def test_client_credentials_authenticate_scoped(client_auth_manager_scoped):
7272
auth_context = client_auth_manager_scoped.authenticate()
7373
_assert_client_credentials_auth_context(auth_context)

0 commit comments

Comments
 (0)