jumpserver/apps/assets/models/asset.py

329 lines
11 KiB
Python
Raw Normal View History

2016-12-20 16:43:52 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
2017-11-23 06:08:01 +00:00
import uuid
import logging
2018-05-28 07:00:06 +00:00
import random
2018-05-31 11:47:57 +00:00
from functools import reduce
from collections import OrderedDict
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 _
2019-06-24 06:23:29 +00:00
from .utils import Connectivity
Dev beta (#3048) * [Update] 统一url地址 * [Update] 修改api * [Update] 使用规范的签名 * [Update] 修改url * [Update] 修改swagger * [Update] 添加serializer class避免报错 * [Update] 修改token * [Update] 支持api key * [Update] 支持生成api key * [Update] 修改api重定向 * [Update] 修改翻译 * [Update] 添加说明文档 * [Update] 修复浏览器关闭后session不失效的问题 * [Update] 修改一些内容 * [Update] 修改 jms脚本 * [Update] 修改重定向 * [Update] 修改搜索trim * [Update] 修改搜索trim * [Update] 添加sys log * [Bugfix] 修改登陆错误 * [Update] 优化User操作private_token的接口 (#3091) * [Update] 优化User操作private_token的接口 * [Update] 优化User操作private_token的接口 2 * [Bugfix] 解决授权了一个节点,当移动节点后,被移动的节点下的资产会放到未分组节点下的问题 * [Update] 升级jquery * [Update] 默认使用page * [Update] 修改使用Orgmodel view set * [Update] 支持 nv的硬盘 https://github.com/jumpserver/jumpserver/issues/1804 * [UPdate] 解决命令执行宽度问题 * [Update] 优化节点 * [Update] 修改nodes过多时创建比较麻烦 * [Update] 修改导入 * [Update] 节点获取更新 * [Update] 修改nodes * [Update] nodes显示full value * [Update] 统一使用nodes select2 函数 * [Update] 修改磁盘大小小数 * [Update] 修改 Node service * [Update] 优化授权节点 * [Update] 修改 node permission * [Update] 修改asset permission * [Stash] * [Update] 修改node assets api * [Update] 修改tree service,支持资产数量 * [Update] 修改暂时完成 * [Update] 修改一些bug
2019-08-21 12:27:21 +00:00
from orgs.mixins.models import OrgModelMixin, OrgManager
2016-12-20 16:43:52 +00:00
__all__ = ['Asset', 'ProtocolsMixin']
2016-12-20 16:43:52 +00:00
logger = logging.getLogger(__name__)
2018-01-09 15:33:14 +00:00
def default_cluster():
from .cluster import Cluster
name = "Default"
defaults = {"name": name}
cluster, created = Cluster.objects.get_or_create(
defaults=defaults, name=name
)
2018-01-10 04:03:35 +00:00
return cluster.id
2018-01-09 15:33:14 +00:00
def default_node():
try:
2018-02-09 07:24:44 +00:00
from .node import Node
root = Node.org_root()
2018-10-16 04:37:42 +00:00
return root
except:
return None
2018-07-13 07:05:46 +00:00
class AssetQuerySet(models.QuerySet):
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)
2019-07-05 10:07:10 +00:00
class ProtocolsMixin:
protocols = ''
PROTOCOL_SSH = 'ssh'
PROTOCOL_RDP = 'rdp'
PROTOCOL_TELNET = 'telnet'
PROTOCOL_VNC = 'vnc'
PROTOCOL_CHOICES = (
(PROTOCOL_SSH, 'ssh'),
(PROTOCOL_RDP, 'rdp'),
(PROTOCOL_TELNET, 'telnet (beta)'),
(PROTOCOL_VNC, 'vnc'),
)
2019-07-05 10:07:10 +00:00
@property
def protocols_as_list(self):
if not self.protocols:
return []
return self.protocols.split(' ')
2019-07-05 10:07:10 +00:00
@property
def protocols_as_dict(self):
d = OrderedDict()
protocols = self.protocols_as_list
for i in protocols:
if '/' not in i:
continue
name, port = i.split('/')[:2]
if not all([name, port]):
continue
d[name] = int(port)
return d
@property
def protocols_as_json(self):
return [
{"name": name, "port": port}
for name, port in self.protocols_as_dict.items()
]
def has_protocol(self, name):
return name in self.protocols_as_dict
@property
def ssh_port(self):
return self.protocols_as_dict.get("ssh", 22)
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):
from .node import Node
nodes = self.nodes.all()
if not nodes:
nodes = Node.objects.filter(id=Node.org_root().id)
return nodes
def get_all_nodes(self, flat=False):
nodes = []
for node in self.get_nodes():
_nodes = node.get_ancestor(with_self=True)
nodes.append(_nodes)
if flat:
nodes = list(reduce(lambda x, y: set(x) | set(y), nodes))
return nodes
class Asset(ProtocolsMixin, NodesRelationMixin, OrgModelMixin):
# Important
PLATFORM_CHOICES = (
('Linux', 'Linux'),
('Unix', 'Unix'),
('MacOS', 'MacOS'),
('BSD', 'BSD'),
('Windows', 'Windows'),
('Windows2016', 'Windows(2016)'),
('Other', 'Other'),
)
2017-11-23 06:08:01 +00:00
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
ip = models.CharField(max_length=128, verbose_name=_('IP'), db_index=True)
2018-07-15 10:39:11 +00:00
hostname = models.CharField(max_length=128, verbose_name=_('Hostname'))
2019-07-05 10:07:10 +00:00
protocol = models.CharField(max_length=128, default=ProtocolsMixin.PROTOCOL_SSH,
choices=ProtocolsMixin.PROTOCOL_CHOICES,
verbose_name=_('Protocol'))
2016-12-20 16:43:52 +00:00
port = models.IntegerField(default=22, verbose_name=_('Port'))
2019-07-05 10:28:56 +00:00
protocols = models.CharField(max_length=128, default='ssh/22', blank=True, verbose_name=_("Protocols"))
2018-07-15 10:39:11 +00:00
platform = models.CharField(max_length=128, choices=PLATFORM_CHOICES, default='Linux', verbose_name=_('Platform'))
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'))
# Auth
admin_user = models.ForeignKey('assets.AdminUser', on_delete=models.PROTECT, null=True, verbose_name=_("Admin user"), related_name='assets')
2017-03-15 16:19:47 +00:00
# Some information
public_ip = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Public IP'))
2018-07-15 10:39:11 +00:00
number = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Asset number'))
2017-03-15 16:19:47 +00:00
# Collect
vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor'))
model = models.CharField(max_length=54, null=True, blank=True, verbose_name=_('Model'))
sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number'))
cpu_model = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU model'))
2017-03-15 16:19:47 +00:00
cpu_count = models.IntegerField(null=True, verbose_name=_('CPU count'))
cpu_cores = models.IntegerField(null=True, verbose_name=_('CPU cores'))
2018-08-15 04:00:47 +00:00
cpu_vcpus = models.IntegerField(null=True, verbose_name=_('CPU vcpus'))
memory = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Memory'))
disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total'))
disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info'))
os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS'))
os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version'))
os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch'))
hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw'))
labels = models.ManyToManyField('assets.Label', blank=True, related_name='assets', verbose_name=_("Labels"))
created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by'))
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date created'))
comment = models.TextField(max_length=128, default='', blank=True, verbose_name=_('Comment'))
2016-12-20 16:43:52 +00:00
2019-06-27 13:43:10 +00:00
objects = OrgManager.from_queryset(AssetQuerySet)()
2019-07-04 13:17:39 +00:00
_connectivity = None
2017-11-01 15:23:11 +00:00
def __str__(self):
return '{0.hostname}({0.ip})'.format(self)
2016-12-20 17:03:52 +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
def is_windows(self):
if self.platform in ("Windows", "Windows2016"):
return True
else:
return False
def is_unixlike(self):
if self.platform not in ("Windows", "Windows2016", "Other"):
return True
else:
return False
def is_support_ansible(self):
return self.has_protocol('ssh') and self.platform not in ("Other",)
@property
def cpu_info(self):
info = ""
if self.cpu_model:
info += self.cpu_model
if self.cpu_count and self.cpu_cores:
info += "{}*{}".format(self.cpu_count, self.cpu_cores)
return info
@property
2017-12-12 04:19:45 +00:00
def hardware_info(self):
if self.cpu_count:
return '{} Core {} {}'.format(
2018-08-15 04:00:47 +00:00
self.cpu_vcpus or self.cpu_count * self.cpu_cores,
2017-12-12 04:19:45 +00:00
self.memory, self.disk_total
)
else:
return ''
@property
def connectivity(self):
2019-07-04 13:17:39 +00:00
if self._connectivity:
return self._connectivity
if not self.admin_user:
2019-06-21 12:57:51 +00:00
return Connectivity.unknown()
2019-07-04 13:17:39 +00:00
connectivity = self.admin_user.get_asset_connectivity(self)
return connectivity
@connectivity.setter
def connectivity(self, value):
if not self.admin_user:
return
2019-07-04 13:17:39 +00:00
self.admin_user.set_asset_connectivity(self, value)
2016-12-20 16:43:52 +00:00
def get_auth_info(self):
if not self.admin_user:
return {}
self.admin_user.load_specific_asset_auth(self)
info = {
'username': self.admin_user.username,
'password': self.admin_user.password,
'private_key': self.admin_user.private_key_file,
}
return info
2018-10-30 04:06:39 +00:00
def as_node(self):
from .node import Node
fake_node = Node()
fake_node.id = self.id
fake_node.key = self.id
fake_node.value = self.hostname
fake_node.asset = self
fake_node.is_node = False
return fake_node
def as_tree_node(self, parent_node):
from common.tree import TreeNode
icon_skin = 'file'
if self.platform.lower() == 'windows':
icon_skin = 'windows'
elif self.platform.lower() == 'linux':
icon_skin = 'linux'
data = {
'id': str(self.id),
'name': self.hostname,
'title': self.ip,
'pId': parent_node.key,
'isParent': False,
'open': False,
'iconSkin': icon_skin,
'meta': {
'type': 'asset',
'asset': {
'id': self.id,
'hostname': self.hostname,
'ip': self.ip,
2019-07-05 10:07:10 +00:00
'protocols': self.protocols_as_list,
'platform': self.platform,
}
}
}
tree_node = TreeNode(**data)
return tree_node
2016-12-20 16:43:52 +00:00
class Meta:
2018-07-20 02:54:16 +00:00
unique_together = [('org_id', 'hostname')]
2018-01-05 09:57:02 +00:00
verbose_name = _("Asset")
2016-12-20 16:43:52 +00:00
@classmethod
def generate_fake(cls, count=100):
from .user import AdminUser, SystemUser
2016-12-20 16:43:52 +00:00
from random import seed, choice
from django.db import IntegrityError
from .node import Node
2019-06-27 13:43:10 +00:00
from orgs.utils import get_current_org
from orgs.models import Organization
org = get_current_org()
if not org or not org.is_real():
Organization.default().change_to()
nodes = list(Node.objects.all())
2016-12-20 16:43:52 +00:00
seed()
for i in range(count):
2018-05-28 07:00:06 +00:00
ip = [str(i) for i in random.sample(range(255), 4)]
asset = cls(ip='.'.join(ip),
2019-06-27 13:43:10 +00:00
hostname='.'.join(ip),
2016-12-20 16:43:52 +00:00
admin_user=choice(AdminUser.objects.all()),
created_by='Fake')
try:
asset.save()
2019-07-05 10:07:10 +00:00
asset.protocols = 'ssh/22'
if nodes and len(nodes) > 3:
_nodes = random.sample(nodes, 3)
else:
_nodes = [Node.default_node()]
asset.nodes.set(_nodes)
2016-12-20 16:43:52 +00:00
logger.debug('Generate fake asset : %s' % asset.ip)
except IntegrityError:
print('Error continue')
continue