Skip to content

Commit 1a2c584

Browse files
committed
DVPL-9735 Move tests to scs
1 parent 7681aa6 commit 1a2c584

26 files changed

Lines changed: 265 additions & 607 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.pyc
22
.venv
3+
*.env
34
.idea
45
.vscode
56
.cache

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ generate:
1616
scripts/copy_generated_files.sh $(TMPDIR)
1717
rm -rf $(TMPDIR)
1818

19+
pep:
20+
tox -e pep8
21+
1922
update_dependencies:
2023
pip3 freeze > requirements.txt
2124

THIRD-PARTY-CREDITS.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The Splunk Developer Cloud SDK for Python contains some libraries that were writ
66
### requests
77
[https://pypi.org/project/requests/](https://pypi.org/project/requests/)
88

9-
requests version 2.22.1
9+
requests version 2.25.1
1010

1111
License: Apache Software License (Apache 2.0)
1212

@@ -17,6 +17,21 @@ urllib3 version 1.25.2
1717

1818
License: MIT License (MIT)
1919

20+
### PyJWT
21+
[https://pypi.org/project/PyJWT/](https://pypi.org/project/PyJWT/)
22+
23+
PyJWT version 2.1.0
24+
25+
License: MIT License (MIT)
26+
27+
### cryptography
28+
[https://pypi.org/project/cryptography/](https://pypi.org/project/cryptography/)
29+
30+
cryptography version 2.6.1
31+
32+
License: Apache Software License (Apache 2.0)
33+
34+
2035
## Test Dependencies
2136

2237

@@ -36,14 +51,6 @@ flake8 version 3.7.7
3651
License: MIT License (MIT)
3752

3853

39-
### cryptography
40-
[https://pypi.org/project/cryptography/](https://pypi.org/project/cryptography/)
41-
42-
cryptography version 2.6.1
43-
44-
License: Apache Software License, BSD License (BSD or Apache License, Version 2.0)
45-
46-
4754
## Build Dependencies
4855

4956
### coverage

example_sdkrc

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
# SPLUNK HOSTS
2-
export SPLUNK_AUTH_HOST=<REDACTED>
1+
# SPLUNK HOSTS, TENANT, REGION
32
export SPLUNK_AUTH_HOST_2=<REDACTED>
4-
export SPLUNK_AUTHN_URL=<REDACTED>
5-
export SPLUNK_HOST=<REDACTED>
6-
export SPLUNK_APP_SERVER=<REDACTED>
73
export SPLUNK_DEBUG=<REDACTED>
84
export SPLUNK_HOST_2=<REDACTED>
9-
10-
# Choose one set of vars for PKCE or CLIENT auth flows
5+
export SPLUNK_REGION=<REDACTED>
6+
export SPLUNK_TENANT_2=<REDACTED>
117

128
# PKCE FLOW
13-
export SPLUNK_APP_CLIENT_ID=<REDACTED>
9+
export SPLUNK_APP_CLIENT_ID_2=<REDACTED>
1410
export TEST_USERNAME=<REDACTED>
1511
export TEST_PASSWORD=<REDACTED>
16-
export SPLUNK_TENANT=<REDACTED>
17-
export SPLUNK_TENANT_ML=<REDACTED>
18-
export SPLUNK_REDIRECT_URL=<REDACTED>
1912

2013
# CLIENT CREDENTIAL FLOW
21-
export SPLUNK_APP_CLIENT_CRED_ID=<REDACTED>
22-
export SPLUNK_APP_CLIENT_CRED_SECRET=<REDACTED>
2314
export SPLUNK_APP_CLIENT_CRED_ID_2=<REDACTED>
2415
export SPLUNK_APP_CLIENT_CRED_SECRET_2=<REDACTED>
25-
export SPLUNK_TENANT_2=<REDACTED>
26-
export SPLUNK_SCOPE=<REDACTED>
16+
17+
# SERVICE PRINCIPAL FLOW
18+
export SPLUNK_APP_PRINCIPAL_NAME_2=<REDACTED>
19+
export SPLUNK_APP_PRINCIPAL_PRIVATE_KEY=<REDACTED>
20+
export SPLUNK_APP_PRINCIPAL_KEY_ID=<REDACTED>
21+
export SPLUNK_APP_PRINCIPAL_KEY_ALG=<REDACTED>

requirements.txt

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
1-
asn1crypto==0.24.0
2-
atomicwrites==1.3.0
3-
attrs==19.1.0
4-
certifi==2019.3.9
5-
cffi==1.12.3
6-
chardet==3.0.4
7-
coverage==4.5.3
8-
cryptography==2.6.1
9-
entrypoints==0.3
10-
flake8==3.7.7
11-
idna==2.8
12-
importlib-metadata==1.6.0
13-
Jinja2==2.11.3
14-
Mako==1.1.3
15-
Markdown==3.3.3
16-
MarkupSafe==1.1.1
17-
mccabe==0.6.1
18-
more-itertools==7.0.0
19-
pluggy==0.11.0
20-
py==1.10.0
21-
pycodestyle==2.5.0
22-
pycparser==2.19
23-
pyflakes==2.1.1
1+
asn1crypto==1.4.0
2+
certifi==2021.5.30
3+
cffi==1.14.6
4+
chardet==4.0.0
5+
idna==2.10
6+
pycparser==2.20
247
PyJWT==1.7.1
25-
pytest==4.5.0
26-
pytest-asyncio==0.10.0
278
requests==2.25.1
28-
six==1.12.0
29-
urllib3==1.26.5
30-
wcwidth==0.1.7
31-
zipp==3.1.0
9+
six==1.16.0
10+
urllib3==1.26.6
11+
cryptography==2.6.1

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
from splunk_sdk import __version__
1515

1616
install_requires = ['requests>=2.25.1,<2.26.1',
17-
'urllib3>=1.26.5,<1.26.7', ]
17+
'urllib3>=1.26.5,<1.26.7',
18+
'pyjwt>=1.7.1,<1.8.0',
19+
'cryptography>=2.6.1,<2.7.0',
20+
]
1821

1922
setup(
2023
name='splunk-cloud-sdk',
@@ -36,7 +39,6 @@
3639
'dev': ['pep8>=1.7.1,<2',
3740
'sphinx >= 2.0.1,<3',
3841
'sphinxcontrib-apidoc>=0.3.0',
39-
'cryptography>=2.6.1,<2.7',
4042
],
4143
'test': ['coverage>=4.5.3,<5',
4244
'flake8>=3.7.7,<4',

splunk_sdk/base_client.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def handle_response(response: Response, klass=None, key=None):
429429
raise Exception("Unexpected http response body: {}".format(data))
430430

431431
else:
432-
raise HTTPError(response.status_code, response.text)
432+
raise HTTPError(response.status_code, response.text, response.headers.get('X-Request-Id', None))
433433

434434

435435
class RetryConfig(object):
@@ -498,9 +498,10 @@ def handle_response(self, client, method, url, retry_count, data=None, json_data
498498
class HTTPError(Exception):
499499
"""The HTTPError class provides an exception wrapper for HTTP error responses."""
500500

501-
def __init__(self, http_status_code: int, details: str):
501+
def __init__(self, http_status_code: int, details: str, request_id: str = None):
502502
self._http_status_code = http_status_code
503503
self._http_details = details
504+
self._request_id = request_id
504505
if details != '':
505506
parsed = json.loads(details)
506507
else:
@@ -532,3 +533,11 @@ def code(self) -> str:
532533
@property
533534
def message(self):
534535
return self._message
536+
537+
@property
538+
def request_id(self) -> str:
539+
return self._request_id
540+
541+
@request_id.setter
542+
def request_id(self, request_id: str):
543+
self._request_id = request_id

test/auth/test_auth_manager.py

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from splunk_sdk.auth import PKCEAuthManager, ClientAuthManager
1111
from test.fixtures import get_auth_manager as pkce_auth_manager # NOQA
1212
from test.fixtures import get_client_auth_manager as client_auth_manager # NOQA
13-
from test.fixtures import get_client_auth_manager_scoped as client_auth_manager_scoped
1413
from test.fixtures import get_service_principal_auth_manager as service_principal_auth_manager # NOQA
1514

1615

@@ -27,10 +26,7 @@ def test_auth_error_properties(pkce_auth_manager):
2726
try:
2827
pkce_auth_manager.authenticate()
2928
except AuthnError as err:
30-
assert (err.status != "")
31-
assert (err.status is not None)
32-
assert (err.server_error_description != "")
33-
assert (err.server_error_description is not None)
29+
assert (err._response is not None)
3430
assert (err.request_id != "")
3531
assert (err.request_id is not None)
3632
finally:
@@ -41,9 +37,12 @@ def test_refresh_token_authenticate(pkce_auth_manager):
4137
auth_context = pkce_auth_manager.authenticate()
4238

4339
# use existing token from auth_context
44-
refresh_token_mgr = RefreshTokenAuthManager(client_id=os.environ.get('SPLUNK_APP_CLIENT_ID'),
40+
refresh_token_mgr = RefreshTokenAuthManager(client_id=os.environ.get('SPLUNK_APP_CLIENT_ID_2'),
4541
refresh_token=auth_context.refresh_token,
46-
host=os.environ.get('SPLUNK_AUTH_HOST'))
42+
tenant_scoped=True,
43+
tenant=os.environ.get('SPLUNK_TENANT_2'),
44+
region=os.environ.get('SPLUNK_REGION'),
45+
host=os.environ.get('SPLUNK_AUTH_HOST_2'))
4746

4847
new_auth_context = refresh_token_mgr.authenticate()
4948

@@ -55,9 +54,12 @@ def test_refresh_token_authenticate(pkce_auth_manager):
5554
assert (new_auth_context.scope is not None)
5655

5756
def test_error_refresh_token_authenticate():
58-
refresh_token_mgr = RefreshTokenAuthManager(client_id=os.environ.get('SPLUNK_APP_CLIENT_ID'),
59-
refresh_token="refresh",
60-
host=os.environ.get('SPLUNK_AUTH_HOST'))
57+
refresh_token_mgr = RefreshTokenAuthManager(client_id=os.environ.get('SPLUNK_APP_CLIENT_ID_2'),
58+
refresh_token='refresh',
59+
tenant_scoped=True,
60+
tenant=os.environ.get('SPLUNK_TENANT_2'),
61+
region=os.environ.get('SPLUNK_REGION'),
62+
host=os.environ.get('SPLUNK_AUTH_HOST_2'))
6163

6264
with pytest.raises(AuthnError):
6365
refresh_token_mgr.authenticate()
@@ -67,11 +69,6 @@ def test_client_credentials_authenticate(client_auth_manager):
6769
auth_context = client_auth_manager.authenticate()
6870
_assert_client_credentials_auth_context(auth_context)
6971

70-
@pytest.mark.usefixtures('client_auth_manager_scoped') # NOQA
71-
def test_client_credentials_authenticate_scoped(client_auth_manager_scoped):
72-
auth_context = client_auth_manager_scoped.authenticate()
73-
_assert_client_credentials_auth_context(auth_context)
74-
7572
@pytest.mark.usefixtures('client_auth_manager') # NOQA
7673
def test_token_authenticate(client_auth_manager):
7774
auth_context = client_auth_manager.authenticate()
@@ -93,10 +90,10 @@ def test_token_authenticate(client_auth_manager):
9390
assert (auth_context.id_token == new_auth_context.id_token)
9491
assert (auth_context.refresh_token == new_auth_context.refresh_token)
9592

96-
context = Context(host=os.environ.get("SPLUNK_HOST"), tenant=os.environ.get("SPLUNK_TENANT"))
93+
context = Context(host=os.environ.get("SPLUNK_HOST_2"), tenant=os.environ.get("SPLUNK_TENANT_2"))
9794
base_client = BaseClient(context=context, auth_manager=token_mgr)
9895
idc = IdentityAndAccessControl(base_client)
99-
assert (idc.validate_token().name.lower() == os.environ.get("SPLUNK_APP_CLIENT_CRED_ID").lower())
96+
assert (idc.validate_token().name.lower() == os.environ.get("SPLUNK_APP_CLIENT_CRED_ID_2").lower())
10097

10198

10299
def test_pkce_requests_hook():
@@ -105,25 +102,34 @@ def test_pkce_requests_hook():
105102
def test_hook(response, **kwargs):
106103
responses.append(response)
107104

108-
PKCEAuthManager(host=os.environ.get('SPLUNK_AUTH_HOST'),
109-
client_id=os.environ.get('SPLUNK_APP_CLIENT_ID'),
105+
PKCEAuthManager(host=os.environ.get('SPLUNK_AUTH_HOST_2'),
106+
client_id=os.environ.get('SPLUNK_APP_CLIENT_ID_2'),
110107
username=os.environ.get('TEST_USERNAME'),
111108
password=os.environ.get('TEST_PASSWORD'),
112-
redirect_uri=os.environ.get('SPLUNK_REDIRECT_URL'),
109+
redirect_uri='https://localhost:8000',
110+
tenant_scoped=True,
111+
tenant=os.environ.get('SPLUNK_TENANT_2'),
112+
region=os.environ.get('SPLUNK_REGION'),
113113
requests_hooks=[test_hook]).authenticate()
114114
assert len(responses) == 4
115-
auth_url = 'https://%s' % os.environ.get('SPLUNK_AUTH_HOST')
115+
auth_url = 'https://region-%s.%s' % (
116+
os.environ.get('SPLUNK_REGION'),
117+
os.environ.get('SPLUNK_AUTH_HOST_2'))
118+
auth_url_scoped = 'https://%s.%s' % (
119+
os.environ.get('SPLUNK_TENANT_2'),
120+
os.environ.get('SPLUNK_AUTH_HOST_2'))
116121
assert responses[0].request.method == 'GET'
117122
assert responses[0].request.url == '%s/csrfToken' % auth_url
118123
assert responses[0].status_code == 200
119124
assert responses[1].request.method == 'POST'
120125
assert responses[1].request.url == '%s/authn' % auth_url
121126
assert responses[1].status_code == 200
122127
assert responses[2].request.method == 'GET'
123-
assert responses[2].request.url.startswith('%s/authorize' % auth_url)
128+
assert responses[2].request.url.startswith('%s/authorize' % auth_url_scoped)
124129
assert responses[2].status_code == 302
125130
assert responses[3].request.method == 'POST'
126-
assert responses[3].request.url == '%s/token' % auth_url
131+
assert responses[3].request.url == '%s/%s/token' % (
132+
auth_url_scoped, os.environ.get('SPLUNK_TENANT_2'))
127133
assert responses[3].status_code == 200
128134

129135

@@ -133,11 +139,14 @@ def test_pkce_no_list_hooks():
133139
def test_hook(*args, **kwargs):
134140
hook_called.append(True)
135141

136-
PKCEAuthManager(host=os.environ.get('SPLUNK_AUTH_HOST'),
137-
client_id=os.environ.get('SPLUNK_APP_CLIENT_ID'),
142+
PKCEAuthManager(host=os.environ.get('SPLUNK_AUTH_HOST_2'),
143+
client_id=os.environ.get('SPLUNK_APP_CLIENT_ID_2'),
138144
username=os.environ.get('TEST_USERNAME'),
139145
password=os.environ.get('TEST_PASSWORD'),
140-
redirect_uri=os.environ.get('SPLUNK_REDIRECT_URL'),
146+
redirect_uri='https://localhost:8000',
147+
tenant_scoped=True,
148+
tenant=os.environ.get('SPLUNK_TENANT_2'),
149+
region=os.environ.get('SPLUNK_REGION'),
141150
requests_hooks=test_hook).authenticate()
142151
assert hook_called
143152

@@ -148,10 +157,13 @@ def test_client_manager_requests_hook():
148157
def test_hook(*args, **kwargs):
149158
hook_called.append(True)
150159

151-
ClientAuthManager(host=os.environ.get('SPLUNK_AUTH_HOST'),
152-
client_id=os.environ.get('SPLUNK_APP_CLIENT_CRED_ID'),
153-
client_secret=os.environ.get('SPLUNK_APP_CLIENT_CRED_SECRET'),
154-
scope=os.environ.get('SPLUNK_SCOPE'),
160+
ClientAuthManager(host=os.environ.get('SPLUNK_AUTH_HOST_2'),
161+
client_id=os.environ.get('SPLUNK_APP_CLIENT_CRED_ID_2'),
162+
client_secret=os.environ.get('SPLUNK_APP_CLIENT_CRED_SECRET_2'),
163+
scope='',
164+
tenant_scoped=True,
165+
tenant=os.environ.get('SPLUNK_TENANT_2'),
166+
region=os.environ.get('SPLUNK_REGION'),
155167
requests_hooks=[test_hook]).authenticate()
156168
assert hook_called
157169

@@ -162,11 +174,14 @@ def test_pkce_manager_no_list_hooks():
162174
def test_hook(*args, **kwargs):
163175
hook_called.append(True)
164176

165-
PKCEAuthManager(host=os.environ.get('SPLUNK_AUTH_HOST'),
166-
client_id=os.environ.get('SPLUNK_APP_CLIENT_ID'),
177+
PKCEAuthManager(host=os.environ.get('SPLUNK_AUTH_HOST_2'),
178+
client_id=os.environ.get('SPLUNK_APP_CLIENT_ID_2'),
167179
username=os.environ.get('TEST_USERNAME'),
168180
password=os.environ.get('TEST_PASSWORD'),
169-
redirect_uri=os.environ.get('SPLUNK_REDIRECT_URL'),
181+
redirect_uri='https://localhost:8000',
182+
tenant_scoped=True,
183+
tenant=os.environ.get('SPLUNK_TENANT_2'),
184+
region=os.environ.get('SPLUNK_REGION'),
170185
requests_hooks=test_hook).authenticate()
171186
assert hook_called
172187

@@ -186,11 +201,11 @@ def _assert_pkce_auth_context(auth_context):
186201
assert ('email' in auth_context.scope)
187202

188203

189-
def _assert_client_credentials_auth_context(auth_context, expires_in=43200):
204+
def _assert_client_credentials_auth_context(auth_context, expires_in=None):
190205
assert (auth_context is not None)
191206
assert (auth_context.access_token is not None)
192-
# assert(auth_context.id_token is None)
193-
assert (auth_context.expires_in == expires_in)
207+
if expires_in is not None:
208+
assert (auth_context.expires_in == expires_in)
194209
assert (auth_context.token_type == 'Bearer')
195210
assert (auth_context.refresh_token is None)
196211
assert (auth_context.scope == 'client_credentials')
@@ -227,7 +242,7 @@ def test_sp_token_authenticate(service_principal_auth_manager):
227242
assert (auth_context.id_token == new_auth_context.id_token)
228243
assert (auth_context.refresh_token == new_auth_context.refresh_token)
229244

230-
context = Context(host=os.environ.get("SPLUNK_HOST"), tenant=os.environ.get("SPLUNK_TENANT"))
245+
context = Context(host=os.environ.get("SPLUNK_HOST_2"), tenant=os.environ.get("SPLUNK_TENANT_2"))
231246
base_client = BaseClient(context=context, auth_manager=token_mgr)
232247
idc = IdentityAndAccessControl(base_client)
233-
assert (idc.validate_token().name.lower() == os.environ.get("SPLUNK_APP_PRINCIPAL_NAME").lower())
248+
assert (idc.validate_token().name.lower() == os.environ.get("SPLUNK_APP_PRINCIPAL_NAME_2").lower())

0 commit comments

Comments
 (0)