Browse Source

perf: 创建内置 platforms

pull/8931/head
ibuler 2 years ago
parent
commit
a35e0c5efa
  1. 14
      apps/assets/const/host.py
  2. 47
      apps/assets/const/types.py

14
apps/assets/const/host.py

@ -16,14 +16,8 @@ class HostTypes(BaseType):
'domain_enabled': True,
'su_enabled': True,
'su_methods': [
{
'name': 'sudo su',
'id': 'sudo su'
},
{
'name': 'su -',
'id': 'su -'
}
{'name': 'sudo su', 'id': 'sudo su'},
{'name': 'su -', 'id': 'su -'}
],
},
cls.WINDOWS: {
@ -72,10 +66,10 @@ class HostTypes(BaseType):
],
cls.WINDOWS: [
{'name': 'Windows'},
{'name': 'Windows-TLS', 'protocol_settings': {
{'name': 'Windows-TLS', 'protocols_setting': {
'rdp': {'security': 'tls'},
}},
{'name': 'Windows-RDP', 'protocol_settings': {
{'name': 'Windows-RDP', 'protocols_setting': {
'rdp': {'security': 'rdp'},
}}
],

47
apps/assets/const/types.py

@ -1,3 +1,5 @@
from copy import deepcopy
from common.db.models import IncludesTextChoicesMeta, ChoicesMixin
from common.tree import TreeNode
@ -161,14 +163,47 @@ class AllTypes(ChoicesMixin, metaclass=IncludesTextChoicesMeta):
@classmethod
def create_or_update_internal_platforms(cls):
from assets.models import Platform
from assets.models import Platform, PlatformAutomation, PlatformProtocol
print("Create internal platforms")
for category, type_cls in cls.category_types():
print("## Category: {}".format(category.label))
data = type_cls.internal_platforms()
for tp, platform_datas in data.items():
for d in platform_datas:
platform_data = {
**d, 'category': category.value, 'type': tp.value,
'internal': True, 'charset': 'utf-8'
}
print(" >> Type: {}".format(tp.label))
default_platform_data = cls.get_type_default_platform(category, tp)
default_automation = default_platform_data.pop('automation', {})
default_protocols = default_platform_data.pop('protocols', [])
for d in platform_datas:
name = d['name']
print(" - Platform: {}".format(name))
_automation = d.pop('automation', {})
_protocols = d.pop('_protocols', [])
_protocols_setting = d.pop('protocols_setting', {})
protocols_data = deepcopy(default_protocols)
if _protocols:
protocols_data = [p for p in protocols_data if p['name'] in _protocols]
for p in protocols_data:
p['setting'] = {**_protocols_setting.get(p['name'], {}), **p.get('setting', {})}
platform_data = {**default_platform_data, **d}
automation_data = {**default_automation, **_automation}
platform, created = Platform.objects.update_or_create(
defaults=platform_data, name=platform_data['name']
)
if not platform.automation:
automation = PlatformAutomation.objects.create()
platform.automation = automation
platform.save()
else:
automation = platform.automation
for k, v in automation_data.items():
setattr(automation, k, v)
automation.save()
platform.protocols.all().delete()
[PlatformProtocol.objects.create(**p, platform=platform) for p in protocols_data]

Loading…
Cancel
Save