mirror of https://github.com/jumpserver/jumpserver
parent
99f5c02d84
commit
276f644794
|
@ -1,5 +1,5 @@
|
||||||
# ~*~ coding: utf-8 ~*~
|
# ~*~ coding: utf-8 ~*~
|
||||||
|
from django.db.models import F
|
||||||
from django.views.generic.detail import SingleObjectMixin
|
from django.views.generic.detail import SingleObjectMixin
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from rest_framework.views import APIView, Response
|
from rest_framework.views import APIView, Response
|
||||||
|
@ -29,6 +29,7 @@ class DomainViewSet(OrgBulkModelViewSet):
|
||||||
|
|
||||||
|
|
||||||
class GatewayViewSet(OrgBulkModelViewSet):
|
class GatewayViewSet(OrgBulkModelViewSet):
|
||||||
|
perm_model = Host
|
||||||
filterset_fields = ("domain__name", "name", "domain")
|
filterset_fields = ("domain__name", "name", "domain")
|
||||||
search_fields = ("domain__name",)
|
search_fields = ("domain__name",)
|
||||||
serializer_class = serializers.GatewaySerializer
|
serializer_class = serializers.GatewaySerializer
|
||||||
|
|
|
@ -4,12 +4,13 @@ from rest_framework import serializers
|
||||||
from rest_framework.generics import get_object_or_404
|
from rest_framework.generics import get_object_or_404
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer, OrgResourceSerializerMixin
|
||||||
from common.drf.serializers import SecretReadableMixin
|
from common.drf.serializers import SecretReadableMixin, WritableNestedModelSerializer
|
||||||
from common.drf.fields import ObjectRelatedField, EncryptedField
|
from common.drf.fields import ObjectRelatedField, EncryptedField
|
||||||
from assets.const import SecretType
|
from assets.models import Platform, Node
|
||||||
from ..models import Domain, Asset, Account
|
from assets.const import SecretType, GATEWAY_NAME
|
||||||
from ..serializers import HostSerializer
|
from ..serializers import AssetProtocolsSerializer
|
||||||
|
from ..models import Domain, Asset, Account, Host
|
||||||
from .utils import validate_password_for_ansible, validate_ssh_key
|
from .utils import validate_password_for_ansible, validate_ssh_key
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,7 +42,7 @@ class DomainSerializer(BulkOrgResourceModelSerializer):
|
||||||
return obj.gateways.count()
|
return obj.gateways.count()
|
||||||
|
|
||||||
|
|
||||||
class GatewaySerializer(HostSerializer):
|
class GatewaySerializer(BulkOrgResourceModelSerializer, WritableNestedModelSerializer):
|
||||||
password = EncryptedField(
|
password = EncryptedField(
|
||||||
label=_('Password'), required=False, allow_blank=True, allow_null=True, max_length=1024,
|
label=_('Password'), required=False, allow_blank=True, allow_null=True, max_length=1024,
|
||||||
validators=[validate_password_for_ansible], write_only=True
|
validators=[validate_password_for_ansible], write_only=True
|
||||||
|
@ -55,13 +56,27 @@ class GatewaySerializer(HostSerializer):
|
||||||
max_length=512,
|
max_length=512,
|
||||||
)
|
)
|
||||||
username = serializers.CharField(
|
username = serializers.CharField(
|
||||||
label=_('Username'), allow_blank=True, max_length=128, required=True,
|
label=_('Username'), allow_blank=True, max_length=128, required=True, write_only=True
|
||||||
)
|
)
|
||||||
|
username_display = serializers.SerializerMethodField(label=_('Username'))
|
||||||
|
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'))
|
||||||
|
|
||||||
class Meta(HostSerializer.Meta):
|
class Meta:
|
||||||
fields = HostSerializer.Meta.fields + [
|
model = Host
|
||||||
'username', 'password', 'private_key', 'passphrase'
|
fields_mini = ['id', 'name', 'address']
|
||||||
|
fields_small = fields_mini + ['is_active', 'comment']
|
||||||
|
fields = fields_small + ['domain', 'protocols'] + [
|
||||||
|
'username', 'password', 'private_key', 'passphrase', 'username_display'
|
||||||
]
|
]
|
||||||
|
extra_kwargs = {
|
||||||
|
'name': {'label': _("Name")},
|
||||||
|
'address': {'label': _('Address')},
|
||||||
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_username_display(obj):
|
||||||
|
account = obj.accounts.order_by('-privileged').first()
|
||||||
|
return account.username if account else ''
|
||||||
|
|
||||||
def validate_private_key(self, secret):
|
def validate_private_key(self, secret):
|
||||||
if not secret:
|
if not secret:
|
||||||
|
@ -79,6 +94,15 @@ class GatewaySerializer(HostSerializer):
|
||||||
validated_data.pop('passphrase', None)
|
validated_data.pop('passphrase', None)
|
||||||
return username, password, private_key
|
return username, password, private_key
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def generate_default_data():
|
||||||
|
platform = Platform.objects.get(name=GATEWAY_NAME, internal=True)
|
||||||
|
# node = Node.objects.all().order_by('date_created').first()
|
||||||
|
data = {
|
||||||
|
'platform': platform,
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_accounts(instance, username, password, private_key):
|
def create_accounts(instance, username, password, private_key):
|
||||||
account_name = f'{instance.name}-{_("Gateway")}'
|
account_name = f'{instance.name}-{_("Gateway")}'
|
||||||
|
@ -112,6 +136,7 @@ class GatewaySerializer(HostSerializer):
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
auth_fields = self.clean_auth_fields(validated_data)
|
auth_fields = self.clean_auth_fields(validated_data)
|
||||||
|
validated_data.update(self.generate_default_data())
|
||||||
instance = super().create(validated_data)
|
instance = super().create(validated_data)
|
||||||
self.create_accounts(instance, *auth_fields)
|
self.create_accounts(instance, *auth_fields)
|
||||||
return instance
|
return instance
|
||||||
|
|
Loading…
Reference in New Issue