Skip to content

Commit 74779f3

Browse files
authored
Merge pull request #104 from victor-torres/shub-jobauth
fallback to SHUB_JOBAUTH environment variable if SH_APIKEY is not set
2 parents 00044f2 + 11e891c commit 74779f3

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

scrapinghub/client/utils.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import logging
66
import binascii
7+
import warnings
78
from codecs import decode
89

910
import six
@@ -101,10 +102,17 @@ def parse_auth(auth):
101102
"""
102103
if auth is None:
103104
apikey = os.environ.get('SH_APIKEY')
104-
if apikey is None:
105-
raise RuntimeError("No API key provided and SH_APIKEY "
106-
"environment variable not set")
107-
return (apikey, '')
105+
if apikey:
106+
return (apikey, '')
107+
108+
jobauth = os.environ.get('SHUB_JOBAUTH')
109+
if jobauth:
110+
warnings.warn("You are using the SHUB_JOBAUTH environment "
111+
"variable which may not work for some API endpoints")
112+
return _search_for_jwt_credentials(jobauth)
113+
114+
raise RuntimeError("No API key provided and neither SH_APIKEY "
115+
"nor SHUB_JOBAUTH environment variables is set")
108116

109117
if isinstance(auth, tuple):
110118
all_strings = all(isinstance(k, six.string_types) for k in auth)

tests/client/test_utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ def test_parse_auth_none_with_env():
1717
assert parse_auth(None) == ('testkey', '')
1818

1919

20+
@mock.patch.dict(os.environ, {'SH_APIKEY': 'testkey', 'SHUB_JOBAUTH': 'jwt'})
21+
def test_parse_auth_none_with_multiple_env():
22+
assert parse_auth(None) == ('testkey', '')
23+
24+
2025
def test_parse_auth_tuple():
2126
assert parse_auth(('test', 'test')) == ('test', 'test')
2227
assert parse_auth(('apikey', '')) == ('apikey', '')
@@ -52,6 +57,28 @@ def test_parse_auth_jwt_token():
5257
assert parse_auth(encoded_token) == (test_job, test_token)
5358

5459

60+
def test_parse_auth_jwt_token_with_jwt_token_env():
61+
dummy_test_job, dummy_test_token = '1/2/3', 'some.dummy.jwt.token'
62+
raw_token = (dummy_test_job + ':' + dummy_test_token).encode('utf8')
63+
dummy_encoded_token = encode(raw_token, 'hex_codec').decode('ascii')
64+
65+
test_job, test_token = '1/2/3', 'some.jwt.token'
66+
raw_token = (test_job + ':' + test_token).encode('utf8')
67+
encoded_token = encode(raw_token, 'hex_codec').decode('ascii')
68+
69+
with mock.patch.dict(os.environ, {'SHUB_JOBAUTH': dummy_encoded_token}):
70+
assert parse_auth(encoded_token) == (test_job, test_token)
71+
72+
73+
def test_parse_auth_none_with_jwt_token_env():
74+
test_job, test_token = '1/2/3', 'some.jwt.token'
75+
raw_token = (test_job + ':' + test_token).encode('utf8')
76+
encoded_token = encode(raw_token, 'hex_codec').decode('ascii')
77+
78+
with mock.patch.dict(os.environ, {'SHUB_JOBAUTH': encoded_token}):
79+
assert parse_auth(None) == (test_job, test_token)
80+
81+
5582
def test_parse_job_key():
5683
job_key = parse_job_key('123/10/11')
5784
assert job_key.project_id == '123'

0 commit comments

Comments
 (0)