|
11 | 11 |
|
12 | 12 | if TYPE_CHECKING: |
13 | 13 | from ansible.parsing.dataloader import DataLoader |
14 | | - from ansible.parsing.vault import VaultSecret |
15 | 14 |
|
16 | 15 |
|
17 | 16 | class AVDVaultHandler: |
18 | 17 | """Handles Ansible Vault encryption and decryption operations.""" |
19 | 18 |
|
20 | | - _loader: DataLoader |
21 | | - _encrypt_vault_id: str | None |
22 | | - _encrypt_secret: VaultSecret | None |
| 19 | + __slots__ = ("__encrypt_secret", "__encrypt_vault_id", "__loader") |
23 | 20 |
|
24 | 21 | def __init__(self, loader: DataLoader, vault_id: str | None = None) -> None: |
25 | 22 | """ |
26 | | - Initialize the vault handler. |
| 23 | + Initialize the Vault handler. |
27 | 24 |
|
28 | 25 | Args: |
29 | 26 | loader: The Ansible DataLoader instance. |
30 | | - vault_id: Optional vault ID to use for encryption. If None, uses the first vault ID in the list (default Ansible behavior). |
| 27 | + vault_id: Optional Vault ID to use for encryption. If None, uses the first Vault ID in the list (default Ansible behavior). |
31 | 28 | """ |
32 | | - self._loader = loader |
| 29 | + self.__loader = loader |
33 | 30 |
|
34 | | - # Pre-compute encryption secret if vault is configured |
| 31 | + # Pre-compute encryption credentials if Vault secrets are configured. |
35 | 32 | if loader._vault.secrets: |
36 | | - self._encrypt_vault_id, self._encrypt_secret = match_encrypt_secret(loader._vault.secrets, vault_id) |
| 33 | + self.__encrypt_vault_id, self.__encrypt_secret = match_encrypt_secret(loader._vault.secrets, vault_id) |
37 | 34 | else: |
38 | | - self._encrypt_vault_id = None |
39 | | - self._encrypt_secret = None |
| 35 | + self.__encrypt_vault_id = None |
| 36 | + self.__encrypt_secret = None |
40 | 37 |
|
41 | 38 | @property |
42 | | - def has_vault(self) -> bool: |
43 | | - """Whether vault secrets are configured.""" |
44 | | - return bool(self._loader._vault.secrets) |
| 39 | + def has_vault_secrets(self) -> bool: |
| 40 | + """Whether Vault secrets are configured.""" |
| 41 | + return self.__encrypt_secret is not None |
45 | 42 |
|
46 | 43 | def encrypt_if_needed(self, data: bytes) -> bytes: |
47 | 44 | """ |
48 | | - Encrypt data if vault secrets are configured. |
| 45 | + Encrypt data if needed. |
49 | 46 |
|
50 | 47 | Args: |
51 | 48 | data: Data to potentially encrypt. |
52 | 49 |
|
53 | 50 | Returns: |
54 | | - Encrypted data if vault is configured, otherwise the original data. |
55 | | -
|
| 51 | + Encrypted data if Vault secrets are configured, otherwise the original data. |
56 | 52 | """ |
57 | | - if not self.has_vault: |
| 53 | + if not self.has_vault_secrets: |
58 | 54 | return data |
59 | 55 |
|
60 | | - return self._loader._vault.encrypt(data, secret=self._encrypt_secret, vault_id=self._encrypt_vault_id) |
| 56 | + return self.__loader._vault.encrypt(data, secret=self.__encrypt_secret, vault_id=self.__encrypt_vault_id) |
61 | 57 |
|
62 | 58 | def decrypt_if_needed(self, data: bytes) -> bytes: |
63 | 59 | """ |
64 | | - Decrypt data if it is vault encrypted. |
| 60 | + Decrypt data if needed. |
65 | 61 |
|
66 | 62 | Args: |
67 | 63 | data: Data to potentially decrypt. |
68 | 64 |
|
69 | 65 | Returns: |
70 | | - Decrypted data if it was vault encrypted, otherwise the original data. |
| 66 | + Decrypted data if Vault secrets are configured and data is encrypted, otherwise the original data. |
71 | 67 |
|
72 | 68 | Raises: |
73 | | - AnsibleVaultError: If vault decryption fails. |
| 69 | + AnsibleVaultError: If Vault decryption fails. |
74 | 70 | """ |
75 | | - if self.has_vault and self._loader._vault.is_encrypted(data): |
76 | | - # Vault is configured and data is encrypted, decrypt it |
77 | | - decrypted_data, _vault_id, _vault_secret = self._loader._vault.decrypt_and_get_vault_id(data) |
78 | | - return decrypted_data |
| 71 | + if self.has_vault_secrets and self.__loader._vault.is_encrypted(data): |
| 72 | + return self.__loader._vault.decrypt(data) |
79 | 73 |
|
80 | | - # Data is not vaulted or no vault configured - return data as-is |
81 | 74 | return data |
0 commit comments