2019-09-17 10:59:54 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
2019-09-18 14:06:46 +00:00
|
|
|
import re
|
2019-09-17 10:59:54 +00:00
|
|
|
from collections import defaultdict
|
2019-09-18 14:06:46 +00:00
|
|
|
|
2019-11-14 03:46:42 +00:00
|
|
|
from celery import shared_task
|
2019-09-17 10:59:54 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2019-11-14 03:46:42 +00:00
|
|
|
from django.utils import timezone
|
2019-09-17 10:59:54 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
from orgs.utils import tmp_to_org, org_aware_func
|
2019-09-18 14:06:46 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
from ..models import GatheredUser, Node
|
2020-03-12 08:24:38 +00:00
|
|
|
from .utils import clean_ansible_task_hosts
|
2019-09-17 10:59:54 +00:00
|
|
|
from . import const
|
|
|
|
|
2019-09-18 14:06:46 +00:00
|
|
|
__all__ = ['gather_asset_users', 'gather_nodes_asset_users']
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
space = re.compile('\s+')
|
|
|
|
ignore_login_shell = re.compile(r'nologin$|sync$|shutdown$|halt$')
|
|
|
|
|
|
|
|
|
|
|
|
def parse_linux_result_to_users(result):
|
2019-11-14 03:46:42 +00:00
|
|
|
users = defaultdict(dict)
|
|
|
|
users_result = result.get('gather host users', {})\
|
|
|
|
.get('ansible_facts', {})\
|
|
|
|
.get('getent_passwd')
|
|
|
|
if not isinstance(users_result, dict):
|
|
|
|
users_result = {}
|
|
|
|
for username, attr in users_result.items():
|
2019-09-18 14:06:46 +00:00
|
|
|
if ignore_login_shell.search(attr[-1]):
|
|
|
|
continue
|
2019-11-14 03:46:42 +00:00
|
|
|
users[username] = {}
|
|
|
|
last_login_result = result.get('get last login', {}).get('stdout_lines', [])
|
|
|
|
for line in last_login_result:
|
|
|
|
data = line.split('@')
|
|
|
|
if len(data) != 3:
|
|
|
|
continue
|
|
|
|
username, ip, dt = data
|
|
|
|
dt += ' +0800'
|
|
|
|
date = timezone.datetime.strptime(dt, '%b %d %H:%M:%S %Y %z')
|
|
|
|
users[username] = {"ip": ip, "date": date}
|
2019-09-18 14:06:46 +00:00
|
|
|
return users
|
|
|
|
|
|
|
|
|
|
|
|
def parse_windows_result_to_users(result):
|
|
|
|
task_result = []
|
|
|
|
for task_name, raw in result.items():
|
|
|
|
res = raw.get('stdout_lines', {})
|
|
|
|
if res:
|
|
|
|
task_result = res
|
|
|
|
break
|
|
|
|
if not task_result:
|
|
|
|
return []
|
|
|
|
|
2019-11-14 03:46:42 +00:00
|
|
|
users = {}
|
2019-09-18 14:06:46 +00:00
|
|
|
|
|
|
|
for i in range(4):
|
|
|
|
task_result.pop(0)
|
|
|
|
for i in range(2):
|
|
|
|
task_result.pop()
|
|
|
|
|
|
|
|
for line in task_result:
|
2021-08-02 08:33:17 +00:00
|
|
|
username_list = space.split(line)
|
|
|
|
# such as: ['Admini', 'appadm', 'DefaultAccount', '']
|
|
|
|
for username in username_list:
|
|
|
|
if not username:
|
|
|
|
continue
|
|
|
|
users[username] = {}
|
2019-09-18 14:06:46 +00:00
|
|
|
return users
|
|
|
|
|
|
|
|
|
|
|
|
def add_asset_users(assets, results):
|
|
|
|
assets_map = {a.hostname: a for a in assets}
|
|
|
|
parser_map = {
|
|
|
|
'linux': parse_linux_result_to_users,
|
|
|
|
'windows': parse_windows_result_to_users
|
|
|
|
}
|
|
|
|
|
|
|
|
assets_users_map = {}
|
|
|
|
|
|
|
|
for platform, platform_results in results.items():
|
|
|
|
for hostname, res in platform_results.items():
|
|
|
|
parse = parser_map.get(platform)
|
|
|
|
users = parse(res)
|
|
|
|
logger.debug('Gathered host users: {} {}'.format(hostname, users))
|
|
|
|
asset = assets_map.get(hostname)
|
|
|
|
if not asset:
|
|
|
|
continue
|
|
|
|
assets_users_map[asset] = users
|
|
|
|
|
|
|
|
for asset, users in assets_users_map.items():
|
|
|
|
with tmp_to_org(asset.org_id):
|
|
|
|
GatheredUser.objects.filter(asset=asset, present=True)\
|
|
|
|
.update(present=False)
|
2019-11-14 03:46:42 +00:00
|
|
|
for username, data in users.items():
|
2019-09-18 14:06:46 +00:00
|
|
|
defaults = {'asset': asset, 'username': username, 'present': True}
|
2019-11-14 03:46:42 +00:00
|
|
|
if data.get("ip"):
|
2020-10-14 11:58:44 +00:00
|
|
|
defaults["ip_last_login"] = data["ip"][:32]
|
2019-11-14 03:46:42 +00:00
|
|
|
if data.get("date"):
|
|
|
|
defaults["date_last_login"] = data["date"]
|
2019-09-18 14:06:46 +00:00
|
|
|
GatheredUser.objects.update_or_create(
|
|
|
|
defaults=defaults, asset=asset, username=username,
|
|
|
|
)
|
|
|
|
|
2019-09-17 10:59:54 +00:00
|
|
|
|
|
|
|
@shared_task(queue="ansible")
|
2020-03-12 08:24:38 +00:00
|
|
|
@org_aware_func("assets")
|
2019-09-18 14:06:46 +00:00
|
|
|
def gather_asset_users(assets, task_name=None):
|
2019-09-17 10:59:54 +00:00
|
|
|
from ops.utils import update_or_create_ansible_task
|
|
|
|
if task_name is None:
|
|
|
|
task_name = _("Gather assets users")
|
2020-03-12 08:24:38 +00:00
|
|
|
assets = clean_ansible_task_hosts(assets)
|
2019-09-18 14:06:46 +00:00
|
|
|
if not assets:
|
|
|
|
return
|
2019-09-17 10:59:54 +00:00
|
|
|
hosts_category = {
|
|
|
|
'linux': {
|
|
|
|
'hosts': [],
|
|
|
|
'tasks': const.GATHER_ASSET_USERS_TASKS
|
|
|
|
},
|
|
|
|
'windows': {
|
|
|
|
'hosts': [],
|
|
|
|
'tasks': const.GATHER_ASSET_USERS_TASKS_WINDOWS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for asset in assets:
|
|
|
|
hosts_list = hosts_category['windows']['hosts'] if asset.is_windows() \
|
|
|
|
else hosts_category['linux']['hosts']
|
|
|
|
hosts_list.append(asset)
|
|
|
|
|
|
|
|
results = {'linux': defaultdict(dict), 'windows': defaultdict(dict)}
|
|
|
|
for k, value in hosts_category.items():
|
|
|
|
if not value['hosts']:
|
|
|
|
continue
|
2019-09-19 06:48:25 +00:00
|
|
|
_task_name = '{}: {}'.format(task_name, k)
|
2019-09-17 10:59:54 +00:00
|
|
|
task, created = update_or_create_ansible_task(
|
2019-09-19 06:48:25 +00:00
|
|
|
task_name=_task_name, hosts=value['hosts'], tasks=value['tasks'],
|
2019-09-17 10:59:54 +00:00
|
|
|
pattern='all', options=const.TASK_OPTIONS,
|
2020-03-12 08:24:38 +00:00
|
|
|
run_as_admin=True,
|
2019-09-17 10:59:54 +00:00
|
|
|
)
|
|
|
|
raw, summary = task.run()
|
|
|
|
results[k].update(raw['ok'])
|
2019-09-18 14:06:46 +00:00
|
|
|
add_asset_users(assets, results)
|
2019-09-17 10:59:54 +00:00
|
|
|
|
2019-09-18 14:06:46 +00:00
|
|
|
|
|
|
|
@shared_task(queue="ansible")
|
|
|
|
def gather_nodes_asset_users(nodes_key):
|
2021-02-05 05:29:29 +00:00
|
|
|
nodes = Node.objects.filter(key__in=nodes_key)
|
|
|
|
assets = Node.get_nodes_all_assets(*nodes)
|
2019-09-18 14:06:46 +00:00
|
|
|
assets_groups_by_100 = [assets[i:i+100] for i in range(0, len(assets), 100)]
|
|
|
|
for _assets in assets_groups_by_100:
|
|
|
|
gather_asset_users(_assets)
|