mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-04-29 11:54:49 +00:00
* perf: change i18n
* perf: pam
* perf: change translate
* perf: add check account
* perf: add date field
* perf: add account filter
* perf: remove some js
* perf: add account status action
* perf: update pam
* perf: 修改 discover account
* perf: update filter
* perf: update gathered account
* perf: 修改账号同步
* perf: squash migrations
* perf: update pam
* perf: change i18n
* perf: update account risk
* perf: 更新风险发现
* perf: remove css
* perf: Admin connection token
* perf: Add a switch to check connectivity after changing the password, and add a custom ssh command for push tasks
* perf: Modify account migration files
* perf: update pam
* perf: remove to check account dir
* perf: Admin connection token
* perf: update check account
* perf: 优化发送结果
* perf: update pam
* perf: update bulk update create
* perf: prepaire using thread timer for bulk_create_decorator
* perf: update bulk create decorator
* perf: 优化 playbook manager
* perf: 优化收集账号的报表
* perf: Update poetry
* perf: Update Dockerfile with new base image tag
* fix: Account migrate 0012 file
* perf: 修改备份
* perf: update pam
* fix: Expand resource_type filter to include raw type
* feat: PAM Service (#14552)
* feat: PAM Service
* perf: import package name
---------
Co-authored-by: jiangweidong <1053570670@qq.com>
* perf: Change secret dashboard (#14551)
Co-authored-by: feng <1304903146@qq.com>
* perf: update migrations
* perf: 修改支持 pam
* perf: Change secret record table dashboard
* perf: update status
* fix: Automation send report
* perf: Change secret report
* feat: windows accounts gather
* perf: update change status
* perf: Account backup
* perf: Account backup report
* perf: Account migrate
* perf: update service to application
* perf: update migrations
* perf: update logo
* feat: oracle accounts gather (#14571)
* feat: oracle accounts gather
* feat: sqlserver accounts gather
* feat: postgresql accounts gather
* feat: mysql accounts gather
---------
Co-authored-by: wangruidong <940853815@qq.com>
* feat: mongodb accounts gather
* perf: Change secret
* perf: Migrate
* perf: Merge conflicting migration files
* perf: Change secret
* perf: Automation filter org
* perf: Account push
* perf: Random secret string
* perf: Enhance SQL query and update risk handling in accounts
* perf: Ticket filter assignee_id
* perf: 修改 account remote
* perf: 修改一些 adhoc 任务
* perf: Change secret
* perf: Remove push account extra api
* perf: update status
* perf: The entire organization can view activity log
* fix: risk field check
* perf: add account details api
* perf: add demo mode
* perf: Delete gather_account
* perf: Perfect solution to account version problem
* perf: Update status action to handle multiple accounts
* perf: Add GatherAccountDetailField and update serializers
* perf: Display account history in combination with password change records
* perf: Lina translate
* fix: Update mysql_filter to handle nested user info
* perf: Admin connection token validate_permission account
* perf: copy move account
* perf: account filter risk
* perf: account risk filter
* perf: Copy move account failed message
* fix: gather account sync account to asset
* perf: Pam dashboard
* perf: Account dashboard total accounts
* perf: Pam dashboard
* perf: Change secret filter account secret_reset
* perf: 修改 risk filter
* perf: pam translate
* feat: Check for leaked duplicate passwords. (#14711)
* feat: Check for leaked duplicate passwords.
* perf: Use SQLite instead of txt as leak password database
---------
Co-authored-by: jiangweidong <1053570670@qq.com>
Co-authored-by: 老广 <ibuler@qq.com>
* perf: merge with remote
* perf: Add risk change_password_add handle
* perf: Pam dashboard
* perf: check account manager import
* perf: 重构扫描
* perf: 修改 db
* perf: Gather account manager
* perf: update change db lib
* perf: dashboard
* perf: Account gather
* perf: 修改 asset get queryset
* perf: automation report
* perf: Pam account
* perf: Pam dashboard api
* perf: risk add account
* perf: 修改 risk check
* perf: Risk account
* perf: update risk add reopen action
* perf: add pylintrc
* Revert "perf: automation report"
This reverts commit 22aee54207
.
* perf: check account engine
* perf: Perf: Optimism Gather Report Style
* Perf: Remove unuser actions
* Perf: Perf push account
* perf: perf gather account
* perf: Automation report
* perf: Push account recorder
* perf: Push account record
* perf: Pam dashboard
* perf: perf
* perf: update intergration
* perf: integrations application detail add account tab page
* feat: Custom change password supports configuration of interactive items
* perf: Go and Python demo code
* perf: Custom secret change
* perf: add user filter
* perf: translate
* perf: Add demo code docs
* perf: update some i18n
* perf: update some i18n
* perf: Add Java, Node, Go, and cURL demo code
* perf: Translate
* perf: Change secret translate
* perf: Translate
* perf: update some i18n
* perf: translate
* perf: Ansible playbook
* perf: update some choice
* perf: update some choice
* perf: update account serializer remote unused code
* perf: conflict
* perf: update import
---------
Co-authored-by: ibuler <ibuler@qq.com>
Co-authored-by: feng <1304903146@qq.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: wangruidong <940853815@qq.com>
Co-authored-by: jiangweidong <1053570670@qq.com>
Co-authored-by: feng626 <57284900+feng626@users.noreply.github.com>
Co-authored-by: zhaojisen <1301338853@qq.com>
444 lines
10 KiB
Python
444 lines
10 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
import datetime
|
|
import ipaddress
|
|
import logging
|
|
import os
|
|
import platform
|
|
import re
|
|
import socket
|
|
import time
|
|
import uuid
|
|
from collections import OrderedDict
|
|
from functools import wraps
|
|
from itertools import chain
|
|
|
|
import html2text
|
|
import psutil
|
|
from django.conf import settings
|
|
from django.templatetags.static import static
|
|
|
|
from common.permissions import ServiceAccountSignaturePermission
|
|
|
|
UUID_PATTERN = re.compile(r'\w{8}(-\w{4}){3}-\w{12}')
|
|
ipip_db = None
|
|
|
|
|
|
def combine_seq(s1, s2, callback=None):
|
|
for s in (s1, s2):
|
|
if not hasattr(s, '__iter__'):
|
|
return []
|
|
|
|
seq = chain(s1, s2)
|
|
if callback:
|
|
seq = map(callback, seq)
|
|
return seq
|
|
|
|
|
|
def get_logger(name=''):
|
|
if '/' in name:
|
|
name = os.path.basename(name).replace('.py', '')
|
|
return logging.getLogger('jumpserver.%s' % name)
|
|
|
|
|
|
def get_syslogger(name=''):
|
|
return logging.getLogger('syslog.%s' % name)
|
|
|
|
|
|
def timesince(dt, since='', default="just now"):
|
|
"""
|
|
Returns string representing "time since" e.g.
|
|
3 days, 5 hours.
|
|
"""
|
|
|
|
if not since:
|
|
since = datetime.datetime.utcnow()
|
|
|
|
if since is None:
|
|
return default
|
|
|
|
diff = since - dt
|
|
|
|
periods = (
|
|
(diff.days / 365, "year", "years"),
|
|
(diff.days / 30, "month", "months"),
|
|
(diff.days / 7, "week", "weeks"),
|
|
(diff.days, "day", "days"),
|
|
(diff.seconds / 3600, "hour", "hours"),
|
|
(diff.seconds / 60, "minute", "minutes"),
|
|
(diff.seconds, "second", "seconds"),
|
|
)
|
|
|
|
for period, singular, plural in periods:
|
|
if period:
|
|
return "%d %s" % (period, singular if period == 1 else plural)
|
|
return default
|
|
|
|
|
|
def setattr_bulk(seq, key, value):
|
|
def set_attr(obj):
|
|
setattr(obj, key, value)
|
|
return obj
|
|
|
|
return map(set_attr, seq)
|
|
|
|
|
|
def set_or_append_attr_bulk(seq, key, value):
|
|
for obj in seq:
|
|
ori = getattr(obj, key, None)
|
|
if ori:
|
|
value += " " + ori
|
|
setattr(obj, key, value)
|
|
|
|
|
|
def capacity_convert(size, expect='auto', rate=1000):
|
|
"""
|
|
:param size: '100MB', '1G'
|
|
:param expect: 'K, M, G, T
|
|
:param rate: Default 1000, may be 1024
|
|
:return:
|
|
"""
|
|
rate_mapping = (
|
|
('K', rate),
|
|
('KB', rate),
|
|
('M', rate ** 2),
|
|
('MB', rate ** 2),
|
|
('G', rate ** 3),
|
|
('GB', rate ** 3),
|
|
('T', rate ** 4),
|
|
('TB', rate ** 4),
|
|
)
|
|
|
|
rate_mapping = OrderedDict(rate_mapping)
|
|
|
|
std_size = 0 # To KB
|
|
for unit in rate_mapping:
|
|
if size.endswith(unit):
|
|
try:
|
|
std_size = float(size.strip(unit).strip()) * rate_mapping[unit]
|
|
except ValueError:
|
|
pass
|
|
|
|
if expect == 'auto':
|
|
for unit, rate_ in rate_mapping.items():
|
|
if rate > std_size / rate_ >= 1 or unit == "T":
|
|
expect = unit
|
|
break
|
|
|
|
if expect not in rate_mapping:
|
|
expect = 'K'
|
|
|
|
expect_size = std_size / rate_mapping[expect]
|
|
return expect_size, expect
|
|
|
|
|
|
def sum_capacity(cap_list):
|
|
total = 0
|
|
for cap in cap_list:
|
|
size, _ = capacity_convert(cap, expect='K')
|
|
total += size
|
|
total = '{} K'.format(total)
|
|
return capacity_convert(total, expect='auto')
|
|
|
|
|
|
def get_short_uuid_str():
|
|
return str(uuid.uuid4()).split('-')[-1]
|
|
|
|
|
|
def is_uuid(seq):
|
|
if isinstance(seq, uuid.UUID):
|
|
return True
|
|
elif isinstance(seq, str) and UUID_PATTERN.match(seq):
|
|
return True
|
|
elif isinstance(seq, (list, tuple)):
|
|
return all([is_uuid(x) for x in seq])
|
|
return False
|
|
|
|
|
|
def get_request_ip(request):
|
|
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR', '').split(',')
|
|
if x_forwarded_for and x_forwarded_for[0]:
|
|
login_ip = x_forwarded_for[0]
|
|
if login_ip.count(':') == 1:
|
|
# format: ipv4:port (非标准格式的 X-Forwarded-For)
|
|
login_ip = login_ip.split(":")[0]
|
|
return login_ip
|
|
|
|
login_ip = request.META.get('REMOTE_ADDR', '')
|
|
return login_ip
|
|
|
|
|
|
def get_request_ip_or_data(request):
|
|
ip = ''
|
|
|
|
if hasattr(request, 'data') and isinstance(request.data, dict) and request.data.get('remote_addr', ''):
|
|
permission = ServiceAccountSignaturePermission()
|
|
if permission.has_permission(request, None):
|
|
ip = request.data.get('remote_addr', '')
|
|
ip = ip or get_request_ip(request)
|
|
return ip
|
|
|
|
|
|
def get_request_user_agent(request):
|
|
user_agent = request.META.get('HTTP_USER_AGENT', '')
|
|
return user_agent
|
|
|
|
|
|
def validate_ip(ip):
|
|
try:
|
|
ipaddress.ip_address(ip)
|
|
return True
|
|
except ValueError:
|
|
pass
|
|
return False
|
|
|
|
|
|
def with_cache(func):
|
|
cache = {}
|
|
key = "_{}.{}".format(func.__module__, func.__name__)
|
|
|
|
@wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
cached = cache.get(key)
|
|
if cached:
|
|
return cached
|
|
res = func(*args, **kwargs)
|
|
cache[key] = res
|
|
return res
|
|
|
|
return wrapper
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
def timeit(func):
|
|
def wrapper(*args, **kwargs):
|
|
name = func
|
|
for attr in ('__qualname__', '__name__'):
|
|
if hasattr(func, attr):
|
|
name = getattr(func, attr)
|
|
break
|
|
|
|
logger.debug("Start call: {}".format(name))
|
|
now = time.time()
|
|
result = func(*args, **kwargs)
|
|
using = (time.time() - now) * 1000
|
|
msg = "Ends call: {}, using: {:.1f}ms".format(name, using)
|
|
logger.debug(msg)
|
|
return result
|
|
|
|
return wrapper
|
|
|
|
|
|
def group_obj_by_count(objs, count=50):
|
|
objs_grouped = [
|
|
objs[i:i + count] for i in range(0, len(objs), count)
|
|
]
|
|
return objs_grouped
|
|
|
|
|
|
def dict_get_any(d, keys):
|
|
for key in keys:
|
|
value = d.get(key)
|
|
if value:
|
|
return value
|
|
return None
|
|
|
|
|
|
class lazyproperty:
|
|
def __init__(self, func):
|
|
self.func = func
|
|
|
|
def __get__(self, instance, cls):
|
|
if instance is None:
|
|
return self
|
|
else:
|
|
value = self.func(instance)
|
|
setattr(instance, self.func.__name__, value)
|
|
return value
|
|
|
|
|
|
def get_disk_usage(path):
|
|
return psutil.disk_usage(path=path).percent
|
|
|
|
|
|
def get_cpu_load():
|
|
cpu_load_1, cpu_load_5, cpu_load_15 = psutil.getloadavg()
|
|
cpu_count = psutil.cpu_count()
|
|
single_cpu_load_1 = cpu_load_1 / cpu_count
|
|
single_cpu_load_1 = '%.2f' % single_cpu_load_1
|
|
return float(single_cpu_load_1)
|
|
|
|
|
|
def get_docker_mem_usage_if_limit():
|
|
try:
|
|
with open('/sys/fs/cgroup/memory/memory.limit_in_bytes') as f:
|
|
limit_in_bytes = int(f.readline())
|
|
total = psutil.virtual_memory().total
|
|
if limit_in_bytes >= total:
|
|
raise ValueError('Not limit')
|
|
|
|
with open('/sys/fs/cgroup/memory/memory.usage_in_bytes') as f:
|
|
usage_in_bytes = int(f.readline())
|
|
|
|
with open('/sys/fs/cgroup/memory/memory.stat') as f:
|
|
inactive_file = 0
|
|
for line in f:
|
|
if line.startswith('total_inactive_file'):
|
|
name, inactive_file = line.split()
|
|
break
|
|
|
|
if line.startswith('inactive_file'):
|
|
name, inactive_file = line.split()
|
|
continue
|
|
|
|
inactive_file = int(inactive_file)
|
|
return ((usage_in_bytes - inactive_file) / limit_in_bytes) * 100
|
|
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
def get_memory_usage():
|
|
usage = get_docker_mem_usage_if_limit()
|
|
if usage is not None:
|
|
return usage
|
|
return psutil.virtual_memory().percent
|
|
|
|
|
|
class Time:
|
|
def __init__(self):
|
|
self._timestamps = []
|
|
self._msgs = []
|
|
|
|
def begin(self):
|
|
self._timestamps.append(time.time())
|
|
|
|
def time(self, msg):
|
|
self._timestamps.append(time.time())
|
|
self._msgs.append(msg)
|
|
|
|
def print(self):
|
|
last, *timestamps = self._timestamps
|
|
for timestamp, msg in zip(timestamps, self._msgs):
|
|
logger.debug(f'TIME_IT: {msg} {timestamp - last}')
|
|
last = timestamp
|
|
|
|
|
|
def bulk_get(d, keys, default=None):
|
|
values = []
|
|
for key in keys:
|
|
values.append(d.get(key, default))
|
|
return values
|
|
|
|
|
|
def unique(objects, key=None):
|
|
seen = OrderedDict()
|
|
|
|
if key is None:
|
|
key = lambda item: item
|
|
|
|
for obj in objects:
|
|
v = key(obj)
|
|
if v not in seen:
|
|
seen[v] = obj
|
|
return list(seen.values())
|
|
|
|
|
|
def get_file_by_arch(dir, filename):
|
|
platform_name = platform.system()
|
|
arch = platform.machine()
|
|
|
|
file_path = os.path.join(
|
|
settings.BASE_DIR, dir, platform_name, arch, filename
|
|
)
|
|
return file_path
|
|
|
|
|
|
def pretty_string(data, max_length=128, ellipsis_str='...'):
|
|
"""
|
|
params:
|
|
data: abcdefgh
|
|
max_length: 7
|
|
ellipsis_str: ...
|
|
return:
|
|
ab...gh
|
|
"""
|
|
data = str(data)
|
|
if len(data) < max_length:
|
|
return data
|
|
remain_length = max_length - len(ellipsis_str)
|
|
half = remain_length // 2
|
|
if half <= 1:
|
|
return data[:max_length]
|
|
start = data[:half]
|
|
end = data[-half:]
|
|
data = f'{start}{ellipsis_str}{end}'
|
|
return data
|
|
|
|
|
|
def group_by_count(it, count):
|
|
return [it[i:i + count] for i in range(0, len(it), count)]
|
|
|
|
|
|
def test_ip_connectivity(host, port, timeout=0.5):
|
|
"""
|
|
timeout: seconds
|
|
"""
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
sock.settimeout(timeout)
|
|
result = sock.connect_ex((host, int(port)))
|
|
sock.close()
|
|
if result == 0:
|
|
connectivity = True
|
|
else:
|
|
connectivity = False
|
|
return connectivity
|
|
|
|
|
|
def static_or_direct(logo_path):
|
|
if logo_path.startswith('img/'):
|
|
return static(logo_path)
|
|
else:
|
|
return logo_path
|
|
|
|
|
|
def make_dirs(name, mode=0o755, exist_ok=False):
|
|
""" 默认权限设置为 0o755 """
|
|
return os.makedirs(name, mode=mode, exist_ok=exist_ok)
|
|
|
|
|
|
def distinct(seq, key=None):
|
|
if key is None:
|
|
# 如果未提供关键字参数,则默认使用元素本身作为比较键
|
|
key = lambda x: x
|
|
seen = set()
|
|
result = []
|
|
for item in seq:
|
|
k = key(item)
|
|
if k not in seen:
|
|
seen.add(k)
|
|
result.append(item)
|
|
return result
|
|
|
|
|
|
def is_macos():
|
|
return platform.system() == 'Darwin'
|
|
|
|
|
|
def convert_html_to_markdown(html_str):
|
|
h = html2text.HTML2Text()
|
|
h.body_width = 0
|
|
h.ignore_links = False
|
|
|
|
markdown = h.handle(html_str)
|
|
markdown = markdown.replace('\n\n', '\n')
|
|
markdown = markdown.replace('\n ', '\n')
|
|
return markdown
|
|
|
|
|
|
def many_get(d, keys, default=None):
|
|
res = [d.get(key, default) for key in keys]
|
|
return res
|