perf: optimizing the code

pull/14515/head
jiangweidong 2 days ago
parent a6fa1dab2a
commit 94634e835d

@ -1,15 +1,10 @@
from common.db.utils import get_logger
from .service import AmazonSecretsManagerClient from .service import AmazonSecretsManagerClient
from ..base.vault import BaseVault from ..base.vault import BaseVault
from ..utils.mixins import GeneralVaultMixin
from ...const import VaultTypeChoices from ...const import VaultTypeChoices
logger = get_logger(__name__) class Vault(GeneralVaultMixin, BaseVault):
__all__ = ['Vault']
class Vault(BaseVault):
type = VaultTypeChoices.aws type = VaultTypeChoices.aws
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -19,27 +14,3 @@ class Vault(BaseVault):
access_key_id=kwargs.get('VAULT_AWS_ACCESS_KEY_ID'), access_key_id=kwargs.get('VAULT_AWS_ACCESS_KEY_ID'),
secret_key=kwargs.get('VAULT_AWS_ACCESS_SECRET_KEY'), secret_key=kwargs.get('VAULT_AWS_ACCESS_SECRET_KEY'),
) )
def is_active(self):
return self.client.is_active()
def _get(self, entry):
secret = self.client.get(name=entry.full_path)
return entry.get_decrypt_secret(secret)
def _create(self, entry):
secret = entry.get_encrypt_secret()
self.client.create(name=entry.full_path, secret=secret)
def _update(self, entry):
secret = entry.get_encrypt_secret()
self.client.update(name=entry.full_path, secret=secret)
def _delete(self, entry):
self.client.delete(name=entry.full_path)
def _save_metadata(self, entry, metadata):
try:
self.client.update_metadata(name=entry.full_path, metadata=metadata)
except Exception as e:
logger.error(f'save metadata error: {e}')

@ -1,15 +1,10 @@
from common.db.utils import get_logger
from .service import AZUREVaultClient from .service import AZUREVaultClient
from ..base.vault import BaseVault from ..base.vault import BaseVault
from ..utils.mixins import GeneralVaultMixin
from ...const import VaultTypeChoices from ...const import VaultTypeChoices
logger = get_logger(__name__) class Vault(GeneralVaultMixin, BaseVault):
__all__ = ['Vault']
class Vault(BaseVault):
type = VaultTypeChoices.azure type = VaultTypeChoices.azure
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -20,27 +15,3 @@ class Vault(BaseVault):
client_id=kwargs.get('VAULT_AZURE_CLIENT_ID'), client_id=kwargs.get('VAULT_AZURE_CLIENT_ID'),
client_secret=kwargs.get('VAULT_AZURE_CLIENT_SECRET') client_secret=kwargs.get('VAULT_AZURE_CLIENT_SECRET')
) )
def is_active(self):
return self.client.is_active()
def _get(self, entry):
secret = self.client.get(name=entry.full_path)
return entry.get_decrypt_secret(secret)
def _create(self, entry):
secret = entry.get_encrypt_secret()
self.client.create(name=entry.full_path, secret=secret)
def _update(self, entry):
secret = entry.get_encrypt_secret()
self.client.update(name=entry.full_path, secret=secret)
def _delete(self, entry):
self.client.delete(name=entry.full_path)
def _save_metadata(self, entry, metadata):
try:
self.client.update_metadata(name=entry.full_path, metadata=metadata)
except Exception as e:
logger.error(f'save metadata error: {e}')

@ -0,0 +1,32 @@
from common.utils import get_logger
logger = get_logger(__name__)
class GeneralVaultMixin(object):
client = None
def is_active(self):
return self.client.is_active()
def _get(self, entry):
secret = self.client.get(name=entry.full_path)
return entry.get_decrypt_secret(secret)
def _create(self, entry):
secret = entry.get_encrypt_secret()
self.client.create(name=entry.full_path, secret=secret)
def _update(self, entry):
secret = entry.get_encrypt_secret()
self.client.update(name=entry.full_path, secret=secret)
def _delete(self, entry):
self.client.delete(name=entry.full_path)
def _save_metadata(self, entry, metadata):
try:
self.client.update_metadata(name=entry.full_path, metadata=metadata)
except Exception as e:
logger.error(f'save metadata error: {e}')
Loading…
Cancel
Save