示例#1
0
    def fetch(self, role_data):
        """
        Downloads the archived role to a temp location based on role data
        """
        if role_data:

            # first grab the file and save it to a temp location
            if "github_user" in role_data and "github_repo" in role_data:
                archive_url = 'https://github.com/%s/%s/archive/%s.tar.gz' % (role_data["github_user"], role_data["github_repo"], self.version)
            else:
                archive_url = self.src

            display.display("- downloading role from %s" % archive_url)

            try:
                url_file = open_url(archive_url, validate_certs=self._validate_certs, http_agent=user_agent())
                temp_file = tempfile.NamedTemporaryFile(delete=False)
                data = url_file.read()
                while data:
                    temp_file.write(data)
                    data = url_file.read()
                temp_file.close()
                return temp_file.name
            except Exception as e:
                display.error(u"failed to download the file: %s" % to_text(e))

        return False
示例#2
0
    def get(self):
        if self._token:
            return self._token

        # - build a request to POST to auth_url
        #  - body is form encoded
        #    - 'request_token' is the offline token stored in assible.cfg
        #    - 'grant_type' is 'refresh_token'
        #    - 'client_id' is 'cloud-services'
        #       - should probably be based on the contents of the
        #         offline_ticket's JWT payload 'aud' (audience)
        #         or 'azp' (Authorized party - the party to which the ID Token was issued)
        payload = self._form_payload()

        resp = open_url(to_native(self.auth_url),
                        data=payload,
                        validate_certs=self.validate_certs,
                        method='POST',
                        http_agent=user_agent())

        # TODO: handle auth errors

        data = json.loads(to_text(resp.read(), errors='surrogate_or_strict'))

        # - extract 'access_token'
        self._token = data.get('access_token')

        return self._token
示例#3
0
 def create_github_token(self):
     '''
     Create a personal authorization token with a note of 'assible-galaxy login'
     '''
     self.remove_github_token()
     args = json.dumps({"scopes": ["public_repo"], "note": "assible-galaxy login"})
     try:
         data = json.load(open_url(self.GITHUB_AUTH, url_username=self.github_username,
                                   url_password=self.github_password, force_basic_auth=True, data=args,
                                   validate_certs=self._validate_certs, http_agent=user_agent()))
     except HTTPError as e:
         res = json.load(e)
         raise AssibleError(res['message'])
     return data['token']
示例#4
0
    def remove_github_token(self):
        '''
        If for some reason an assible-galaxy token was left from a prior login, remove it. We cannot
        retrieve the token after creation, so we are forced to create a new one.
        '''
        try:
            tokens = json.load(open_url(self.GITHUB_AUTH, url_username=self.github_username,
                                        url_password=self.github_password, force_basic_auth=True,
                                        validate_certs=self._validate_certs, http_agent=user_agent()))
        except HTTPError as e:
            res = json.load(e)
            raise AssibleError(res['message'])

        for token in tokens:
            if token['note'] == 'assible-galaxy login':
                display.vvvvv('removing token: %s' % token['token_last_eight'])
                try:
                    open_url('https://api.github.com/authorizations/%d' % token['id'],
                             url_username=self.github_username, url_password=self.github_password, method='DELETE',
                             force_basic_auth=True, validate_certs=self._validate_certs, http_agent=user_agent())
                except HTTPError as e:
                    res = json.load(e)
                    raise AssibleError(res['message'])
示例#5
0
    def send(self, path, data, **kwargs):
        """
        Sends the command to the device over api
        """
        url_kwargs = dict(
            timeout=self.get_option("persistent_command_timeout"),
            validate_certs=self.get_option("validate_certs"),
            use_proxy=self.get_option("use_proxy"),
            headers={},
        )
        url_kwargs.update(kwargs)
        if self._auth:
            # Avoid modifying passed-in headers
            headers = dict(kwargs.get("headers", {}))
            headers.update(self._auth)
            url_kwargs["headers"] = headers
        else:
            url_kwargs["force_basic_auth"] = True
            url_kwargs["url_username"] = self.get_option("remote_user")
            url_kwargs["url_password"] = self.get_option("password")

        try:
            url = self._url + path
            self._log_messages("send url '%s' with data '%s' and kwargs '%s'" %
                               (url, data, url_kwargs))
            response = open_url(url, data=data, **url_kwargs)
        except HTTPError as exc:
            is_handled = self.handle_httperror(exc)
            if is_handled is True:
                return self.send(path, data, **kwargs)
            elif is_handled is False:
                raise
            else:
                response = is_handled
        except URLError as exc:
            raise AssibleConnectionFailure(
                "Could not connect to {0}: {1}".format(self._url + path,
                                                       exc.reason))

        response_buffer = BytesIO()
        resp_data = response.read()
        self._log_messages("received response: '%s'" % resp_data)
        response_buffer.write(resp_data)

        # Try to assign a new auth token if one is given
        self._auth = self.update_auth(response, response_buffer) or self._auth

        response_buffer.seek(0)

        return response, response_buffer
示例#6
0
 def authenticate(self, github_token):
     """
     Retrieve an authentication token
     """
     url = _urljoin(self.api_server, self.available_api_versions['v1'],
                    "tokens") + '/'
     args = urlencode({"github_token": github_token})
     resp = open_url(url,
                     data=args,
                     validate_certs=self.validate_certs,
                     method="POST",
                     http_agent=user_agent())
     data = json.loads(to_text(resp.read(), errors='surrogate_or_strict'))
     return data
示例#7
0
def test_open_url(urlopen_mock, install_opener_mock, mocker):
    req_mock = mocker.patch('assible.module_utils.urls.Request.open')
    open_url('https://assible.com/')
    req_mock.assert_called_once_with('GET',
                                     'https://assible.com/',
                                     data=None,
                                     headers=None,
                                     use_proxy=True,
                                     force=False,
                                     last_mod_time=None,
                                     timeout=10,
                                     validate_certs=True,
                                     url_username=None,
                                     url_password=None,
                                     http_agent=None,
                                     force_basic_auth=False,
                                     follow_redirects='urllib2',
                                     client_cert=None,
                                     client_key=None,
                                     cookies=None,
                                     use_gssapi=False,
                                     unix_socket=None,
                                     ca_path=None,
                                     unredirected_headers=None)
示例#8
0
    def run(self, terms, variables=None, **kwargs):

        self.set_options(var_options=variables, direct=kwargs)

        ret = []
        for term in terms:
            display.vvvv("url lookup connecting to %s" % term)
            try:
                response = open_url(
                    term,
                    validate_certs=self.get_option('validate_certs'),
                    use_proxy=self.get_option('use_proxy'),
                    url_username=self.get_option('username'),
                    url_password=self.get_option('password'),
                    headers=self.get_option('headers'),
                    force=self.get_option('force'),
                    timeout=self.get_option('timeout'),
                    http_agent=self.get_option('http_agent'),
                    force_basic_auth=self.get_option('force_basic_auth'),
                    follow_redirects=self.get_option('follow_redirects'),
                    use_gssapi=self.get_option('use_gssapi'),
                    unix_socket=self.get_option('unix_socket'),
                    ca_path=self.get_option('ca_path'),
                    unredirected_headers=self.get_option(
                        'unredirected_headers'))
            except HTTPError as e:
                raise AssibleError("Received HTTP error for %s : %s" %
                                   (term, to_native(e)))
            except URLError as e:
                raise AssibleError("Failed lookup url for %s : %s" %
                                   (term, to_native(e)))
            except SSLValidationError as e:
                raise AssibleError(
                    "Error validating the server's certificate for %s: %s" %
                    (term, to_native(e)))
            except ConnectionError as e:
                raise AssibleError("Error connecting to %s: %s" %
                                   (term, to_native(e)))

            if self.get_option('split_lines'):
                for line in response.read().splitlines():
                    ret.append(to_text(line))
            else:
                ret.append(to_text(response.read()))
        return ret
示例#9
0
def main():
    """Entrypoint to the script"""

    paths = sys.argv[1:] or sys.stdin.read().splitlines()

    bundled_libs = get_bundled_libs(paths)
    files_with_bundled_metadata = get_files_with_bundled_metadata(paths)

    for filename in files_with_bundled_metadata.difference(bundled_libs):
        print(
            '{0}: ERROR: File contains _BUNDLED_METADATA but needs to be added to'
            ' test/sanity/code-smell/update-bundled.py'.format(filename))

    for filename in bundled_libs:
        try:
            metadata = get_bundled_metadata(filename)
        except ValueError as e:
            print('{0}: ERROR: {1}'.format(filename, e))
            continue
        except (IOError, OSError) as e:
            if e.errno == 2:
                print(
                    '{0}: ERROR: {1}.  Perhaps the bundled library has been removed'
                    ' or moved and the bundled library test needs to be modified as'
                    ' well?'.format(filename, e))

        if metadata is None:
            continue

        pypi_fh = open_url('https://pypi.org/pypi/{0}/json'.format(
            metadata['pypi_name']))
        pypi_data = json.loads(pypi_fh.read().decode('utf-8'))

        constraints = metadata.get('version_constraints', None)
        latest_version = get_latest_applicable_version(pypi_data, constraints)

        if LooseVersion(metadata['version']) < LooseVersion(latest_version):
            print('{0}: UPDATE {1} from {2} to {3} {4}'.format(
                filename, metadata['pypi_name'], metadata['version'],
                latest_version, 'https://pypi.org/pypi/{0}/json'.format(
                    metadata['pypi_name'])))
示例#10
0
    def _call_galaxy(self,
                     url,
                     args=None,
                     headers=None,
                     method=None,
                     auth_required=False,
                     error_context_msg=None):
        headers = headers or {}
        self._add_auth_token(headers, url, required=auth_required)

        try:
            display.vvvv("Calling Galaxy at %s" % url)
            resp = open_url(to_native(url),
                            data=args,
                            validate_certs=self.validate_certs,
                            headers=headers,
                            method=method,
                            timeout=20,
                            http_agent=user_agent(),
                            follow_redirects='safe')
        except HTTPError as e:
            raise GalaxyError(e, error_context_msg)
        except Exception as e:
            raise AssibleError(
                "Unknown error when attempting to call Galaxy at '%s': %s" %
                (url, to_native(e)))

        resp_data = to_text(resp.read(), errors='surrogate_or_strict')
        try:
            data = json.loads(resp_data)
        except ValueError:
            raise AssibleError(
                "Failed to parse Galaxy response from '%s' as JSON:\n%s" %
                (resp.url, to_native(resp_data)))

        return data