ansible-vars 1.0.14__py3-none-any.whl → 1.0.16__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ansible_vars/cli.py CHANGED
@@ -178,8 +178,7 @@ The sync works as long as the command is running, after which the target root di
178
178
  ''',
179
179
  'cmd_get': '''
180
180
  Looks up the value of a key in a vault and displays it if it exists.
181
- If the key resolves to a leaf value, the value is recursively decrypted and displayed.
182
- For a list or dictionary, the full YAML code is printed, but child values are not automatically decrypted.
181
+ The value will be shown in (recursively) decrypted form.
183
182
 
184
183
  JSON mode formatting:
185
184
  - [ ... ] or { ... } for lists/dictionaries, "<value>" for strings, <value> for numbers
@@ -506,9 +505,12 @@ def print_yaml(code: str) -> None:
506
505
  return std_print(code)
507
506
  std_print(highlight(code, yaml_highlight_lexer, highlight_formatter).strip('\n'))
508
507
 
509
- def print_diff(diff: str) -> None:
508
+ def print_diff(diff: str | None) -> None:
510
509
  '''Print a diff with highlighting if a `color_mode` is available.'''
511
510
  _color_map: dict = { '-': Color.TREE_REMOVED, '+': Color.TREE_ADDED, '@': Color.INFO, '*': Color.TREE_UNCHANGED }
511
+ if not diff:
512
+ print('Vaults are identical.', Color.TREE_UNCHANGED)
513
+ return
512
514
  for line in diff.split('\n'):
513
515
  color: Color = _color_map[line[0]] if (len(line) > 0 and line[0] in [ '-', '+', '@' ]) else _color_map['*']
514
516
  print(line, color)
@@ -708,7 +710,7 @@ if config.command in [ 'create', 'edit' ]:
708
710
  new_editable: str = edit_file.read()
709
711
  if editable != new_editable:
710
712
  try:
711
- new_vault: VaultFile = VaultFile.from_editable(vault, new_editable)
713
+ new_vault = VaultFile.from_editable(vault, new_editable)
712
714
  except YAMLFormatError as e:
713
715
  print('Invalid YAML format:', Color.BAD)
714
716
  print(e.parent if e.parent else e, Color.BAD)
ansible_vars/py.typed ADDED
File without changes
ansible_vars/vault.py CHANGED
@@ -30,7 +30,7 @@ class EncryptedVar():
30
30
 
31
31
  def __init__(self, cipher: str, name: str | None = None) -> None:
32
32
  '''Initialize an encrypted variable with an optional variable name. The name is only used for internal representation.'''
33
- # Encrypted has to hold a string like '$ANSIBLE_VAULT;1.2;AES256;ramiio\n123456<...>' (the newline is important)
33
+ # Encrypted has to hold a string like '$ANSIBLE_VAULT;1.2;AES256;someid\n123456<...>' (the newline is important)
34
34
  self.cipher: str = cipher
35
35
  self.name: str | None = name
36
36
 
@@ -107,7 +107,7 @@ class Vault():
107
107
  Parses a vault's (potentially encrypted) contents. Automatically detects if the content is wholly encrypted.
108
108
  If no keyring is supplied, only plain vars and content are supported.
109
109
  '''
110
- # If no keyring is supplied, create an empty one which will raise an Error if we try to en-/decrypt anything
110
+ # If no keyring is supplied, create an empty one which will raise an error if we try to en-/decrypt anything
111
111
  self.keyring: VaultKeyring = keyring or VaultKeyring(keys=None, detect_available_keys=False)
112
112
  # Full vault encryption, may also contain single encrypted variables either way
113
113
  self.full_encryption: bool
@@ -208,7 +208,7 @@ class Vault():
208
208
  def has(self, path: DictPath) -> bool:
209
209
  '''Checks if the given key path is present in the vault's data.'''
210
210
  try: self._traverse(path, decrypt=False)
211
- except KeyError: return False
211
+ except ( KeyError, IndexError ): return False
212
212
  return True
213
213
 
214
214
  def get(
@@ -591,12 +591,13 @@ class Vault():
591
591
 
592
592
  # Comparing to older versions of this vault
593
593
 
594
- def diff(self, prev_vault: 'Vault', context_lines: int = 3, show_filenames: bool = True) -> str:
594
+ def diff(self, prev_vault: 'Vault', context_lines: int = 3, show_filenames: bool = True) -> str | None:
595
595
  '''
596
596
  Generates a diff for the edit mode Jinja2 YAML vault code (from `Vault.as_editable`) of a previous vault to this one's.
597
597
  Set `context_lines` to specify how many lines of context are shown before and after the actual diff lines.
598
598
  If `show_filenames` is set to True and the vaults are `VaultFile` objects,
599
599
  the previous and current filenames will be shown in the diff header.
600
+ If there is no difference between the vaults, None is returned.
600
601
  '''
601
602
  # Generate filenames
602
603
  prev_filename: str = 'Previous vault'
@@ -615,7 +616,7 @@ class Vault():
615
616
  n = context_lines,
616
617
  lineterm = ''
617
618
  )
618
- )
619
+ ) or None # diff function returns empty list if there are no changes, even skipping the header
619
620
 
620
621
  def changes(self, prev_vault: 'Vault') -> tuple[ChangeList, ChangeList, ChangeList, ChangeList]:
621
622
  '''
@@ -3,6 +3,7 @@
3
3
  # Standard library imports
4
4
  import os, re
5
5
  from typing import Type, cast
6
+ from contextlib import chdir
6
7
 
7
8
  # External library imports
8
9
  import ansible.constants as Ansible
@@ -202,16 +203,17 @@ class VaultKeyring():
202
203
  # Load secrets for discovered vault IDs
203
204
  if not vault_ids:
204
205
  return []
205
- prev_dir: str = os.getcwd()
206
- try:
207
- # XXX Hacky, but loading a vault ID like 'vaultid@get-password.sh' will be resolved from CWD
208
- # Could possibly be avoided by splitting the detected vault IDs and transforming any right-hand paths
209
- os.chdir(pardir)
210
- secrets: list[tuple[str | None, VaultSecret]] = \
211
- CLI.setup_vault_secrets(DataLoader(), vault_ids, auto_prompt=False, initialize_context=False) # type: ignore
206
+ # XXX Hacky, but the right-hand path from loading a vault ID like 'vaultid@get-password.sh' will be resolved from CWD
207
+ # Could possibly be avoided by splitting the detected vault IDs and transforming any right-hand paths
208
+ with chdir(pardir):
209
+ # Seems version-dependent if `initialize_context` is recognized and required
210
+ try:
211
+ secrets: list[tuple[str | None, VaultSecret]] = \
212
+ CLI.setup_vault_secrets(DataLoader(), vault_ids, auto_prompt=False, initialize_context=False) # type: ignore
213
+ except TypeError:
214
+ secrets: list[tuple[str | None, VaultSecret]] = \
215
+ CLI.setup_vault_secrets(DataLoader(), vault_ids, auto_prompt=False)
212
216
  return list(map(VaultKey.from_ansible_secret, secrets))
213
- finally:
214
- os.chdir(prev_dir)
215
217
 
216
218
  def __repr__(self) -> str:
217
219
  return f"VaultKeyring({ ', '.join(map(lambda key: key.id, self.keys)) or 'no keys' })"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ansible-vars
3
- Version: 1.0.14
3
+ Version: 1.0.16
4
4
  Summary: Manage vaults and variable files for Ansible
5
5
  Project-URL: Homepage, https://github.com/xorwow/ansible-vars
6
6
  Project-URL: Issues, https://github.com/xorwow/ansible-vars/issues
@@ -219,7 +219,7 @@ Starts a daemon which mirrors the decrypted contents of one or multiple vault or
219
219
 
220
220
  #### get
221
221
 
222
- Displays the (optionally recursively decrypted) value of a specified key in a vault or variable file. Supports dictionary and list traversal, and JSON output.
222
+ Displays the (by default recursively decrypted) value of a specified key in a vault or variable file. Supports dictionary and list traversal, and JSON output.
223
223
 
224
224
  #### set, del (experimental)
225
225
 
@@ -269,7 +269,7 @@ Contains the classes `Vault` and `VaultFile`. A `Vault` is initialized using the
269
269
 
270
270
  The `VaultKey` class represents a single vault secret, comprised of an identifier and an `ansible.parsing.vault.VaultSecret`. Can be initialized using a plain passphrase instead of a `VaultSecret` as well.
271
271
 
272
- The `VaultKeyring` combines a collection of `VaultKey`s. It supports auto-detection of any secrets available in the present working directory using the `ansible.cli` module, appending them to the `<keyring>.keys` collection. While all keys are tried in order for decryption operations, only one key can be used for encrypting data. This key is usually the first key in the `<keyring>.keys` collection, unless explicitly specified otherwise using `<keyring>.default_encryption_key` or passing a key to the `<keyring>.encrypt()` method.
272
+ The `VaultKeyring` combines a collection of `VaultKey`s. It supports auto-detection of any secrets available in the present working directory (or a custom source) using the `ansible.cli` module, appending them to the `<keyring>.keys` collection. While all keys are tried in order for decryption operations, only one key can be used for encrypting data. This key is usually the first key in the `<keyring>.keys` collection, unless explicitly specified otherwise using `<keyring>.default_encryption_key` or passing a key to the `<keyring>.encrypt()` method.
273
273
 
274
274
  #### util module
275
275
 
@@ -0,0 +1,13 @@
1
+ ansible_vars/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ ansible_vars/cli.py,sha256=cp891u9-kZoJireRIsTh-zy_9cTZPifLio2U7NqRoQM,66428
3
+ ansible_vars/constants.py,sha256=VNr78qbzdJ0Vn0psbzjjQngNG3VbHhk6NXTz30VNUno,1622
4
+ ansible_vars/errors.py,sha256=6dzyksPKWira9O2-Ir3MIOwr4XjN9MSBiRp5e6siY6Q,1256
5
+ ansible_vars/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ ansible_vars/util.py,sha256=UwGPBT19pee7lBpWuBzLPAvcrHUBAn6i1MrJvzM9OQ4,21265
7
+ ansible_vars/vault.py,sha256=pBZVz64qdj1vstC9rZBIJ3O9n-_AHhhbySN1Q7eMWho,47199
8
+ ansible_vars/vault_crypt.py,sha256=ZXa6QywyWdRVhY3YkFuk-COYs50MG252uakqe3bUUog,12116
9
+ ansible_vars-1.0.16.dist-info/METADATA,sha256=QqgRP-yb3BHIis97g15QFpQNn5kL2stPVRmz5KZmQ6A,21071
10
+ ansible_vars-1.0.16.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
11
+ ansible_vars-1.0.16.dist-info/entry_points.txt,sha256=RrhkEH0MbfRzflguVrfYfthsFC5V2fkFnizUG3uHMtQ,55
12
+ ansible_vars-1.0.16.dist-info/licenses/LICENSE,sha256=ocyJHLG5wD12qB4uam2pqWTHIJmzloiyNyTex6Q2DKo,1062
13
+ ansible_vars-1.0.16.dist-info/RECORD,,
@@ -1,12 +0,0 @@
1
- ansible_vars/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- ansible_vars/cli.py,sha256=0RScnMrG__L5mXu_OmEgxB3x4lTJ1lAyowlOJvQNcrE,66476
3
- ansible_vars/constants.py,sha256=VNr78qbzdJ0Vn0psbzjjQngNG3VbHhk6NXTz30VNUno,1622
4
- ansible_vars/errors.py,sha256=6dzyksPKWira9O2-Ir3MIOwr4XjN9MSBiRp5e6siY6Q,1256
5
- ansible_vars/util.py,sha256=UwGPBT19pee7lBpWuBzLPAvcrHUBAn6i1MrJvzM9OQ4,21265
6
- ansible_vars/vault.py,sha256=aGTU_GmJLw0QGjBQiNlRE_E-rRMEVVbwdr8IV2U2duk,47011
7
- ansible_vars/vault_crypt.py,sha256=58CECQrjHSKubUGL38TLuYF9h8KmO2e70swRgkS-Rn0,11870
8
- ansible_vars-1.0.14.dist-info/METADATA,sha256=iGS5L6S7IQF8agJHlDDvL5NvAv3eFcjbBhS9XtkIogA,21050
9
- ansible_vars-1.0.14.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
10
- ansible_vars-1.0.14.dist-info/entry_points.txt,sha256=RrhkEH0MbfRzflguVrfYfthsFC5V2fkFnizUG3uHMtQ,55
11
- ansible_vars-1.0.14.dist-info/licenses/LICENSE,sha256=ocyJHLG5wD12qB4uam2pqWTHIJmzloiyNyTex6Q2DKo,1062
12
- ansible_vars-1.0.14.dist-info/RECORD,,