perf: account specific

pull/9013/head
feng 2022-11-03 12:42:57 +08:00
parent 0fdc30bed3
commit 7087d5a74e
2 changed files with 22 additions and 12 deletions

View File

@ -15,7 +15,7 @@ from django.db.models import QuerySet
from common.db import fields from common.db import fields
from common.utils import ( from common.utils import (
ssh_key_string_to_obj, ssh_key_gen, get_logger, ssh_key_string_to_obj, ssh_key_gen, get_logger,
random_string, ssh_pubkey_gen, random_string, ssh_pubkey_gen, lazyproperty
) )
from assets.const import Connectivity, SecretType from assets.const import Connectivity, SecretType
from orgs.mixins.models import OrgModelMixin from orgs.mixins.models import OrgModelMixin
@ -61,36 +61,42 @@ class BaseAccount(OrgModelMixin):
date_updated = models.DateTimeField(auto_now=True, verbose_name=_("Date updated")) date_updated = models.DateTimeField(auto_now=True, verbose_name=_("Date updated"))
created_by = models.CharField(max_length=128, null=True, verbose_name=_('Created by')) created_by = models.CharField(max_length=128, null=True, verbose_name=_('Created by'))
@property
def password(self):
return self.secret
@property @property
def has_secret(self): def has_secret(self):
return bool(self.secret) return bool(self.secret)
@property
def specific(self):
data = {}
if self.secret_type != SecretType.ssh_key:
return data
data['ssh_key_fingerprint'] = self.ssh_key_fingerprint
return data
@property @property
def private_key(self): def private_key(self):
if self.secret_type == SecretType.ssh_key: if self.secret_type == SecretType.ssh_key:
return self.secret return self.secret
return None return None
@property
def public_key(self):
return ''
@private_key.setter @private_key.setter
def private_key(self, value): def private_key(self, value):
self.secret = value self.secret = value
self.secret_type = SecretType.ssh_key self.secret_type = SecretType.ssh_key
@lazyproperty
def public_key(self):
if self.secret_type == SecretType.ssh_key:
return ssh_pubkey_gen(private_key=self.private_key)
return None
@property @property
def ssh_key_fingerprint(self): def ssh_key_fingerprint(self):
if self.public_key: if self.public_key:
public_key = self.public_key public_key = self.public_key
elif self.private_key: elif self.private_key:
try: try:
public_key = ssh_pubkey_gen(private_key=self.private_key, password=self.password) public_key = ssh_pubkey_gen(private_key=self.private_key)
except IOError as e: except IOError as e:
return str(e) return str(e)
else: else:
@ -103,7 +109,7 @@ class BaseAccount(OrgModelMixin):
@property @property
def private_key_obj(self): def private_key_obj(self):
if self.private_key: if self.private_key:
key_obj = ssh_key_string_to_obj(self.private_key, password=self.password) key_obj = ssh_key_string_to_obj(self.private_key)
return key_obj return key_obj
else: else:
return None return None

View File

@ -21,9 +21,13 @@ class BaseAccountSerializer(BulkOrgResourceModelSerializer):
class Meta: class Meta:
model = BaseAccount model = BaseAccount
fields_mini = ['id', 'name', 'username'] fields_mini = ['id', 'name', 'username']
fields_small = fields_mini + ['privileged', 'secret_type', 'secret', 'has_secret'] fields_small = fields_mini + ['privileged', 'secret_type', 'secret', 'has_secret', 'specific']
fields_other = ['created_by', 'date_created', 'date_updated', 'comment'] fields_other = ['created_by', 'date_created', 'date_updated', 'comment']
fields = fields_small + fields_other fields = fields_small + fields_other
read_only_fields = [
'has_secret', 'specific',
'date_verified', 'created_by', 'date_created',
]
extra_kwargs = { extra_kwargs = {
'secret': {'write_only': True}, 'secret': {'write_only': True},
'passphrase': {'write_only': True}, 'passphrase': {'write_only': True},