diff --git a/apps/accounts/backends/aws/main.py b/apps/accounts/backends/aws/main.py index 1db02ce11..9ebaa65b0 100644 --- a/apps/accounts/backends/aws/main.py +++ b/apps/accounts/backends/aws/main.py @@ -1,15 +1,10 @@ -from common.db.utils import get_logger from .service import AmazonSecretsManagerClient from ..base.vault import BaseVault +from ..utils.mixins import GeneralVaultMixin from ...const import VaultTypeChoices -logger = get_logger(__name__) - -__all__ = ['Vault'] - - -class Vault(BaseVault): +class Vault(GeneralVaultMixin, BaseVault): type = VaultTypeChoices.aws def __init__(self, *args, **kwargs): @@ -19,27 +14,3 @@ class Vault(BaseVault): access_key_id=kwargs.get('VAULT_AWS_ACCESS_KEY_ID'), 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}') diff --git a/apps/accounts/backends/azure/main.py b/apps/accounts/backends/azure/main.py index 3add27ef3..41ab59665 100644 --- a/apps/accounts/backends/azure/main.py +++ b/apps/accounts/backends/azure/main.py @@ -1,15 +1,10 @@ -from common.db.utils import get_logger from .service import AZUREVaultClient from ..base.vault import BaseVault +from ..utils.mixins import GeneralVaultMixin from ...const import VaultTypeChoices -logger = get_logger(__name__) - -__all__ = ['Vault'] - - -class Vault(BaseVault): +class Vault(GeneralVaultMixin, BaseVault): type = VaultTypeChoices.azure def __init__(self, *args, **kwargs): @@ -20,27 +15,3 @@ class Vault(BaseVault): client_id=kwargs.get('VAULT_AZURE_CLIENT_ID'), 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}') diff --git a/apps/accounts/backends/utils/__init__.py b/apps/accounts/backends/utils/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/apps/accounts/backends/utils/mixins.py b/apps/accounts/backends/utils/mixins.py new file mode 100644 index 000000000..a0fbc8192 --- /dev/null +++ b/apps/accounts/backends/utils/mixins.py @@ -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}')