jumpserver/apps/assets/const.py

257 lines
7.3 KiB
Python
Raw Normal View History

2022-04-07 10:51:35 +00:00
from django.db import models
from django.utils.translation import gettext_lazy as _
2022-08-22 03:47:45 +00:00
from common.db.models import IncludesTextChoicesMeta, ChoicesMixin
2022-08-08 02:41:37 +00:00
from common.tree import TreeNode
2022-04-07 10:51:35 +00:00
__all__ = [
2022-08-24 08:14:32 +00:00
'Category', 'HostTypes', 'NetworkingTypes', 'DatabaseTypes',
2022-08-05 11:11:17 +00:00
'WebTypes', 'CloudTypes', 'Protocol', 'AllTypes',
2022-04-07 10:51:35 +00:00
]
2022-04-30 15:19:43 +00:00
class PlatformMixin:
@classmethod
2022-08-10 11:27:08 +00:00
def platform_constraints(cls):
return {
'has_domain': False,
'has_su': False,
'has_ping': False,
'has_change_password': False,
'has_verify_account': False,
'has_create_account': False,
'_protocols': []
}
2022-04-30 15:19:43 +00:00
2022-08-22 03:47:45 +00:00
class Category(PlatformMixin, ChoicesMixin, models.TextChoices):
2022-04-07 10:51:35 +00:00
HOST = 'host', _('Host')
2022-08-24 08:14:32 +00:00
NETWORKING = 'networking', _("NetworkDevice")
2022-04-07 10:51:35 +00:00
DATABASE = 'database', _("Database")
CLOUD = 'cloud', _("Clouding")
2022-08-05 11:11:17 +00:00
WEB = 'web', _("Web")
2022-04-07 10:51:35 +00:00
2022-04-30 15:19:43 +00:00
@classmethod
2022-08-10 11:27:08 +00:00
def platform_constraints(cls) -> dict:
2022-04-30 15:19:43 +00:00
return {
cls.HOST: {
'has_domain': True,
2022-08-10 11:27:08 +00:00
'has_ping': True,
'has_verify_account': True,
'has_change_password': True,
'has_create_account': True,
'_protocols': ['ssh', 'telnet']
2022-04-30 15:19:43 +00:00
},
2022-08-24 08:14:32 +00:00
cls.NETWORKING: {
2022-04-30 15:19:43 +00:00
'has_domain': True,
2022-08-10 11:27:08 +00:00
'_protocols': ['ssh', 'telnet']
2022-04-30 15:19:43 +00:00
},
cls.DATABASE: {
2022-08-10 11:27:08 +00:00
'has_domain': True,
2022-04-30 15:19:43 +00:00
},
2022-08-05 11:11:17 +00:00
cls.WEB: {
'has_domain': False,
2022-08-10 11:27:08 +00:00
'_protocols': []
2022-04-30 15:19:43 +00:00
},
cls.CLOUD: {
2022-05-05 08:18:05 +00:00
'has_domain': False,
2022-08-10 11:27:08 +00:00
'_protocols': []
2022-04-30 15:19:43 +00:00
}
}
2022-08-22 03:47:45 +00:00
class HostTypes(PlatformMixin, ChoicesMixin, models.TextChoices):
2022-04-07 10:51:35 +00:00
LINUX = 'linux', 'Linux'
WINDOWS = 'windows', 'Windows'
UNIX = 'unix', 'Unix'
BSD = 'bsd', 'BSD'
MACOS = 'macos', 'MacOS'
MAINFRAME = 'mainframe', _("Mainframe")
OTHER_HOST = 'other_host', _("Other host")
2022-04-30 15:19:43 +00:00
@classmethod
2022-08-10 11:27:08 +00:00
def platform_constraints(cls):
return {
cls.LINUX: {
'_protocols': ['ssh', 'rdp', 'vnc', 'telnet']
},
cls.WINDOWS: {
2022-08-30 06:13:33 +00:00
'_protocols': ['ssh', 'rdp', 'vnc'],
'has_su': False
2022-08-10 11:27:08 +00:00
},
cls.MACOS: {
'_protocols': ['ssh', 'vnc']
}
2022-04-30 15:19:43 +00:00
}
2022-08-24 08:14:32 +00:00
class NetworkingTypes(PlatformMixin, ChoicesMixin, models.TextChoices):
2022-04-07 10:51:35 +00:00
SWITCH = 'switch', _("Switch")
ROUTER = 'router', _("Router")
FIREWALL = 'firewall', _("Firewall")
OTHER_NETWORK = 'other_network', _("Other device")
2022-08-22 03:47:45 +00:00
class DatabaseTypes(PlatformMixin, ChoicesMixin, models.TextChoices):
2022-04-07 10:51:35 +00:00
MYSQL = 'mysql', 'MySQL'
MARIADB = 'mariadb', 'MariaDB'
POSTGRESQL = 'postgresql', 'PostgreSQL'
ORACLE = 'oracle', 'Oracle'
SQLSERVER = 'sqlserver', 'SQLServer'
MONGODB = 'mongodb', 'MongoDB'
REDIS = 'redis', 'Redis'
2022-04-30 15:19:43 +00:00
@classmethod
2022-08-10 11:27:08 +00:00
def platform_constraints(cls):
2022-04-30 15:19:43 +00:00
meta = {}
2022-05-05 08:18:05 +00:00
for name, label in cls.choices:
2022-04-30 15:19:43 +00:00
meta[name] = {
2022-08-10 11:27:08 +00:00
'protocols': [name]
2022-04-30 15:19:43 +00:00
}
2022-05-02 13:37:42 +00:00
return meta
2022-04-07 10:51:35 +00:00
2022-04-30 15:19:43 +00:00
2022-08-22 03:47:45 +00:00
class WebTypes(PlatformMixin, ChoicesMixin, models.TextChoices):
2022-08-05 11:11:17 +00:00
General = 'general', 'General'
2022-04-07 10:51:35 +00:00
2022-08-22 03:47:45 +00:00
class CloudTypes(PlatformMixin, ChoicesMixin, models.TextChoices):
2022-04-07 10:51:35 +00:00
K8S = 'k8s', 'Kubernetes'
2022-08-22 03:47:45 +00:00
class AllTypes(ChoicesMixin, metaclass=IncludesTextChoicesMeta):
2022-04-07 10:51:35 +00:00
choices: list
includes = [
2022-08-24 08:14:32 +00:00
HostTypes, NetworkingTypes, DatabaseTypes,
2022-08-05 11:11:17 +00:00
WebTypes, CloudTypes
2022-04-07 10:51:35 +00:00
]
2022-05-05 08:18:05 +00:00
@classmethod
2022-08-10 11:27:08 +00:00
def get_constraints(cls, category, tp):
constraints = PlatformMixin.platform_constraints()
category_constraints = Category.platform_constraints().get(category) or {}
constraints.update(category_constraints)
2022-05-05 08:18:05 +00:00
types_cls = dict(cls.category_types()).get(category)
if not types_cls:
2022-08-10 11:27:08 +00:00
return constraints
type_constraints = types_cls.platform_constraints().get(tp) or {}
constraints.update(type_constraints)
2022-05-05 08:18:05 +00:00
2022-08-10 11:27:08 +00:00
_protocols = constraints.pop('_protocols', [])
2022-05-05 08:18:05 +00:00
default_ports = Protocol.default_ports()
2022-08-10 11:27:08 +00:00
protocols = []
for p in _protocols:
2022-05-05 08:18:05 +00:00
port = default_ports.get(p, 0)
2022-08-10 11:27:08 +00:00
protocols.append({'name': p, 'port': port})
constraints['protocols'] = protocols
return constraints
2022-05-05 08:18:05 +00:00
2022-04-30 15:19:43 +00:00
@classmethod
def category_types(cls):
return (
(Category.HOST, HostTypes),
2022-08-24 08:14:32 +00:00
(Category.NETWORKING, NetworkingTypes),
2022-04-30 15:19:43 +00:00
(Category.DATABASE, DatabaseTypes),
2022-08-05 11:11:17 +00:00
(Category.WEB, WebTypes),
2022-04-30 15:19:43 +00:00
(Category.CLOUD, CloudTypes)
)
2022-04-28 14:54:18 +00:00
@classmethod
def grouped_choices(cls):
2022-04-30 15:19:43 +00:00
grouped_types = [(str(ca), tp.choices) for ca, tp in cls.category_types()]
2022-04-28 14:54:18 +00:00
return grouped_types
@classmethod
def grouped_choices_to_objs(cls):
choices = cls.serialize_to_objs(Category.choices)
mapper = dict(cls.grouped_choices())
for choice in choices:
children = cls.serialize_to_objs(mapper[choice['value']])
choice['children'] = children
return choices
@staticmethod
def serialize_to_objs(choices):
title = ['value', 'display_name']
return [dict(zip(title, choice)) for choice in choices]
2022-08-08 02:41:37 +00:00
@staticmethod
def choice_to_node(choice, pid, opened=True, is_parent=True, meta=None):
node = TreeNode(**{
'id': choice.name,
'name': choice.label,
'title': choice.label,
'pId': pid,
'open': opened,
'isParent': is_parent,
})
if meta:
node.meta = meta
return node
@classmethod
def to_tree_nodes(cls):
root = TreeNode(id='ROOT', name='类型节点', title='类型节点')
nodes = [root]
for category, types in cls.category_types():
category_node = cls.choice_to_node(category, 'ROOT', meta={'type': 'category'})
nodes.append(category_node)
for tp in types:
tp_node = cls.choice_to_node(tp, category_node.id, meta={'type': 'type'})
nodes.append(tp_node)
return nodes
2022-04-28 14:54:18 +00:00
2022-08-22 03:47:45 +00:00
class Protocol(ChoicesMixin, models.TextChoices):
2022-04-07 10:51:35 +00:00
ssh = 'ssh', 'SSH'
rdp = 'rdp', 'RDP'
telnet = 'telnet', 'Telnet'
vnc = 'vnc', 'VNC'
mysql = 'mysql', 'MySQL'
mariadb = 'mariadb', 'MariaDB'
oracle = 'oracle', 'Oracle'
postgresql = 'postgresql', 'PostgreSQL'
sqlserver = 'sqlserver', 'SQLServer'
redis = 'redis', 'Redis'
mongodb = 'mongodb', 'MongoDB'
k8s = 'k8s', 'K8S'
2022-08-24 08:14:32 +00:00
http = 'http', 'HTTP'
https = 'https', 'HTTPS'
2022-04-07 10:51:35 +00:00
@classmethod
def host_protocols(cls):
return [cls.ssh, cls.rdp, cls.telnet, cls.vnc]
@classmethod
def db_protocols(cls):
return [
cls.mysql, cls.mariadb, cls.postgresql, cls.oracle,
cls.sqlserver, cls.redis, cls.mongodb,
]
2022-04-30 15:19:43 +00:00
@classmethod
def default_ports(cls):
return {
cls.ssh: 22,
cls.rdp: 3389,
cls.vnc: 5900,
cls.telnet: 21,
cls.mysql: 3306,
cls.mariadb: 3306,
cls.postgresql: 5432,
cls.oracle: 1521,
cls.sqlserver: 1433,
cls.mongodb: 27017,
cls.redis: 6379,
2022-08-24 08:14:32 +00:00
cls.k8s: 0,
cls.http: 80,
cls.https: 443
2022-04-30 15:19:43 +00:00
}