2016-12-20 16:43:52 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-04-02 10:35:46 +00:00
|
|
|
#
|
2016-12-20 16:43:52 +00:00
|
|
|
|
2017-12-11 09:08:43 +00:00
|
|
|
import logging
|
2022-08-11 07:45:03 +00:00
|
|
|
import uuid
|
2018-05-31 11:47:57 +00:00
|
|
|
from functools import reduce
|
2017-11-23 06:08:01 +00:00
|
|
|
|
2016-12-20 16:43:52 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2022-08-11 07:45:03 +00:00
|
|
|
from orgs.mixins.models import OrgManager, JMSOrgBaseModel
|
2022-04-02 10:35:46 +00:00
|
|
|
from ..platform import Platform
|
|
|
|
from ..base import AbsConnectivity
|
2021-07-08 06:23:18 +00:00
|
|
|
|
2022-08-04 02:44:11 +00:00
|
|
|
__all__ = ['Asset', 'AssetQuerySet', 'default_node']
|
2016-12-20 16:43:52 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-02-08 03:34:21 +00:00
|
|
|
def default_node():
|
2018-02-09 03:12:40 +00:00
|
|
|
try:
|
2022-04-02 10:35:46 +00:00
|
|
|
from assets.models import Node
|
2019-08-23 10:23:07 +00:00
|
|
|
root = Node.org_root()
|
2021-04-28 07:26:40 +00:00
|
|
|
return Node.objects.filter(id=root.id)
|
2018-02-09 03:12:40 +00:00
|
|
|
except:
|
|
|
|
return None
|
2018-02-08 03:34:21 +00:00
|
|
|
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
class AssetManager(OrgManager):
|
2020-09-27 08:02:44 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-07-13 07:05:46 +00:00
|
|
|
class AssetQuerySet(models.QuerySet):
|
2018-04-27 03:41:47 +00:00
|
|
|
def active(self):
|
|
|
|
return self.filter(is_active=True)
|
|
|
|
|
|
|
|
def valid(self):
|
|
|
|
return self.active()
|
|
|
|
|
2019-07-05 10:07:10 +00:00
|
|
|
def has_protocol(self, name):
|
|
|
|
return self.filter(protocols__contains=name)
|
|
|
|
|
2018-04-27 03:41:47 +00:00
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
class NodesRelationMixin:
|
|
|
|
NODES_CACHE_KEY = 'ASSET_NODES_{}'
|
|
|
|
ALL_ASSET_NODES_CACHE_KEY = 'ALL_ASSETS_NODES'
|
|
|
|
CACHE_TIME = 3600 * 24 * 7
|
|
|
|
id = ""
|
|
|
|
_all_nodes_keys = None
|
|
|
|
|
|
|
|
def get_nodes(self):
|
2022-04-02 10:35:46 +00:00
|
|
|
from assets.models import Node
|
2019-08-23 10:23:07 +00:00
|
|
|
nodes = self.nodes.all()
|
|
|
|
if not nodes:
|
|
|
|
nodes = Node.objects.filter(id=Node.org_root().id)
|
2019-07-11 10:12:14 +00:00
|
|
|
return nodes
|
|
|
|
|
|
|
|
def get_all_nodes(self, flat=False):
|
|
|
|
nodes = []
|
|
|
|
for node in self.get_nodes():
|
2019-09-24 07:00:32 +00:00
|
|
|
_nodes = node.get_ancestors(with_self=True)
|
2019-07-11 10:12:14 +00:00
|
|
|
nodes.append(_nodes)
|
|
|
|
if flat:
|
|
|
|
nodes = list(reduce(lambda x, y: set(x) | set(y), nodes))
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
|
2022-08-11 07:45:03 +00:00
|
|
|
class Asset(AbsConnectivity, NodesRelationMixin, JMSOrgBaseModel):
|
2017-11-23 06:08:01 +00:00
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2022-08-11 07:45:03 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Hostname'))
|
2022-04-02 10:35:46 +00:00
|
|
|
ip = models.CharField(max_length=128, verbose_name=_('IP'), db_index=True)
|
2022-08-04 02:44:11 +00:00
|
|
|
protocols = models.ManyToManyField('Protocol', verbose_name=_("Protocols"), blank=True)
|
2022-04-02 10:35:46 +00:00
|
|
|
platform = models.ForeignKey(Platform, default=Platform.default, on_delete=models.PROTECT,
|
|
|
|
verbose_name=_("Platform"), related_name='assets')
|
|
|
|
domain = models.ForeignKey("assets.Domain", null=True, blank=True, related_name='assets',
|
|
|
|
verbose_name=_("Domain"), on_delete=models.SET_NULL)
|
|
|
|
nodes = models.ManyToManyField('assets.Node', default=default_node, related_name='assets',
|
|
|
|
verbose_name=_("Nodes"))
|
2017-03-15 16:19:47 +00:00
|
|
|
is_active = models.BooleanField(default=True, verbose_name=_('Is active'))
|
|
|
|
|
2018-12-18 09:28:45 +00:00
|
|
|
labels = models.ManyToManyField('assets.Label', blank=True, related_name='assets', verbose_name=_("Labels"))
|
2020-10-19 03:37:27 +00:00
|
|
|
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
|
2016-12-20 16:43:52 +00:00
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
objects = AssetManager.from_queryset(AssetQuerySet)()
|
2018-04-27 03:41:47 +00:00
|
|
|
|
2017-11-01 15:23:11 +00:00
|
|
|
def __str__(self):
|
2022-08-16 03:09:30 +00:00
|
|
|
return '{0.name}({0.ip})'.format(self)
|
2016-12-20 17:03:52 +00:00
|
|
|
|
2022-04-12 09:45:10 +00:00
|
|
|
def get_target_ip(self):
|
|
|
|
return self.ip
|
2021-07-08 06:23:18 +00:00
|
|
|
|
2016-12-20 16:43:52 +00:00
|
|
|
@property
|
|
|
|
def is_valid(self):
|
|
|
|
warning = ''
|
|
|
|
if not self.is_active:
|
|
|
|
warning += ' inactive'
|
2019-07-05 10:07:10 +00:00
|
|
|
if warning:
|
|
|
|
return False, warning
|
|
|
|
return True, warning
|
2019-06-13 10:58:43 +00:00
|
|
|
|
2020-10-30 02:16:49 +00:00
|
|
|
def nodes_display(self):
|
|
|
|
names = []
|
|
|
|
for n in self.nodes.all():
|
2020-10-30 02:43:44 +00:00
|
|
|
names.append(n.full_value)
|
2020-10-30 02:16:49 +00:00
|
|
|
return names
|
|
|
|
|
2021-11-26 07:16:14 +00:00
|
|
|
def labels_display(self):
|
|
|
|
names = []
|
|
|
|
for n in self.labels.all():
|
|
|
|
names.append(n.name + ':' + n.value)
|
|
|
|
return names
|
|
|
|
|
2022-08-11 07:45:03 +00:00
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
return self.platform.type
|
|
|
|
|
|
|
|
@property
|
|
|
|
def category(self):
|
|
|
|
return self.platform.category
|
|
|
|
|
2018-10-30 04:06:39 +00:00
|
|
|
def as_node(self):
|
2022-04-02 10:35:46 +00:00
|
|
|
from assets.models import Node
|
2018-10-30 04:06:39 +00:00
|
|
|
fake_node = Node()
|
|
|
|
fake_node.id = self.id
|
|
|
|
fake_node.key = self.id
|
2022-08-11 07:45:03 +00:00
|
|
|
fake_node.value = self.name
|
2018-10-30 04:06:39 +00:00
|
|
|
fake_node.asset = self
|
|
|
|
fake_node.is_node = False
|
|
|
|
return fake_node
|
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
def as_tree_node(self, parent_node):
|
|
|
|
from common.tree import TreeNode
|
|
|
|
icon_skin = 'file'
|
2022-08-11 07:45:03 +00:00
|
|
|
platform_type = self.platform.type.lower()
|
|
|
|
if platform_type == 'windows':
|
2018-12-17 10:20:44 +00:00
|
|
|
icon_skin = 'windows'
|
2022-08-11 07:45:03 +00:00
|
|
|
elif platform_type == 'linux':
|
2018-12-17 10:20:44 +00:00
|
|
|
icon_skin = 'linux'
|
|
|
|
data = {
|
|
|
|
'id': str(self.id),
|
2022-08-11 07:45:03 +00:00
|
|
|
'name': self.name,
|
2018-12-17 10:20:44 +00:00
|
|
|
'title': self.ip,
|
|
|
|
'pId': parent_node.key,
|
|
|
|
'isParent': False,
|
|
|
|
'open': False,
|
|
|
|
'iconSkin': icon_skin,
|
|
|
|
'meta': {
|
|
|
|
'type': 'asset',
|
2021-07-30 07:19:00 +00:00
|
|
|
'data': {
|
2018-12-17 10:20:44 +00:00
|
|
|
'id': self.id,
|
2022-08-11 07:45:03 +00:00
|
|
|
'name': self.name,
|
2018-12-17 10:20:44 +00:00
|
|
|
'ip': self.ip,
|
2022-08-04 02:44:11 +00:00
|
|
|
'protocols': self.protocols,
|
2018-12-17 10:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tree_node = TreeNode(**data)
|
|
|
|
return tree_node
|
|
|
|
|
2016-12-20 16:43:52 +00:00
|
|
|
class Meta:
|
2022-08-11 07:45:03 +00:00
|
|
|
unique_together = [('org_id', 'name')]
|
2018-01-05 09:57:02 +00:00
|
|
|
verbose_name = _("Asset")
|
2022-08-11 07:45:03 +00:00
|
|
|
ordering = ["name", ]
|
2022-02-17 12:13:31 +00:00
|
|
|
permissions = [
|
2022-03-07 06:12:32 +00:00
|
|
|
('refresh_assethardwareinfo', _('Can refresh asset hardware info')),
|
2022-03-02 12:48:43 +00:00
|
|
|
('test_assetconnectivity', _('Can test asset connectivity')),
|
|
|
|
('push_assetsystemuser', _('Can push system user to asset')),
|
2022-03-04 02:16:21 +00:00
|
|
|
('match_asset', _('Can match asset')),
|
2022-03-10 03:25:33 +00:00
|
|
|
('add_assettonode', _('Add asset to node')),
|
|
|
|
('move_assettonode', _('Move asset to node')),
|
2022-02-17 12:13:31 +00:00
|
|
|
]
|