def test_get_by_subnet(self, apiobj):
        """Pass."""
        specfield = "specific_data.data.network_interfaces.subnets"
        findfield = "specific_data.data.network_interfaces.ips"
        withfields = [findfield, specfield]
        asset = self.get_single_asset(apiobj=apiobj,
                                      with_fields=withfields,
                                      fields=withfields)
        assert specfield in asset, list(asset)
        asset_value = asset[specfield]

        value = tools.listify(obj=asset_value)[0]

        found = apiobj.get_by_subnet(
            value=value,
            max_rows=1,
            fields=findfield,
            query_post=" and {}".format(QUERY_FIELD_EXISTS(field=findfield)),
        )

        assert isinstance(found, tools.LIST)

        found_value = tools.listify(obj=found[0][findfield])[0]
        assert found_value == tools.listify(obj=asset[findfield],
                                            dictkeys=False)[0]
    def do_checks(self, user_row, user, checks):
        """Pass."""
        for check in checks:
            user_row[check["col"]] = False

        if not user:
            user_row[COL_DVCLINK] = "NO USER FOUND"
            return [user_row]

        dvcs = self.find_dvcs(user=user)

        if not dvcs:
            user_row[COL_DVCLINK] = "NO ASSOCIATED DEVICES FOUND"
            return [user_row]

        user_dvc_rows = []
        for dvc in dvcs:
            agent_vers = dvc.get(AGENT_VERS)
            user_row_dvc = copy.deepcopy(user_row)
            user_dvc_rows.append(user_row_dvc)
            user_row_dvc[COL_DVCLINK] = self.get_axon_link(asset=dvc,
                                                           type="devices")
            user_row_dvc[COL_DVCNAME] = "\n".join(listify(dvc.get(NAME)))
            user_row_dvc[COL_DVCHOSTNAME] = "\n".join(
                listify(dvc.get(HOSTNAME)))
            user_row_dvc[COL_DVCIP] = "\n".join(listify(dvc.get(IPS)))
            user_row_dvc[COL_DVCOS] = "\n".join(listify(dvc.get(OS_STR)))
            user_row_dvc[COL_DVCAGENTS] = self.get_agent_vers(
                agent_vers=agent_vers)
            for check in checks:
                user_row_dvc[check["col"]] = self.do_check(agent_vers, check)
        return user_dvc_rows
 def get_by_specifics(self, apiobj, specfield, specmethod):
     """Pass."""
     asset = self.get_single_asset(apiobj=apiobj, fields=specfield)
     asset_value = asset[specfield]
     value = tools.listify(obj=asset_value)[0]
     query_post = "and {}".format(QUERY_FIELD_EXISTS(field=specfield))
     found = getattr(apiobj, specmethod)(value=value,
                                         query_post=query_post,
                                         match_count=1,
                                         fields=specfield)
     assert isinstance(found, dict)
     found_value = found[specfield]
     assert tools.listify(found_value) == tools.listify(asset_value)
    def test_get_by_field_value_list(self, apiobj):
        """Pass."""
        asset = self.get_single_asset(apiobj=apiobj,
                                      fields=apiobj._default_fields)
        assert isinstance(asset, dict)
        field = apiobj.TEST_DATA["single_field"]["exp"]
        value = asset[field]
        value_list = tools.listify(value)
        assert value
        found = apiobj.get_by_value(value=value_list,
                                    field=field,
                                    fields=apiobj._default_fields,
                                    match_count=1)
        found_value = found[field]

        assert tools.listify(found_value) == value_list
    def get_single_asset(self,
                         apiobj,
                         with_fields=None,
                         fields=None,
                         query=None,
                         refetch=None):
        """Pass."""
        if not query and refetch:
            query = QUERY_ID(**refetch)

        if not query:
            if not with_fields:
                with_fields = apiobj._default_fields

            query_fields = tools.listify(with_fields)
            query_fields = [x for x in query_fields if x not in ["labels"]]
            query_lines = [QUERY_FIELD_EXISTS(field=x) for x in query_fields]
            query = " and ".join(query_lines)

        if not fields:
            fields = apiobj._default_fields

        data = apiobj._get(query=query, fields=fields, page_size=1)
        assert isinstance(data, dict)

        assets = data["assets"]
        assert len(assets) == 1

        assert isinstance(assets, tools.LIST)
        asset = assets[0]

        return asset
    def find_user(self, user_row, idx, username):
        """Pass."""
        row_txt = f"for username {username!r} in row {idx + 1}"

        if not username:
            self.spew_warn(f"Empty value {row_txt}")
            user_row[COL_USERLINK] = "Empty username"
            return

        found = [
            x for x in self.user_assets if username in listify(x.get(USERNAME))
        ]

        if not found:
            self.spew_warn(f"No user found {row_txt}")
            user_row[COL_USERLINK] = "No user found"
            return

        if len(found) > 1:
            self.spew_warn(f"Found more than one user {row_txt}")
            user_row[
                COL_USERLINK] = f"More than one matching user found ({len(found)})"
            return

        user = found[0]
        user_row[COL_USERLINK] = self.get_axon_link(asset=user, type="users")
        user["row"] = user_row
        user["row_username"] = username
        user["row_idx"] = idx
        user["row_txt"] = row_txt
        return user
def get_value(asset: dict, field: str, pre: str = "specific_data.data"):
    value = listify(asset.get(f"{pre}.{field}", []))
    value = sorted(list(set([x for x in value if x])))
    if len(value) == 0:
        return ""
    if len(value) == 1:
        return value[0]
    return value
 def get_agent_vers(self, agent_vers):
     """Pass."""
     lines = []
     for agent_ver in listify(agent_vers):
         name = agent_ver.get("adapter_name") or ""
         status = agent_ver.get("agent_status") or ""
         version = agent_ver.get("agent_version") or ""
         lines.append(f"name: {name}, version: {version}, status: {status}")
     return "\n".join(lines)
Esempio n. 9
0
def parse_user(user, client):
    ass_dvcs = listify(user.get(ASS_DVC_FIELD, []))
    # username = listify(asset.get("specific_data.data.username"), [])

    if not ass_dvcs:
        # print(f"NO associated devices found for user {username}")
        return

    for ass_dvc in ass_dvcs:
        dvc = parse_ass_dvc(user=user, ass_dvc=ass_dvc, client=client)
        check_agents(user=user, dvc=dvc)
    def validate_raw_data(self, apiobj, row, field):
        adapters = row["adapters"]
        raw_datas = row[field]
        assert isinstance(raw_datas, (dict, list)) and raw_datas

        raw_datas = listify(raw_datas)
        for raw_data in raw_datas:
            assert isinstance(raw_data["data"], dict)
            assert isinstance(raw_data["plugin_name"],
                              str) and raw_data["plugin_name"]
            assert isinstance(raw_data["client_used"],
                              str) and raw_data["client_used"]
            assert raw_data["plugin_name"] in adapters
 def test_get_by_field_value_regex(self, apiobj):
     """Pass."""
     field = apiobj.TEST_DATA["single_field"]["exp"]
     value_re = "[a-zA-Z]"
     found = apiobj.get_by_value(value=value_re,
                                 value_regex=True,
                                 field=field,
                                 match_count=1,
                                 max_rows=1)
     assert isinstance(found, dict)
     assert any([
         re.search(value_re, f)
         for f in tools.listify(obj=found[field], dictkeys=False)
     ])
Esempio n. 12
0
def parse_ass_dvc(user, ass_dvc, client):
    username = listify(user.get("specific_data.data.username"), [])
    caption = ass_dvc.get("device_caption")
    query = FIND_DVC_QUERY_TMPL.format(value=caption)
    found = client.devices.get(query=query, fields=DVC_FIELDS)
    # NEED TO PASS devices apiobj

    if not found:
        print(f"No devices found that match associated device {caption} for " f"user {username}")
        return

    if len(found) > 1:
        print(
            f"Too many devices ({len(found)}) found that match associated device "
            f"{caption} for user {username}"
        )
        return

    return found[0]
    def _read_csv(self, path, required):
        """Pass."""
        path, content = path_read(obj=path)
        content = bom_strip(content=content)
        reader = csv.DictReader(io.StringIO(content))

        rows = [x for x in reader]
        if not rows:
            self.spew_error("No rows supplied in CSV!")

        columns = listify(reader.fieldnames)
        self.spew_debug(f"CSV file {path} has columns {columns}")
        missing = [x for x in required if x not in columns]
        if missing:
            missing = ", ".join(missing)
            self.spew_error(
                f"Missing required columns {missing} in CSV file {path}")

        return columns, rows
 def find_dvcs(self, user):
     """Pass."""
     row_txt = user["row_txt"]
     dvcs = listify(user.get(ASS_DVC))
     found = []
     for dvc in dvcs:
         wiz_entries = self.dvc_wiz_entries(dvc=dvc)
         query = self.devices.wizard.parse(wiz_entries)["query"]
         self.spew_debug(
             f"Looking for devices searching for {dvc!r} using query {query}"
         )
         dvcs = self.devices.get(
             query=query,
             field_null=True,
             fields_default=False,
             fields=FIELDS_DEVICE,
             field_compress=True,
         )
         self.spew_debug(
             f"Found {len(dvcs)} devices searching for {dvc!r} {row_txt}")
         found += dvcs
     self.spew_info(f"Found {len(found)} devices {row_txt}")
     return found
    def test_get_by_subnet_not(self, apiobj):
        """Pass."""
        specfield = "specific_data.data.network_interfaces.subnets"
        findfield = "specific_data.data.network_interfaces.ips"
        withfields = [findfield, specfield]
        asset = self.get_single_asset(apiobj=apiobj,
                                      with_fields=withfields,
                                      fields=withfields)
        assert specfield in asset, list(asset)
        asset_value = asset[specfield]

        value = tools.listify(obj=asset_value)[0]

        found = apiobj.get_by_subnet(
            value=value,
            value_not=True,
            max_rows=1,
            fields=findfield,
            query_post=" and {}".format(QUERY_FIELD_EXISTS(field=findfield)),
        )
        # could do value checking here, but we'd have to get every asset
        # lets not do that...
        assert isinstance(found, tools.LIST)
Esempio n. 16
0
 def test_str(self):
     """Simple test."""
     x = listify(obj="1")
     assert x == ["1"]
Esempio n. 17
0
 def test_int(self):
     """Simple test."""
     x = listify(obj=1)
     assert x == [1]
Esempio n. 18
0
 def test_list(self):
     """Simple test."""
     x = listify(obj=[1, 2])
     assert x == [1, 2]
Esempio n. 19
0
 def test_tuple(self):
     """Simple test."""
     x = listify(obj=(1, 2))
     assert x == [1, 2]
Esempio n. 20
0
 def test_dict_notkeys(self):
     """Simple test."""
     x = listify(obj={"x": 1, "y": 1}, dictkeys=False)
     assert x == [{"x": 1, "y": 1}]
Esempio n. 21
0
 def test_dict_keys(self):
     """Simple test."""
     x = listify(obj={"x": 1, "y": 1}, dictkeys=True)
     assert x == ["x", "y"]
Esempio n. 22
0
def check_agents(user, dvc):
    agents = listify(dvc.get(AGENT_VERSIONS_FIELD, []))
    for agent in agents:
        for check in AGENT_CHECKS:
            pass
Esempio n. 23
0
 def test_none(self):
     """Simple test."""
     x = listify(obj=None)
     assert x == []
def get_field_values(rows, field):
    """Pass."""
    values = [x[field] for x in rows if x.get(field)]
    values = [x for y in values for x in listify(y)]
    return values
Esempio n. 25
0
def get_field_vals(rows, field):
    """Test utility."""
    values = [x[field] for x in listify(rows) if x.get(field)]
    values = [x for y in values for x in listify(y)]
    return values