-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathOpenSSL.py
250 lines (192 loc) · 8.78 KB
/
OpenSSL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import demistomock as demisto # noqa: F401
from CommonServerPython import * # noqa: F401
import json
import traceback
from typing import Dict, cast
import ansible_runner
import ssh_agent_setup
# Dict to Markdown Converter adapted from https://github.com/PolBaladas/torsimany/
def dict2md(json_block, depth=0):
markdown = ""
if isinstance(json_block, dict):
markdown = parseDict(json_block, depth)
if isinstance(json_block, list):
markdown = parseList(json_block, depth)
return markdown
def parseDict(d, depth):
markdown = ""
for k in d:
if isinstance(d[k], (dict, list)):
markdown += addHeader(k, depth)
markdown += dict2md(d[k], depth + 1)
else:
markdown += buildValueChain(k, d[k], depth)
return markdown
def parseList(rawlist, depth):
markdown = ""
for value in rawlist:
if not isinstance(value, (dict, list)):
index = rawlist.index(value)
markdown += buildValueChain(index, value, depth)
else:
markdown += parseDict(value, depth)
return markdown
def buildHeaderChain(depth):
list_tag = '* '
htag = '#'
chain = list_tag * (bool(depth)) + htag * (depth + 1) + \
' value ' + (htag * (depth + 1) + '\n')
return chain
def buildValueChain(key, value, depth):
tab = " "
list_tag = '* '
chain = tab * (bool(depth - 1)) + list_tag + \
str(key) + ": " + str(value) + "\n"
return chain
def addHeader(value, depth):
chain = buildHeaderChain(depth)
chain = chain.replace('value', value.title())
return chain
# Remove ansible branding from results
def rec_ansible_key_strip(obj):
if isinstance(obj, dict):
return {key.replace('ansible_', ''): rec_ansible_key_strip(val) for key, val in obj.items()}
return obj
# COMMAND FUNCTIONS
def generic_ansible(integration_name, command, args: Dict[str, Any]) -> CommandResults:
readable_output = ""
sshkey = ""
fork_count = 1 # default to executing against 1 host at a time
if args.get('concurrency'):
fork_count = cast(int, args.get('concurrency'))
inventory: Dict[str, dict] = {}
inventory['all'] = {}
inventory['all']['hosts'] = {}
if type(args['host']) is list:
# host arg can be a array of multiple hosts
hosts = args['host']
else:
# host arg could also be csv
hosts = [host.strip() for host in args['host'].split(',')]
for host in hosts:
new_host = {}
new_host['ansible_host'] = host
if ":" in host:
address = host.split(':')
new_host['ansible_port'] = address[1]
new_host['ansible_host'] = address[0]
else:
new_host['ansible_host'] = host
if demisto.params().get('port'):
new_host['ansible_port'] = demisto.params().get('port')
# Linux
# Different credential options
# SSH Key saved in credential manager selection
if demisto.params().get('creds', {}).get('credentials').get('sshkey'):
username = demisto.params().get('creds', {}).get('credentials').get('user')
sshkey = demisto.params().get('creds', {}).get('credentials').get('sshkey')
new_host['ansible_user'] = username
# Password saved in credential manager selection
elif demisto.params().get('creds', {}).get('credentials').get('password'):
username = demisto.params().get('creds', {}).get('credentials').get('user')
password = demisto.params().get('creds', {}).get('credentials').get('password')
new_host['ansible_user'] = username
new_host['ansible_password'] = password
# username/password individually entered
else:
username = demisto.params().get('creds', {}).get('identifier')
password = demisto.params().get('creds', {}).get('password')
new_host['ansible_user'] = username
new_host['ansible_password'] = password
inventory['all']['hosts'][host] = new_host
module_args = ""
# build module args list
for arg_key, arg_value in args.items():
# skip hardcoded host arg, as it doesn't related to module
if arg_key == 'host':
continue
module_args += "%s=\"%s\" " % (arg_key, arg_value)
r = ansible_runner.run(inventory=inventory, host_pattern='all', module=command, quiet=True,
omit_event_data=True, ssh_key=sshkey, module_args=module_args, forks=fork_count)
results = []
for each_host_event in r.events:
# Troubleshooting
# demisto.log("%s: %s\n" % (each_host_event['event'], each_host_event))
if each_host_event['event'] in ["runner_on_ok", "runner_on_unreachable", "runner_on_failed"]:
# parse results
result = json.loads('{' + each_host_event['stdout'].split('{', 1)[1])
host = each_host_event['stdout'].split('|', 1)[0].strip()
status = each_host_event['stdout'].replace('=>', '|').split('|', 3)[1]
# if successful build outputs
if each_host_event['event'] == "runner_on_ok":
if 'fact' in command:
result = result['ansible_facts']
else:
if result.get(command) is not None:
result = result[command]
else:
result.pop("ansible_facts", None)
result = rec_ansible_key_strip(result)
if host != "localhost":
readable_output += "# %s - %s\n" % (host, status)
else:
# This is integration is not host based
readable_output += "# %s\n" % status
readable_output += dict2md(result)
# add host and status to result
result['host'] = host
result['status'] = status
results.append(result)
if each_host_event['event'] == "runner_on_unreachable":
msg = "Host %s unreachable\nError Details: %s" % (host, result)
return_error(msg)
if each_host_event['event'] == "runner_on_failed":
msg = "Host %s failed running command\nError Details: %s" % (host, result)
return_error(msg)
return CommandResults(
readable_output=readable_output,
outputs_prefix=integration_name + '.' + command,
outputs_key_field='',
outputs=results
)
# MAIN FUNCTION
def main() -> None:
"""main function, parses params and runs command functions
:return:
:rtype:
"""
# SSH Key integration requires ssh_agent to be running in the background
ssh_agent_setup.setup()
try:
if demisto.command() == 'test-module':
# This is the call made when pressing the integration Test button.
return_results('ok')
elif demisto.command() == 'openssl-certificate':
return_results(generic_ansible('openssl', 'openssl_certificate', demisto.args()))
elif demisto.command() == 'openssl-certificate-info':
return_results(generic_ansible('openssl', 'openssl_certificate_info', demisto.args()))
elif demisto.command() == 'openssl-csr':
return_results(generic_ansible('openssl', 'openssl_csr', demisto.args()))
elif demisto.command() == 'openssl-csr-info':
return_results(generic_ansible('openssl', 'openssl_csr_info', demisto.args()))
elif demisto.command() == 'openssl-dhparam':
return_results(generic_ansible('openssl', 'openssl_dhparam', demisto.args()))
elif demisto.command() == 'openssl-pkcs12':
return_results(generic_ansible('openssl', 'openssl_pkcs12', demisto.args()))
elif demisto.command() == 'openssl-privatekey':
return_results(generic_ansible('openssl', 'openssl_privatekey', demisto.args()))
elif demisto.command() == 'openssl-privatekey-info':
return_results(generic_ansible('openssl', 'openssl_privatekey_info', demisto.args()))
elif demisto.command() == 'openssl-publickey':
return_results(generic_ansible('openssl', 'openssl_publickey', demisto.args()))
elif demisto.command() == 'openssl-certificate-complete-chain':
return_results(generic_ansible('openssl', 'certificate_complete_chain', demisto.args()))
elif demisto.command() == 'openssl-get-certificate':
return_results(generic_ansible('openssl', 'get_certificate', demisto.args()))
# Log exceptions and return errors
except Exception as e:
demisto.error(traceback.format_exc()) # print the traceback
return_error(f'Failed to execute {demisto.command()} command.\nError:\n{str(e)}')
# ENTRY POINT
if __name__ in ('__main__', '__builtin__', 'builtins'):
main()