2020-07-13 07:37:20 +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:37:20 +02:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = r"""
|
2021-01-12 07:12:03 +01:00
|
|
|
name: tss
|
2020-08-09 21:50:08 +02:00
|
|
|
author: Adam Migus (@amigus) <adam@migus.org>
|
2020-07-13 07:37:20 +02:00
|
|
|
short_description: Get secrets from Thycotic Secret Server
|
|
|
|
version_added: 1.0.0
|
|
|
|
description:
|
|
|
|
- Uses the Thycotic Secret Server Python SDK to get Secrets from Secret
|
2023-06-10 10:53:50 +02:00
|
|
|
Server using token authentication with O(username) and O(password) on
|
|
|
|
the REST API at O(base_url).
|
2022-03-11 06:55:08 +01:00
|
|
|
- When using self-signed certificates the environment variable
|
2023-06-20 11:08:32 +02:00
|
|
|
E(REQUESTS_CA_BUNDLE) can be set to a file containing the trusted certificates
|
2022-03-11 06:55:08 +01:00
|
|
|
(in C(.pem) format).
|
|
|
|
- For example, C(export REQUESTS_CA_BUNDLE='/etc/ssl/certs/ca-bundle.trust.crt').
|
2020-07-13 07:37:20 +02:00
|
|
|
requirements:
|
|
|
|
- python-tss-sdk - https://pypi.org/project/python-tss-sdk/
|
|
|
|
options:
|
|
|
|
_terms:
|
|
|
|
description: The integer ID of the secret.
|
|
|
|
required: true
|
|
|
|
type: int
|
2023-06-09 17:51:37 +02:00
|
|
|
fetch_secret_ids_from_folder:
|
|
|
|
description:
|
|
|
|
- Boolean flag which indicates whether secret ids are in a folder is fetched by folder ID or not.
|
|
|
|
- V(true) then the terms will be considered as a folder IDs. Otherwise (default), they are considered as secret IDs.
|
|
|
|
required: false
|
|
|
|
type: bool
|
|
|
|
version_added: 7.1.0
|
2023-05-03 22:12:09 +02:00
|
|
|
fetch_attachments:
|
|
|
|
description:
|
|
|
|
- Boolean flag which indicates whether attached files will get downloaded or not.
|
2023-06-10 10:53:50 +02:00
|
|
|
- The download will only happen if O(file_download_path) has been provided.
|
2023-05-03 22:12:09 +02:00
|
|
|
required: false
|
|
|
|
type: bool
|
|
|
|
version_added: 7.0.0
|
|
|
|
file_download_path:
|
|
|
|
description: Indicate the file attachment download location.
|
|
|
|
required: false
|
|
|
|
type: path
|
|
|
|
version_added: 7.0.0
|
2020-07-13 07:37:20 +02:00
|
|
|
base_url:
|
2023-06-10 10:53:50 +02:00
|
|
|
description: The base URL of the server, for example V(https://localhost/SecretServer).
|
2020-07-13 07:37:20 +02:00
|
|
|
env:
|
|
|
|
- name: TSS_BASE_URL
|
|
|
|
ini:
|
|
|
|
- section: tss_lookup
|
|
|
|
key: base_url
|
|
|
|
required: true
|
|
|
|
username:
|
|
|
|
description: The username with which to request the OAuth2 Access Grant.
|
|
|
|
env:
|
|
|
|
- name: TSS_USERNAME
|
|
|
|
ini:
|
|
|
|
- section: tss_lookup
|
|
|
|
key: username
|
|
|
|
password:
|
2021-09-14 13:34:59 +02:00
|
|
|
description:
|
|
|
|
- The password associated with the supplied username.
|
2023-06-10 10:53:50 +02:00
|
|
|
- Required when O(token) is not provided.
|
2020-07-13 07:37:20 +02:00
|
|
|
env:
|
|
|
|
- name: TSS_PASSWORD
|
|
|
|
ini:
|
|
|
|
- section: tss_lookup
|
|
|
|
key: password
|
2021-08-20 13:54:29 +02:00
|
|
|
domain:
|
|
|
|
default: ""
|
2021-08-25 06:41:05 +02:00
|
|
|
description:
|
|
|
|
- The domain with which to request the OAuth2 Access Grant.
|
2023-06-10 10:53:50 +02:00
|
|
|
- Optional when O(token) is not provided.
|
2021-08-25 06:41:05 +02:00
|
|
|
- Requires C(python-tss-sdk) version 1.0.0 or greater.
|
2021-08-20 13:54:29 +02:00
|
|
|
env:
|
|
|
|
- name: TSS_DOMAIN
|
|
|
|
ini:
|
|
|
|
- section: tss_lookup
|
|
|
|
key: domain
|
|
|
|
required: false
|
|
|
|
version_added: 3.6.0
|
2021-09-14 13:34:59 +02:00
|
|
|
token:
|
|
|
|
description:
|
|
|
|
- Existing token for Thycotic authorizer.
|
2023-06-10 10:53:50 +02:00
|
|
|
- If provided, O(username) and O(password) are not needed.
|
2021-09-14 13:34:59 +02:00
|
|
|
- Requires C(python-tss-sdk) version 1.0.0 or greater.
|
|
|
|
env:
|
|
|
|
- name: TSS_TOKEN
|
|
|
|
ini:
|
|
|
|
- section: tss_lookup
|
|
|
|
key: token
|
|
|
|
version_added: 3.7.0
|
2020-07-13 07:37:20 +02:00
|
|
|
api_path_uri:
|
|
|
|
default: /api/v1
|
|
|
|
description: The path to append to the base URL to form a valid REST
|
|
|
|
API request.
|
|
|
|
env:
|
|
|
|
- name: TSS_API_PATH_URI
|
|
|
|
required: false
|
|
|
|
token_path_uri:
|
|
|
|
default: /oauth2/token
|
|
|
|
description: The path to append to the base URL to form a valid OAuth2
|
|
|
|
Access Grant request.
|
|
|
|
env:
|
|
|
|
- name: TSS_TOKEN_PATH_URI
|
|
|
|
required: false
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = r"""
|
|
|
|
_list:
|
|
|
|
description:
|
|
|
|
- The JSON responses to C(GET /secrets/{id}).
|
|
|
|
- See U(https://updates.thycotic.net/secretserver/restapiguide/TokenAuth/#operation--secrets--id--get).
|
2020-09-16 11:06:45 +02:00
|
|
|
type: list
|
|
|
|
elements: dict
|
2020-07-13 07:37:20 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = r"""
|
|
|
|
- hosts: localhost
|
|
|
|
vars:
|
2021-09-14 13:34:59 +02:00
|
|
|
secret: >-
|
|
|
|
{{
|
|
|
|
lookup(
|
|
|
|
'community.general.tss',
|
|
|
|
102,
|
|
|
|
base_url='https://secretserver.domain.com/SecretServer/',
|
|
|
|
username='user.name',
|
|
|
|
password='password'
|
|
|
|
)
|
|
|
|
}}
|
2020-07-13 07:37:20 +02:00
|
|
|
tasks:
|
2021-01-19 07:00:14 +01:00
|
|
|
- ansible.builtin.debug:
|
|
|
|
msg: >
|
2021-01-19 08:24:25 +01:00
|
|
|
the password is {{
|
|
|
|
(secret['items']
|
|
|
|
| items2dict(key_name='slug',
|
|
|
|
value_name='itemValue'))['password']
|
|
|
|
}}
|
2021-03-02 21:58:55 +01:00
|
|
|
|
|
|
|
- hosts: localhost
|
|
|
|
vars:
|
|
|
|
secret: >-
|
|
|
|
{{
|
|
|
|
lookup(
|
|
|
|
'community.general.tss',
|
|
|
|
102,
|
|
|
|
base_url='https://secretserver.domain.com/SecretServer/',
|
|
|
|
username='user.name',
|
2021-09-14 13:34:59 +02:00
|
|
|
password='password',
|
|
|
|
domain='domain'
|
2021-03-02 21:58:55 +01:00
|
|
|
)
|
|
|
|
}}
|
|
|
|
tasks:
|
|
|
|
- ansible.builtin.debug:
|
|
|
|
msg: >
|
|
|
|
the password is {{
|
|
|
|
(secret['items']
|
|
|
|
| items2dict(key_name='slug',
|
|
|
|
value_name='itemValue'))['password']
|
|
|
|
}}
|
2021-05-27 07:57:06 +02:00
|
|
|
|
|
|
|
- hosts: localhost
|
|
|
|
vars:
|
|
|
|
secret_password: >-
|
2021-09-14 13:34:59 +02:00
|
|
|
{{
|
|
|
|
((lookup(
|
|
|
|
'community.general.tss',
|
|
|
|
102,
|
|
|
|
base_url='https://secretserver.domain.com/SecretServer/',
|
|
|
|
token='thycotic_access_token',
|
|
|
|
) | from_json).get('items') | items2dict(key_name='slug', value_name='itemValue'))['password']
|
|
|
|
}}
|
2021-05-27 07:57:06 +02:00
|
|
|
tasks:
|
|
|
|
- ansible.builtin.debug:
|
|
|
|
msg: the password is {{ secret_password }}
|
2023-05-03 22:12:09 +02:00
|
|
|
|
|
|
|
# Private key stores into certificate file which is attached with secret.
|
|
|
|
# If fetch_attachments=True then private key file will be download on specified path
|
|
|
|
# and file content will display in debug message.
|
|
|
|
- hosts: localhost
|
|
|
|
vars:
|
|
|
|
secret: >-
|
|
|
|
{{
|
|
|
|
lookup(
|
|
|
|
'community.general.tss',
|
|
|
|
102,
|
|
|
|
fetch_attachments=True,
|
|
|
|
file_download_path='/home/certs',
|
|
|
|
base_url='https://secretserver.domain.com/SecretServer/',
|
|
|
|
token='thycotic_access_token'
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
tasks:
|
|
|
|
- ansible.builtin.debug:
|
|
|
|
msg: >
|
|
|
|
the private key is {{
|
|
|
|
(secret['items']
|
|
|
|
| items2dict(key_name='slug',
|
|
|
|
value_name='itemValue'))['private-key']
|
|
|
|
}}
|
2023-06-09 17:51:37 +02:00
|
|
|
|
|
|
|
# If fetch_secret_ids_from_folder=true then secret IDs are in a folder is fetched based on folder ID
|
|
|
|
- hosts: localhost
|
|
|
|
vars:
|
|
|
|
secret: >-
|
|
|
|
{{
|
|
|
|
lookup(
|
|
|
|
'community.general.tss',
|
|
|
|
102,
|
|
|
|
fetch_secret_ids_from_folder=true,
|
|
|
|
base_url='https://secretserver.domain.com/SecretServer/',
|
|
|
|
token='thycotic_access_token'
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
tasks:
|
|
|
|
- ansible.builtin.debug:
|
|
|
|
msg: >
|
|
|
|
the secret id's are {{
|
|
|
|
secret
|
|
|
|
}}
|
2020-07-13 07:37:20 +02:00
|
|
|
"""
|
|
|
|
|
2021-08-25 06:41:05 +02:00
|
|
|
import abc
|
2023-05-03 22:12:09 +02:00
|
|
|
import os
|
2021-08-25 06:41:05 +02:00
|
|
|
from ansible.errors import AnsibleError, AnsibleOptionsError
|
|
|
|
from ansible.module_utils import six
|
|
|
|
from ansible.plugins.lookup import LookupBase
|
|
|
|
from ansible.utils.display import Display
|
2020-07-13 07:37:20 +02:00
|
|
|
|
|
|
|
try:
|
2023-06-09 17:51:37 +02:00
|
|
|
from delinea.secrets.server import SecretServer, SecretServerError
|
2020-07-13 07:37:20 +02:00
|
|
|
|
2021-08-25 06:41:05 +02:00
|
|
|
HAS_TSS_SDK = True
|
2023-06-09 17:51:37 +02:00
|
|
|
HAS_DELINEA_SS_SDK = True
|
2021-08-18 09:26:44 +02:00
|
|
|
except ImportError:
|
2022-08-23 21:53:06 +02:00
|
|
|
try:
|
2023-06-09 17:51:37 +02:00
|
|
|
from thycotic.secrets.server import SecretServer, SecretServerError
|
2022-08-23 21:53:06 +02:00
|
|
|
|
|
|
|
HAS_TSS_SDK = True
|
2023-06-09 17:51:37 +02:00
|
|
|
HAS_DELINEA_SS_SDK = False
|
2022-08-23 21:53:06 +02:00
|
|
|
except ImportError:
|
|
|
|
SecretServer = None
|
|
|
|
SecretServerError = None
|
|
|
|
HAS_TSS_SDK = False
|
2023-06-09 17:51:37 +02:00
|
|
|
HAS_DELINEA_SS_SDK = False
|
2021-08-18 09:26:44 +02:00
|
|
|
|
|
|
|
try:
|
2021-09-14 13:34:59 +02:00
|
|
|
from thycotic.secrets.server import PasswordGrantAuthorizer, DomainPasswordGrantAuthorizer, AccessTokenAuthorizer
|
2021-08-20 13:54:29 +02:00
|
|
|
|
2021-08-25 06:41:05 +02:00
|
|
|
HAS_TSS_AUTHORIZER = True
|
2021-08-18 09:26:44 +02:00
|
|
|
except ImportError:
|
2022-08-23 21:53:06 +02:00
|
|
|
try:
|
|
|
|
from delinea.secrets.server import PasswordGrantAuthorizer, DomainPasswordGrantAuthorizer, AccessTokenAuthorizer
|
|
|
|
|
|
|
|
HAS_TSS_AUTHORIZER = True
|
|
|
|
except ImportError:
|
|
|
|
PasswordGrantAuthorizer = None
|
|
|
|
DomainPasswordGrantAuthorizer = None
|
|
|
|
AccessTokenAuthorizer = None
|
|
|
|
HAS_TSS_AUTHORIZER = False
|
2021-08-18 09:26:44 +02:00
|
|
|
|
2020-07-13 07:37:20 +02:00
|
|
|
|
|
|
|
display = Display()
|
|
|
|
|
|
|
|
|
2021-08-25 06:41:05 +02:00
|
|
|
@six.add_metaclass(abc.ABCMeta)
|
|
|
|
class TSSClient(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._client = None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_params(**server_parameters):
|
|
|
|
if HAS_TSS_AUTHORIZER:
|
|
|
|
return TSSClientV1(**server_parameters)
|
|
|
|
else:
|
|
|
|
return TSSClientV0(**server_parameters)
|
|
|
|
|
2023-05-03 22:12:09 +02:00
|
|
|
def get_secret(self, term, fetch_file_attachments, file_download_path):
|
2021-08-25 06:41:05 +02:00
|
|
|
display.debug("tss_lookup term: %s" % term)
|
|
|
|
secret_id = self._term_to_secret_id(term)
|
|
|
|
display.vvv(u"Secret Server lookup of Secret with ID %d" % secret_id)
|
|
|
|
|
2023-05-03 22:12:09 +02:00
|
|
|
if fetch_file_attachments:
|
|
|
|
obj = self._client.get_secret(secret_id, fetch_file_attachments)
|
|
|
|
for i in obj['items']:
|
|
|
|
if file_download_path and os.path.isdir(file_download_path):
|
|
|
|
if i['isFile']:
|
|
|
|
try:
|
|
|
|
with open(os.path.join(file_download_path, str(obj['id']) + "_" + i['slug']), "w") as f:
|
|
|
|
f.write(i['itemValue'].text)
|
|
|
|
i['itemValue'] = "*** Not Valid For Display ***"
|
|
|
|
except ValueError:
|
|
|
|
raise AnsibleOptionsError("Failed to download {0}".format(str(i['slug'])))
|
|
|
|
else:
|
|
|
|
raise AnsibleOptionsError("File download path does not exist")
|
|
|
|
return obj
|
|
|
|
else:
|
|
|
|
return self._client.get_secret_json(secret_id)
|
2021-08-25 06:41:05 +02:00
|
|
|
|
2023-06-09 17:51:37 +02:00
|
|
|
def get_secret_ids_by_folderid(self, term):
|
|
|
|
display.debug("tss_lookup term: %s" % term)
|
|
|
|
folder_id = self._term_to_folder_id(term)
|
|
|
|
display.vvv(u"Secret Server lookup of Secret id's with Folder ID %d" % folder_id)
|
|
|
|
|
|
|
|
return self._client.get_secret_ids_by_folderid(folder_id)
|
|
|
|
|
2020-07-13 07:37:20 +02:00
|
|
|
@staticmethod
|
2021-08-25 06:41:05 +02:00
|
|
|
def _term_to_secret_id(term):
|
|
|
|
try:
|
|
|
|
return int(term)
|
|
|
|
except ValueError:
|
|
|
|
raise AnsibleOptionsError("Secret ID must be an integer")
|
|
|
|
|
2023-06-09 17:51:37 +02:00
|
|
|
@staticmethod
|
|
|
|
def _term_to_folder_id(term):
|
|
|
|
try:
|
|
|
|
return int(term)
|
|
|
|
except ValueError:
|
|
|
|
raise AnsibleOptionsError("Folder ID must be an integer")
|
|
|
|
|
2021-08-25 06:41:05 +02:00
|
|
|
|
|
|
|
class TSSClientV0(TSSClient):
|
|
|
|
def __init__(self, **server_parameters):
|
|
|
|
super(TSSClientV0, self).__init__()
|
|
|
|
|
|
|
|
if server_parameters.get("domain"):
|
|
|
|
raise AnsibleError("The 'domain' option requires 'python-tss-sdk' version 1.0.0 or greater")
|
|
|
|
|
|
|
|
self._client = SecretServer(
|
|
|
|
server_parameters["base_url"],
|
|
|
|
server_parameters["username"],
|
|
|
|
server_parameters["password"],
|
|
|
|
server_parameters["api_path_uri"],
|
|
|
|
server_parameters["token_path_uri"],
|
|
|
|
)
|
|
|
|
|
2021-08-05 19:28:32 +02:00
|
|
|
|
2021-08-25 06:41:05 +02:00
|
|
|
class TSSClientV1(TSSClient):
|
|
|
|
def __init__(self, **server_parameters):
|
|
|
|
super(TSSClientV1, self).__init__()
|
|
|
|
|
|
|
|
authorizer = self._get_authorizer(**server_parameters)
|
|
|
|
self._client = SecretServer(
|
|
|
|
server_parameters["base_url"], authorizer, server_parameters["api_path_uri"]
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _get_authorizer(**server_parameters):
|
2021-09-14 13:34:59 +02:00
|
|
|
if server_parameters.get("token"):
|
|
|
|
return AccessTokenAuthorizer(
|
|
|
|
server_parameters["token"],
|
|
|
|
)
|
|
|
|
|
2021-08-25 06:41:05 +02:00
|
|
|
if server_parameters.get("domain"):
|
|
|
|
return DomainPasswordGrantAuthorizer(
|
2021-08-05 19:28:32 +02:00
|
|
|
server_parameters["base_url"],
|
|
|
|
server_parameters["username"],
|
2021-08-25 06:41:05 +02:00
|
|
|
server_parameters["domain"],
|
2021-08-05 19:28:32 +02:00
|
|
|
server_parameters["password"],
|
|
|
|
server_parameters["token_path_uri"],
|
|
|
|
)
|
2020-07-13 07:37:20 +02:00
|
|
|
|
2021-08-25 06:41:05 +02:00
|
|
|
return PasswordGrantAuthorizer(
|
|
|
|
server_parameters["base_url"],
|
|
|
|
server_parameters["username"],
|
|
|
|
server_parameters["password"],
|
|
|
|
server_parameters["token_path_uri"],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class LookupModule(LookupBase):
|
2020-07-13 07:37:20 +02:00
|
|
|
def run(self, terms, variables, **kwargs):
|
2021-08-25 06:41:05 +02:00
|
|
|
if not HAS_TSS_SDK:
|
2020-07-13 07:37:20 +02:00
|
|
|
raise AnsibleError("python-tss-sdk must be installed to use this plugin")
|
|
|
|
|
|
|
|
self.set_options(var_options=variables, direct=kwargs)
|
|
|
|
|
2021-08-25 06:41:05 +02:00
|
|
|
tss = TSSClient.from_params(
|
|
|
|
base_url=self.get_option("base_url"),
|
|
|
|
username=self.get_option("username"),
|
|
|
|
password=self.get_option("password"),
|
|
|
|
domain=self.get_option("domain"),
|
2021-09-14 13:34:59 +02:00
|
|
|
token=self.get_option("token"),
|
2021-08-25 06:41:05 +02:00
|
|
|
api_path_uri=self.get_option("api_path_uri"),
|
|
|
|
token_path_uri=self.get_option("token_path_uri"),
|
2020-07-13 07:37:20 +02:00
|
|
|
)
|
2021-08-25 06:41:05 +02:00
|
|
|
|
|
|
|
try:
|
2023-06-09 17:51:37 +02:00
|
|
|
if self.get_option("fetch_secret_ids_from_folder"):
|
|
|
|
if HAS_DELINEA_SS_SDK:
|
|
|
|
return [tss.get_secret_ids_by_folderid(term) for term in terms]
|
|
|
|
else:
|
|
|
|
raise AnsibleError("latest python-tss-sdk must be installed to use this plugin")
|
|
|
|
else:
|
|
|
|
return [tss.get_secret(term, self.get_option("fetch_attachments"), self.get_option("file_download_path")) for term in terms]
|
2021-08-25 06:41:05 +02:00
|
|
|
except SecretServerError as error:
|
|
|
|
raise AnsibleError("Secret Server lookup failure: %s" % error.message)
|