| 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: tss |
| 8 |
author: Adam Migus (@amigus) <[email protected]> |
| 9 |
short_description: Get secrets from Delinea Secret Server |
| 10 |
version_added: 1.0.0 |
| 11 |
description: |
| 12 |
- Uses the Thycotic Secret Server Python SDK to get Secrets from Secret Server using token authentication with O(username) |
| 13 |
and O(password) on the REST API at O(base_url). |
| 14 |
- When using self-signed certificates the environment variable E(REQUESTS_CA_BUNDLE) can be set to a file containing the |
| 15 |
trusted certificates (in C(.pem) format). |
| 16 |
- For example, C(export REQUESTS_CA_BUNDLE='/etc/ssl/certs/ca-bundle.trust.crt'). |
| 17 |
requirements: |
| 18 |
- python-tss-sdk - https://pypi.org/project/python-tss-sdk/ |
| 19 |
options: |
| 20 |
_terms: |
| 21 |
description: The integer ID of the secret. |
| 22 |
required: true |
| 23 |
type: list |
| 24 |
elements: int |
| 25 |
secret_path: |
| 26 |
description: Indicate a full path of secret including folder and secret name when the secret ID is set to 0. |
| 27 |
required: false |
| 28 |
type: str |
| 29 |
version_added: 7.2.0 |
| 30 |
fetch_secret_ids_from_folder: |
| 31 |
description: |
| 32 |
- Boolean flag which indicates whether secret IDs are in a folder is fetched by folder ID or not. |
| 33 |
- V(true) then the terms are considered as a folder IDs. Otherwise (default), they are considered as secret IDs. |
| 34 |
required: false |
| 35 |
type: bool |
| 36 |
version_added: 7.1.0 |
| 37 |
fetch_attachments: |
| 38 |
description: |
| 39 |
- Boolean flag which indicates whether attached files are downloaded or not. |
| 40 |
- The download only happens if O(file_download_path) has been provided. |
| 41 |
required: false |
| 42 |
type: bool |
| 43 |
version_added: 7.0.0 |
| 44 |
file_download_path: |
| 45 |
description: Indicate the file attachment download location. |
| 46 |
required: false |
| 47 |
type: path |
| 48 |
version_added: 7.0.0 |
| 49 |
base_url: |
| 50 |
description: The base URL of the server, for example V(https://localhost/SecretServer). |
| 51 |
type: string |
| 52 |
env: |
| 53 |
- name: TSS_BASE_URL |
| 54 |
ini: |
| 55 |
- section: tss_lookup |
| 56 |
key: base_url |
| 57 |
required: true |
| 58 |
username: |
| 59 |
description: The username with which to request the OAuth2 Access Grant. |
| 60 |
type: string |
| 61 |
env: |
| 62 |
- name: TSS_USERNAME |
| 63 |
ini: |
| 64 |
- section: tss_lookup |
| 65 |
key: username |
| 66 |
password: |
| 67 |
description: |
| 68 |
- The password associated with the supplied username. |
| 69 |
- Required when O(token) is not provided. |
| 70 |
type: string |
| 71 |
env: |
| 72 |
- name: TSS_PASSWORD |
| 73 |
ini: |
| 74 |
- section: tss_lookup |
| 75 |
key: password |
| 76 |
domain: |
| 77 |
default: "" |
| 78 |
description: |
| 79 |
- The domain with which to request the OAuth2 Access Grant. |
| 80 |
- Optional when O(token) is not provided. |
| 81 |
- Requires C(python-tss-sdk) version 1.0.0 or greater. |
| 82 |
type: string |
| 83 |
env: |
| 84 |
- name: TSS_DOMAIN |
| 85 |
ini: |
| 86 |
- section: tss_lookup |
| 87 |
key: domain |
| 88 |
required: false |
| 89 |
version_added: 3.6.0 |
| 90 |
token: |
| 91 |
description: |
| 92 |
- Existing token for Thycotic authorizer. |
| 93 |
- If provided, O(username) and O(password) are not needed. |
| 94 |
- Requires C(python-tss-sdk) version 1.0.0 or greater. |
| 95 |
type: string |
| 96 |
env: |
| 97 |
- name: TSS_TOKEN |
| 98 |
ini: |
| 99 |
- section: tss_lookup |
| 100 |
key: token |
| 101 |
version_added: 3.7.0 |
| 102 |
api_path_uri: |
| 103 |
default: /api/v1 |
| 104 |
description: The path to append to the base URL to form a valid REST API request. |
| 105 |
type: string |
| 106 |
env: |
| 107 |
- name: TSS_API_PATH_URI |
| 108 |
required: false |
| 109 |
token_path_uri: |
| 110 |
default: /oauth2/token |
| 111 |
description: The path to append to the base URL to form a valid OAuth2 Access Grant request. |
| 112 |
type: string |
| 113 |
env: |
| 114 |
- name: TSS_TOKEN_PATH_URI |
| 115 |
required: false |
| 116 |
""" |
| 117 |
|
| 118 |
RETURN = r""" |
| 119 |
_list: |
| 120 |
description: |
| 121 |
- The JSON responses to C(GET /secrets/{id}) and C(GET /secrets/{path}). |
| 122 |
- See U(https://updates.thycotic.net/secretserver/restapiguide/TokenAuth/#operation--secrets--id--get). |
| 123 |
type: list |
| 124 |
elements: dict |
| 125 |
""" |
| 126 |
|
| 127 |
EXAMPLES = r""" |
| 128 |
# Using Secret Server Authentication |
| 129 |
- name: Lookup secret using Secret Server user credentials |
| 130 |
hosts: localhost |
| 131 |
vars: |
| 132 |
secret: >- |
| 133 |
{{ |
| 134 |
lookup( |
| 135 |
'community.general.tss', |
| 136 |
102, |
| 137 |
base_url='https://secretserver.domain.com/SecretServer/', |
| 138 |
username='user.name', |
| 139 |
password='password' |
| 140 |
) |
| 141 |
}} |
| 142 |
tasks: |
| 143 |
- ansible.builtin.debug: |
| 144 |
msg: > |
| 145 |
the password is {{ |
| 146 |
(secret['items'] |
| 147 |
| items2dict(key_name='slug', |
| 148 |
value_name='itemValue'))['password'] |
| 149 |
}} |
| 150 |
|
| 151 |
- name: Lookup secret with domain user |
| 152 |
hosts: localhost |
| 153 |
vars: |
| 154 |
secret: >- |
| 155 |
{{ |
| 156 |
lookup( |
| 157 |
'community.general.tss', |
| 158 |
102, |
| 159 |
base_url='https://secretserver.domain.com/SecretServer/', |
| 160 |
username='user.name', |
| 161 |
password='password', |
| 162 |
domain='domain' |
| 163 |
) |
| 164 |
}} |
| 165 |
tasks: |
| 166 |
- ansible.builtin.debug: |
| 167 |
msg: > |
| 168 |
the password is {{ |
| 169 |
(secret['items'] |
| 170 |
| items2dict(key_name='slug', |
| 171 |
value_name='itemValue'))['password'] |
| 172 |
}} |
| 173 |
|
| 174 |
- name: Lookup secret using Secret Server token |
| 175 |
hosts: localhost |
| 176 |
vars: |
| 177 |
secret_password: >- |
| 178 |
{{ |
| 179 |
((lookup( |
| 180 |
'community.general.tss', |
| 181 |
102, |
| 182 |
base_url='https://secretserver.domain.com/SecretServer/', |
| 183 |
token='thycotic_access_token', |
| 184 |
) | from_json).get('items') | items2dict(key_name='slug', value_name='itemValue'))['password'] |
| 185 |
}} |
| 186 |
tasks: |
| 187 |
- ansible.builtin.debug: |
| 188 |
msg: the password is {{ secret_password }} |
| 189 |
|
| 190 |
# Private key stores into certificate file which is attached with secret. |
| 191 |
# If fetch_attachments=True then private key file will be download on specified path |
| 192 |
# and file content will display in debug message. |
| 193 |
- name: Lookup secret and fetch attachments using Secret Server token |
| 194 |
hosts: localhost |
| 195 |
vars: |
| 196 |
secret: >- |
| 197 |
{{ |
| 198 |
lookup( |
| 199 |
'community.general.tss', |
| 200 |
102, |
| 201 |
fetch_attachments=True, |
| 202 |
file_download_path='/home/certs', |
| 203 |
base_url='https://secretserver.domain.com/SecretServer/', |
| 204 |
token='thycotic_access_token' |
| 205 |
) |
| 206 |
}} |
| 207 |
tasks: |
| 208 |
- ansible.builtin.debug: |
| 209 |
msg: > |
| 210 |
the private key is {{ |
| 211 |
(secret['items'] |
| 212 |
| items2dict(key_name='slug', |
| 213 |
value_name='itemValue'))['private-key'] |
| 214 |
}} |
| 215 |
|
| 216 |
# If fetch_secret_ids_from_folder=true then secret IDs are in a folder is fetched based on folder ID |
| 217 |
- name: Lookup secret IDs by folder ID using Secret Server token |
| 218 |
hosts: localhost |
| 219 |
vars: |
| 220 |
secret: >- |
| 221 |
{{ |
| 222 |
lookup( |
| 223 |
'community.general.tss', |
| 224 |
102, |
| 225 |
fetch_secret_ids_from_folder=true, |
| 226 |
base_url='https://secretserver.domain.com/SecretServer/', |
| 227 |
token='thycotic_access_token' |
| 228 |
) |
| 229 |
}} |
| 230 |
tasks: |
| 231 |
- ansible.builtin.debug: |
| 232 |
msg: > |
| 233 |
the secret id's are {{ |
| 234 |
secret |
| 235 |
}} |
| 236 |
|
| 237 |
# If secret ID is 0 and secret_path has value then secret is fetched by secret path |
| 238 |
- name: Lookup secret by secret path using Secret Server user credentials |
| 239 |
hosts: localhost |
| 240 |
vars: |
| 241 |
secret: >- |
| 242 |
{{ |
| 243 |
lookup( |
| 244 |
'community.general.tss', |
| 245 |
0, |
| 246 |
secret_path='\folderName\secretName' |
| 247 |
base_url='https://secretserver.domain.com/SecretServer/', |
| 248 |
username='user.name', |
| 249 |
password='password' |
| 250 |
) |
| 251 |
}} |
| 252 |
tasks: |
| 253 |
- ansible.builtin.debug: |
| 254 |
msg: >- |
| 255 |
the password is {{ |
| 256 |
(secret['items'] |
| 257 |
| items2dict(key_name='slug', |
| 258 |
value_name='itemValue'))['password'] |
| 259 |
}} |
| 260 |
|
| 261 |
# Using Platform Authentication |
| 262 |
- name: Lookup secret using Platform service user credentials |
| 263 |
hosts: localhost |
| 264 |
vars: |
| 265 |
secret: >- |
| 266 |
{{ |
| 267 |
lookup( |
| 268 |
'community.general.tss', |
| 269 |
102, |
| 270 |
base_url='https://platform.delinea.app/', |
| 271 |
username='platform_service_username', |
| 272 |
password='platform_service_user_password' |
| 273 |
) |
| 274 |
}} |
| 275 |
tasks: |
| 276 |
- ansible.builtin.debug: |
| 277 |
msg: > |
| 278 |
the password is {{ |
| 279 |
(secret['items'] |
| 280 |
| items2dict(key_name='slug', |
| 281 |
value_name='itemValue'))['password'] |
| 282 |
}} |
| 283 |
|
| 284 |
- name: Lookup secret using platform token |
| 285 |
hosts: localhost |
| 286 |
vars: |
| 287 |
secret_password: >- |
| 288 |
{{ |
| 289 |
((lookup( |
| 290 |
'community.general.tss', |
| 291 |
102, |
| 292 |
base_url='https://platform.delinea.app/', |
| 293 |
token='delinea_platform_access_token', |
| 294 |
) | from_json).get('items') | items2dict(key_name='slug', value_name='itemValue'))['password'] |
| 295 |
}} |
| 296 |
tasks: |
| 297 |
- ansible.builtin.debug: |
| 298 |
msg: the password is {{ secret_password }} |
| 299 |
""" |
| 300 |
|
| 301 |
import abc |
| 302 |
import os |
| 303 |
|
| 304 |
from ansible.errors import AnsibleError, AnsibleOptionsError |
| 305 |
from ansible.plugins.lookup import LookupBase |
| 306 |
from ansible.utils.display import Display |
| 307 |
|
| 308 |
from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms |
| 309 |
|
| 310 |
try: |
| 311 |
from delinea.secrets.server import ( |
| 312 |
AccessTokenAuthorizer, |
| 313 |
DomainPasswordGrantAuthorizer, |
| 314 |
PasswordGrantAuthorizer, |
| 315 |
SecretServer, |
| 316 |
SecretServerError, |
| 317 |
) |
| 318 |
|
| 319 |
HAS_TSS_SDK = True |
| 320 |
HAS_DELINEA_SS_SDK = True |
| 321 |
HAS_TSS_AUTHORIZER = True |
| 322 |
except ImportError: |
| 323 |
try: |
| 324 |
from thycotic.secrets.server import ( |
| 325 |
AccessTokenAuthorizer, |
| 326 |
DomainPasswordGrantAuthorizer, |
| 327 |
PasswordGrantAuthorizer, |
| 328 |
SecretServer, |
| 329 |
SecretServerError, |
| 330 |
) |
| 331 |
|
| 332 |
HAS_TSS_SDK = True |
| 333 |
HAS_DELINEA_SS_SDK = False |
| 334 |
HAS_TSS_AUTHORIZER = True |
| 335 |
except ImportError: |
| 336 |
SecretServer = None |
| 337 |
SecretServerError = None |
| 338 |
HAS_TSS_SDK = False |
| 339 |
HAS_DELINEA_SS_SDK = False |
| 340 |
PasswordGrantAuthorizer = None |
| 341 |
DomainPasswordGrantAuthorizer = None |
| 342 |
AccessTokenAuthorizer = None |
| 343 |
HAS_TSS_AUTHORIZER = False |
| 344 |
|
| 345 |
|
| 346 |
display = Display() |
| 347 |
|
| 348 |
|
| 349 |
class TSSClient(metaclass=abc.ABCMeta): # noqa: B024 |
| 350 |
def __init__(self): |
| 351 |
self._client = None |
| 352 |
|
| 353 |
@staticmethod |
| 354 |
def from_params(**server_parameters): |
| 355 |
if HAS_TSS_AUTHORIZER: |
| 356 |
return TSSClientV1(**server_parameters) |
| 357 |
else: |
| 358 |
return TSSClientV0(**server_parameters) |
| 359 |
|
| 360 |
def get_secret(self, term, secret_path, fetch_file_attachments, file_download_path): |
| 361 |
display.debug(f"tss_lookup term: {term}") |
| 362 |
secret_id = self._term_to_secret_id(term) |
| 363 |
if secret_id == 0 and secret_path: |
| 364 |
fetch_secret_by_path = True |
| 365 |
display.vvv(f"Secret Server lookup of Secret with path {secret_path}") |
| 366 |
else: |
| 367 |
fetch_secret_by_path = False |
| 368 |
display.vvv(f"Secret Server lookup of Secret with ID {secret_id}") |
| 369 |
|
| 370 |
if fetch_file_attachments: |
| 371 |
if fetch_secret_by_path: |
| 372 |
obj = self._client.get_secret_by_path(secret_path, fetch_file_attachments) |
| 373 |
else: |
| 374 |
obj = self._client.get_secret(secret_id, fetch_file_attachments) |
| 375 |
for i in obj["items"]: |
| 376 |
if file_download_path and os.path.isdir(file_download_path): |
| 377 |
if i["isFile"]: |
| 378 |
try: |
| 379 |
file_content = i["itemValue"].content |
| 380 |
with open(os.path.join(file_download_path, f"{obj['id']}_{i['slug']}"), "wb") as f: |
| 381 |
f.write(file_content) |
| 382 |
except ValueError as e: |
| 383 |
raise AnsibleOptionsError(f"Failed to download {i['slug']}") from e |
| 384 |
except AttributeError: |
| 385 |
display.warning(f"Could not read file content for {i['slug']}") |
| 386 |
finally: |
| 387 |
i["itemValue"] = "*** Not Valid For Display ***" |
| 388 |
else: |
| 389 |
raise AnsibleOptionsError("File download path does not exist") |
| 390 |
return obj |
| 391 |
else: |
| 392 |
if fetch_secret_by_path: |
| 393 |
return self._client.get_secret_by_path(secret_path, False) |
| 394 |
else: |
| 395 |
return self._client.get_secret_json(secret_id) |
| 396 |
|
| 397 |
def get_secret_ids_by_folderid(self, term): |
| 398 |
display.debug(f"tss_lookup term: {term}") |
| 399 |
folder_id = self._term_to_folder_id(term) |
| 400 |
display.vvv(f"Secret Server lookup of Secret id's with Folder ID {folder_id}") |
| 401 |
|
| 402 |
return self._client.get_secret_ids_by_folderid(folder_id) |
| 403 |
|
| 404 |
@staticmethod |
| 405 |
def _term_to_secret_id(term): |
| 406 |
try: |
| 407 |
return int(term) |
| 408 |
except ValueError as e: |
| 409 |
raise AnsibleOptionsError("Secret ID must be an integer") from e |
| 410 |
|
| 411 |
@staticmethod |
| 412 |
def _term_to_folder_id(term): |
| 413 |
try: |
| 414 |
return int(term) |
| 415 |
except ValueError as e: |
| 416 |
raise AnsibleOptionsError("Folder ID must be an integer") from e |
| 417 |
|
| 418 |
|
| 419 |
class TSSClientV0(TSSClient): |
| 420 |
def __init__(self, **server_parameters): |
| 421 |
super().__init__() |
| 422 |
|
| 423 |
if server_parameters.get("domain"): |
| 424 |
raise AnsibleError("The 'domain' option requires 'python-tss-sdk' version 1.0.0 or greater") |
| 425 |
|
| 426 |
self._client = SecretServer( |
| 427 |
server_parameters["base_url"], |
| 428 |
server_parameters["username"], |
| 429 |
server_parameters["password"], |
| 430 |
server_parameters["api_path_uri"], |
| 431 |
server_parameters["token_path_uri"], |
| 432 |
) |
| 433 |
|
| 434 |
|
| 435 |
class TSSClientV1(TSSClient): |
| 436 |
def __init__(self, **server_parameters): |
| 437 |
super().__init__() |
| 438 |
|
| 439 |
authorizer = self._get_authorizer(**server_parameters) |
| 440 |
self._client = SecretServer(server_parameters["base_url"], authorizer, server_parameters["api_path_uri"]) |
| 441 |
|
| 442 |
@staticmethod |
| 443 |
def _get_authorizer(**server_parameters): |
| 444 |
if server_parameters.get("token"): |
| 445 |
return AccessTokenAuthorizer(server_parameters["token"], server_parameters["base_url"]) |
| 446 |
|
| 447 |
if server_parameters.get("domain"): |
| 448 |
return DomainPasswordGrantAuthorizer( |
| 449 |
server_parameters["base_url"], |
| 450 |
server_parameters["username"], |
| 451 |
server_parameters["domain"], |
| 452 |
server_parameters["password"], |
| 453 |
server_parameters["token_path_uri"], |
| 454 |
) |
| 455 |
|
| 456 |
return PasswordGrantAuthorizer( |
| 457 |
server_parameters["base_url"], |
| 458 |
server_parameters["username"], |
| 459 |
server_parameters["password"], |
| 460 |
server_parameters["token_path_uri"], |
| 461 |
) |
| 462 |
|
| 463 |
|
| 464 |
class LookupModule(LookupBase): |
| 465 |
def run(self, terms, variables, **kwargs): |
| 466 |
if not HAS_TSS_SDK: |
| 467 |
raise AnsibleError("python-tss-sdk must be installed to use this plugin") |
| 468 |
|
| 469 |
self.set_options(var_options=variables, direct=kwargs) |
| 470 |
check_for_wrong_terms(self, direct=kwargs) |
| 471 |
|
| 472 |
tss = TSSClient.from_params( |
| 473 |
base_url=self.get_option("base_url"), |
| 474 |
username=self.get_option("username"), |
| 475 |
password=self.get_option("password"), |
| 476 |
domain=self.get_option("domain"), |
| 477 |
token=self.get_option("token"), |
| 478 |
api_path_uri=self.get_option("api_path_uri"), |
| 479 |
token_path_uri=self.get_option("token_path_uri"), |
| 480 |
) |
| 481 |
|
| 482 |
try: |
| 483 |
if self.get_option("fetch_secret_ids_from_folder"): |
| 484 |
if HAS_DELINEA_SS_SDK: |
| 485 |
return [tss.get_secret_ids_by_folderid(term) for term in terms] |
| 486 |
else: |
| 487 |
raise AnsibleError("latest python-tss-sdk must be installed to use this plugin") |
| 488 |
else: |
| 489 |
return [ |
| 490 |
tss.get_secret( |
| 491 |
term, |
| 492 |
self.get_option("secret_path"), |
| 493 |
self.get_option("fetch_attachments"), |
| 494 |
self.get_option("file_download_path"), |
| 495 |
) |
| 496 |
for term in terms |
| 497 |
] |
| 498 |
except SecretServerError as error: |
| 499 |
raise AnsibleError(f"Secret Server lookup failure: {error.message}") from error |