| 1 |
# Copyright (c) 2017, Edward Nunez <[email protected]> |
| 2 |
# Copyright (c) 2017 Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
author: Unknown (!UNKNOWN) |
| 9 |
name: cyberarkpassword |
| 10 |
short_description: Get secrets from CyberArk AIM |
| 11 |
requirements: |
| 12 |
- CyberArk AIM tool installed |
| 13 |
description: |
| 14 |
- Get secrets from CyberArk AIM. |
| 15 |
options: |
| 16 |
_command: |
| 17 |
description: Cyberark CLI utility. |
| 18 |
type: string |
| 19 |
env: |
| 20 |
- name: AIM_CLIPASSWORDSDK_CMD |
| 21 |
default: '/opt/CARKaim/sdk/clipasswordsdk' |
| 22 |
appid: |
| 23 |
description: Defines the unique ID of the application that is issuing the password request. |
| 24 |
type: string |
| 25 |
required: true |
| 26 |
query: |
| 27 |
description: Describes the filter criteria for the password retrieval. |
| 28 |
type: string |
| 29 |
required: true |
| 30 |
output: |
| 31 |
description: |
| 32 |
- Specifies the desired output fields separated by commas. |
| 33 |
- 'They could be: Password, PassProps.<property>, PasswordChangeInProcess.' |
| 34 |
type: string |
| 35 |
default: 'password' |
| 36 |
_extra: |
| 37 |
description: For extra_params values please check parameters for clipasswordsdk in CyberArk's "Credential Provider and |
| 38 |
ASCP Implementation Guide". |
| 39 |
notes: |
| 40 |
- For Ansible on Windows, please change the -parameters (C(-p), C(-d), and C(-o)) to /parameters (C(/p), C(/d), and C(/o)) |
| 41 |
and change the location of C(CLIPasswordSDK.exe). |
| 42 |
""" |
| 43 |
|
| 44 |
EXAMPLES = r""" |
| 45 |
- name: passing options to the lookup |
| 46 |
ansible.builtin.debug: |
| 47 |
msg: '{{ lookup("community.general.cyberarkpassword", cyquery) }}' |
| 48 |
vars: |
| 49 |
cyquery: |
| 50 |
appid: "app_ansible" |
| 51 |
query: "safe=CyberArk_Passwords;folder=root;object=AdminPass" |
| 52 |
output: "Password,PassProps.UserName,PassProps.Address,PasswordChangeInProcess" |
| 53 |
|
| 54 |
|
| 55 |
- name: used in a loop |
| 56 |
ansible.builtin.debug: |
| 57 |
msg: "{{item}}" |
| 58 |
with_community.general.cyberarkpassword: |
| 59 |
appid: 'app_ansible' |
| 60 |
query: 'safe=CyberArk_Passwords;folder=root;object=AdminPass' |
| 61 |
output: 'Password,PassProps.UserName,PassProps.Address,PasswordChangeInProcess' |
| 62 |
""" |
| 63 |
|
| 64 |
RETURN = r""" |
| 65 |
_result: |
| 66 |
description: A list containing one dictionary. |
| 67 |
type: list |
| 68 |
elements: dictionary |
| 69 |
contains: |
| 70 |
password: |
| 71 |
description: |
| 72 |
- The actual value stored. |
| 73 |
passprops: |
| 74 |
description: Properties assigned to the entry. |
| 75 |
type: dictionary |
| 76 |
passwordchangeinprocess: |
| 77 |
description: Did the password change? |
| 78 |
""" |
| 79 |
|
| 80 |
import os |
| 81 |
import subprocess |
| 82 |
from subprocess import PIPE, Popen |
| 83 |
|
| 84 |
from ansible.errors import AnsibleError |
| 85 |
from ansible.module_utils.common.text.converters import to_bytes, to_native |
| 86 |
from ansible.plugins.lookup import LookupBase |
| 87 |
from ansible.utils.display import Display |
| 88 |
|
| 89 |
display = Display() |
| 90 |
|
| 91 |
CLIPASSWORDSDK_CMD = os.getenv("AIM_CLIPASSWORDSDK_CMD", "/opt/CARKaim/sdk/clipasswordsdk") |
| 92 |
|
| 93 |
|
| 94 |
class CyberarkPassword: |
| 95 |
def __init__(self, appid=None, query=None, output=None, **kwargs): |
| 96 |
self.appid = appid |
| 97 |
self.query = query |
| 98 |
self.output = output |
| 99 |
|
| 100 |
# Support for Generic parameters to be able to specify |
| 101 |
# FailRequestOnPasswordChange, Queryformat, Reason, etc. |
| 102 |
self.extra_parms = [] |
| 103 |
for key, value in kwargs.items(): |
| 104 |
self.extra_parms.append("-p") |
| 105 |
self.extra_parms.append(f"{key}={value}") |
| 106 |
|
| 107 |
if self.appid is None: |
| 108 |
raise AnsibleError("CyberArk Error: No Application ID specified") |
| 109 |
if self.query is None: |
| 110 |
raise AnsibleError("CyberArk Error: No Vault query specified") |
| 111 |
|
| 112 |
if self.output is None: |
| 113 |
# If no output is specified, return at least the password |
| 114 |
self.output = "password" |
| 115 |
else: |
| 116 |
# To avoid reference issues/confusion to values, all |
| 117 |
# output 'keys' will be in lowercase. |
| 118 |
self.output = self.output.lower() |
| 119 |
|
| 120 |
self.b_delimiter = b"@#@" # Known delimiter to split output results |
| 121 |
|
| 122 |
def get(self): |
| 123 |
result_dict = {} |
| 124 |
|
| 125 |
try: |
| 126 |
all_parms = [ |
| 127 |
CLIPASSWORDSDK_CMD, |
| 128 |
"GetPassword", |
| 129 |
"-p", |
| 130 |
f"AppDescs.AppID={self.appid}", |
| 131 |
"-p", |
| 132 |
f"Query={self.query}", |
| 133 |
"-o", |
| 134 |
self.output, |
| 135 |
"-d", |
| 136 |
self.b_delimiter, |
| 137 |
] |
| 138 |
all_parms.extend(self.extra_parms) |
| 139 |
|
| 140 |
b_credential = b"" |
| 141 |
b_all_params = [to_bytes(v) for v in all_parms] |
| 142 |
tmp_output, tmp_error = Popen(b_all_params, stdout=PIPE, stderr=PIPE, stdin=PIPE).communicate() |
| 143 |
|
| 144 |
if tmp_output: |
| 145 |
b_credential = to_bytes(tmp_output) |
| 146 |
|
| 147 |
if tmp_error: |
| 148 |
raise AnsibleError(f"ERROR => {tmp_error} ") |
| 149 |
|
| 150 |
if b_credential and b_credential.endswith(b"\n"): |
| 151 |
b_credential = b_credential[:-1] |
| 152 |
|
| 153 |
output_names = self.output.split(",") |
| 154 |
output_values = b_credential.split(self.b_delimiter) |
| 155 |
|
| 156 |
for i in range(len(output_names)): |
| 157 |
if output_names[i].startswith("passprops."): |
| 158 |
if "passprops" not in result_dict: |
| 159 |
result_dict["passprops"] = {} |
| 160 |
output_prop_name = output_names[i][10:] |
| 161 |
result_dict["passprops"][output_prop_name] = to_native(output_values[i]) |
| 162 |
else: |
| 163 |
result_dict[output_names[i]] = to_native(output_values[i]) |
| 164 |
|
| 165 |
except subprocess.CalledProcessError as e: |
| 166 |
raise AnsibleError(e.output) from e |
| 167 |
except OSError as e: |
| 168 |
raise AnsibleError( |
| 169 |
f"ERROR - AIM not installed or clipasswordsdk not in standard location. ERROR=({e.errno}) => {e.strerror} " |
| 170 |
) from e |
| 171 |
|
| 172 |
return [result_dict] |
| 173 |
|
| 174 |
|
| 175 |
class LookupModule(LookupBase): |
| 176 |
""" |
| 177 |
USAGE: |
| 178 |
|
| 179 |
""" |
| 180 |
|
| 181 |
def run(self, terms, variables=None, **kwargs): |
| 182 |
# TODO: use new-style option parsing |
| 183 |
display.vvvv(f"{terms}") |
| 184 |
if isinstance(terms, list): |
| 185 |
return_values = [] |
| 186 |
for term in terms: |
| 187 |
display.vvvv(f"Term: {term}") |
| 188 |
cyberark_conn = CyberarkPassword(**term) |
| 189 |
return_values.append(cyberark_conn.get()) |
| 190 |
return return_values |
| 191 |
else: |
| 192 |
cyberark_conn = CyberarkPassword(**terms) |
| 193 |
result = cyberark_conn.get() |
| 194 |
return result |