Debian Codegraph @

ansible_collections/community/crypto/plugins/lookup/gpg_fingerprint.py

raw
1 # Copyright (c) 2023, Felix Fontein <[email protected]>
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: gpg_fingerprint
9 short_description: Retrieve a GPG fingerprint from a GPG public or private key file
10 author: Felix Fontein (@felixfontein)
11 version_added: 2.15.0
12 description:
13 - Takes a list of filenames pointing to GPG public or private key files. Returns the fingerprints for each of these keys.
14 options:
15 _terms:
16 description:
17 - A path to a GPG public or private key.
18 type: list
19 elements: path
20 required: true
21 requirements:
22 - GnuPG (C(gpg) executable)
23 seealso:
24 - plugin: community.crypto.gpg_fingerprint
25 plugin_type: filter
26 """
27
28 EXAMPLES = r"""
29 ---
30 - name: Show fingerprint of GPG public key
31 ansible.builtin.debug:
32 msg: "{{ lookup('community.crypto.gpg_fingerprint', '/path/to/public_key.gpg') }}"
33 """
34
35 RETURN = r"""
36 _value:
37 description:
38 - The fingerprints of the provided public or private GPG keys.
39 - The list has one entry for every path provided.
40 type: list
41 elements: string
42 """
43
44 import os
45 import typing as t
46
47 from ansible.errors import AnsibleLookupError
48 from ansible.module_utils.common.text.converters import to_text
49 from ansible.plugins.lookup import LookupBase
50
51 from ansible_collections.community.crypto.plugins.module_utils._gnupg.cli import (
52 GPGError,
53 get_fingerprint_from_file,
54 )
55 from ansible_collections.community.crypto.plugins.plugin_utils._gnupg import (
56 PluginGPGRunner,
57 )
58
59
60 class LookupModule(LookupBase):
61 def run(
62 self, terms: list[t.Any], variables: None = None, **kwargs: t.Any
63 ) -> list[str]:
64 self.set_options(direct=kwargs)
65 if self._loader is None:
66 raise AssertionError(
67 "Contract violation: self._loader is None"
68 ) # pragma: no cover
69
70 try:
71 gpg = PluginGPGRunner(cwd=self._loader.get_basedir())
72 result = []
73 for i, path in enumerate(terms):
74 if not isinstance(path, (str, bytes, os.PathLike)):
75 raise AnsibleLookupError(
76 f"Lookup parameter #{i} should be string or a path object, but got {type(path)}"
77 )
78 result.append(
79 get_fingerprint_from_file(gpg_runner=gpg, path=to_text(path))
80 )
81 return result
82 except GPGError as exc:
83 raise AnsibleLookupError(str(exc)) from exc