Debian Codegraph @

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

raw
1 # Copyright (c) 2021, RevBits <[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 from __future__ import annotations
5
6 DOCUMENTATION = r"""
7 name: revbitspss
8 author: RevBits (@RevBits) <[email protected]>
9 short_description: Get secrets from RevBits PAM server
10 version_added: 4.1.0
11 description:
12 - Uses the revbits_ansible Python SDK to get Secrets from RevBits PAM Server using API key authentication with the REST
13 API.
14 requirements:
15 - revbits_ansible - U(https://pypi.org/project/revbits_ansible/)
16 options:
17 _terms:
18 description:
19 - This is an array of keys for secrets which you want to fetch from RevBits PAM.
20 required: true
21 type: list
22 elements: string
23 base_url:
24 description:
25 - This is the base URL of the server, for example V(https://server-url-here).
26 required: true
27 type: string
28 api_key:
29 description:
30 - This is the API key for authentication. You can get it from the RevBits PAM secret manager module.
31 required: true
32 type: string
33 """
34
35 RETURN = r"""
36 _list:
37 description:
38 - The JSON responses which you can access with defined keys.
39 - If you are fetching secrets named as UUID, PASSWORD it returns the dict of all secrets.
40 type: list
41 elements: dict
42 """
43
44 EXAMPLES = r"""
45 ---
46 - hosts: localhost
47 vars:
48 secret: >-
49 {{
50 lookup(
51 'community.general.revbitspss',
52 'UUIDPAM', 'DB_PASS',
53 base_url='https://server-url-here',
54 api_key='API_KEY_GOES_HERE'
55 )
56 }}
57 tasks:
58 - ansible.builtin.debug:
59 msg: >-
60 UUIDPAM is {{ (secret['UUIDPAM']) }} and DB_PASS is {{ (secret['DB_PASS']) }}
61 """
62
63 from ansible.errors import AnsibleError
64 from ansible.plugins.lookup import LookupBase
65 from ansible.utils.display import Display
66
67 from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms
68
69 ANOTHER_LIBRARY_IMPORT_ERROR: ImportError | None
70 try:
71 from pam.revbits_ansible.server import SecretServer
72 except ImportError as imp_exc:
73 ANOTHER_LIBRARY_IMPORT_ERROR = imp_exc
74 else:
75 ANOTHER_LIBRARY_IMPORT_ERROR = None
76
77
78 display = Display()
79
80
81 class LookupModule(LookupBase):
82 @staticmethod
83 def Client(server_parameters):
84 return SecretServer(**server_parameters)
85
86 def run(self, terms, variables, **kwargs):
87 if ANOTHER_LIBRARY_IMPORT_ERROR:
88 raise AnsibleError("revbits_ansible must be installed to use this plugin") from ANOTHER_LIBRARY_IMPORT_ERROR
89 self.set_options(var_options=variables, direct=kwargs)
90 check_for_wrong_terms(self, direct=kwargs)
91 secret_server = LookupModule.Client(
92 {
93 "base_url": self.get_option("base_url"),
94 "api_key": self.get_option("api_key"),
95 }
96 )
97 result = []
98 for term in terms:
99 try:
100 display.vvv(f"Secret Server lookup of Secret with ID {term}")
101 result.append({term: secret_server.get_pam_secret(term)})
102 except Exception as error:
103 raise AnsibleError(f"Secret Server lookup failure: {error.message}") from error
104 return result