Skip to content

Commit

Permalink
Merge pull request #67 from QualiSystems/wip/adam.s/add_gitVendor_token
Browse files Browse the repository at this point in the history
add auth with private-token patterna and test
  • Loading branch information
nahumtimerman committed Apr 29, 2021
2 parents 4d95905 + 055fa1f commit e74a747
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions package/cloudshell/cm/customscript/domain/script_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ def download(self, url, auth, verify_certificate):

if response_valid:
file_name = self._get_filename(response)

# try again with authorization {"Private-Token": "%s" % token}, since gitlab uses that pattern
if not response_valid and auth.token is not None:
self.logger.info("Token provided. Starting download script with Token (private-token pattern)...")
headers = {"Private-Token": "Bearer %s" % auth.token }
response = requests.get(url, stream=True, headers=headers, verify=verify_certificate)

response_valid = self._is_response_valid(response, "Token")

if response_valid:
file_name = self._get_filename(response)

# repo is private and credentials provided, and Token did not provided or did not work. this will NOT work for github. github require Token
if not response_valid and (auth.username is not None and auth.password is not None):
Expand Down
10 changes: 10 additions & 0 deletions package/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def mocked_requests_get(*args, **kwargs):
repo_dict = {
"public": 'https://raw.repocontentservice.com/SomeUser/SomePublicRepo/master/bashScript.sh',
"private_token": 'https://raw.repocontentservice.com/SomeUser/SomePrivateTokenRepo/master/bashScript.sh',
"private_token_auth_pattern": 'https://gitlab.mock.com/api/v4/SomeUser/SomePrivateTokenRepo/master/bashScript.sh',
"private_cred": 'https://raw.repocontentservice.com/SomeUser/SomePrivateCredRepo/master/bashScript.sh',
"content": 'SomeBashScriptContent'
}
Expand Down Expand Up @@ -38,6 +39,15 @@ def iter_content(self, chunk):
if kwargs["headers"]["Authorization"] == 'Bearer 551e48b030e1a9f334a330121863e48e43f58c55':
response = MockResponse(repo_dict['content'], 200, {"Content-Type": "text/plain"}, repo_dict['private_token'])
return response

if args[0] == repo_dict['private_token_auth_pattern']:
if 'headers' in kwargs:
if 'Authorization' in kwargs["headers"]:
response = MockResponse("error", 404, {"Content-Type": "text/plain"}, "error content")
return response
elif kwargs["headers"]["Private-Token"] == 'Bearer 551e48b030e1a9f334a330121863e48e43f58c55':
response = MockResponse(repo_dict['content'], 200, {"Content-Type": "text/plain"}, repo_dict['private_token_auth_pattern'])
return response

if args[0] == repo_dict['private_cred']:
if 'auth' in kwargs and kwargs["auth"] is not None:
Expand Down
15 changes: 15 additions & 0 deletions package/tests/test_script_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ def test_download_as_private_with_token(self, mocked_requests_get):
self.assertEqual(script_file.name, "bashScript.sh")
self.assertEqual(script_file.text, "SomeBashScriptContent")

@mock.patch('cloudshell.cm.customscript.domain.script_downloader.requests.get', side_effect=mocked_requests_get)
def test_download_as_private_with_token_with_private_token_pattern(self, mocked_requests_get):
# private - url, with token
private_repo_url = 'https://gitlab.mock.com/api/v4/SomeUser/SomePrivateTokenRepo/master/bashScript.sh'
self.auth = HttpAuth('','','551e48b030e1a9f334a330121863e48e43f58c55')

# set downloaded and downaload
self.logger.info = print_logs
script_downloader = ScriptDownloader(self.logger, self.cancel_sampler)
script_file = script_downloader.download(private_repo_url, self.auth, True)

# assert name and content
self.assertEqual(script_file.name, "bashScript.sh")
self.assertEqual(script_file.text, "SomeBashScriptContent")

@mock.patch('cloudshell.cm.customscript.domain.script_downloader.requests.get', side_effect=mocked_requests_get)
def test_download_as_private_with_credentials_and_failed_token(self, mocked_requests_get):
# private - url, with token that fails and user\password. note - this is will not work on GitHub repo, they require token
Expand Down

0 comments on commit e74a747

Please sign in to comment.