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
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
import json
|
2022-11-22 09:33:09 +00:00
|
|
|
import logging
|
2022-10-09 12:54:11 +00:00
|
|
|
from collections import defaultdict
|
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 _
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from assets import const
|
2023-01-31 05:03:45 +00:00
|
|
|
from common.db.fields import EncryptMixin
|
2023-01-31 09:46:56 +00:00
|
|
|
from common.utils import lazyproperty
|
2022-08-11 07:45:03 +00:00
|
|
|
from orgs.mixins.models import OrgManager, JMSOrgBaseModel
|
2022-04-02 10:35:46 +00:00
|
|
|
from ..base import AbsConnectivity
|
2022-12-20 12:23:42 +00:00
|
|
|
from ..platform import Platform
|
2021-07-08 06:23:18 +00:00
|
|
|
|
2022-09-19 12:11:55 +00:00
|
|
|
__all__ = ['Asset', 'AssetQuerySet', 'default_node', 'Protocol']
|
2016-12-20 16:43:52 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-02-08 03:34:21 +00:00
|
|
|
def default_node():
|
2023-02-19 07:43:56 +00:00
|
|
|
return []
|
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)
|
|
|
|
|
2022-10-09 12:54:11 +00:00
|
|
|
def group_by_platform(self) -> dict:
|
|
|
|
groups = defaultdict(list)
|
|
|
|
for asset in self.all():
|
|
|
|
groups[asset.platform].append(asset)
|
|
|
|
return groups
|
|
|
|
|
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
|
2022-12-20 12:39:48 +00:00
|
|
|
id: str
|
2019-07-11 10:12:14 +00:00
|
|
|
_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
|
|
|
|
|
2022-12-09 05:13:02 +00:00
|
|
|
def get_all_nodes(self, flat=False):
|
2022-09-15 02:46:57 +00:00
|
|
|
from ..node import Node
|
2022-12-09 05:13:02 +00:00
|
|
|
node_keys = self.get_all_node_keys()
|
2022-09-15 02:46:57 +00:00
|
|
|
nodes = Node.objects.filter(key__in=node_keys).distinct()
|
2022-12-08 11:30:16 +00:00
|
|
|
if not flat:
|
2022-09-15 02:46:57 +00:00
|
|
|
return nodes
|
2022-12-08 11:30:16 +00:00
|
|
|
node_ids = set(nodes.values_list('id', flat=True))
|
|
|
|
return node_ids
|
|
|
|
|
2022-12-09 05:13:02 +00:00
|
|
|
def get_all_node_keys(self):
|
|
|
|
node_keys = set()
|
|
|
|
for node in self.get_nodes():
|
|
|
|
ancestor_keys = node.get_ancestor_keys(with_self=True)
|
|
|
|
node_keys.update(ancestor_keys)
|
|
|
|
return node_keys
|
|
|
|
|
2022-12-08 11:30:16 +00:00
|
|
|
@classmethod
|
|
|
|
def get_all_nodes_for_assets(cls, assets):
|
|
|
|
from ..node import Node
|
|
|
|
node_keys = set()
|
|
|
|
for asset in assets:
|
2022-12-09 05:13:02 +00:00
|
|
|
asset_node_keys = asset.get_all_node_keys()
|
2022-12-08 11:30:16 +00:00
|
|
|
node_keys.update(asset_node_keys)
|
|
|
|
nodes = Node.objects.filter(key__in=node_keys)
|
|
|
|
return nodes
|
2019-07-11 10:12:14 +00:00
|
|
|
|
|
|
|
|
2022-09-19 12:11:55 +00:00
|
|
|
class Protocol(models.Model):
|
|
|
|
name = models.CharField(max_length=32, verbose_name=_("Name"))
|
|
|
|
port = models.IntegerField(verbose_name=_("Port"))
|
|
|
|
asset = models.ForeignKey('Asset', on_delete=models.CASCADE, related_name='protocols', verbose_name=_("Asset"))
|
|
|
|
|
2022-09-20 05:54:25 +00:00
|
|
|
def __str__(self):
|
|
|
|
return '{}/{}'.format(self.name, self.port)
|
|
|
|
|
2022-09-19 12:11:55 +00:00
|
|
|
|
2022-10-22 03:17:02 +00:00
|
|
|
class Asset(NodesRelationMixin, AbsConnectivity, JMSOrgBaseModel):
|
2023-01-16 11:02:09 +00:00
|
|
|
Category = const.Category
|
|
|
|
Type = const.AllTypes
|
|
|
|
|
2022-09-07 09:12:53 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
2023-02-27 05:32:15 +00:00
|
|
|
address = models.CharField(max_length=767, verbose_name=_('Address'), db_index=True)
|
2022-10-25 12:09:05 +00:00
|
|
|
platform = models.ForeignKey(Platform, on_delete=models.PROTECT, verbose_name=_("Platform"), related_name='assets')
|
2022-04-02 10:35:46 +00:00
|
|
|
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"))
|
2023-04-10 02:57:44 +00:00
|
|
|
gathered_info = models.JSONField(verbose_name=_('Gathered info'), default=dict, blank=True) # 资产的一些信息,如 硬件信息
|
|
|
|
custom_info = models.JSONField(verbose_name=_('Custom info'), default=dict)
|
2022-10-22 03:17:02 +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-09-22 07:07:03 +00:00
|
|
|
return '{0.name}({0.address})'.format(self)
|
2016-12-20 17:03:52 +00:00
|
|
|
|
2023-01-31 09:46:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_spec_values(instance, fields):
|
2023-01-16 11:02:09 +00:00
|
|
|
info = {}
|
2023-01-31 09:46:56 +00:00
|
|
|
for i in fields:
|
2023-01-16 11:02:09 +00:00
|
|
|
v = getattr(instance, i.name)
|
|
|
|
if isinstance(i, models.JSONField) and not isinstance(v, (list, dict)):
|
|
|
|
v = json.loads(v)
|
|
|
|
info[i.name] = v
|
|
|
|
return info
|
|
|
|
|
2023-01-31 09:46:56 +00:00
|
|
|
@lazyproperty
|
2023-01-16 11:02:09 +00:00
|
|
|
def spec_info(self):
|
|
|
|
instance = getattr(self, self.category, None)
|
|
|
|
if not instance:
|
2023-01-31 09:46:56 +00:00
|
|
|
return {}
|
|
|
|
spec_fields = self.get_spec_fields(instance)
|
|
|
|
return self.get_spec_values(instance, spec_fields)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_spec_fields(instance, secret=False):
|
|
|
|
spec_fields = [i for i in instance._meta.local_fields if i.name != 'asset_ptr']
|
|
|
|
spec_fields = [i for i in spec_fields if isinstance(i, EncryptMixin) == secret]
|
|
|
|
return spec_fields
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def secret_info(self):
|
|
|
|
instance = getattr(self, self.category, None)
|
|
|
|
if not instance:
|
|
|
|
return {}
|
|
|
|
spec_fields = self.get_spec_fields(instance, secret=True)
|
|
|
|
return self.get_spec_values(instance, spec_fields)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
2023-04-10 03:23:21 +00:00
|
|
|
@lazyproperty
|
|
|
|
def info(self):
|
|
|
|
info = {}
|
|
|
|
info.update(self.gathered_info or {})
|
|
|
|
info.update(self.custom_info or {})
|
|
|
|
info.update(self.spec_info or {})
|
|
|
|
return info
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
@lazyproperty
|
2023-04-10 02:57:44 +00:00
|
|
|
def auto_config(self):
|
2023-01-16 11:02:09 +00:00
|
|
|
platform = self.platform
|
|
|
|
automation = self.platform.automation
|
2023-04-10 02:57:44 +00:00
|
|
|
auto_config = {
|
2023-01-16 11:02:09 +00:00
|
|
|
'su_enabled': platform.su_enabled,
|
|
|
|
'domain_enabled': platform.domain_enabled,
|
2023-04-10 02:57:44 +00:00
|
|
|
'ansible_enabled': False
|
|
|
|
}
|
|
|
|
if not automation:
|
|
|
|
return auto_config
|
|
|
|
auto_config.update({
|
|
|
|
'ping_enabled': automation.ping_enabled,
|
2023-01-16 11:02:09 +00:00
|
|
|
'ansible_enabled': automation.ansible_enabled,
|
2023-02-23 10:55:15 +00:00
|
|
|
'push_account_enabled': automation.push_account_enabled,
|
2023-01-16 11:02:09 +00:00
|
|
|
'gather_facts_enabled': automation.gather_facts_enabled,
|
|
|
|
'change_secret_enabled': automation.change_secret_enabled,
|
|
|
|
'verify_account_enabled': automation.verify_account_enabled,
|
|
|
|
'gather_accounts_enabled': automation.gather_accounts_enabled,
|
2023-04-10 02:57:44 +00:00
|
|
|
})
|
|
|
|
return auto_config
|
2023-01-16 11:02:09 +00:00
|
|
|
|
2022-04-12 09:45:10 +00:00
|
|
|
def get_target_ip(self):
|
2022-09-21 03:17:14 +00:00
|
|
|
return self.address
|
2021-07-08 06:23:18 +00:00
|
|
|
|
2022-09-14 07:51:04 +00:00
|
|
|
def get_target_ssh_port(self):
|
|
|
|
protocol = self.protocols.all().filter(name='ssh').first()
|
|
|
|
return protocol.port if protocol else 22
|
|
|
|
|
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-22 10:32:33 +00:00
|
|
|
@lazyproperty
|
2022-08-11 07:45:03 +00:00
|
|
|
def type(self):
|
|
|
|
return self.platform.type
|
|
|
|
|
2022-08-22 10:32:33 +00:00
|
|
|
@lazyproperty
|
2022-08-11 07:45:03 +00:00
|
|
|
def category(self):
|
|
|
|
return self.platform.category
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
def is_category(self, category):
|
|
|
|
return self.category == category
|
|
|
|
|
|
|
|
def is_type(self, tp):
|
|
|
|
return self.type == tp
|
|
|
|
|
2023-01-18 09:14:02 +00:00
|
|
|
@property
|
|
|
|
def is_gateway(self):
|
|
|
|
return self.platform.name == const.GATEWAY_NAME
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
@lazyproperty
|
|
|
|
def gateway(self):
|
2023-02-16 08:51:42 +00:00
|
|
|
if not self.domain_id:
|
|
|
|
return
|
|
|
|
if not self.platform.domain_enabled:
|
|
|
|
return
|
|
|
|
return self.domain.select_gateway()
|
2023-01-16 11:02:09 +00:00
|
|
|
|
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,
|
2022-09-21 03:17:14 +00:00
|
|
|
'title': self.address,
|
2018-12-17 10:20:44 +00:00
|
|
|
'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,
|
2022-09-21 03:17:14 +00:00
|
|
|
'address': self.address,
|
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
|
|
|
|
|
2023-04-12 09:59:13 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_secret_type_assets(asset_ids, secret_type):
|
|
|
|
assets = Asset.objects.filter(id__in=asset_ids)
|
|
|
|
asset_protocol = assets.prefetch_related('protocols').values_list('id', 'protocols__name')
|
|
|
|
protocol_secret_types_map = const.Protocol.protocol_secret_types()
|
|
|
|
asset_secret_types_mapp = defaultdict(set)
|
|
|
|
|
|
|
|
for asset_id, protocol in asset_protocol:
|
|
|
|
secret_types = set(protocol_secret_types_map.get(protocol, []))
|
|
|
|
asset_secret_types_mapp[asset_id].update(secret_types)
|
|
|
|
|
|
|
|
return [
|
|
|
|
asset for asset in assets
|
|
|
|
if secret_type in asset_secret_types_mapp.get(asset.id, [])
|
|
|
|
]
|
|
|
|
|
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')),
|
2022-03-04 02:16:21 +00:00
|
|
|
('match_asset', _('Can match asset')),
|
2023-02-21 05:11:56 +00:00
|
|
|
('change_assetnodes', _('Can change asset nodes')),
|
2022-02-17 12:13:31 +00:00
|
|
|
]
|