def execute_login(self): """ verify user's identify via Github and retrieve an auth token from Ansible Galaxy. """ # Authenticate with github and retrieve a token if self.options.token is None: if C.GALAXY_TOKEN: github_token = C.GALAXY_TOKEN else: login = GalaxyLogin(self.galaxy) github_token = login.create_github_token() else: github_token = self.options.token galaxy_response = self.api.authenticate(github_token) if self.options.token is None and C.GALAXY_TOKEN is None: # Remove the token we created login.remove_github_token() # Store the Galaxy token token = GalaxyToken() token.set(galaxy_response['token']) display.display("Successfully logged into Galaxy as %s" % galaxy_response['username']) return 0
def __init__(self, galaxy): self.galaxy = galaxy self.token = GalaxyToken() self._api_server = C.GALAXY_SERVER self._validate_certs = C.GALAXY_IGNORE_CERTS # set validate_certs if galaxy.options.validate_certs == False: self._validate_certs = False display.vvv('Check for valid certs: %s' % self._validate_certs) # set the API server if galaxy.options.api_server != C.GALAXY_SERVER: self._api_server = galaxy.options.api_server display.vvv("Connecting to galaxy_server: %s" % self._api_server) server_version = self.get_server_api_version() if server_version in self.SUPPORTED_VERSIONS: self.baseurl = '%s/api/%s' % (self._api_server, server_version) self.version = server_version # for future use display.vvv("Base API: %s" % self.baseurl) else: raise AnsibleError("Unsupported Galaxy server API version: %s" % server_version)
def __init__(self, galaxy): self.galaxy = galaxy self.token = GalaxyToken() self._api_server = C.GALAXY_SERVER self._validate_certs = not galaxy.options.ignore_certs self.baseurl = None self.version = None self.initialized = False display.debug('Validate TLS certificates: %s' % self._validate_certs) # set the API server if galaxy.options.api_server != C.GALAXY_SERVER: self._api_server = galaxy.options.api_server
def __init__(self, galaxy): self.galaxy = galaxy self.token = GalaxyToken() self._api_server = C.GALAXY_SERVER self._validate_certs = not context.CLIARGS['ignore_certs'] self.baseurl = None self.version = None self.initialized = False display.debug('Validate TLS certificates: %s' % self._validate_certs) # set the API server if context.CLIARGS['api_server'] != C.GALAXY_SERVER: self._api_server = context.CLIARGS['api_server']
def test_api_token_auth_with_v2_url(): token = GalaxyToken(token=u"my_token") api = GalaxyAPI(None, "test", "https://galaxy.ansible.com/api/", token=token) actual = {} # Add v3 to random part of URL but response should only see the v2 as the full URI path segment. api._add_auth_token(actual, "https://galaxy.ansible.com/api/v2/resourcev3/name", required=True) assert actual == {'Authorization': 'Token my_token'}
def test_initialise_galaxy_with_auth(monkeypatch): mock_open = MagicMock() mock_open.side_effect = [ StringIO(u'{"available_versions":{"v1":"v1/"}}'), StringIO(u'{"token":"my token"}'), ] monkeypatch.setattr(galaxy_api, 'open_url', mock_open) api = GalaxyAPI(None, "test", "https://galaxy.ansible.com/api/", token=GalaxyToken(token='my_token')) actual = api.authenticate("github_token") assert len(api.available_api_versions) == 2 assert api.available_api_versions['v1'] == u'v1/' assert api.available_api_versions['v2'] == u'v2/' assert actual == {u'token': u'my token'} assert mock_open.call_count == 2 assert mock_open.mock_calls[0][1][0] == 'https://galaxy.ansible.com/api/' assert 'ansible-galaxy' in mock_open.mock_calls[0][2]['http_agent'] assert mock_open.mock_calls[1][1][ 0] == 'https://galaxy.ansible.com/api/v1/tokens/' assert 'ansible-galaxy' in mock_open.mock_calls[1][2]['http_agent'] assert mock_open.mock_calls[1][2]['data'] == 'github_token=github_token'
def test_api_token_auth_with_v3_url(): token = GalaxyToken(token=u"my_token") api = GalaxyAPI(None, "test", "https://galaxy.ansible.com", token=token) actual = {} api._add_auth_token(actual, "https://galaxy.ansible.com/api/v3/resource/name") assert actual == {'Authorization': 'Bearer my_token'}
def test_initialise_automation_hub(monkeypatch): mock_open = MagicMock() mock_open.side_effect = [ urllib_error.HTTPError('https://galaxy.ansible.com/api', 401, 'msg', {}, StringIO()), # AH won't return v1 but we do for authenticate() to work. StringIO(u'{"available_versions":{"v1":"/api/v1","v3":"/api/v3"}}'), StringIO(u'{"token":"my token"}'), ] monkeypatch.setattr(galaxy_api, 'open_url', mock_open) api = GalaxyAPI(None, "test", "https://galaxy.ansible.com", token=GalaxyToken(token='my_token')) actual = api.authenticate("github_token") assert len(api.available_api_versions) == 2 assert api.available_api_versions['v1'] == u'/api/v1' assert api.available_api_versions['v3'] == u'/api/v3' assert actual == {u'token': u'my token'} assert mock_open.call_count == 3 assert mock_open.mock_calls[0][1][0] == 'https://galaxy.ansible.com/api' assert mock_open.mock_calls[0][2]['headers'] == { 'Authorization': 'Token my_token' } assert mock_open.mock_calls[1][1][0] == 'https://galaxy.ansible.com/api' assert mock_open.mock_calls[1][2]['headers'] == { 'Authorization': 'Bearer my_token' } assert mock_open.mock_calls[2][1][ 0] == 'https://galaxy.ansible.com/api/v1/tokens/' assert mock_open.mock_calls[2][2]['data'] == 'github_token=github_token'
def get_test_galaxy_api(url, version, token_ins=None, token_value=None): token_value = token_value or "my token" token_ins = token_ins or GalaxyToken(token_value) api = GalaxyAPI(None, "test", url) api._available_api_versions = {version: '/api/%s' % version} api.token = token_ins return api
def test_api_token_auth(): token = GalaxyToken(token=u"my_token") api = GalaxyAPI(None, "test", "https://galaxy.ansible.com/api/", token=token) actual = {} api._add_auth_token(actual, "", required=True) assert actual == {'Authorization': 'Token my_token'}
def get_test_galaxy_api(url, version, token_ins=None, token_value=None): token_value = token_value or "my token" token_ins = token_ins or GalaxyToken(token_value) api = GalaxyAPI(None, "test", url) # Warning, this doesn't test g_connect() because _availabe_api_versions is set here. That means # that urls for v2 servers have to append '/api/' themselves in the input data. api._available_api_versions = {version: '%s' % version} api.token = token_ins return api
def execute_publish(self): """ Publish a collection into Ansible Galaxy. """ api_key = context.CLIARGS['api_key'] or GalaxyToken().get() api_server = context.CLIARGS['api_server'] collection_path = GalaxyCLI._resolve_path(context.CLIARGS['args']) ignore_certs = context.CLIARGS['ignore_certs'] wait = context.CLIARGS['wait'] publish_collection(collection_path, api_server, api_key, ignore_certs, wait)
def test_initialise_unknown(monkeypatch): mock_open = MagicMock() mock_open.side_effect = [ urllib_error.HTTPError('https://galaxy.ansible.com/api/', 500, 'msg', {}, StringIO(u'{"msg":"raw error"}')), urllib_error.HTTPError('https://galaxy.ansible.com/api/api/', 500, 'msg', {}, StringIO(u'{"msg":"raw error"}')), ] monkeypatch.setattr(galaxy_api, 'open_url', mock_open) api = GalaxyAPI(None, "test", "https://galaxy.ansible.com/api/", token=GalaxyToken(token='my_token')) expected = "Error when finding available api versions from test (%s) (HTTP Code: 500, Message: msg)" \ % api.api_server with pytest.raises(AnsibleError, match=re.escape(expected)): api.authenticate("github_token")
def __init__(self, galaxy): self.galaxy = galaxy self.token = GalaxyToken() self._api_server = C.GALAXY_SERVER self._validate_certs = not C.GALAXY_IGNORE_CERTS # set validate_certs if galaxy.options.ignore_certs: self._validate_certs = False display.vvv('Validate TLS certificates: %s' % self._validate_certs) # set the API server if galaxy.options.api_server != C.GALAXY_SERVER: self._api_server = galaxy.options.api_server display.vvv("Connecting to galaxy_server: %s" % self._api_server) server_version = self.get_server_api_version() if not server_version in self.SUPPORTED_VERSIONS: raise AnsibleError("Unsupported Galaxy server API version: %s" % server_version) self.baseurl = '%s/api/%s' % (self._api_server, server_version) self.version = server_version # for future use display.vvv("Base API: %s" % self.baseurl)
def test_token_from_file(b_token_file): assert GalaxyToken().get() == "file"
def test_api_token_auth_with_token_type(): token = GalaxyToken(token=u"my_token") api = GalaxyAPI(None, "test", "https://galaxy.ansible.com", token=token) actual = {} api._add_auth_token(actual, "", token_type="Bearer") assert actual == {'Authorization': 'Bearer my_token'}
class GalaxyAPI(object): ''' This class is meant to be used as a API client for an Ansible Galaxy server ''' SUPPORTED_VERSIONS = ['v1'] def __init__(self, galaxy): self.galaxy = galaxy self.token = GalaxyToken() self._api_server = C.GALAXY_SERVER self._validate_certs = not C.GALAXY_IGNORE_CERTS # set validate_certs if galaxy.options.ignore_certs: self._validate_certs = False display.vvv('Validate TLS certificates: %s' % self._validate_certs) # set the API server if galaxy.options.api_server != C.GALAXY_SERVER: self._api_server = galaxy.options.api_server display.vvv("Connecting to galaxy_server: %s" % self._api_server) server_version = self.get_server_api_version() if not server_version in self.SUPPORTED_VERSIONS: raise AnsibleError("Unsupported Galaxy server API version: %s" % server_version) self.baseurl = '%s/api/%s' % (self._api_server, server_version) self.version = server_version # for future use display.vvv("Base API: %s" % self.baseurl) def __auth_header(self): token = self.token.get() if token is None: raise AnsibleError( "No access token. You must first use login to authenticate and obtain an access token." ) return {'Authorization': 'Token ' + token} def __call_galaxy(self, url, args=None, headers=None, method=None): if args and not headers: headers = self.__auth_header() try: display.vvv(url) resp = open_url(url, data=args, validate_certs=self._validate_certs, headers=headers, method=method) data = json.load(resp) except HTTPError as e: res = json.load(e) raise AnsibleError(res['detail']) return data @property def api_server(self): return self._api_server @property def validate_certs(self): return self._validate_certs def get_server_api_version(self): """ Fetches the Galaxy API current version to ensure the API server is up and reachable. """ try: url = '%s/api/' % self._api_server data = json.load(open_url(url, validate_certs=self._validate_certs)) return data['current_version'] except Exception as e: raise AnsibleError( "The API server (%s) is not responding, please try again later." % url) def authenticate(self, github_token): """ Retrieve an authentication token """ url = '%s/tokens/' % self.baseurl args = urllib.urlencode({"github_token": github_token}) resp = open_url(url, data=args, validate_certs=self._validate_certs, method="POST") data = json.load(resp) return data def create_import_task(self, github_user, github_repo, reference=None): """ Post an import request """ url = '%s/imports/' % self.baseurl args = urllib.urlencode({ "github_user": github_user, "github_repo": github_repo, "github_reference": reference if reference else "" }) data = self.__call_galaxy(url, args=args) if data.get('results', None): return data['results'] return data def get_import_task(self, task_id=None, github_user=None, github_repo=None): """ Check the status of an import task. """ url = '%s/imports/' % self.baseurl if not task_id is None: url = "%s?id=%d" % (url, task_id) elif not github_user is None and not github_repo is None: url = "%s?github_user=%s&github_repo=%s" % (url, github_user, github_repo) else: raise AnsibleError( "Expected task_id or github_user and github_repo") data = self.__call_galaxy(url) return data['results'] def lookup_role_by_name(self, role_name, notify=True): """ Find a role by name. """ role_name = urlquote(role_name) try: parts = role_name.split(".") user_name = ".".join(parts[0:-1]) role_name = parts[-1] if notify: display.display("- downloading role '%s', owned by %s" % (role_name, user_name)) except: raise AnsibleError( "Invalid role name (%s). Specify role as format: username.rolename" % role_name) url = '%s/roles/?owner__username=%s&name=%s' % (self.baseurl, user_name, role_name) data = self.__call_galaxy(url) if len(data["results"]) != 0: return data["results"][0] return None def fetch_role_related(self, related, role_id): """ Fetch the list of related items for the given role. The url comes from the 'related' field of the role. """ try: url = '%s/roles/%d/%s/?page_size=50' % (self.baseurl, int(role_id), related) data = self.__call_galaxy(url) results = data['results'] done = (data.get('next_link', None) is None) while not done: url = '%s%s' % (self._api_server, data['next_link']) data = self.__call_galaxy(url) results += data['results'] done = (data.get('next_link', None) is None) return results except: return None def get_list(self, what): """ Fetch the list of items specified. """ try: url = '%s/%s/?page_size' % (self.baseurl, what) data = self.__call_galaxy(url) if "results" in data: results = data['results'] else: results = data done = True if "next" in data: done = (data.get('next_link', None) is None) while not done: url = '%s%s' % (self._api_server, data['next_link']) data = self.__call_galaxy(url) results += data['results'] done = (data.get('next_link', None) is None) return results except Exception as error: raise AnsibleError("Failed to download the %s list: %s" % (what, str(error))) def search_roles(self, search, **kwargs): search_url = self.baseurl + '/search/roles/?' if search: search_url += '&autocomplete=' + urlquote(search) tags = kwargs.get('tags', None) platforms = kwargs.get('platforms', None) page_size = kwargs.get('page_size', None) author = kwargs.get('author', None) if tags and isinstance(tags, basestring): tags = tags.split(',') search_url += '&tags_autocomplete=' + '+'.join(tags) if platforms and isinstance(platforms, basestring): platforms = platforms.split(',') search_url += '&platforms_autocomplete=' + '+'.join(platforms) if page_size: search_url += '&page_size=%s' % page_size if author: search_url += '&username_autocomplete=%s' % author data = self.__call_galaxy(search_url) return data def add_secret(self, source, github_user, github_repo, secret): url = "%s/notification_secrets/" % self.baseurl args = urllib.urlencode({ "source": source, "github_user": github_user, "github_repo": github_repo, "secret": secret }) data = self.__call_galaxy(url, args=args) return data def list_secrets(self): url = "%s/notification_secrets" % self.baseurl data = self.__call_galaxy(url, headers=self.__auth_header()) return data def remove_secret(self, secret_id): url = "%s/notification_secrets/%s/" % (self.baseurl, secret_id) data = self.__call_galaxy(url, headers=self.__auth_header(), method='DELETE') return data def delete_role(self, github_user, github_repo): url = "%s/removerole/?github_user=%s&github_repo=%s" % ( self.baseurl, github_user, github_repo) data = self.__call_galaxy(url, headers=self.__auth_header(), method='DELETE') return data
mock_open = MagicMock() mock_open.side_effect = urllib_error.HTTPError( expected_url, 500, 'msg', {}, StringIO(to_text(json.dumps(response)))) monkeypatch.setattr(galaxy_api, 'open_url', mock_open) with pytest.raises(GalaxyError, match=re.escape(to_native(expected % api.api_server))): api.publish_collection(collection_artifact) @pytest.mark.parametrize( 'server_url, api_version, token_type, token_ins, import_uri, full_import_uri', [ ('https://galaxy.server.com/api', 'v2', 'Token', GalaxyToken('my token'), '1234', 'https://galaxy.server.com/api/v2/collection-imports/1234/'), ('https://galaxy.server.com/api/automation-hub/', 'v3', 'Bearer', KeycloakToken(auth_url='https://api.test/'), '1234', 'https://galaxy.server.com/api/automation-hub/v3/imports/collections/1234/' ), ]) def test_wait_import_task(server_url, api_version, token_type, token_ins, import_uri, full_import_uri, monkeypatch): api = get_test_galaxy_api(server_url, api_version, token_ins=token_ins) if token_ins: mock_token_get = MagicMock() mock_token_get.return_value = 'my token' monkeypatch.setattr(token_ins, 'get', mock_token_get)
class GalaxyAPI(object): ''' This class is meant to be used as a API client for an Ansible Galaxy server ''' SUPPORTED_VERSIONS = ['v1'] def __init__(self, galaxy): self.galaxy = galaxy self.token = GalaxyToken() self._api_server = C.GALAXY_SERVER self._validate_certs = not galaxy.options.ignore_certs self.baseurl = None self.version = None self.initialized = False display.debug('Validate TLS certificates: %s' % self._validate_certs) # set the API server if galaxy.options.api_server != C.GALAXY_SERVER: self._api_server = galaxy.options.api_server def __auth_header(self): token = self.token.get() if token is None: raise AnsibleError("No access token. You must first use login to authenticate and obtain an access token.") return {'Authorization': 'Token ' + token} @g_connect def __call_galaxy(self, url, args=None, headers=None, method=None): if args and not headers: headers = self.__auth_header() try: display.vvv(url) resp = open_url(url, data=args, validate_certs=self._validate_certs, headers=headers, method=method, timeout=20) data = json.loads(to_text(resp.read(), errors='surrogate_or_strict')) except HTTPError as e: res = json.loads(to_text(e.fp.read(), errors='surrogate_or_strict')) raise AnsibleError(res['detail']) return data @property def api_server(self): return self._api_server @property def validate_certs(self): return self._validate_certs def _get_server_api_version(self): """ Fetches the Galaxy API current version to ensure the API server is up and reachable. """ url = '%s/api/' % self._api_server try: return_data = open_url(url, validate_certs=self._validate_certs) except Exception as e: raise AnsibleError("Failed to get data from the API server (%s): %s " % (url, to_native(e))) try: data = json.loads(to_text(return_data.read(), errors='surrogate_or_strict')) except Exception as e: raise AnsibleError("Could not process data from the API server (%s): %s " % (url, to_native(e))) if 'current_version' not in data: raise AnsibleError("missing required 'current_version' from server response (%s)" % url) return data['current_version'] @g_connect def authenticate(self, github_token): """ Retrieve an authentication token """ url = '%s/tokens/' % self.baseurl args = urlencode({"github_token": github_token}) resp = open_url(url, data=args, validate_certs=self._validate_certs, method="POST") data = json.loads(to_text(resp.read(), errors='surrogate_or_strict')) return data @g_connect def create_import_task(self, github_user, github_repo, reference=None, role_name=None): """ Post an import request """ url = '%s/imports/' % self.baseurl args = { "github_user": github_user, "github_repo": github_repo, "github_reference": reference if reference else "" } if role_name: args['alternate_role_name'] = role_name elif github_repo.startswith('ansible-role'): args['alternate_role_name'] = github_repo[len('ansible-role') + 1:] data = self.__call_galaxy(url, args=urlencode(args)) if data.get('results', None): return data['results'] return data @g_connect def get_import_task(self, task_id=None, github_user=None, github_repo=None): """ Check the status of an import task. """ url = '%s/imports/' % self.baseurl if task_id is not None: url = "%s?id=%d" % (url, task_id) elif github_user is not None and github_repo is not None: url = "%s?github_user=%s&github_repo=%s" % (url, github_user, github_repo) else: raise AnsibleError("Expected task_id or github_user and github_repo") data = self.__call_galaxy(url) return data['results'] @g_connect def lookup_role_by_name(self, role_name, notify=True): """ Find a role by name. """ role_name = urlquote(role_name) try: parts = role_name.split(".") user_name = ".".join(parts[0:-1]) role_name = parts[-1] if notify: display.display("- downloading role '%s', owned by %s" % (role_name, user_name)) except: raise AnsibleError("Invalid role name (%s). Specify role as format: username.rolename" % role_name) url = '%s/roles/?owner__username=%s&name=%s' % (self.baseurl, user_name, role_name) data = self.__call_galaxy(url) if len(data["results"]) != 0: return data["results"][0] return None @g_connect def fetch_role_related(self, related, role_id): """ Fetch the list of related items for the given role. The url comes from the 'related' field of the role. """ try: url = '%s/roles/%s/%s/?page_size=50' % (self.baseurl, role_id, related) data = self.__call_galaxy(url) results = data['results'] done = (data.get('next_link', None) is None) while not done: url = '%s%s' % (self._api_server, data['next_link']) data = self.__call_galaxy(url) results += data['results'] done = (data.get('next_link', None) is None) return results except: return None @g_connect def get_list(self, what): """ Fetch the list of items specified. """ try: url = '%s/%s/?page_size' % (self.baseurl, what) data = self.__call_galaxy(url) if "results" in data: results = data['results'] else: results = data done = True if "next" in data: done = (data.get('next_link', None) is None) while not done: url = '%s%s' % (self._api_server, data['next_link']) data = self.__call_galaxy(url) results += data['results'] done = (data.get('next_link', None) is None) return results except Exception as error: raise AnsibleError("Failed to download the %s list: %s" % (what, str(error))) @g_connect def search_roles(self, search, **kwargs): search_url = self.baseurl + '/search/roles/?' if search: search_url += '&autocomplete=' + urlquote(search) tags = kwargs.get('tags', None) platforms = kwargs.get('platforms', None) page_size = kwargs.get('page_size', None) author = kwargs.get('author', None) if tags and isinstance(tags, string_types): tags = tags.split(',') search_url += '&tags_autocomplete=' + '+'.join(tags) if platforms and isinstance(platforms, string_types): platforms = platforms.split(',') search_url += '&platforms_autocomplete=' + '+'.join(platforms) if page_size: search_url += '&page_size=%s' % page_size if author: search_url += '&username_autocomplete=%s' % author data = self.__call_galaxy(search_url) return data @g_connect def add_secret(self, source, github_user, github_repo, secret): url = "%s/notification_secrets/" % self.baseurl args = urlencode({ "source": source, "github_user": github_user, "github_repo": github_repo, "secret": secret }) data = self.__call_galaxy(url, args=args) return data @g_connect def list_secrets(self): url = "%s/notification_secrets" % self.baseurl data = self.__call_galaxy(url, headers=self.__auth_header()) return data @g_connect def remove_secret(self, secret_id): url = "%s/notification_secrets/%s/" % (self.baseurl, secret_id) data = self.__call_galaxy(url, headers=self.__auth_header(), method='DELETE') return data @g_connect def delete_role(self, github_user, github_repo): url = "%s/removerole/?github_user=%s&github_repo=%s" % (self.baseurl, github_user, github_repo) data = self.__call_galaxy(url, headers=self.__auth_header(), method='DELETE') return data
def test_token_none(b_token_file): assert GalaxyToken(token=NoTokenSentinel).get() is None
def test_publish_failure(api_version, collection_url, response, expected, collection_artifact, monkeypatch): api = get_test_galaxy_api('https://galaxy.server.com/api/', api_version) expected_url = '%s/api/%s/%s' % (api.api_server, api_version, collection_url) mock_open = MagicMock() mock_open.side_effect = urllib_error.HTTPError(expected_url, 500, 'msg', {}, StringIO(to_text(json.dumps(response)))) monkeypatch.setattr(galaxy_api, 'open_url', mock_open) with pytest.raises(GalaxyError, match=re.escape(to_native(expected % api.api_server))): api.publish_collection(collection_artifact) @pytest.mark.parametrize('server_url, api_version, token_type, token_ins, import_uri, full_import_uri', [ ('https://galaxy.server.com/api', 'v2', 'Token', GalaxyToken('my token'), '1234', 'https://galaxy.server.com/api/v2/collection-imports/1234'), ('https://galaxy.server.com/api/automation-hub/', 'v3', 'Bearer', KeycloakToken(auth_url='https://api.test/'), '1234', 'https://galaxy.server.com/api/automation-hub/v3/imports/collections/1234/'), ]) def test_wait_import_task(server_url, api_version, token_type, token_ins, import_uri, full_import_uri, monkeypatch): api = get_test_galaxy_api(server_url, api_version, token_ins=token_ins) if token_ins: mock_token_get = MagicMock() mock_token_get.return_value = 'my token' monkeypatch.setattr(token_ins, 'get', mock_token_get) mock_open = MagicMock()
def get_test_galaxy_api(url, version): api = GalaxyAPI(None, "test", url) api._available_api_versions = {version: '/api/%s' % version} api.token = GalaxyToken(token="my token") return api
class GalaxyAPI(object): ''' This class is meant to be used as a API client for an Ansible Galaxy server ''' SUPPORTED_VERSIONS = ['v1'] def __init__(self, galaxy): self.galaxy = galaxy self.token = GalaxyToken() self._api_server = C.GALAXY_SERVER self._validate_certs = not context.CLIARGS['ignore_certs'] self.baseurl = None self.version = None self.initialized = False display.debug('Validate TLS certificates: %s' % self._validate_certs) # set the API server if context.CLIARGS['api_server'] != C.GALAXY_SERVER: self._api_server = context.CLIARGS['api_server'] def __auth_header(self): token = self.token.get() if token is None: raise AnsibleError( "No access token. You must first use login to authenticate and obtain an access token." ) return {'Authorization': 'Token ' + token} @g_connect def __call_galaxy(self, url, args=None, headers=None, method=None): if args and not headers: headers = self.__auth_header() try: display.vvv(url) resp = open_url(url, data=args, validate_certs=self._validate_certs, headers=headers, method=method, timeout=20) data = json.loads( to_text(resp.read(), errors='surrogate_or_strict')) except HTTPError as e: res = json.loads(to_text(e.fp.read(), errors='surrogate_or_strict')) raise AnsibleError(res['detail']) return data @property def api_server(self): return self._api_server @property def validate_certs(self): return self._validate_certs def _get_server_api_version(self): """ Fetches the Galaxy API current version to ensure the API server is up and reachable. """ url = '%s/api/' % self._api_server try: return_data = open_url(url, validate_certs=self._validate_certs) except Exception as e: raise AnsibleError( "Failed to get data from the API server (%s): %s " % (url, to_native(e))) try: data = json.loads( to_text(return_data.read(), errors='surrogate_or_strict')) except Exception as e: raise AnsibleError( "Could not process data from the API server (%s): %s " % (url, to_native(e))) if 'current_version' not in data: raise AnsibleError( "missing required 'current_version' from server response (%s)" % url) return data['current_version'] @g_connect def authenticate(self, github_token): """ Retrieve an authentication token """ url = '%s/tokens/' % self.baseurl args = urlencode({"github_token": github_token}) resp = open_url(url, data=args, validate_certs=self._validate_certs, method="POST") data = json.loads(to_text(resp.read(), errors='surrogate_or_strict')) return data @g_connect def create_import_task(self, github_user, github_repo, reference=None, role_name=None): """ Post an import request """ url = '%s/imports/' % self.baseurl args = { "github_user": github_user, "github_repo": github_repo, "github_reference": reference if reference else "" } if role_name: args['alternate_role_name'] = role_name elif github_repo.startswith('ansible-role'): args['alternate_role_name'] = github_repo[len('ansible-role') + 1:] data = self.__call_galaxy(url, args=urlencode(args), method="POST") if data.get('results', None): return data['results'] return data @g_connect def get_import_task(self, task_id=None, github_user=None, github_repo=None): """ Check the status of an import task. """ url = '%s/imports/' % self.baseurl if task_id is not None: url = "%s?id=%d" % (url, task_id) elif github_user is not None and github_repo is not None: url = "%s?github_user=%s&github_repo=%s" % (url, github_user, github_repo) else: raise AnsibleError( "Expected task_id or github_user and github_repo") data = self.__call_galaxy(url) return data['results'] @g_connect def lookup_role_by_name(self, role_name, notify=True): """ Find a role by name. """ role_name = to_text(urlquote(to_bytes(role_name))) try: parts = role_name.split(".") user_name = ".".join(parts[0:-1]) role_name = parts[-1] if notify: display.display("- downloading role '%s', owned by %s" % (role_name, user_name)) except Exception: raise AnsibleError( "Invalid role name (%s). Specify role as format: username.rolename" % role_name) url = '%s/roles/?owner__username=%s&name=%s' % (self.baseurl, user_name, role_name) data = self.__call_galaxy(url) if len(data["results"]) != 0: return data["results"][0] return None @g_connect def fetch_role_related(self, related, role_id): """ Fetch the list of related items for the given role. The url comes from the 'related' field of the role. """ try: url = '%s/roles/%s/%s/?page_size=50' % (self.baseurl, role_id, related) data = self.__call_galaxy(url) results = data['results'] done = (data.get('next_link', None) is None) while not done: url = '%s%s' % (self._api_server, data['next_link']) data = self.__call_galaxy(url) results += data['results'] done = (data.get('next_link', None) is None) return results except Exception: return None @g_connect def get_list(self, what): """ Fetch the list of items specified. """ try: url = '%s/%s/?page_size' % (self.baseurl, what) data = self.__call_galaxy(url) if "results" in data: results = data['results'] else: results = data done = True if "next" in data: done = (data.get('next_link', None) is None) while not done: url = '%s%s' % (self._api_server, data['next_link']) data = self.__call_galaxy(url) results += data['results'] done = (data.get('next_link', None) is None) return results except Exception as error: raise AnsibleError("Failed to download the %s list: %s" % (what, to_native(error))) @g_connect def search_roles(self, search, **kwargs): search_url = self.baseurl + '/search/roles/?' if search: search_url += '&autocomplete=' + to_text(urlquote( to_bytes(search))) tags = kwargs.get('tags', None) platforms = kwargs.get('platforms', None) page_size = kwargs.get('page_size', None) author = kwargs.get('author', None) if tags and isinstance(tags, string_types): tags = tags.split(',') search_url += '&tags_autocomplete=' + '+'.join(tags) if platforms and isinstance(platforms, string_types): platforms = platforms.split(',') search_url += '&platforms_autocomplete=' + '+'.join(platforms) if page_size: search_url += '&page_size=%s' % page_size if author: search_url += '&username_autocomplete=%s' % author data = self.__call_galaxy(search_url) return data @g_connect def add_secret(self, source, github_user, github_repo, secret): url = "%s/notification_secrets/" % self.baseurl args = urlencode({ "source": source, "github_user": github_user, "github_repo": github_repo, "secret": secret }) data = self.__call_galaxy(url, args=args, method="POST") return data @g_connect def list_secrets(self): url = "%s/notification_secrets" % self.baseurl data = self.__call_galaxy(url, headers=self.__auth_header()) return data @g_connect def remove_secret(self, secret_id): url = "%s/notification_secrets/%s/" % (self.baseurl, secret_id) data = self.__call_galaxy(url, headers=self.__auth_header(), method='DELETE') return data @g_connect def delete_role(self, github_user, github_repo): url = "%s/removerole/?github_user=%s&github_repo=%s" % ( self.baseurl, github_user, github_repo) data = self.__call_galaxy(url, headers=self.__auth_header(), method='DELETE') return data
def test_token_from_file_missing(b_token_file): assert GalaxyToken().get() is None
expected_url = '%s/api/%s/%s' % (api.api_server, api_version, collection_url) mock_open = MagicMock() mock_open.side_effect = urllib_error.HTTPError( expected_url, 500, 'msg', {}, StringIO(to_text(json.dumps(response)))) monkeypatch.setattr(galaxy_api, 'open_url', mock_open) with pytest.raises(GalaxyError, match=re.escape(to_native(expected % api.api_server))): api.publish_collection(collection_artifact) @pytest.mark.parametrize('api_version, token_type, token_ins', [ ('v2', 'Token', GalaxyToken('my token')), ('v3', 'Bearer', KeycloakToken(auth_url='https://api.test/')), ]) def test_wait_import_task(api_version, token_type, token_ins, monkeypatch): api = get_test_galaxy_api('https://galaxy.server.com', api_version, token_ins=token_ins) import_uri = 'https://galaxy.server.com/api/%s/task/1234' % api_version if token_ins: mock_token_get = MagicMock() mock_token_get.return_value = 'my token' monkeypatch.setattr(token_ins, 'get', mock_token_get) mock_open = MagicMock() mock_open.return_value = StringIO(
def test_token_explicit_override_file(b_token_file): assert GalaxyToken(token="explicit").get() == "explicit"
def test_api_token_auth(): token = GalaxyToken(token=u"my_token") api = GalaxyAPI(None, "test", "https://galaxy.ansible.com", token=token) actual = api._auth_header() assert actual == {'Authorization': 'Token my_token'}