Debian Codegraph @

ansible_collections/community/general/plugins/lookup/lastpass.py

raw
1 # Copyright (c) 2016, Andrew Zenk <[email protected]>
2 # Copyright (c) 2017 Ansible Project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5 from __future__ import annotations
6
7 DOCUMENTATION = r"""
8 name: lastpass
9 author:
10 - Andrew Zenk (!UNKNOWN) <[email protected]>
11 requirements:
12 - lpass (command line utility)
13 - must have already logged into LastPass
14 short_description: Fetch data from LastPass
15 description:
16 - Use the lpass command line utility to fetch specific fields from LastPass.
17 options:
18 _terms:
19 description: Key from which you want to retrieve the field.
20 required: true
21 type: list
22 elements: str
23 field:
24 description: Field to return from LastPass.
25 default: 'password'
26 type: str
27 """
28
29 EXAMPLES = r"""
30 - name: get 'custom_field' from LastPass entry 'entry-name'
31 ansible.builtin.debug:
32 msg: "{{ lookup('community.general.lastpass', 'entry-name', field='custom_field') }}"
33 """
34
35 RETURN = r"""
36 _raw:
37 description: Secrets stored.
38 type: list
39 elements: str
40 """
41
42 from subprocess import PIPE, Popen
43
44 from ansible.errors import AnsibleError
45 from ansible.module_utils.common.text.converters import to_bytes, to_text
46 from ansible.plugins.lookup import LookupBase
47
48 from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms
49
50
51 class LPassException(AnsibleError):
52 pass
53
54
55 class LPass:
56 def __init__(self, path="lpass"):
57 self._cli_path = path
58
59 @property
60 def cli_path(self):
61 return self._cli_path
62
63 @property
64 def logged_in(self):
65 out, err = self._run(self._build_args("logout"), stdin="n\n", expected_rc=1)
66 return err.startswith("Are you sure you would like to log out?")
67
68 def _run(self, args, stdin=None, expected_rc=0):
69 p = Popen([self.cli_path] + args, stdout=PIPE, stderr=PIPE, stdin=PIPE)
70 out, err = p.communicate(to_bytes(stdin))
71 rc = p.wait()
72 if rc != expected_rc:
73 raise LPassException(err)
74 return to_text(out, errors="surrogate_or_strict"), to_text(err, errors="surrogate_or_strict")
75
76 def _build_args(self, command, args=None):
77 if args is None:
78 args = []
79 args = [command] + args
80 args += ["--color=never"]
81 return args
82
83 def get_field(self, key, field):
84 if field in ["username", "password", "url", "notes", "id", "name"]:
85 out, err = self._run(self._build_args("show", [f"--{field}", key]))
86 else:
87 out, err = self._run(self._build_args("show", [f"--field={field}", key]))
88 return out.strip()
89
90
91 class LookupModule(LookupBase):
92 def run(self, terms, variables=None, **kwargs):
93 self.set_options(var_options=variables, direct=kwargs)
94 check_for_wrong_terms(self, direct=kwargs)
95 field = self.get_option("field")
96
97 lp = LPass()
98
99 if not lp.logged_in:
100 raise AnsibleError("Not logged into LastPass: please run 'lpass login' first")
101
102 values = []
103 for term in terms:
104 values.append(lp.get_field(term, field))
105 return values