2018-03-23 11:46:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import uuid
|
2022-11-22 09:33:09 +00:00
|
|
|
import socket
|
2018-03-25 13:47:29 +00:00
|
|
|
import random
|
2018-12-10 05:03:02 +00:00
|
|
|
import paramiko
|
2022-11-22 09:33:09 +00:00
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
from django.db import models
|
2022-11-22 09:33:09 +00:00
|
|
|
from django.core.cache import cache
|
|
|
|
from django.db.models.query import QuerySet
|
2018-03-23 11:46:46 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2022-09-20 05:54:25 +00:00
|
|
|
from common.db import fields
|
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-11-23 10:36:42 +00:00
|
|
|
from assets.models import Host
|
2022-08-16 03:09:30 +00:00
|
|
|
from .base import BaseAccount
|
2022-11-23 10:36:42 +00:00
|
|
|
from ..const import SecretType
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2021-07-26 08:32:01 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
2022-11-22 09:33:09 +00:00
|
|
|
__all__ = ['Domain', 'GatewayMixin']
|
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-07-11 10:09:06 +00:00
|
|
|
@lazyproperty
|
2018-03-25 13:47:29 +00:00
|
|
|
def gateways(self):
|
2022-11-23 10:36:42 +00:00
|
|
|
return Host.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-11-22 09:33:09 +00:00
|
|
|
class GatewayMixin:
|
|
|
|
id: uuid.UUID
|
|
|
|
port: int
|
|
|
|
address: str
|
|
|
|
accounts: QuerySet
|
|
|
|
private_key_path: str
|
|
|
|
private_key_obj: paramiko.RSAKey
|
|
|
|
UNCONNECTED_KEY_TMPL = 'asset_unconnective_gateway_{}'
|
|
|
|
UNCONNECTED_SILENCE_PERIOD_KEY_TMPL = 'asset_unconnective_gateway_silence_period_{}'
|
|
|
|
UNCONNECTED_SILENCE_PERIOD_BEGIN_VALUE = 60 * 5
|
|
|
|
|
|
|
|
def set_unconnected(self):
|
|
|
|
unconnected_key = self.UNCONNECTED_KEY_TMPL.format(self.id)
|
|
|
|
unconnected_silence_period_key = self.UNCONNECTED_SILENCE_PERIOD_KEY_TMPL.format(self.id)
|
|
|
|
unconnected_silence_period = cache.get(
|
|
|
|
unconnected_silence_period_key, self.UNCONNECTED_SILENCE_PERIOD_BEGIN_VALUE
|
|
|
|
)
|
|
|
|
cache.set(unconnected_silence_period_key, unconnected_silence_period * 2)
|
|
|
|
cache.set(unconnected_key, unconnected_silence_period, unconnected_silence_period)
|
2021-07-26 08:32:01 +00:00
|
|
|
|
|
|
|
def set_connective(self):
|
2022-11-22 09:33:09 +00:00
|
|
|
unconnected_key = self.UNCONNECTED_KEY_TMPL.format(self.id)
|
|
|
|
unconnected_silence_period_key = self.UNCONNECTED_SILENCE_PERIOD_KEY_TMPL.format(self.id)
|
2021-07-26 08:32:01 +00:00
|
|
|
|
2022-11-22 09:33:09 +00:00
|
|
|
cache.delete(unconnected_key)
|
|
|
|
cache.delete(unconnected_silence_period_key)
|
2021-07-26 08:32:01 +00:00
|
|
|
|
2022-11-22 09:33:09 +00:00
|
|
|
def get_is_unconnected(self):
|
|
|
|
unconnected_key = self.UNCONNECTED_KEY_TMPL.format(self.id)
|
|
|
|
return cache.get(unconnected_key, False)
|
2021-07-26 08:32:01 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_connective(self):
|
2022-11-22 09:33:09 +00:00
|
|
|
return not self.get_is_unconnected()
|
2021-07-26 08:32:01 +00:00
|
|
|
|
|
|
|
@is_connective.setter
|
|
|
|
def is_connective(self, value):
|
|
|
|
if value:
|
|
|
|
self.set_connective()
|
|
|
|
else:
|
2022-11-22 09:33:09 +00:00
|
|
|
self.set_unconnected()
|
2021-07-26 08:32:01 +00:00
|
|
|
|
2019-03-19 11:09:09 +00:00
|
|
|
def test_connective(self, local_port=None):
|
2022-11-22 09:33:09 +00:00
|
|
|
# TODO 走ansible runner
|
2019-03-19 11:09:09 +00:00
|
|
|
if local_port is None:
|
|
|
|
local_port = self.port
|
2019-11-13 08:40:34 +00:00
|
|
|
|
2018-12-10 05:03:02 +00:00
|
|
|
client = paramiko.SSHClient()
|
|
|
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
|
proxy = paramiko.SSHClient()
|
|
|
|
proxy.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
|
|
|
|
|
try:
|
2022-11-22 09:33:09 +00:00
|
|
|
proxy.connect(self.address, port=self.port,
|
2018-12-10 05:03:02 +00:00
|
|
|
username=self.username,
|
|
|
|
password=self.password,
|
|
|
|
pkey=self.private_key_obj)
|
|
|
|
except(paramiko.AuthenticationException,
|
|
|
|
paramiko.BadAuthenticationType,
|
2019-07-18 03:13:58 +00:00
|
|
|
paramiko.SSHException,
|
2021-09-13 10:47:20 +00:00
|
|
|
paramiko.ChannelException,
|
2021-07-14 03:17:48 +00:00
|
|
|
paramiko.ssh_exception.NoValidConnectionsError,
|
|
|
|
socket.gaierror) as e:
|
2021-07-26 07:50:09 +00:00
|
|
|
err = str(e)
|
|
|
|
if err.startswith('[Errno None] Unable to connect to port'):
|
2022-09-21 03:17:14 +00:00
|
|
|
err = _('Unable to connect to port {port} on {address}')
|
2022-11-22 09:33:09 +00:00
|
|
|
err = err.format(port=self.port, ip=self.address)
|
2021-07-26 07:50:09 +00:00
|
|
|
elif err == 'Authentication failed.':
|
|
|
|
err = _('Authentication failed')
|
2021-09-13 10:47:20 +00:00
|
|
|
elif err == 'Connect failed':
|
|
|
|
err = _('Connect failed')
|
2021-07-26 08:32:01 +00:00
|
|
|
self.is_connective = False
|
2021-07-26 07:50:09 +00:00
|
|
|
return False, err
|
2018-12-10 05:03:02 +00:00
|
|
|
|
|
|
|
try:
|
2019-03-19 11:09:09 +00:00
|
|
|
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", port=local_port,
|
2018-12-10 05:03:02 +00:00
|
|
|
username=self.username,
|
|
|
|
password=self.password,
|
2022-11-22 09:33:09 +00:00
|
|
|
key_filename=self.private_key_path,
|
2018-12-10 05:03:02 +00:00
|
|
|
sock=sock,
|
|
|
|
timeout=5)
|
2021-09-16 02:59:01 +00:00
|
|
|
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')
|
2021-07-26 08:32:01 +00:00
|
|
|
self.is_connective = False
|
2021-09-16 02:59:01 +00:00
|
|
|
return False, err
|
2018-12-10 05:03:02 +00:00
|
|
|
finally:
|
|
|
|
client.close()
|
2021-07-26 08:32:01 +00:00
|
|
|
self.is_connective = True
|
2018-12-10 05:03:02 +00:00
|
|
|
return True, None
|
2022-11-22 09:33:09 +00:00
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def username(self):
|
|
|
|
account = self.accounts.all().first()
|
|
|
|
if account:
|
|
|
|
return account.username
|
|
|
|
logger.error(f'Gateway {self} has no account')
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def get_secret(self, secret_type):
|
|
|
|
account = self.accounts.filter(secret_type=secret_type).first()
|
|
|
|
if account:
|
|
|
|
return account.secret
|
|
|
|
logger.error(f'Gateway {self} has no {secret_type} account')
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def password(self):
|
|
|
|
secret_type = SecretType.PASSWORD
|
|
|
|
return self.get_secret(secret_type)
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def private_key(self):
|
|
|
|
secret_type = SecretType.SSH_KEY
|
|
|
|
return self.get_secret(secret_type)
|
|
|
|
|
|
|
|
|
|
|
|
class Gateway(BaseAccount):
|
|
|
|
class Protocol(models.TextChoices):
|
|
|
|
ssh = 'ssh', 'SSH'
|
|
|
|
|
|
|
|
name = models.CharField(max_length=128, verbose_name='Name')
|
|
|
|
ip = models.CharField(max_length=128, verbose_name=_('IP'), db_index=True)
|
|
|
|
port = models.IntegerField(default=22, verbose_name=_('Port'))
|
|
|
|
protocol = models.CharField(
|
|
|
|
choices=Protocol.choices, max_length=16, default=Protocol.ssh, verbose_name=_("Protocol")
|
|
|
|
)
|
|
|
|
domain = models.ForeignKey(Domain, on_delete=models.CASCADE, verbose_name=_("Domain"))
|
|
|
|
comment = models.CharField(max_length=128, blank=True, null=True, verbose_name=_("Comment"))
|
|
|
|
is_active = models.BooleanField(default=True, verbose_name=_("Is active"))
|
|
|
|
password = fields.EncryptCharField(max_length=256, blank=True, null=True, verbose_name=_('Password'))
|
|
|
|
private_key = fields.EncryptTextField(blank=True, null=True, verbose_name=_('SSH private key'))
|
|
|
|
public_key = fields.EncryptTextField(blank=True, null=True, verbose_name=_('SSH public key'))
|
|
|
|
|
|
|
|
secret = None
|
|
|
|
secret_type = None
|
|
|
|
privileged = None
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = [('name', 'org_id')]
|
|
|
|
verbose_name = _("Gateway")
|
|
|
|
permissions = [
|
|
|
|
('test_gateway', _('Test gateway'))
|
|
|
|
]
|