Skip to content

Commit

Permalink
Merge 96db16d into be50cb8
Browse files Browse the repository at this point in the history
  • Loading branch information
nahumtimerman committed Apr 21, 2021
2 parents be50cb8 + 96db16d commit 235557b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 20 deletions.
5 changes: 5 additions & 0 deletions drivers/ansible_shellPackage/DataModel/datamodel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
</AttributeInfo>
<AttributeInfo Name="Timeout Minutes" Type="Numeric" DefaultValue="0" Description="Maximum number of minutes to connect to the target machine." IsReadOnly="false">
</AttributeInfo>
<AttributeInfo Name="Verify Certificate" Type="String" DefaultValue="True"
Description="Verify server certificate when getting script if True, otherwise ignore." IsReadOnly="false">
</AttributeInfo>
</Attributes>
<ResourceFamilies>
<ResourceFamily Name="Configuration Services" Description="" IsAdminOnly="true" IsService="true" ServiceType="Regular">
Expand All @@ -34,12 +37,14 @@
<AllowedValues />
</AttachedAttribute>
<AttachedAttribute Name="Timeout Minutes" IsLocal="true" IsOverridable="true"/>
<AttachedAttribute Name="Verify Certificate" IsLocal="true" IsOverridable="true" />
</AttachedAttributes>
<AttributeValues>
<AttributeValue Name="Ansible Additional Arguments" Value="" />
<AttributeValue Name="Supports Ansible" Value="True" />
<AttributeValue Name="Execution Server Selector" Value="" />
<AttributeValue Name="Timeout Minutes" Value="20" />
<AttributeValue Name="Verify Certificate" Value="True" />
</AttributeValues>
<ParentModels />
<Drivers>
Expand Down
6 changes: 5 additions & 1 deletion package/cloudshell/cm/ansible/ansible_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ def _download_playbook(self, ansi_conf, cancellation_sampler, logger):
auth = None
if ansi_conf.playbook_repo.username or ansi_conf.playbook_repo.token:
auth = HttpAuth(repo.username, repo.password, repo.token)
playbook_name = self.downloader.get(ansi_conf.playbook_repo.url, auth, logger, cancellation_sampler)

logger.info('Verify certificate: ' + str(ansi_conf.verify_certificate))
playbook_name = self.downloader.get(ansi_conf.playbook_repo.url,
auth, logger, cancellation_sampler,
ansi_conf.verify_certificate)
logger.info('download playbook file' + str(playbook_name))
return playbook_name

Expand Down
2 changes: 2 additions & 0 deletions package/cloudshell/cm/ansible/domain/ansible_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(self, playbook_repo=None, hosts_conf=None, additional_cmd_args=None
self.playbook_repo = playbook_repo or PlaybookRepository()
self.hosts_conf = hosts_conf or []
self.additional_cmd_args = additional_cmd_args
self.verify_certificate = True


class PlaybookRepository(object):
Expand Down Expand Up @@ -57,6 +58,7 @@ def json_to_object(self, json_str):
ansi_conf = AnsibleConfiguration()
ansi_conf.additional_cmd_args = json_obj.get('additionalArgs')
ansi_conf.timeout_minutes = json_obj.get('timeoutMinutes', 0.0)
ansi_conf.verify_certificate = json_obj.get('verifyCertificate', 'true').lower()=='true'

if json_obj.get('repositoryDetails'):
ansi_conf.playbook_repo.url = json_obj['repositoryDetails'].get('url')
Expand Down
8 changes: 4 additions & 4 deletions package/cloudshell/cm/ansible/domain/http_request_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


class HttpRequestService(object):
def get_response(self, url, auth):
return requests.get(url, auth=(auth.username, auth.password) if auth else None, stream=True)
def get_response(self, url, auth, verify_certificate):
return requests.get(url, auth=(auth.username, auth.password) if auth else None, stream=True, verify=verify_certificate)

def get_response_with_headers(self, url, headers):
return requests.get(url, headers=headers, stream=True)
def get_response_with_headers(self, url, headers, verify_certificate):
return requests.get(url, headers=headers, stream=True, verify=verify_certificate)
14 changes: 8 additions & 6 deletions package/cloudshell/cm/ansible/domain/playbook_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,32 @@ def __init__(self, file_system, zip_service, http_request_service, filename_extr
self.http_request_service = http_request_service
self.filename_extractor = filename_extractor

def get(self, url, auth, logger, cancel_sampler):
def get(self, url, auth, logger, cancel_sampler, verify_certificate):
"""
Download the file from the url (unzip if needed).
:param str url: Http url of the file.
:param HttpAuth auth: Authentication to the http server (optional).
:param Logger logger:
:param CancellationSampler cancel_sampler:
:param boolean verify_certificate:
:rtype [str,int]
:return The downloaded playbook file name
"""
file_name, file_size = self._download(url, auth, logger, cancel_sampler)
file_name, file_size = self._download(url, auth, logger, cancel_sampler, verify_certificate)

if file_name.endswith(".zip"):
file_name = self._unzip(file_name, logger)

return file_name

def _download(self, url, auth, logger, cancel_sampler):
def _download(self, url, auth, logger, cancel_sampler, verify_certificate):
"""
Download the file from the url.
:param str url: Http url of the file.
:param HttpAuth auth: Authentication to the http server (optional).
:param Logger logger:
:param CancellationSampler cancel_sampler:
:param boolean verify_certificate:
:rtype [str,int]
:return The downloaded file name
"""
Expand All @@ -55,7 +57,7 @@ def _download(self, url, auth, logger, cancel_sampler):

# assume repo is public, try to download without credentials
logger.info('Starting download script as public... from \'%s\' ...'%url)
response = self.http_request_service.get_response(url, auth)
response = self.http_request_service.get_response(url, auth, verify_certificate)
response_valid = self._is_response_valid(logger ,response, "public")

if response_valid:
Expand All @@ -65,7 +67,7 @@ def _download(self, url, auth, logger, cancel_sampler):
if not response_valid and auth.token is not None:
logger.info("Token provided. Starting download script with Token...")
headers = {"Authorization": "Bearer %s" % auth.token }
response = self.http_request_service.get_response_with_headers(url, headers)
response = self.http_request_service.get_response_with_headers(url, headers, verify_certificate)

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

Expand All @@ -75,7 +77,7 @@ def _download(self, url, auth, logger, cancel_sampler):
# 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):
logger.info("username\password provided, Starting download script with username\password...")
response = self.http_request_service.get_response(url, auth)
response = self.http_request_service.get_response(url, auth, verify_certificate)

response_valid = self._is_response_valid(logger, response, "username\password")

Expand Down
4 changes: 2 additions & 2 deletions package/tests/test_ansible_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def test_download_playbook_without_auth(self):

self._execute_playbook()

self.downloader.get.assert_called_once_with('someurl', Any(), Any(), Any())
self.downloader.get.assert_called_once_with('someurl', Any(), Any(), Any(), True)

def test_download_playbook_with_auth(self):
self.conf.playbook_repo.url = 'someurl'
Expand All @@ -209,7 +209,7 @@ def test_download_playbook_with_auth(self):

self.downloader.get.assert_called_once_with('someurl',
Any(lambda x: x.username == 'user' and x.password == 'pass'), Any(),
Any())
Any(), True)

# Playbook Executor

Expand Down
14 changes: 7 additions & 7 deletions package/tests/test_playbook_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_playbook_downloader_zip_file_one_yaml(self):
self.http_request_serivce.get_response_with_headers = Mock(return_value=self.reqeust)
self.playbook_downloader._is_response_valid = Mock(return_value=True)

file_name = self.playbook_downloader.get("", auth, self.logger, Mock())
file_name = self.playbook_downloader.get("", auth, self.logger, Mock(), True)
self.assertEqual(file_name, "lie.yaml")


Expand All @@ -50,7 +50,7 @@ def test_playbook_downloader_zip_file_two_yaml_correct(self):
self.http_request_serivce.get_response_with_headers = Mock(return_value=self.reqeust)
self.playbook_downloader._is_response_valid = Mock(return_value=True)

file_name = self.playbook_downloader.get("", auth, self.logger, Mock())
file_name = self.playbook_downloader.get("", auth, self.logger, Mock(), True)

self.assertEqual(file_name, "site.yaml")

Expand All @@ -65,7 +65,7 @@ def test_playbook_downloader_zip_file_two_yaml_incorrect(self):
self.http_request_serivce.get_response_with_headers = Mock(return_value=self.reqeust)
self.playbook_downloader._is_response_valid = Mock(return_value=True)
with self.assertRaises(Exception) as e:
self.playbook_downloader.get("", auth, self.logger, Mock())
self.playbook_downloader.get("", auth, self.logger, Mock(), True)
self.assertEqual(str(e.exception),"Playbook file name was not found in zip file")

def test_playbook_downloader_with_one_yaml(self):
Expand All @@ -78,7 +78,7 @@ def test_playbook_downloader_with_one_yaml(self):
self.http_request_serivce.get_response_with_headers = Mock(return_value=self.reqeust)
self.playbook_downloader._is_response_valid = Mock(return_value=True)

file_name = self.playbook_downloader.get("", auth, self.logger, Mock())
file_name = self.playbook_downloader.get("", auth, self.logger, Mock(), True)

self.assertEqual(file_name, "lie.yaml")

Expand All @@ -92,7 +92,7 @@ def test_playbook_downloader_no_parsing_from_rfc(self):
self.http_request_serivce.get_response_with_headers = Mock(return_value=self.reqeust)
self.playbook_downloader._is_response_valid = Mock(return_value=True)

file_name = self.playbook_downloader.get("", auth, self.logger, Mock())
file_name = self.playbook_downloader.get("", auth, self.logger, Mock(), True)

self.assertEqual(file_name, "lie.yaml")

Expand All @@ -106,7 +106,7 @@ def test_playbook_downloader_with_one_yaml_only_credentials(self):
self.http_request_serivce.get_response_with_headers = Mock(return_value=self.reqeust)
self.playbook_downloader._is_response_valid = Mock(side_effect=self.mock_response_valid_for_credentials)

file_name = self.playbook_downloader.get("", auth, self.logger, Mock())
file_name = self.playbook_downloader.get("", auth, self.logger, Mock(), True)

self.assertEqual(file_name, "lie.yaml")

Expand All @@ -120,7 +120,7 @@ def test_playbook_downloader_with_one_yaml_only_token(self):
self.http_request_serivce.get_response_with_headers = Mock(return_value=self.reqeust)
self.playbook_downloader._is_response_valid = Mock(side_effect=self.mock_response_valid_for_not_public)

file_name = self.playbook_downloader.get("", auth, self.logger, Mock())
file_name = self.playbook_downloader.get("", auth, self.logger, Mock(), True)

self.assertEqual(file_name, "lie.yaml")

Expand Down

0 comments on commit 235557b

Please sign in to comment.