Debian Codegraph @

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

raw
1 # Copyright (c) 2023, Ansible Project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 DOCUMENTATION = r"""
8 name: onepassword_doc
9 author:
10 - Sam Doran (@samdoran)
11 short_description: Fetch documents stored in 1Password
12 version_added: "8.1.0"
13 description:
14 - P(community.general.onepassword_doc#lookup) wraps C(op) command line utility to fetch one or more documents from 1Password.
15 notes:
16 - The document contents are a string exactly as stored in 1Password.
17 options:
18 _terms:
19 description: Identifier(s) (case-insensitive UUID or name) of item(s) to retrieve.
20 required: true
21 type: list
22 elements: string
23
24 extends_documentation_fragment:
25 - community.general._onepassword
26 - community.general._onepassword.lookup
27 """
28
29 EXAMPLES = r"""
30 ---
31 - name: Retrieve a private key from 1Password
32 ansible.builtin.debug:
33 var: lookup('community.general.onepassword_doc', 'Private key')
34 """
35
36 RETURN = r"""
37 _raw:
38 description: Requested document.
39 type: list
40 elements: string
41 """
42
43 from ansible.plugins.lookup import LookupBase
44
45 from ansible_collections.community.general.plugins.lookup.onepassword import OnePass, OnePassCLIv2
46 from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms
47
48
49 class OnePassCLIv2Doc(OnePassCLIv2):
50 def get_raw(self, item_id, vault=None, token=None):
51 args = ["document", "get", item_id]
52 return self._add_parameters_and_run(args, vault=vault, token=token)
53
54
55 class LookupModule(LookupBase):
56 def run(self, terms, variables=None, **kwargs):
57 self.set_options(var_options=variables, direct=kwargs)
58 check_for_wrong_terms(self, direct=kwargs)
59
60 vault = self.get_option("vault")
61 subdomain = self.get_option("subdomain")
62 domain = self.get_option("domain", "1password.com")
63 username = self.get_option("username")
64 secret_key = self.get_option("secret_key")
65 master_password = self.get_option("master_password")
66 service_account_token = self.get_option("service_account_token")
67 account_id = self.get_option("account_id")
68 connect_host = self.get_option("connect_host")
69 connect_token = self.get_option("connect_token")
70
71 op = OnePass(
72 subdomain=subdomain,
73 domain=domain,
74 username=username,
75 secret_key=secret_key,
76 master_password=master_password,
77 service_account_token=service_account_token,
78 account_id=account_id,
79 connect_host=connect_host,
80 connect_token=connect_token,
81 cli_class=OnePassCLIv2Doc,
82 )
83 op.assert_logged_in()
84
85 values = []
86 for term in terms:
87 values.append(op.get_raw(term, vault))
88
89 return values