Debian Codegraph @

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

raw
1 # Copyright (c) 2020, Adam Migus <[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: dsv
8 author: Adam Migus (@amigus) <[email protected]>
9 short_description: Get secrets from Thycotic DevOps Secrets Vault
10 version_added: 1.0.0
11 description:
12 - Uses the Thycotic DevOps Secrets Vault Python SDK to get Secrets from a DSV O(tenant) using a O(client_id) and O(client_secret).
13 requirements:
14 - python-dsv-sdk - https://pypi.org/project/python-dsv-sdk/
15 options:
16 _terms:
17 description: The path to the secret, for example V(/staging/servers/web1).
18 required: true
19 tenant:
20 description: The first format parameter in the default O(url_template).
21 type: string
22 env:
23 - name: DSV_TENANT
24 ini:
25 - section: dsv_lookup
26 key: tenant
27 required: true
28 tld:
29 default: com
30 description: The top-level domain of the tenant; the second format parameter in the default O(url_template).
31 type: string
32 env:
33 - name: DSV_TLD
34 ini:
35 - section: dsv_lookup
36 key: tld
37 required: false
38 client_id:
39 description: The client_id with which to request the Access Grant.
40 type: string
41 env:
42 - name: DSV_CLIENT_ID
43 ini:
44 - section: dsv_lookup
45 key: client_id
46 required: true
47 client_secret:
48 description: The client secret associated with the specific O(client_id).
49 type: string
50 env:
51 - name: DSV_CLIENT_SECRET
52 ini:
53 - section: dsv_lookup
54 key: client_secret
55 required: true
56 url_template:
57 default: https://{}.secretsvaultcloud.{}/v1
58 description: The path to prepend to the base URL to form a valid REST API request.
59 type: string
60 env:
61 - name: DSV_URL_TEMPLATE
62 ini:
63 - section: dsv_lookup
64 key: url_template
65 required: false
66 """
67
68 RETURN = r"""
69 _list:
70 description:
71 - One or more JSON responses to C(GET /secrets/{path}).
72 - See U(https://dsv.thycotic.com/api/index.html#operation/getSecret).
73 type: list
74 elements: dict
75 """
76
77 EXAMPLES = r"""
78 - hosts: localhost
79 vars:
80 secret: "{{ lookup('community.general.dsv', '/test/secret') }}"
81 tasks:
82 - ansible.builtin.debug:
83 msg: 'the password is {{ secret["data"]["password"] }}'
84 """
85
86 from ansible.errors import AnsibleError, AnsibleOptionsError
87
88 from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms
89
90 sdk_is_missing = False
91
92 try:
93 from thycotic.secrets.vault import (
94 SecretsVault,
95 SecretsVaultError,
96 )
97 except ImportError:
98 sdk_is_missing = True
99
100 from ansible.plugins.lookup import LookupBase
101 from ansible.utils.display import Display
102
103 display = Display()
104
105
106 class LookupModule(LookupBase):
107 @staticmethod
108 def Client(vault_parameters):
109 try:
110 vault = SecretsVault(**vault_parameters)
111 return vault
112 except TypeError as e:
113 raise AnsibleError("python-dsv-sdk==0.0.1 must be installed to use this plugin") from e
114
115 def run(self, terms, variables, **kwargs):
116 if sdk_is_missing:
117 raise AnsibleError("python-dsv-sdk==0.0.1 must be installed to use this plugin")
118
119 self.set_options(var_options=variables, direct=kwargs)
120 check_for_wrong_terms(self, direct=kwargs)
121
122 vault = LookupModule.Client(
123 {
124 "tenant": self.get_option("tenant"),
125 "client_id": self.get_option("client_id"),
126 "client_secret": self.get_option("client_secret"),
127 "tld": self.get_option("tld"),
128 "url_template": self.get_option("url_template"),
129 }
130 )
131 result = []
132
133 for term in terms:
134 display.debug(f"dsv_lookup term: {term}")
135 try:
136 path = term.lstrip("[/:]")
137
138 if path == "":
139 raise AnsibleOptionsError(f"Invalid secret path: {term}")
140
141 display.vvv(f"DevOps Secrets Vault GET /secrets/{path}")
142 result.append(vault.get_secret_json(path))
143 except SecretsVaultError as error:
144 raise AnsibleError(f"DevOps Secrets Vault lookup failure: {error.message}") from error
145 return result