2018-03-23 11:46:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import uuid
|
2018-03-25 13:47:29 +00:00
|
|
|
import random
|
2022-12-02 12:28:49 +00:00
|
|
|
import socket
|
2022-12-01 07:21:53 +00:00
|
|
|
import paramiko
|
2022-12-02 12:28:49 +00:00
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2022-11-22 09:33:09 +00:00
|
|
|
from common.utils import get_logger, lazyproperty
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2022-12-01 10:22:41 +00:00
|
|
|
from assets.models import Host, Platform
|
2022-12-02 12:28:49 +00:00
|
|
|
from assets.const import GATEWAY_NAME, SecretType, Connectivity
|
2022-12-01 10:22:41 +00:00
|
|
|
from orgs.mixins.models import OrgManager
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2021-07-26 08:32:01 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
2022-12-01 10:22:41 +00:00
|
|
|
__all__ = ['Domain', 'Gateway']
|
2018-03-23 11:46:46 +00:00
|
|
|
|
|
|
|
|
2018-07-12 16:00:35 +00:00
|
|
|
class Domain(OrgModelMixin):
|
2018-03-23 11:46:46 +00:00
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2020-07-02 10:51:16 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
2018-03-23 11:46:46 +00:00
|
|
|
comment = models.TextField(blank=True, verbose_name=_('Comment'))
|
2022-09-01 06:46:31 +00:00
|
|
|
date_created = models.DateTimeField(auto_now_add=True, null=True, verbose_name=_('Date created'))
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Domain")
|
2020-07-02 10:51:16 +00:00
|
|
|
unique_together = [('org_id', 'name')]
|
2021-01-17 04:44:12 +00:00
|
|
|
ordering = ('name',)
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2022-12-01 07:21:53 +00:00
|
|
|
@classmethod
|
|
|
|
def get_gateway_queryset(cls):
|
2022-12-01 10:22:41 +00:00
|
|
|
return Gateway.objects.all()
|
2022-12-01 07:21:53 +00:00
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
@lazyproperty
|
2018-03-25 13:47:29 +00:00
|
|
|
def gateways(self):
|
2022-12-01 07:21:53 +00:00
|
|
|
return self.get_gateway_queryset().filter(domain=self, is_active=True)
|
2018-03-25 13:47:29 +00:00
|
|
|
|
2022-09-29 12:44:45 +00:00
|
|
|
def select_gateway(self):
|
|
|
|
return self.random_gateway()
|
|
|
|
|
2018-03-25 13:47:29 +00:00
|
|
|
def random_gateway(self):
|
2021-07-26 08:32:01 +00:00
|
|
|
gateways = [gw for gw in self.gateways if gw.is_connective]
|
|
|
|
if gateways:
|
|
|
|
return random.choice(gateways)
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
|
|
logger.warn(f'Gateway all bad. domain={self}, gateway_num={len(self.gateways)}.')
|
|
|
|
if self.gateways:
|
2021-07-26 08:32:01 +00:00
|
|
|
return random.choice(self.gateways)
|
2018-03-25 13:47:29 +00:00
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2022-12-01 10:22:41 +00:00
|
|
|
class GatewayManager(OrgManager):
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
queryset = queryset.filter(platform__name=GATEWAY_NAME)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def bulk_create(self, objs, batch_size=None, ignore_conflicts=False):
|
|
|
|
platform = Gateway().default_platform
|
|
|
|
for obj in objs:
|
|
|
|
obj.platform_id = platform.id
|
|
|
|
return super().bulk_create(objs, batch_size, ignore_conflicts)
|
|
|
|
|
|
|
|
|
2022-12-01 07:21:53 +00:00
|
|
|
class Gateway(Host):
|
2022-12-01 10:22:41 +00:00
|
|
|
objects = GatewayManager()
|
|
|
|
|
2022-12-01 07:21:53 +00:00
|
|
|
class Meta:
|
|
|
|
proxy = True
|
2021-07-26 08:32:01 +00:00
|
|
|
|
2022-12-01 10:22:41 +00:00
|
|
|
@lazyproperty
|
|
|
|
def default_platform(self):
|
|
|
|
return Platform.objects.get(name=GATEWAY_NAME, internal=True)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
platform = self.default_platform
|
|
|
|
self.platform_id = platform.id
|
|
|
|
return super().save(*args, **kwargs)
|
2022-12-02 09:36:55 +00:00
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def select_accounts(self) -> dict:
|
|
|
|
account_dict = {}
|
|
|
|
accounts = self.accounts.filter(is_active=True).order_by('-privileged', '-date_updated')
|
|
|
|
password_account = accounts.filter(secret_type=SecretType.PASSWORD).first()
|
|
|
|
if password_account:
|
|
|
|
account_dict[SecretType.PASSWORD] = password_account
|
|
|
|
|
|
|
|
ssh_key_account = accounts.filter(secret_type=SecretType.SSH_KEY).first()
|
|
|
|
if ssh_key_account:
|
|
|
|
account_dict[SecretType.SSH_KEY] = ssh_key_account
|
|
|
|
return account_dict
|
|
|
|
|
|
|
|
@property
|
|
|
|
def password(self):
|
|
|
|
account = self.select_accounts.get(SecretType.PASSWORD)
|
|
|
|
return account.secret if account else None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def private_key(self):
|
|
|
|
account = self.select_accounts.get(SecretType.SSH_KEY)
|
2022-12-02 12:28:49 +00:00
|
|
|
return account.private_key if account else None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def private_key_obj(self):
|
|
|
|
account = self.select_accounts.get(SecretType.SSH_KEY)
|
|
|
|
return account.private_key_obj if account else None
|
2022-12-02 09:36:55 +00:00
|
|
|
|
2022-12-02 12:28:49 +00:00
|
|
|
@property
|
2022-12-02 09:36:55 +00:00
|
|
|
def private_key_path(self):
|
|
|
|
account = self.select_accounts.get(SecretType.SSH_KEY)
|
|
|
|
return account.private_key_path if account else None
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def username(self):
|
|
|
|
accounts = self.select_accounts.values()
|
|
|
|
if len(accounts) == 0:
|
|
|
|
return None
|
|
|
|
accounts = sorted(
|
|
|
|
accounts, key=lambda x: x['privileged'], reverse=True
|
|
|
|
)
|
|
|
|
return accounts[0].username
|
2022-12-02 12:28:49 +00:00
|
|
|
|
|
|
|
def test_connective(self, local_port=None):
|
|
|
|
local_port = self.port if local_port is None else local_port
|
|
|
|
client = paramiko.SSHClient()
|
|
|
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
|
proxy = paramiko.SSHClient()
|
|
|
|
proxy.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
|
|
|
|
|
try:
|
|
|
|
proxy.connect(
|
|
|
|
self.address,
|
|
|
|
port=self.port,
|
|
|
|
username=self.username,
|
|
|
|
password=self.password,
|
|
|
|
pkey=self.private_key_obj
|
|
|
|
)
|
|
|
|
except(
|
|
|
|
paramiko.AuthenticationException,
|
|
|
|
paramiko.BadAuthenticationType,
|
|
|
|
paramiko.SSHException,
|
|
|
|
paramiko.ChannelException,
|
|
|
|
paramiko.ssh_exception.NoValidConnectionsError,
|
|
|
|
socket.gaierror
|
|
|
|
) as e:
|
|
|
|
err = str(e)
|
|
|
|
if err.startswith('[Errno None] Unable to connect to port'):
|
|
|
|
err = _('Unable to connect to port {port} on {address}')
|
|
|
|
err = err.format(port=self.port, address=self.address)
|
|
|
|
elif err == 'Authentication failed.':
|
|
|
|
err = _('Authentication failed')
|
|
|
|
elif err == 'Connect failed':
|
|
|
|
err = _('Connect failed')
|
|
|
|
self.set_connectivity(Connectivity.FAILED)
|
|
|
|
return False, err
|
|
|
|
|
|
|
|
try:
|
|
|
|
sock = proxy.get_transport().open_channel(
|
|
|
|
'direct-tcpip', ('127.0.0.1', local_port), ('127.0.0.1', 0)
|
|
|
|
)
|
|
|
|
client.connect(
|
|
|
|
'127.0.0.1',
|
|
|
|
sock=sock,
|
|
|
|
timeout=5,
|
|
|
|
port=local_port,
|
|
|
|
username=self.username,
|
|
|
|
password=self.password,
|
|
|
|
key_filename=self.private_key_path,
|
|
|
|
)
|
|
|
|
except (
|
|
|
|
paramiko.SSHException,
|
|
|
|
paramiko.ssh_exception.SSHException,
|
|
|
|
paramiko.ChannelException,
|
|
|
|
paramiko.AuthenticationException,
|
|
|
|
TimeoutError
|
|
|
|
) as e:
|
|
|
|
|
|
|
|
err = getattr(e, 'text', str(e))
|
|
|
|
if err == 'Connect failed':
|
|
|
|
err = _('Connect failed')
|
|
|
|
self.set_connectivity(Connectivity.FAILED)
|
|
|
|
return False, err
|
|
|
|
finally:
|
|
|
|
client.close()
|
|
|
|
self.set_connectivity(Connectivity.OK)
|
|
|
|
return True, None
|