Beispiel #1
0
    def _set_config(self, name, **kwargs):
        headers = {"Content-Type": "application/json"}
        self.request = Request(headers=headers, timeout=60)

        configurators = [self._read_config_vars]
        for configurator in configurators:
            self._config = configurator(name, **kwargs)
            if self._config:
                break
        if self._config is None:
            raise SessionConfigurationException(to_native("No Configuration Found."))

        # set up auth if passed
        entrust_api_user = self.get_config("entrust_api_user")
        entrust_api_key = self.get_config("entrust_api_key")
        if entrust_api_user and entrust_api_key:
            self.request.url_username = entrust_api_user
            self.request.url_password = entrust_api_key
        else:
            raise SessionConfigurationException(to_native("User and key must be provided."))

        # set up client certificate if passed (support all-in one or cert + key)
        entrust_api_cert = self.get_config("entrust_api_cert")
        entrust_api_cert_key = self.get_config("entrust_api_cert_key")
        if entrust_api_cert:
            self.request.client_cert = entrust_api_cert
            if entrust_api_cert_key:
                self.request.client_key = entrust_api_cert_key
        else:
            raise SessionConfigurationException(to_native("Client certificate for authentication to the API must be provided."))

        # set up the spec
        entrust_api_specification_path = self.get_config("entrust_api_specification_path")

        if not entrust_api_specification_path.startswith("http") and not os.path.isfile(entrust_api_specification_path):
            raise SessionConfigurationException(to_native("OpenAPI specification was not found at location {0}.".format(entrust_api_specification_path)))
        if not valid_file_format.match(entrust_api_specification_path):
            raise SessionConfigurationException(to_native("OpenAPI specification filename must end in .json, .yml or .yaml"))

        self.verify = True

        if entrust_api_specification_path.startswith("http"):
            try:
                http_response = Request().open(method="GET", url=entrust_api_specification_path)
                http_response_contents = http_response.read()
                if entrust_api_specification_path.endswith(".json"):
                    self._spec = json.load(http_response_contents)
                elif entrust_api_specification_path.endswith(".yml") or entrust_api_specification_path.endswith(".yaml"):
                    self._spec = yaml.safe_load(http_response_contents)
            except HTTPError as e:
                raise SessionConfigurationException(to_native("Error downloading specification from address '{0}', received error code '{1}'".format(
                    entrust_api_specification_path, e.getcode())))
        else:
            with open(entrust_api_specification_path) as f:
                if ".json" in entrust_api_specification_path:
                    self._spec = json.load(f)
                elif ".yml" in entrust_api_specification_path or ".yaml" in entrust_api_specification_path:
                    self._spec = yaml.safe_load(f)
Beispiel #2
0
def test_Request_open_not_gzip(urlopen_mock):
    h = http_client.HTTPResponse(
        Sock(RESP),
        method='GET',
    )
    h.begin()

    if PY3:
        urlopen_mock.return_value = h
    else:
        urlopen_mock.return_value = addinfourl(
            h.fp,
            h.msg,
            'http://ansible.com/',
            h.status,
        )
        urlopen_mock.return_value.msg = h.reason

    r = Request().open('GET', 'https://ansible.com/')
    assert not isinstance(r.fp, GzipDecodedReader)
    assert r.read() == JSON_DATA
Beispiel #3
0
    def parse(self,
              inventory,
              loader,
              path,
              cache=True):  # Plugin interface (2)
        super(InventoryModule, self).parse(inventory, loader, path)
        self._read_config_data(path)
        host = self.get_option('host')
        ping_url = urljoin(host, 'api/v2/ping')

        try:
            response = Request().get(ping_url)
            text = response.read()
            data = json.loads(text)
        except (ConnectionError, urllib_error.URLError) as e:
            raise AnsibleParserError(
                "Unable to connect Tower or AWX server %s: %s" % (host, e))
        except (ValueError, TypeError) as e:
            raise AnsibleParserError(
                'Failed to parse json data from host, error: %s, data: %s' %
                (e, text))

        for instance_data in data['instances']:
            self.inventory.add_host(
                instance_data['node'])  # Inventory interface (1)
            self.inventory.set_variable(
                instance_data['node'], 'capacity',
                instance_data['capacity'])  # inventory interface (2)

        for group_data in data['instance_groups']:
            group_name = self.inventory.add_group(
                group_data['name'])  # Inventory interface (3)
            self.inventory.set_variable(
                group_name, 'group_capacity',
                group_data['capacity'])  # Inventory interface (2)
            for instance in group_data['instances']:
                self.inventory.add_child(group_name,
                                         instance)  # Inventory interface (4)
    def run(self, tmp=None, task_vars=None):
        if task_vars is None:
            task_vars = dict()

        result = super(ActionModule, self).run(tmp, task_vars)
        del tmp  # tmp no longer has any effect

        module_args = self._task.args.copy()

        barn_host = module_args.get("barn_host", None)
        barn_port = module_args.get("barn_port", 443)
        barn_url = module_args.get("barn_url", None)
        barn_https = ensure_type(module_args.get("barn_https", True), 'bool')
        validate_certs = module_args.get("validate_certs", True)
        barn_user = module_args.get("barn_user", None)
        barn_password = module_args.get("barn_password", None)
        token = module_args.get("barn_token", None)
        load_to_facts = module_args.get("load_to_facts", False)
        include = ensure_type(module_args.get("include", []), 'list')
        exclude = ensure_type(module_args.get("exclude", []), 'list')

        if not barn_url and barn_host and barn_port:
            protocol = "https" if barn_https else "http"
            barn_url = "{}://{}:{}".format(protocol, barn_host, barn_port)
            self._display.warning(
                "The options barn_host and barn_port are deprecated. Please use barn_url instead.")
        elif barn_url and not barn_url.startswith("https://") and not barn_url.startswith("http://"):
            protocol = "http" if "barn_https" in module_args and not barn_https else "https"
            barn_url = "{}://{}".format(protocol, barn_url)
        barn_url = barn_url.rstrip("/")

        if barn_url is None:
            result['changed'] = False
            result['failed'] = True
            result['msg'] = "barn_host is required"
            return result

        query_args = dict(
            follow_redirects=True,
            validate_certs=validate_certs
            )
        query_args["headers"] = {'Content-type': 'application/json'}
        if token:
            query_args["headers"]["x-access-tokens"] = token
        if barn_user:
            query_args["url_username"] = barn_user
            query_args["force_basic_auth"] = True
        if barn_password:
            query_args["url_password"] = barn_password
            query_args["force_basic_auth"] = True

        try:
            data = {
                "name": task_vars.get("inventory_hostname"),
                'type': "host"
            }
            query_args["data"] = json.dumps(data).encode('utf-8')
            self._display.vvv(
                "POST {}/api/v1/inventory/hosts".format(barn_url))
            r = Request().open("POST", "{}/api/v1/inventory/hosts".format(barn_url), **query_args)
            barn_resp = json.loads(r.read())
            self._display.vvv("Response form Barn: %s" % (barn_resp))
            barn_vars = barn_resp.get(
                "result", {})[0].get("vars", {})

            if len(include) > 0:
                barn_vars = {i: barn_vars[i] for i in include}
            if len(exclude) > 0:
                barn_vars = {i: barn_vars[i]
                             for i in barn_vars if i not in exclude}

            result["vars"] = barn_vars
            if load_to_facts:
                result['ansible_facts'] = barn_vars
        except urllib_error.HTTPError as e:
            result["status"] = int(getattr(e, 'code', -1))
            try:
                result = json.loads(e.read())
            except AttributeError:
                result["status"] = 500
                result["msg"] = "Can't parse API response to json response"
                result["failed"] = True
        except timeout:
            result["status"] = 503
            result["msg"] = "Connection timeout"
            result["failed"] = True
        except urllib_error.URLError as e: #SSL_cert_verificate_error
            result["status"] = int(e.code) if hasattr(e, 'code') else 503
            result["msg"] = str(e.reason) if hasattr(e, 'reason') else "Can't connect to barn"
            result["failed"] = True
        except urllib_error as e:
            result["status"] = 503
            result["msg"] = e.description if hasattr(e, 'description') else "Can't connect to barn"
            result["failed"] = True

        except Exception as e:
            raise AnsibleActionFail(e)

        return result
Beispiel #5
0
    def parse(self, inventory, loader, path, cache=True):
        ''' parses the inventory file '''
        barn_vars = {}
        if path.endswith("@barn"):
            for path_set in BARN_CONFIG_PATHS:
                for path in path_set:
                    if os.path.exists(path):
                        try:
                            self.display.vv("Use config file: {}".format(path))
                            barn_vars = merge_hash(
                                loader.load_from_file(path, cache=False),
                                barn_vars)
                        except Exception as e:
                            raise AnsibleParserError(e)
                        break
        else:
            barn_vars = loader.load_from_file(path, cache=False)

        barn_url = barn_vars.get("barn_url")
        barn_host = barn_vars.get("barn_host",
                                  barn_vars.get("barn_hostname", None))
        barn_port = barn_vars.get("barn_port", 443)
        barn_https = ensure_type(barn_vars.get("barn_https", True), 'bool')
        validate_certs = barn_vars.get("validate_certs", True)
        barn_user = barn_vars.get("barn_user", None)
        barn_password = barn_vars.get("barn_password", None)
        token = barn_vars.get("barn_token", None)

        if not barn_url and barn_host and barn_port:
            protocol = "https" if barn_https else "http"
            barn_url = "{}://{}:{}".format(protocol, barn_host, barn_port)
            self.display.warning(
                "The options barn_host and barn_port are deprecated. Please use barn_url instead."
            )
        elif barn_url and not barn_url.startswith(
                "https://") and not barn_url.startswith("http://"):
            protocol = "http" if "barn_https" in barn_vars and not barn_https else "https"
            barn_url = "{}://{}".format(protocol, barn_url)
        barn_url = barn_url.rstrip("/")

        if not barn_url:
            return

        query_args = dict(follow_redirects=True, validate_certs=validate_certs)
        query_args["headers"] = {'Content-type': 'application/json'}
        if token:
            query_args["headers"]["x-access-tokens"] = token
        if barn_user:
            query_args["url_username"] = barn_user
            query_args["force_basic_auth"] = True
        if barn_password:
            query_args["url_password"] = barn_password
            query_args["force_basic_auth"] = True

        hosts = []
        groups = []
        try:
            self.display.vvv(
                "Connect to {}/api/v1/inventory/hosts".format(barn_url))
            r = Request().open("GET",
                               "{}/api/v1/inventory/hosts".format(barn_url),
                               **query_args)
            hosts = json.loads(r.read()).get("result")
            self.display.vvv(json.dumps(dict(hosts=hosts)))

            self.display.vvv(
                "Connect to {}/api/v1/inventory/groups".format(barn_url))
            r = Request().open("GET",
                               "{}/api/v1/inventory/groups".format(barn_url),
                               **query_args)
            groups = json.loads(r.read()).get("result")
            self.display.vvv(json.dumps(dict(groups=groups)))
        except urllib_error.HTTPError as e:
            try:
                body = json.loads(e.read())
            except AttributeError:
                body = {}

            raise AnsibleParserError(message="{}: {}".format(
                body.get("status", "Unknown Error"), body.get("msg", "")))
        except urllib_error.URLError as e:  # SSL_cert_verificate_error
            msg = "{}: {}".format(
                e.code if hasattr(e, 'code') else "Unknown Error",
                str(e.reason)
                if hasattr(e, 'reason') else "Can't connect to barn")
            raise AnsibleParserError(message=msg)

        for h in hosts:
            inventory.add_host(h.get("name"))
            if barn_vars.get("fetch_variables", False):
                for k, v in h.get("vars", {}).items():
                    inventory.set_variable(h.get("name"), k, v)
        for g in groups:
            inventory.add_group(g.get("name"))
            if barn_vars.get("fetch_variables", False):
                for k, v in g.get("vars", {}).items():
                    inventory.set_variable(g.get("name"), k, v)
            for h in g.get("hosts", []):
                inventory.add_child(g.get("name"), h)
        # Add child groups
        for g in groups:
            for cg in g.get("child_groups", []):
                if cg in inventory.groups:
                    inventory.add_child(g.get("name"), cg)
    def run(self, tmp=None, task_vars=None):
        if task_vars is None:
            task_vars = dict()

        result = super(ActionModule, self).run(tmp, task_vars)
        del tmp  # tmp no longer has any effect

        module_args = self._task.args.copy()

        barn_host = module_args.get("barn_host", None)
        barn_port = module_args.get("barn_port", 443)
        barn_url = module_args.get("barn_url", None)
        barn_https = ensure_type(module_args.get("barn_https", True), 'bool')
        validate_certs = module_args.get("validate_certs", True)
        barn_user = module_args.get("barn_user", None)
        barn_password = module_args.get("barn_password", None)
        token = module_args.get("barn_token", None)
        state = module_args.get("state", None)

        if not barn_url and barn_host and barn_port:
            protocol = "https" if barn_https else "http"
            barn_url = "{}://{}:{}".format(protocol, barn_host, barn_port)
            self._display.warning(
                "The options barn_host and barn_port are deprecated. Please use barn_url instead.")
        elif barn_url and not barn_url.startswith("https://") and not barn_url.startswith("http://"):
            protocol = "http" if "barn_https" in module_args and not barn_https else "https"
            barn_url = "{}://{}".format(protocol, barn_url)
        barn_url = barn_url.rstrip("/")

        if barn_host is None:
            result['changed'] = False
            result['failed'] = True
            result['msg'] = "barn_host is required"
            return result

        query_args = dict(follow_redirects=True)
        query_args["headers"] = {'Content-type': 'application/json'}
        if token:
            query_args["headers"]["x-access-tokens"] = token
        if barn_user:
            query_args["url_username"] = barn_user
            query_args["force_basic_auth"] = True
        if barn_password:
            query_args["url_password"] = barn_password
            query_args["force_basic_auth"] = True
        try:
            if state == 'present':
                data = {
                    "name": task_vars.get("inventory_hostname")
                }
                if module_args.get("vars", None):
                    data["vars"] = module_args.get("vars")
                if module_args.get("remove_vars", None):
                    data["vars_absent"] = ensure_type(
                        module_args.get("remove_vars"), 'list')
                if module_args.get("groups", None):
                    data["groups"] = ensure_type(
                        module_args.get("groups"), 'list')
                if module_args.get("groups_present", None):
                    data["groups_present"] = ensure_type(
                        module_args.get("groups_present"), 'list')
                if module_args.get("groups_absent", None):
                    data["groups_absent"] = ensure_type(
                        module_args.get("groups_absent"), 'list')
                if module_args.get("groups_set", None):
                    if not any(k in module_args for k in ("groups", "groups_present", "groups_absent")):
                        data["groups_set"] = ensure_type(
                            module_args.get("groups_set"), 'list')
                    else:
                        self._display.warning(
                            "groups_set can't be used in combination with groups, groups_present and groups_absent. 'groups_set' will be ignored.")

                query_args["data"] = json.dumps(data).encode('utf-8')
                self._display.vvv(
                    "PUT {}/api/v1/inventory/hosts".format(barn_url))
                resp = Request().open("PUT", "{}/api/v1/inventory/hosts".format(barn_url),
                                      validate_certs=validate_certs, **query_args)
                result = json.loads(resp.read())

            elif state == "absent":
                data = dict(name=task_vars.get("inventory_hostname"))
                query_args["data"] = json.dumps(data).encode('utf-8')
                resp = Request().open("DELETE", "{}/api/v1/inventory/hosts".format(barn_url),
                                      validate_certs=validate_certs, **query_args)
                result = json.loads(resp.read())

        except urllib_error.HTTPError as e:
            result["status"] = int(getattr(e, 'code', -1))
            try:
                result = json.loads(e.read())
            except AttributeError:
                result["status"] = 500
                result["msg"] = "Can't parse API response to json response"
                result["failed"] = True
        except timeout:
            result["status"] = 503
            result["msg"] = "Connection timeout"
            result["failed"] = True
        except urllib_error.URLError as e:
            result["status"] = 503
            result["msg"] = "Can't connect to barn"
            result["failed"] = True
        except Exception as e:
            raise AnsibleActionFail(e)

        return result