Skip to content

Commit

Permalink
Fix path detection for gopass (#4955) (#4965)
Browse files Browse the repository at this point in the history
* Fix path detection for gopass

As per https://github.com/gopasspw/gopass/blob/fc8c9a228618fa4a146a87c9027fa0434b0737fa/docs/features.md#initializing-a-password-store, gopass defaults to ~/.local/share/gopass/stores/root for its password store root location.

However, the user can also override this, and this will be stored in the gopass config file (https://github.com/gopasspw/gopass/blob/ed7451678c5e8138d5a8eaafa278d5065a9eb9fe/docs/config.md#configuration-options).

This patch ensures that the config setting in gopass is respected, falling back to the default gopass path. pass' behaviour remains unchanged.

* Formatting improvements

Co-authored-by: Felix Fontein <felix@fontein.de>

* Add changelog fragment

* Formatting improvement

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
(cherry picked from commit c31e641)

Co-authored-by: Sylvia van Os <sylvia@hackerchick.me>
  • Loading branch information
patchback[bot] and TheLastProject authored Jul 21, 2022
1 parent d7ecd40 commit 97b3ad6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/4955-fix-path-detection-for-gopass.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- passwordstore - fix password store path detection for gopass (https://github.com/ansible-collections/community.general/pull/4955).
27 changes: 22 additions & 5 deletions plugins/lookup/passwordstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@
description: query key.
required: True
passwordstore:
description: location of the password store.
default: '~/.password-store'
description:
- Location of the password store.
- 'The value is decided by checking the following in order:'
- If set, this value is used.
- If C(directory) is set, that value will be used.
- If I(backend=pass), then C(~/.password-store) is used.
- If I(backend=gopass), then the C(path) field in C(~/.config/gopass/config.yml) is used,
falling back to C(~/.local/share/gopass/stores/root) if not defined.
directory:
description: The directory of the password store.
env:
Expand Down Expand Up @@ -428,11 +434,22 @@ def setup(self, variables):
raise AnsibleError("{0} is not a correct value for locktimeout".format(timeout))
unit_to_seconds = {"s": 1, "m": 60, "h": 3600}
self.lock_timeout = int(timeout[:-1]) * unit_to_seconds[timeout[-1]]

directory = variables.get('passwordstore', os.environ.get('PASSWORD_STORE_DIR', None))

if directory is None:
if self.backend == 'gopass':
try:
with open(os.path.expanduser('~/.config/gopass/config.yml')) as f:
directory = yaml.safe_load(f)['path']
except (FileNotFoundError, KeyError, yaml.YAMLError):
directory = os.path.expanduser('~/.local/share/gopass/stores/root')
else:
directory = os.path.expanduser('~/.password-store')

self.paramvals = {
'subkey': 'password',
'directory': variables.get('passwordstore', os.environ.get(
'PASSWORD_STORE_DIR',
os.path.expanduser('~/.password-store'))),
'directory': directory,
'create': False,
'returnall': False,
'overwrite': False,
Expand Down

0 comments on commit 97b3ad6

Please sign in to comment.