jumpserver/apps/perms/utils/asset_permission.py

672 lines
22 KiB
Python
Raw Normal View History

# coding: utf-8
import time
2019-03-05 11:47:14 +00:00
import uuid
2018-04-07 16:16:37 +00:00
from collections import defaultdict
2019-03-26 11:46:04 +00:00
import json
from hashlib import md5
2019-06-28 14:07:22 +00:00
import itertools
2019-03-26 11:46:04 +00:00
2019-03-05 11:47:14 +00:00
from django.utils import timezone
2018-05-31 11:47:57 +00:00
from django.db.models import Q
2019-03-04 12:45:57 +00:00
from django.core.cache import cache
from django.conf import settings
2016-09-16 01:38:07 +00:00
from orgs.utils import set_to_root_org
from common.utils import get_logger, timeit
from common.tree import TreeNode
from assets.utils import NodeUtil
2019-06-12 09:04:49 +00:00
from .. import const
2019-07-02 08:45:26 +00:00
from ..models import AssetPermission, Action
2019-07-01 10:22:40 +00:00
from ..hands import Node, Asset
from .stack import PermSystemUserNodeUtil, PermAssetsAmountUtil
2017-03-09 06:55:33 +00:00
logger = get_logger(__file__)
2016-09-16 01:38:07 +00:00
__all__ = [
'AssetPermissionUtil', 'is_obj_attr_has', 'sort_assets',
'ParserNode',
]
class GenerateTree:
def __init__(self):
"""
2019-07-01 10:22:40 +00:00
nodes = {
node.key: {
2019-07-01 10:22:40 +00:00
"system_users": {
system_user.id: actions,
2019-07-01 10:22:40 +00:00
},
"assets": set([asset.id,]),
},
2019-07-01 10:22:40 +00:00
}
assets = {
asset.id: {
system_user.id: actions,
2019-07-01 10:22:40 +00:00
},
}
"""
2019-07-01 10:22:40 +00:00
self._node_util = None
self.nodes = defaultdict(lambda: {
"system_users": defaultdict(int), "assets": set(),
"assets_amount": 0, "all_assets": set(),
})
2019-07-01 10:22:40 +00:00
self.assets = defaultdict(lambda: defaultdict(int))
2019-06-12 09:04:49 +00:00
self._root_node = None
self._ungroup_node = None
2019-07-01 10:22:40 +00:00
self._nodes_with_assets = None
self._all_assets_nodes_key = None
self._asset_counter = 0
self._system_user_counter = 0
self._nodes_assets_counter = 0
2019-07-01 10:22:40 +00:00
@property
def node_util(self):
if not self._node_util:
self._node_util = NodeUtil()
return self._node_util
2019-06-12 09:04:49 +00:00
@staticmethod
def key_sort(key):
key_list = [int(i) for i in key.split(':')]
return len(key_list), key_list
2019-06-12 09:04:49 +00:00
@property
def root_key(self):
2019-06-12 09:04:49 +00:00
if self._root_node:
return self._root_node
all_keys = self.nodes.keys()
2019-06-12 09:04:49 +00:00
# 如果没有授权节点,就放到默认的根节点下
if not all_keys:
2019-06-27 13:43:10 +00:00
return None
root_key = min(all_keys, key=self.key_sort)
self._root_key = root_key
return root_key
2019-06-12 09:04:49 +00:00
@property
def all_assets_nodes_keys(self):
if not self._all_assets_nodes_key:
self._all_assets_nodes_key = Asset.get_all_nodes_keys()
return self._all_assets_nodes_key
@property
def ungrouped_key(self):
if self._ungroup_node:
return self._ungroup_node
if self.root_key:
node_key = "{}:{}".format(self.root_key, '-1')
2019-07-01 10:22:40 +00:00
else:
node_key = '1:-1'
self._ungroup_node = node_key
return node_key
@timeit
def add_assets_without_system_users(self, assets_ids):
for asset_id in assets_ids:
self.add_asset(asset_id, {})
@timeit
def add_assets(self, assets_ids_with_system_users):
for asset_id, system_users_ids in assets_ids_with_system_users.items():
self.add_asset(asset_id, system_users_ids)
# @timeit
def add_asset(self, asset_id, system_users_ids=None):
"""
:param asset_id:
:param system_users_ids: {system_user.id: actions, }
:return:
"""
if not system_users_ids:
system_users_ids = defaultdict(int)
# 获取已有资产的系统用户和actions并更新到最新系统用户信息中
old_system_users_ids = self.assets[asset_id]
for system_user_id, action in old_system_users_ids.items():
system_users_ids[system_user_id] |= action
asset_nodes_keys = self.all_assets_nodes_keys.get(asset_id, [])
# {asset.id: [node.key, ], }
# 获取用户在的节点
in_nodes = set(self.nodes.keys()) & set(asset_nodes_keys)
2019-07-01 10:22:40 +00:00
if not in_nodes:
self.nodes[self.ungrouped_key]["assets"].add(asset_id)
self.assets[asset_id] = system_users_ids
2019-07-01 10:22:40 +00:00
return
# 遍历用户应该在的节点
for key in in_nodes:
# 把自己加入到树上的节点中
self.nodes[key]["assets"].add(asset_id)
# 获取自己所在节点的系统用户,并添加进去
node_system_users_ids = self.nodes[key]["system_users"]
for system_user_id, action in node_system_users_ids.items():
system_users_ids[system_user_id] |= action
self.assets[asset_id] = system_users_ids
def add_node(self, node_key, system_users_ids=None):
"""
:param node_key: node.key
:param system_users_ids: {system_user.id: actions,}
:return:
"""
if not system_users_ids:
system_users_ids = defaultdict(int)
self.nodes[node_key]["system_users"] = system_users_ids
# 添加树节点
@timeit
def add_nodes(self, nodes_keys_with_system_users_ids):
"""
:param nodes_keys_with_system_users_ids:
{node.key: {system_user.id: actions,}, }
:return:
"""
util = PermSystemUserNodeUtil()
family = util.get_nodes_family_and_system_users(nodes_keys_with_system_users_ids)
for key, system_users in family.items():
self.add_node(key, system_users)
2019-07-01 10:22:40 +00:00
def get_assets(self):
"""
:return:
[
{"id": asset.id, "system_users": {system_user.id: actions, }},
]
"""
assets = []
for asset_id, system_users in self.assets.items():
assets.append({"id": asset_id, "system_users": system_users})
return assets
2019-07-01 10:22:40 +00:00
def set_ungrouped_assets_nodes_if_need(self):
if settings.PERM_SINGLE_ASSET_TO_UNGROUP_NODE:
return
ungrouped_assets_ids = self.nodes[self.ungrouped_key]["assets"]
for asset_id in ungrouped_assets_ids:
in_nodes = self.all_assets_nodes_keys.get(asset_id, [])
for node_key in in_nodes:
parents_keys = self.node_util.get_nodes_parents_keys_by_key(node_key, with_self=False)
for parent_key in parents_keys:
n = self.nodes[parent_key]
self.nodes[node_key]["assets"].add(asset_id)
self.nodes.pop(self.ungrouped_key, None)
@timeit
2019-07-01 10:22:40 +00:00
def get_nodes_with_assets(self):
"""
:return:
[
{
'key': node.key,
'assets_amount': 10
'assets': {
asset.id: {
system_user.id: actions,
},
},
},
]
"""
2019-07-01 10:22:40 +00:00
if self._nodes_with_assets:
return self._nodes_with_assets
self.set_ungrouped_assets_nodes_if_need()
util = PermAssetsAmountUtil()
nodes_with_assets_amount = util.compute_nodes_assets_amount(self.nodes)
nodes = []
for key, values in nodes_with_assets_amount.items():
assets = {asset_id: self.assets.get(asset_id) for asset_id in values["assets"]}
all_assets = {asset_id: self.assets.get(asset_id) for asset_id in values["all_assets"]}
nodes.append({
"key": key, "assets": assets, "all_assets": all_assets,
"assets_amount": values["assets_amount"]
})
# 如果返回空节点,页面构造授权资产树报错
if not nodes:
nodes.append({
"key": const.EMPTY_NODE_KEY, "assets": {}, "assets_amount": 0,
"all_assets": {},
})
nodes.sort(key=lambda n: self.key_sort(n["key"]))
2019-07-01 10:22:40 +00:00
self._nodes_with_assets = nodes
return nodes
2019-07-02 04:08:25 +00:00
def get_nodes(self):
self.set_ungrouped_assets_nodes_if_need()
nodes = list(self.nodes.keys())
if not nodes:
nodes.append(const.EMPTY_NODE_KEY)
return list(nodes)
2019-07-02 04:08:25 +00:00
2018-06-01 07:34:08 +00:00
def get_user_permissions(user, include_group=True):
if include_group:
groups = user.groups.all()
arg = Q(users=user) | Q(user_groups__in=groups)
2018-06-01 07:34:08 +00:00
else:
arg = Q(users=user)
2019-06-30 12:10:34 +00:00
return AssetPermission.get_queryset_with_prefetch().filter(arg)
2018-05-25 09:28:53 +00:00
2018-06-01 07:34:08 +00:00
def get_user_group_permissions(user_group):
2019-06-30 12:10:34 +00:00
return AssetPermission.get_queryset_with_prefetch().filter(
2018-06-01 07:34:08 +00:00
user_groups=user_group
)
2018-05-25 09:28:53 +00:00
2018-06-01 07:34:08 +00:00
def get_asset_permissions(asset, include_node=True):
if include_node:
nodes = asset.get_all_nodes(flat=True)
arg = Q(assets=asset) | Q(nodes__in=nodes)
2018-06-01 07:34:08 +00:00
else:
arg = Q(assets=asset)
2019-06-28 14:07:22 +00:00
return AssetPermission.objects.valid().filter(arg)
2018-05-25 09:28:53 +00:00
2018-04-07 16:16:37 +00:00
2018-06-01 07:34:08 +00:00
def get_node_permissions(node):
2019-06-28 14:07:22 +00:00
return AssetPermission.objects.valid().filter(nodes=node)
2018-04-07 16:16:37 +00:00
2018-06-01 07:34:08 +00:00
def get_system_user_permissions(system_user):
2019-06-28 14:07:22 +00:00
return AssetPermission.objects.valid().filter(
2018-06-01 07:34:08 +00:00
system_users=system_user
)
2018-04-07 16:16:37 +00:00
2019-06-27 13:43:10 +00:00
class AssetPermissionCacheMixin:
CACHE_KEY_PREFIX = '_ASSET_PERM_CACHE_V2_'
CACHE_META_KEY_PREFIX = '_ASSET_PERM_META_KEY_V2_'
2019-03-04 12:45:57 +00:00
CACHE_TIME = settings.ASSETS_PERM_CACHE_TIME
CACHE_POLICY_MAP = (('0', 'never'), ('1', 'using'), ('2', 'refresh'))
2019-07-01 13:48:25 +00:00
cache_policy = '1'
2019-07-02 06:17:56 +00:00
obj_id = ''
_filter_id = None
2019-03-04 12:45:57 +00:00
2019-03-26 11:46:04 +00:00
@classmethod
def is_not_using_cache(cls, cache_policy):
return cls.CACHE_TIME == 0 or cache_policy in cls.CACHE_POLICY_MAP[0]
@classmethod
def is_using_cache(cls, cache_policy):
return cls.CACHE_TIME != 0 and cache_policy in cls.CACHE_POLICY_MAP[1]
@classmethod
def is_refresh_cache(cls, cache_policy):
return cache_policy in cls.CACHE_POLICY_MAP[2]
def _is_not_using_cache(self):
return self.is_not_using_cache(self.cache_policy)
def _is_using_cache(self):
return self.is_using_cache(self.cache_policy)
def _is_refresh_cache(self):
return self.is_refresh_cache(self.cache_policy)
2018-04-07 16:16:37 +00:00
2019-07-01 13:48:25 +00:00
#@timeit
2019-03-26 11:46:04 +00:00
def get_cache_key(self, resource):
cache_key = self.CACHE_KEY_PREFIX + '{obj_id}_{filter_id}_{resource}'
return cache_key.format(
2019-03-26 11:46:04 +00:00
obj_id=self.obj_id, filter_id=self._filter_id,
resource=resource
)
2019-07-01 13:48:25 +00:00
@property
2019-07-02 14:08:50 +00:00
def node_asset_key(self):
2019-03-26 11:46:04 +00:00
return self.get_cache_key('NODES_WITH_ASSETS')
2019-07-02 14:08:50 +00:00
@property
def node_key(self):
return self.get_cache_key('NODES')
2019-07-01 13:48:25 +00:00
@property
2019-03-26 11:46:04 +00:00
def asset_key(self):
2019-07-01 13:48:25 +00:00
key = self.get_cache_key('ASSETS')
return key
2019-03-26 11:46:04 +00:00
2019-07-01 13:48:25 +00:00
@property
2019-03-26 11:46:04 +00:00
def system_key(self):
return self.get_cache_key('SYSTEM_USER')
2019-07-02 14:08:50 +00:00
def get_resource_from_cache(self, resource):
logger.debug("Try get resource from cache")
2019-07-02 14:08:50 +00:00
key_map = {
"assets": self.asset_key,
"nodes": self.node_key,
"nodes_with_assets": self.node_asset_key,
"system_users": self.system_key
}
key = key_map.get(resource)
if not key:
raise ValueError("Not a valid resource: {}".format(resource))
cached = cache.get(key)
2019-03-05 11:47:14 +00:00
if not cached:
logger.debug("Not found resource cache, update it")
2019-03-05 11:47:14 +00:00
self.update_cache()
2019-07-02 14:08:50 +00:00
cached = cache.get(key)
2019-03-05 11:47:14 +00:00
return cached
2019-03-04 12:45:57 +00:00
2019-07-02 14:08:50 +00:00
def get_resource(self, resource):
2019-03-26 11:46:04 +00:00
if self._is_using_cache():
logger.debug("Using cache to get resource")
2019-07-02 14:08:50 +00:00
return self.get_resource_from_cache(resource)
2019-03-26 11:46:04 +00:00
elif self._is_refresh_cache():
logger.debug("Need refresh cache")
2019-03-04 12:45:57 +00:00
self.expire_cache()
2019-07-02 14:08:50 +00:00
data = self.get_resource_from_cache(resource)
return data
2019-03-04 12:45:57 +00:00
else:
logger.debug("Not using cache get source")
2019-07-02 14:08:50 +00:00
return self.get_resource_without_cache(resource)
2019-03-04 12:45:57 +00:00
2019-07-02 14:08:50 +00:00
def get_resource_without_cache(self, resource):
attr = 'get_{}_without_cache'.format(resource)
return getattr(self, attr)()
def get_nodes_with_assets(self):
return self.get_resource("nodes_with_assets")
2019-03-04 12:45:57 +00:00
2019-07-01 10:22:40 +00:00
def get_assets(self):
2019-07-02 14:08:50 +00:00
return self.get_resource("assets")
def get_nodes(self):
return self.get_resource("nodes")
2019-07-01 10:22:40 +00:00
2019-03-04 12:45:57 +00:00
def get_system_users(self):
2019-07-02 14:08:50 +00:00
return self.get_resource("system_users")
2019-03-04 12:45:57 +00:00
2019-03-26 11:46:04 +00:00
def get_meta_cache_key(self):
cache_key = self.CACHE_META_KEY_PREFIX + '{obj_id}_{filter_id}'
key = cache_key.format(
2019-07-02 06:17:56 +00:00
obj_id=self.obj_id, filter_id=self._filter_id
2019-03-26 11:46:04 +00:00
)
return key
2019-03-05 11:47:14 +00:00
@property
def cache_meta(self):
2019-03-26 11:46:04 +00:00
key = self.get_meta_cache_key()
2019-07-01 13:48:25 +00:00
meta = cache.get(key) or {}
# print("Meta key: {}".format(key))
# print("Meta id: {}".format(meta["id"]))
return meta
2019-03-05 11:47:14 +00:00
2019-07-02 14:08:50 +00:00
def update_cache(self):
assets = self.get_resource_without_cache("assets")
nodes_with_assets = self.get_resource_without_cache("nodes_with_assets")
system_users = self.get_resource_without_cache("system_users")
nodes = self.get_resource_without_cache("nodes")
cache.set(self.asset_key, assets, self.CACHE_TIME)
cache.set(self.node_asset_key, nodes_with_assets, self.CACHE_TIME)
cache.set(self.system_key, system_users, self.CACHE_TIME)
cache.set(self.node_key, nodes, self.CACHE_TIME)
self.set_meta_to_cache()
2019-03-26 11:46:04 +00:00
def set_meta_to_cache(self):
key = self.get_meta_cache_key()
2019-03-05 11:47:14 +00:00
meta = {
'id': str(uuid.uuid4()),
'datetime': timezone.now(),
'object': str(self.object)
}
2019-07-01 13:48:25 +00:00
# print("Set meta key: {}".format(key))
# print("set meta to cache: {}".format(meta["id"]))
2019-03-05 11:47:14 +00:00
cache.set(key, meta, self.CACHE_TIME)
def expire_cache_meta(self):
cache_key = self.CACHE_META_KEY_PREFIX + '{obj_id}_*'
2019-07-02 06:17:56 +00:00
key = cache_key.format(obj_id=self.obj_id)
cache.delete_pattern(key)
2019-03-05 11:47:14 +00:00
2019-03-04 12:45:57 +00:00
def expire_cache(self):
2019-03-05 11:47:14 +00:00
"""
因为 获取用户的节点资产系统用户等都能会缓存这里会清理所有与该对象有关的
缓存以免造成不统一的情况
:return:
"""
cache_key = self.CACHE_KEY_PREFIX + '{obj_id}_*'
key = cache_key.format(obj_id='*')
2019-03-05 11:47:14 +00:00
cache.delete_pattern(key)
self.expire_cache_meta()
2019-03-04 12:45:57 +00:00
@classmethod
def expire_all_cache(cls):
key = cls.CACHE_KEY_PREFIX + '*'
2019-03-05 11:47:14 +00:00
cache.delete_pattern(key)
meta_key = cls.CACHE_META_KEY_PREFIX + '*'
cache.delete_pattern(meta_key)
2019-03-04 12:45:57 +00:00
2018-02-07 15:25:15 +00:00
2019-06-27 13:43:10 +00:00
class AssetPermissionUtil(AssetPermissionCacheMixin):
get_permissions_map = {
"User": get_user_permissions,
"UserGroup": get_user_group_permissions,
"Asset": get_asset_permissions,
"Node": get_node_permissions,
"SystemUser": get_system_user_permissions,
}
2019-07-02 14:08:50 +00:00
assets_only = (
'id', 'hostname', 'ip', "platform", "domain_id",
'comment', 'is_active', 'os', 'org_id'
)
2019-06-27 13:43:10 +00:00
def __init__(self, obj, cache_policy='0'):
self.object = obj
self.obj_id = str(obj.id)
self._permissions = None
self._permissions_id = None # 标记_permission的唯一值
self._assets = None
self._filter_id = 'None' # 当通过filter更改 permission是标记
self.cache_policy = cache_policy
self.tree = GenerateTree()
self.change_org_if_need()
self.nodes = None
2019-07-01 10:22:40 +00:00
self._nodes = None
2019-07-02 14:08:50 +00:00
self._assets_direct = None
self._nodes_direct = None
self.node_util = NodeUtil()
self.tree._node_util = self.node_util
2019-06-27 13:43:10 +00:00
@staticmethod
def change_org_if_need():
set_to_root_org()
@property
def permissions(self):
if self._permissions:
return self._permissions
object_cls = self.object.__class__.__name__
func = self.get_permissions_map[object_cls]
permissions = func(self.object)
self._permissions = permissions
return permissions
@timeit
2019-06-27 13:43:10 +00:00
def filter_permissions(self, **filters):
filters_json = json.dumps(filters, sort_keys=True)
self._permissions = self.permissions.filter(**filters)
self._filter_id = md5(filters_json.encode()).hexdigest()
@timeit
2019-06-27 13:43:10 +00:00
def get_nodes_direct(self):
"""
返回直接授权的节点
并将节点添加到tree.nodes中并将节点下的资产添加到tree.assets中
:return:
{node.key: {system_user.id: actions,}, }
2019-06-27 13:43:10 +00:00
"""
2019-07-02 14:08:50 +00:00
if self._nodes_direct:
return self._nodes_direct
nodes_keys = defaultdict(lambda: defaultdict(int))
2019-06-30 12:10:34 +00:00
for perm in self.permissions:
2019-07-01 10:22:40 +00:00
actions = [perm.actions]
system_users_ids = [s.id for s in perm.system_users.all()]
_nodes_keys = [n.key for n in perm.nodes.all()]
iterable = itertools.product(_nodes_keys, system_users_ids, actions)
for node_key, sys_id, action in iterable:
nodes_keys[node_key][sys_id] |= action
self.tree.add_nodes(nodes_keys)
all_nodes_keys = set()
for key in nodes_keys:
children_keys = self.node_util.get_all_children_keys_by_key(key)
all_nodes_keys.update(set(children_keys))
if all_nodes_keys:
assets_ids = Asset.objects.filter(
nodes__key__in=all_nodes_keys
).valid().values_list("id", flat=True).distinct()
else:
assets_ids = []
self.tree.add_assets_without_system_users(assets_ids)
self._nodes_direct = nodes_keys
return nodes_keys
2019-06-27 13:43:10 +00:00
2019-07-02 14:08:50 +00:00
def get_nodes_without_cache(self):
self.get_assets_without_cache()
2019-07-02 04:08:25 +00:00
return self.tree.get_nodes()
@timeit
2019-06-27 13:43:10 +00:00
def get_assets_direct(self):
"""
返回直接授权的资产
并添加到tree.assets中
:return:
{asset.id: {system_user.id: actions, }, }
2019-06-27 13:43:10 +00:00
"""
2019-07-02 14:08:50 +00:00
if self._assets_direct:
return self._assets_direct
assets_ids = defaultdict(lambda: defaultdict(int))
2019-06-30 12:10:34 +00:00
for perm in self.permissions:
2019-07-01 10:22:40 +00:00
actions = [perm.actions]
_assets_ids = perm.assets.valid().values_list("id", flat=True)
system_users_ids = perm.system_users.values_list("id", flat=True)
iterable = itertools.product(_assets_ids, system_users_ids, actions)
for asset_id, sys_id, action in iterable:
assets_ids[asset_id][sys_id] |= action
self.tree.add_assets(assets_ids)
self._assets_direct = assets_ids
return assets_ids
@timeit
2019-06-27 13:43:10 +00:00
def get_assets_without_cache(self):
"""
:return:
[
{"id": asset.id, "system_users": {system_user.id: actions, }},
]
2019-06-27 13:43:10 +00:00
"""
if self._assets:
return self._assets
self.get_nodes_direct()
2019-07-02 14:08:50 +00:00
self.get_assets_direct()
2019-07-01 10:22:40 +00:00
assets = self.tree.get_assets()
self._assets = assets
return assets
2019-06-27 13:43:10 +00:00
@timeit
2019-06-27 13:43:10 +00:00
def get_nodes_with_assets_without_cache(self):
2019-07-01 10:22:40 +00:00
self.get_assets_without_cache()
2019-07-02 14:08:50 +00:00
nodes_assets = self.tree.get_nodes_with_assets()
return nodes_assets
2019-06-27 13:43:10 +00:00
2019-07-02 14:08:50 +00:00
def get_system_users_without_cache(self):
2019-06-27 13:43:10 +00:00
system_users = set()
permissions = self.permissions.prefetch_related('system_users')
for perm in permissions:
system_users.update(perm.system_users.all())
return system_users
def is_obj_attr_has(obj, val, attrs=("hostname", "ip", "comment")):
if not attrs:
vals = [val for val in obj.__dict__.values() if isinstance(val, (str, int))]
else:
vals = [getattr(obj, attr) for attr in attrs if
hasattr(obj, attr) and isinstance(hasattr(obj, attr), (str, int))]
for v in vals:
if str(v).find(val) != -1:
return True
return False
def sort_assets(assets, order_by='hostname', reverse=False):
if order_by == 'ip':
assets = sorted(assets, key=lambda asset: [int(d) for d in asset.ip.split('.') if d.isdigit()], reverse=reverse)
else:
assets = sorted(assets, key=lambda asset: getattr(asset, order_by), reverse=reverse)
return assets
class ParserNode:
nodes_only_fields = ("key", "value", "id")
assets_only_fields = ("platform", "hostname", "id", "ip", "protocols")
system_users_only_fields = (
"id", "name", "username", "protocol", "priority", "login_mode",
)
@staticmethod
def parse_node_to_tree_node(node):
name = '{} ({})'.format(node.value, node.assets_amount)
data = {
'id': node.key,
'name': name,
'title': name,
'pId': node.parent_key,
'isParent': True,
'open': node.is_root(),
'meta': {
'node': {
"id": node.id,
"key": node.key,
"value": node.value,
},
'type': 'node'
}
}
tree_node = TreeNode(**data)
return tree_node
@staticmethod
def parse_asset_to_tree_node(node, asset, system_users):
icon_skin = 'file'
if asset.platform.lower() == 'windows':
icon_skin = 'windows'
elif asset.platform.lower() == 'linux':
icon_skin = 'linux'
_system_users = []
for system_user in system_users:
_system_users.append({
'id': system_user.id,
'name': system_user.name,
'username': system_user.username,
'protocol': system_user.protocol,
'priority': system_user.priority,
'login_mode': system_user.login_mode,
'actions': [Action.value_to_choices(system_user.actions)],
})
data = {
'id': str(asset.id),
'name': asset.hostname,
'title': asset.ip,
'pId': node.key,
'isParent': False,
'open': False,
'iconSkin': icon_skin,
'meta': {
'system_users': _system_users,
'type': 'asset',
'asset': {
'id': asset.id,
'hostname': asset.hostname,
'ip': asset.ip,
'protocols': asset.protocols_as_list,
'platform': asset.platform,
},
}
}
tree_node = TreeNode(**data)
return tree_node