This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
forked from candlepin/rho
/
scancommand.py
635 lines (525 loc) · 24.2 KB
/
scancommand.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#
# Copyright (c) 2009-2016 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
# pylint: disable=too-few-public-methods, too-many-lines
""" Rho CLI Commands """
from __future__ import print_function
import os
import sys
import re
import time
import json
from collections import defaultdict
from getpass import getpass
import pexpect
import yaml
from rho import utilities
from rho.clicommand import CliCommand
from rho.vault import get_vault_and_password
from rho.utilities import (
multi_arg, _read_in_file, str_to_ascii, iteritems, PING_LOG_PATH,
ANSIBLE_LOG_PATH, SCAN_LOG_PATH, PING_INVENTORY_PATH, PROFILE_HOSTS_SUFIX,
PROFILE_HOST_AUTH_MAPPING_SUFFIX, log
)
from rho.translation import _
# Read ssh key from file
def _read_key_file(filename):
keyfile = open(os.path.expanduser(
os.path.expandvars(filename)), "r")
sshkey = keyfile.read()
keyfile.close()
return sshkey
def auth_as_ansible_host_vars(auth):
"""Get the Ansible host vars that implement an auth.
:param auth: the auth. A dictionary with fields 'id', 'name',
'username', 'password', and 'ssh_key_file'.
:returns: a dict that can be used as the host variables in an
Ansible inventory.
"""
username = auth.get('username')
password = auth.get('password')
ssh_key_file = auth.get('ssh_key_file')
sudo_password = auth.get('sudo_password')
ansible_vars = {}
ansible_vars['ansible_user'] = str_to_ascii(username)
if password:
ansible_vars['ansible_ssh_pass'] = str_to_ascii(password)
if ssh_key_file:
ansible_vars['ansible_ssh_private_key_file'] = \
str_to_ascii(ssh_key_file)
if sudo_password:
ansible_vars['ansible_become_pass'] = sudo_password
return ansible_vars
def redacted_auth_string(auth):
"""Format an auth for the host auth mapping file.
:param auth: the auth. A dictionary with fields 'id', 'name',
'username', 'password', and 'ssh_key_file'.
:returns: a string of the form
'name, username, ********, ssh_key_file'
"""
name = auth.get('name')
username = auth.get('username')
ssh_key_file = auth.get('ssh_key_file')
return '{0}, {1}, {2}, {3}'.format(
name, username, utilities.PASSWORD_MASKING, ssh_key_file)
def redact_dict(redact_key_list, a_dict):
"""Redact_values in a dictionary
:param redact_key_list: keys in dictionary that value should be redacted
:param a_dict: A dictionary to redact
:returns: the redacted dictionary
"""
for key in redact_key_list:
if a_dict is not None and key in a_dict:
a_dict[key] = utilities.PASSWORD_MASKING
return a_dict
def log_yaml_inventory(label, inventory):
"""Log yaml inventory but mask passwords
:param inventory: A dictionary of the ansible inventory
"""
alpha = inventory.get('alpha')
hosts_dict = alpha.get('hosts')
vars_dict = alpha.get('vars')
redact_key_list = ['ansible_become_pass', 'ansible_ssh_pass']
# pylint: disable=unused-variable
for host, host_dict in iteritems(hosts_dict):
host_dict = redact_dict(redact_key_list, host_dict)
vars_dict = redact_dict(redact_key_list, vars_dict)
log.debug('%s:\n%s', label, yaml.dump(inventory))
return inventory
def process_ping_output(out_lines):
"""Find successful hosts from the output of a ping command.
Use this function by using ansible to run echo "Hello" on remote
hosts, then sending the output to this function.
:param out_lines: an iterator returning lines of Ansible output.
:returns: the hosts that pinged successfully, as a set and those that
failed, as a set.
"""
success_hosts = set()
failed_hosts = set()
# Ansible output has the format
# host | UNREACHABLE! => {
# "changed": false,
# "msg": "Failed to connect to the host via ssh ...",
# "unreachable": true
# }
# hostname | SUCCESS | rc=0 >>
# Hello
# with the above two lines repeated for each host
for line in out_lines:
ansi_escape = re.compile(r'\x1b[^m]*m')
line = ansi_escape.sub('', line)
pieces = line.split('|')
if len(pieces) == 3 and pieces[1].strip() == 'SUCCESS':
success_hosts.add(pieces[0].strip())
elif len(pieces) == 2 and pieces[1].strip().startswith('UNREACHABLE'):
failed_hosts.add(pieces[0].strip())
log.debug('Ping log reached hosts: %s', success_hosts)
log.debug('Ping log did not reached hosts: %s', failed_hosts)
return success_hosts, failed_hosts
# Creates the inventory for pinging all hosts and records
# successful auths and the hosts they worked on
# pylint: disable=too-many-statements, too-many-arguments, unused-argument
def _create_ping_inventory(vault, vault_pass, profile_ranges, profile_port,
credential, forks, ansible_verbosity):
"""Find which auths work with which hosts.
:param vault: a Vault object
:param vault_pass: password for the Vault?
:param profile_ranges: hosts for the profile
:param profile_port: the SSH port to use
:param credential: auth to use
:param forks: the number of Ansible forks to use
:returns: a tuple of
(list of IP addresses that worked for any auth,
map from host IPs to SSH ports that worked with them,
map from host IPs to lists of auths that worked with them
)
"""
# pylint: disable=too-many-locals
success_hosts = set()
failed_hosts = set()
success_port_map = defaultdict()
success_auth_map = defaultdict(list)
hosts_dict = {}
for profile_range in profile_ranges:
# pylint: disable=anomalous-backslash-in-string
reg = "[0-9]*.[0-9]*.[0-9]*.\[[0-9]*:[0-9]*\]"
profile_range = profile_range.strip(',').strip()
hostname = str_to_ascii(profile_range)
if not re.match(reg, profile_range):
hosts_dict[profile_range] = {'ansible_host': profile_range,
'ansible_port': profile_port}
else:
hosts_dict[hostname] = None
vars_dict = auth_as_ansible_host_vars(credential)
yml_dict = {'alpha': {'hosts': hosts_dict, 'vars': vars_dict}}
vault.dump_as_yaml_to_file(yml_dict, PING_INVENTORY_PATH)
log_yaml_inventory('Ping inventory', yml_dict)
cmd_string = 'ansible alpha ' \
'-i ' + PING_INVENTORY_PATH \
+ ' --ask-vault-pass -f ' + forks \
+ ' -a \'echo "Hello"\''
my_env = os.environ.copy()
my_env["ANSIBLE_HOST_KEY_CHECKING"] = "False"
# Don't pass ansible_verbosity here as adding too much
# verbosity can break our parsing of Ansible's output. This is
# a temporary fix - a better solution would be less-fragile
# output parsing.
run_ansible_with_vault(cmd_string, vault_pass,
log_path=PING_LOG_PATH,
env=my_env,
log_to_stdout=True,
ansible_verbosity=0)
with open(PING_LOG_PATH, 'r') as ping_log:
success_hosts, failed_hosts = process_ping_output(ping_log)
for host in success_hosts:
success_auth_map[host].append(credential)
success_port_map[host] = profile_port
return list(success_hosts), success_port_map, success_auth_map, \
list(failed_hosts)
# Helper function to create a file to store the mapping
# between hosts and ALL the auths that were ever succesful
# with them arranged according to profile and date of scan.
def _create_hosts_auths_file(success_auth_map, profile):
host_auth_mapping = profile + PROFILE_HOST_AUTH_MAPPING_SUFFIX
host_auth_mapping_path = utilities.get_config_path(host_auth_mapping)
with open(host_auth_mapping_path, 'a') as host_auth_file:
string_to_write = time.strftime("%c") + '\n-' \
'---' \
'---' \
'---' \
'---' \
'---' \
'---' \
'---' \
'---' \
'---' \
'---\n'
for host, line in iteritems(success_auth_map):
string_to_write += host + '\n----------------------\n'
for auth in line:
string_to_write += redacted_auth_string(auth)
string_to_write += '\n\n'
string_to_write += '\n*******************************' \
'*********************************' \
'**************\n\n'
host_auth_file.write(string_to_write)
# Creates the filtered main inventory on which the custom
# modules to collect facts are run. This inventory can be
# used multiple times later after a profile has first been
# processed and the valid mapping as been figured out by
# pinging.
# pylint: disable=too-many-locals
def make_inventory_dict(success_hosts, success_port_map, auth_map):
"""Make the inventory for the scan, as a dict.
:param success_hosts: a list of hosts for the inventory
:param success_port_map: mapping from hosts to SSH ports
:param auth_map: map from host IP to a list of auths it works with
:returns: a dict with the structure:
.. code-block:: python
{'alpha':
{'hosts':
{'IP address 1': {'host-vars-1'},
'IP address 2': {'host-vars-2'},
# ...
}
}
}
"""
yml_dict = {}
# Create section of successfully connected hosts
alpha_hosts = {}
for host in success_hosts:
ascii_host = str_to_ascii(host)
ascii_port = str_to_ascii(str(success_port_map[host]))
host_vars = {'ansible_host': ascii_host,
'ansible_port': ascii_port}
host_vars.update(auth_as_ansible_host_vars(auth_map[host][0]))
alpha_hosts[ascii_host] = host_vars
yml_dict['alpha'] = {'hosts': alpha_hosts}
return yml_dict
def _create_main_inventory(vault, success_hosts, success_port_map,
auth_map, profile):
yml_dict = make_inventory_dict(success_hosts, success_port_map, auth_map)
hosts_yml = profile + PROFILE_HOSTS_SUFIX
hosts_yml_path = utilities.get_config_path(hosts_yml)
vault.dump_as_yaml_to_file(yml_dict, hosts_yml_path)
log_yaml_inventory('Main inventory', yml_dict)
def run_ansible_with_vault(cmd_string, vault_pass, env=None, log_path=None,
log_to_stdout=True, ansible_verbosity=0):
""" Runs ansible command allowing for password to be provided after
process triggered.
Returns after the process completes.
:param cmd_string: the command to run.
:param vault_pass: the password to the user's Ansible Vault.
:param env: the environment to run the subprocess in.
:param log_path: a path to write the process's log to. Defaults to
'XDG_DATA_HOME/rho/ansible_log'.
:param log_to_stdout: if True, write Ansible's log to stdout. Defaults to
True.
:param ansible_verbosity: the number of v's of Ansible verbosity.
:returns: the popen.spawn object for the process.
"""
# pexpect provides the ability to send the process's output to a
# single Python file object. We want to send it to a file and
# maybe also stdout. The solution is to have pexpect log to the
# file and then use 'tail -f' to copy that to stdout.
if not log_path:
log_path = ANSIBLE_LOG_PATH
if ansible_verbosity:
cmd_string = cmd_string + ' -' + 'v' * ansible_verbosity
result = None
try:
utilities.ensure_data_dir_exists()
with open(log_path, 'wb') as logfile:
pass
with open(log_path, 'r+b') as logfile:
log.debug('Running Ansible: %s', cmd_string)
child = pexpect.spawn(cmd_string, timeout=None,
env=env)
if log_to_stdout:
utilities.threaded_tailing(log_path, ansible_verbosity)
result = child.expect('Vault password:')
child.sendline(vault_pass)
# Set the log file *after* we send the user's Vault
# password to Ansible, so we don't log the password.
child.logfile = logfile
last_pos = logfile.tell()
i = child.expect([pexpect.EOF, 'Enter passphrase for key .*:'])
while i:
new_pos = logfile.tell()
logfile.seek(last_pos)
logfile_lines = logfile.readlines()
log.info(logfile_lines)
print(logfile_lines[-1].replace('\r\n', ''))
logfile.seek(new_pos)
last_pos = new_pos
child.logfile = None
# Ansible has already printed a prompt; it would be
# confusing if getpass printed another one.
child.sendline(getpass(''))
child.logfile = logfile
i = child.expect([pexpect.EOF, 'Enter passphrase for key .*:'])
if child.isalive():
child.wait()
if log_to_stdout:
time.sleep(2)
return child
except pexpect.EOF:
print(str(result))
print('pexpect unexpected EOF')
except pexpect.TIMEOUT:
print(str(result))
print('pexpect timed out')
class ScanCommand(CliCommand):
""" The command that performs the scanning and collection of
facts by making the playbook, inventory and running ansible.
"""
def __init__(self):
usage = _("usage: %prog scan [options] PROFILE")
shortdesc = _("scan given host profile")
desc = _("scans the host profile")
CliCommand.__init__(self, "scan", usage, shortdesc, desc)
self.parser.add_option("--cache", dest="cache", action="store_true",
metavar="RESET", default=False,
help=_("Use if profiles/auths previously "
"discovered should be reused"))
self.parser.add_option("--profile", dest="profile", metavar="PROFILE",
help=_("NAME of the profile - REQUIRED"))
self.parser.add_option("--reportfile", dest="report_path",
metavar="REPORTFILE",
help=_("Report file path - REQUIRED"))
self.parser.add_option("--facts", dest="facts", metavar="FACTS",
action="callback", callback=multi_arg,
default=[], help=_("'default', list or file"))
self.parser.add_option("--scan-dirs", dest="scan_dirs",
metavar="SCANDIRS", action="callback",
callback=multi_arg, default=[],
help=_("list of remote directories to scan"))
self.parser.add_option("--ansible-forks", dest="ansible_forks",
metavar="FORKS",
help=_("number of ansible forks"))
self.parser.add_option("--vault", dest="vaultfile", metavar="VAULT",
help=_("file containing vault password for"
" scripting"))
self.parser.add_option("--logfile", dest="logfile", metavar="LOGFILE",
help=_("file to log scan output to"))
self.facts_to_collect = None
# pylint: disable=too-many-branches
def _validate_options(self):
CliCommand._validate_options(self)
if not self.options.profile:
print(_("No profile specified."))
self.parser.print_help()
sys.exit(1)
if not self.options.report_path:
print(_("No report location specified."))
self.parser.print_help()
sys.exit(1)
if self.options.ansible_forks:
try:
if int(self.options.ansible_forks) <= 0:
print(_("--ansible-forks can only be a positive integer."))
self.parser.print_help()
sys.exit(1)
except ValueError:
print(_("--ansible-forks can only be a positive integer."))
self.parser.print_help()
sys.exit(1)
# perform fact validation
facts = self.options.facts
if facts == [] or facts == ['default'] or facts == ['all']:
self.facts_to_collect = list(utilities.DEFAULT_FACTS)
elif facts == ['jboss']:
self.facts_to_collect = list(utilities.JBOSS_FACTS +
utilities.CONNECTION_FACTS_TUPLE)
elif facts == ['rhel']:
self.facts_to_collect = list(utilities.RHEL_FACTS +
utilities.CONNECTION_FACTS_TUPLE)
elif os.path.isfile(facts[0]):
self.facts_to_collect = _read_in_file(facts[0])
else:
assert isinstance(facts, list)
self.facts_to_collect = facts
# check facts_to_collect is subset of utilities.DEFAULT_FACTS
all_facts = utilities.DEFAULT_FACTS
facts_to_collect_set = set(self.facts_to_collect)
if not facts_to_collect_set.issubset(all_facts):
invalid_facts = facts_to_collect_set.difference(all_facts)
print(_("Invalid facts were supplied to scan command: " +
",".join(invalid_facts)))
self.parser.print_help()
sys.exit(1)
if self.options.scan_dirs == []:
self.options.scan_dirs = ['/', '/opt', '/app', '/home', '/usr']
elif os.path.isfile(self.options.scan_dirs[0]):
self.options.scan_dirs = \
_read_in_file(self.options.scan_dirs[0])
else:
assert isinstance(self.options.scan_dirs, list)
# check that all values in scan_dirs are valid abs paths
invalid_paths = utilities.check_path_validity(self.options.scan_dirs)
if invalid_paths != []:
print(_("Invalid paths were supplied to the --scan-dirs option: " +
",".join(invalid_paths)))
self.parser.print_help()
sys.exit(1)
def _do_command(self):
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
vault, vault_pass = get_vault_and_password(self.options.vaultfile)
profile_found = False
profile_auth_list = []
profile_ranges = []
profile_port = 22
profile = self.options.profile
forks = self.options.ansible_forks \
if self.options.ansible_forks else '50'
report_path = os.path.abspath(os.path.normpath(
self.options.report_path))
hosts_yml = profile + PROFILE_HOSTS_SUFIX
hosts_yml_path = utilities.get_config_path(hosts_yml)
# Checks if profile exists and stores information
# about that profile for later use.
if not os.path.isfile(utilities.PROFILES_PATH):
print(_('No profiles exist yet.'))
sys.exit(1)
if not os.path.isfile(utilities.CREDENTIALS_PATH):
print(_('No auth credentials exist yet.'))
sys.exit(1)
profiles_list = vault.load_as_json(utilities.PROFILES_PATH)
for curr_profile in profiles_list:
if self.options.profile == curr_profile.get('name'):
profile_found = True
profile_ranges = curr_profile.get('hosts')
profile_auths = curr_profile.get('auth')
profile_port = curr_profile.get('ssh_port')
cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)
for auth in profile_auths:
for cred in cred_list:
if auth.get('id') == cred.get('id'):
profile_auth_list.append(cred)
break
if not profile_found:
print(_("Invalid profile. Create profile first"))
sys.exit(1)
# cache is used when the profile/auth mapping has been previously
# used and does not need to be rerun.
if not self.options.cache:
success_hosts = []
success_port_map = {}
auth_map = {}
remaining_hosts = profile_ranges
for cred_item in profile_auth_list:
success_hosts_, success_port_map_, \
auth_map_, remaining_hosts_ = \
_create_ping_inventory(vault, vault_pass,
remaining_hosts,
profile_port,
cred_item, forks,
self.verbosity)
success_hosts = success_hosts + success_hosts_
remaining_hosts = remaining_hosts_
success_port_map.update(success_port_map_)
auth_map.update(auth_map_)
if not success_hosts:
print(_('All auths are invalid for this profile'))
sys.exit(1)
_create_hosts_auths_file(auth_map, profile)
_create_main_inventory(vault, success_hosts, success_port_map,
auth_map, profile)
elif os.path.isfile(hosts_yml_path):
print("Profile '" + profile + "' has not been processed. " +
"Please run without using --cache with the profile first.")
sys.exit(1)
# always output connection.x
for key in utilities.CONNECTION_FACTS_TUPLE:
if key not in self.facts_to_collect:
self.facts_to_collect.append(key)
scan_dirs = ' '.join(self.options.scan_dirs)
ansible_vars = {'facts_to_collect': self.facts_to_collect,
'report_path': report_path,
'scan_dirs': scan_dirs}
playbook = utilities.PLAYBOOK_DEV_PATH
if not os.path.isfile(playbook):
playbook = utilities.PLAYBOOK_RPM_PATH
if not os.path.isfile(playbook):
print(_("rho scan playbook not found locally or in '%s'")
% playbook)
sys.exit(1)
cmd_string = ('ansible-playbook {playbook} '
'-i {inventory} -f {forks} '
'--ask-vault-pass '
'--extra-vars \'{vars}\'').format(
playbook=playbook,
inventory=hosts_yml_path,
forks=forks,
vars=json.dumps(ansible_vars))
# process finally runs ansible on the
# playbook and inventories thus created.
if self.options.logfile:
log_path = self.options.logfile
else:
log_path = SCAN_LOG_PATH
print('Running:', cmd_string)
process = run_ansible_with_vault(cmd_string, vault_pass,
log_path=log_path,
log_to_stdout=True,
ansible_verbosity=self.verbosity)
if process.exitstatus == 0 and process.signalstatus is None:
host_auth_mapping = \
self.options.profile + PROFILE_HOST_AUTH_MAPPING_SUFFIX
host_auth_mapping_path = \
utilities.get_config_path(host_auth_mapping)
print(_("Scanning has completed. The mapping has been"
" stored in file '" + host_auth_mapping_path +
"'. The facts have been stored in '" +
report_path + "'"))
else:
print(_("An error has occurred during the scan. Please review" +
" the output to resolve the given issue."))