2020-07-13 07:45:07 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2020, Adam Migus <adam@migus.org>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-07-13 07:45:07 +02:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = r"""
|
2021-01-12 07:12:03 +01:00
|
|
|
name: dsv
|
2020-08-09 21:50:08 +02:00
|
|
|
author: Adam Migus (@amigus) <adam@migus.org>
|
2020-07-13 07:45:07 +02:00
|
|
|
short_description: Get secrets from Thycotic DevOps Secrets Vault
|
|
|
|
version_added: 1.0.0
|
|
|
|
description:
|
|
|
|
- Uses the Thycotic DevOps Secrets Vault Python SDK to get Secrets from a
|
2023-06-10 09:28:40 +02:00
|
|
|
DSV O(tenant) using a O(client_id) and O(client_secret).
|
2020-07-13 07:45:07 +02:00
|
|
|
requirements:
|
|
|
|
- python-dsv-sdk - https://pypi.org/project/python-dsv-sdk/
|
|
|
|
options:
|
|
|
|
_terms:
|
2023-06-10 09:28:40 +02:00
|
|
|
description: The path to the secret, for example V(/staging/servers/web1).
|
2020-07-13 07:45:07 +02:00
|
|
|
required: true
|
|
|
|
tenant:
|
2023-06-10 09:28:40 +02:00
|
|
|
description: The first format parameter in the default O(url_template).
|
2024-07-21 22:16:02 +02:00
|
|
|
type: string
|
2020-07-13 07:45:07 +02:00
|
|
|
env:
|
|
|
|
- name: DSV_TENANT
|
|
|
|
ini:
|
|
|
|
- section: dsv_lookup
|
|
|
|
key: tenant
|
|
|
|
required: true
|
|
|
|
tld:
|
|
|
|
default: com
|
|
|
|
description: The top-level domain of the tenant; the second format
|
2023-06-10 09:28:40 +02:00
|
|
|
parameter in the default O(url_template).
|
2024-07-21 22:16:02 +02:00
|
|
|
type: string
|
2020-07-13 07:45:07 +02:00
|
|
|
env:
|
|
|
|
- name: DSV_TLD
|
|
|
|
ini:
|
|
|
|
- section: dsv_lookup
|
|
|
|
key: tld
|
|
|
|
required: false
|
|
|
|
client_id:
|
|
|
|
description: The client_id with which to request the Access Grant.
|
2024-07-21 22:16:02 +02:00
|
|
|
type: string
|
2020-07-13 07:45:07 +02:00
|
|
|
env:
|
|
|
|
- name: DSV_CLIENT_ID
|
|
|
|
ini:
|
|
|
|
- section: dsv_lookup
|
|
|
|
key: client_id
|
|
|
|
required: true
|
|
|
|
client_secret:
|
2023-06-10 09:28:40 +02:00
|
|
|
description: The client secret associated with the specific O(client_id).
|
2024-07-21 22:16:02 +02:00
|
|
|
type: string
|
2020-07-13 07:45:07 +02:00
|
|
|
env:
|
|
|
|
- name: DSV_CLIENT_SECRET
|
|
|
|
ini:
|
|
|
|
- section: dsv_lookup
|
|
|
|
key: client_secret
|
|
|
|
required: true
|
|
|
|
url_template:
|
|
|
|
default: https://{}.secretsvaultcloud.{}/v1
|
|
|
|
description: The path to prepend to the base URL to form a valid REST
|
|
|
|
API request.
|
2024-07-21 22:16:02 +02:00
|
|
|
type: string
|
2020-07-13 07:45:07 +02:00
|
|
|
env:
|
|
|
|
- name: DSV_URL_TEMPLATE
|
|
|
|
ini:
|
|
|
|
- section: dsv_lookup
|
|
|
|
key: url_template
|
|
|
|
required: false
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = r"""
|
|
|
|
_list:
|
|
|
|
description:
|
|
|
|
- One or more JSON responses to C(GET /secrets/{path}).
|
|
|
|
- See U(https://dsv.thycotic.com/api/index.html#operation/getSecret).
|
2020-09-16 11:06:45 +02:00
|
|
|
type: list
|
|
|
|
elements: dict
|
2020-07-13 07:45:07 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = r"""
|
|
|
|
- hosts: localhost
|
|
|
|
vars:
|
|
|
|
secret: "{{ lookup('community.general.dsv', '/test/secret') }}"
|
|
|
|
tasks:
|
2020-07-14 17:28:08 +02:00
|
|
|
- ansible.builtin.debug:
|
2020-07-13 07:45:07 +02:00
|
|
|
msg: 'the password is {{ secret["data"]["password"] }}'
|
|
|
|
"""
|
|
|
|
|
|
|
|
from ansible.errors import AnsibleError, AnsibleOptionsError
|
|
|
|
|
|
|
|
sdk_is_missing = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
from thycotic.secrets.vault import (
|
|
|
|
SecretsVault,
|
|
|
|
SecretsVaultError,
|
|
|
|
)
|
|
|
|
except ImportError:
|
|
|
|
sdk_is_missing = True
|
|
|
|
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
from ansible.plugins.lookup import LookupBase
|
|
|
|
|
|
|
|
|
|
|
|
display = Display()
|
|
|
|
|
|
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
|
|
@staticmethod
|
|
|
|
def Client(vault_parameters):
|
2022-04-05 07:25:41 +02:00
|
|
|
try:
|
|
|
|
vault = SecretsVault(**vault_parameters)
|
|
|
|
return vault
|
|
|
|
except TypeError:
|
|
|
|
raise AnsibleError("python-dsv-sdk==0.0.1 must be installed to use this plugin")
|
2020-07-13 07:45:07 +02:00
|
|
|
|
|
|
|
def run(self, terms, variables, **kwargs):
|
|
|
|
if sdk_is_missing:
|
2022-04-05 07:25:41 +02:00
|
|
|
raise AnsibleError("python-dsv-sdk==0.0.1 must be installed to use this plugin")
|
2020-07-13 07:45:07 +02:00
|
|
|
|
|
|
|
self.set_options(var_options=variables, direct=kwargs)
|
|
|
|
|
|
|
|
vault = LookupModule.Client(
|
2020-08-10 06:52:24 +02:00
|
|
|
{
|
2020-07-13 07:45:07 +02:00
|
|
|
"tenant": self.get_option("tenant"),
|
|
|
|
"client_id": self.get_option("client_id"),
|
|
|
|
"client_secret": self.get_option("client_secret"),
|
2022-07-04 20:25:34 +02:00
|
|
|
"tld": self.get_option("tld"),
|
2020-07-13 07:45:07 +02:00
|
|
|
"url_template": self.get_option("url_template"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
result = []
|
|
|
|
|
|
|
|
for term in terms:
|
|
|
|
display.debug("dsv_lookup term: %s" % term)
|
|
|
|
try:
|
|
|
|
path = term.lstrip("[/:]")
|
|
|
|
|
|
|
|
if path == "":
|
|
|
|
raise AnsibleOptionsError("Invalid secret path: %s" % term)
|
|
|
|
|
|
|
|
display.vvv(u"DevOps Secrets Vault GET /secrets/%s" % path)
|
|
|
|
result.append(vault.get_secret_json(path))
|
|
|
|
except SecretsVaultError as error:
|
|
|
|
raise AnsibleError(
|
|
|
|
"DevOps Secrets Vault lookup failure: %s" % error.message
|
|
|
|
)
|
|
|
|
return result
|