jumpserver/apps/terminal/models.py

170 lines
6.3 KiB
Python
Raw Normal View History

2016-10-15 09:14:56 +00:00
from __future__ import unicode_literals
2017-11-23 06:08:01 +00:00
import uuid
2016-10-15 09:14:56 +00:00
from django.db import models
2016-10-15 10:28:49 +00:00
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
2018-01-20 14:22:09 +00:00
from django.conf import settings
2016-10-15 09:14:56 +00:00
2016-10-15 10:28:49 +00:00
from users.models import User
2017-11-29 11:27:04 +00:00
from .backends.command.models import AbstractSessionCommand
2016-10-15 10:28:49 +00:00
class Terminal(models.Model):
2017-11-23 06:08:01 +00:00
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
2017-12-25 04:22:49 +00:00
name = models.CharField(max_length=32, verbose_name=_('Name'))
2017-10-31 03:34:20 +00:00
remote_addr = models.CharField(max_length=128, verbose_name=_('Remote Address'))
ssh_port = models.IntegerField(verbose_name=_('SSH Port'), default=2222)
http_port = models.IntegerField(verbose_name=_('HTTP Port'), default=5000)
2018-01-23 02:36:20 +00:00
command_storage = models.CharField(max_length=128, verbose_name=_("Command storage"), default='default')
2018-01-20 14:22:09 +00:00
replay_storage = models.CharField(max_length=128, verbose_name=_("Replay storage"), default='default')
2017-10-31 03:34:20 +00:00
user = models.OneToOneField(User, related_name='terminal', verbose_name='Application User', null=True, on_delete=models.CASCADE)
is_accepted = models.BooleanField(default=False, verbose_name='Is Accepted')
2017-11-14 01:44:16 +00:00
is_deleted = models.BooleanField(default=False)
2016-10-15 15:34:02 +00:00
date_created = models.DateTimeField(auto_now_add=True)
2016-10-19 10:33:14 +00:00
comment = models.TextField(blank=True, verbose_name=_('Comment'))
2016-10-15 10:28:49 +00:00
@property
def is_active(self):
if self.user and self.user.is_active:
return True
return False
@is_active.setter
def is_active(self, active):
if self.user:
self.user.is_active = active
self.user.save()
2018-01-20 14:22:09 +00:00
def get_common_storage(self):
2018-01-21 07:12:59 +00:00
storage_all = settings.TERMINAL_COMMAND_STORAGE
if self.command_storage in storage_all:
storage = storage_all.get(self.command_storage)
else:
storage = storage_all.get('default')
return {"TERMINAL_COMMAND_STORAGE": storage}
2018-01-20 14:22:09 +00:00
def get_replay_storage(self):
2018-02-26 10:11:46 +00:00
storage_all = settings.TERMINAL_REPLAY_STORAGE
if self.replay_storage in storage_all:
storage = storage_all.get(self.replay_storage)
else:
storage = storage_all.get('default')
return {"TERMINAL_REPLAY_STORAGE": storage}
2018-01-20 14:22:09 +00:00
@property
def config(self):
2018-01-21 07:12:59 +00:00
configs = {}
for k in dir(settings):
if k.startswith('TERMINAL'):
configs[k] = getattr(settings, k)
configs.update(self.get_common_storage())
2018-02-26 10:11:46 +00:00
configs.update(self.get_replay_storage())
2018-01-21 07:12:59 +00:00
return configs
2018-01-20 14:22:09 +00:00
2017-11-14 01:44:16 +00:00
def create_app_user(self):
2017-12-25 04:22:49 +00:00
random = uuid.uuid4().hex[:6]
user, access_key = User.create_app_user(name="{}-{}".format(self.name, random), comment=self.comment)
2016-12-25 09:44:39 +00:00
self.user = user
self.save()
return user, access_key
2016-10-17 07:24:41 +00:00
2017-01-02 16:11:44 +00:00
def delete(self, using=None, keep_parents=False):
if self.user:
self.user.delete()
2017-12-25 04:22:49 +00:00
self.user = None
2017-11-14 01:44:16 +00:00
self.is_deleted = True
self.save()
return
2017-01-02 16:11:44 +00:00
2017-10-31 03:34:20 +00:00
def __str__(self):
2017-11-14 01:44:16 +00:00
status = "Active"
if not self.is_accepted:
status = "NotAccept"
elif self.is_deleted:
status = "Deleted"
elif not self.is_active:
status = "Disable"
return '%s: %s' % (self.name, status)
2016-10-15 10:28:49 +00:00
class Meta:
2016-12-26 16:59:52 +00:00
ordering = ('is_accepted',)
db_table = "terminal"
2016-10-15 10:28:49 +00:00
class Status(models.Model):
2017-11-23 06:08:01 +00:00
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
2017-11-14 01:44:16 +00:00
session_online = models.IntegerField(verbose_name=_("Session Online"), default=0)
cpu_used = models.FloatField(verbose_name=_("CPU Usage"))
memory_used = models.FloatField(verbose_name=_("Memory Used"))
connections = models.IntegerField(verbose_name=_("Connections"))
threads = models.IntegerField(verbose_name=_("Threads"))
boot_time = models.FloatField(verbose_name=_("Boot Time"))
terminal = models.ForeignKey(Terminal, null=True, on_delete=models.CASCADE)
date_created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = 'terminal_status'
2017-12-01 13:22:32 +00:00
get_latest_by = 'date_created'
def __str__(self):
return self.date_created.strftime("%Y-%m-%d %H:%M:%S")
2017-11-14 01:44:16 +00:00
class Session(models.Model):
2017-11-14 01:44:16 +00:00
LOGIN_FROM_CHOICES = (
('ST', 'SSH Terminal'),
('WT', 'Web Terminal'),
)
2018-04-23 03:32:46 +00:00
PROTOCOL_CHOICES = (
('ssh', 'ssh'),
('rdp', 'rdp')
)
2017-11-14 01:44:16 +00:00
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
2017-11-14 01:44:16 +00:00
user = models.CharField(max_length=128, verbose_name=_("User"))
asset = models.CharField(max_length=1024, verbose_name=_("Asset"))
system_user = models.CharField(max_length=128, verbose_name=_("System user"))
2017-11-14 01:44:16 +00:00
login_from = models.CharField(max_length=2, choices=LOGIN_FROM_CHOICES, default="ST")
2018-01-05 10:37:36 +00:00
remote_addr = models.CharField(max_length=15, verbose_name=_("Remote addr"), blank=True, null=True)
2017-11-14 01:44:16 +00:00
is_finished = models.BooleanField(default=False)
2017-11-22 02:54:59 +00:00
has_replay = models.BooleanField(default=False, verbose_name=_("Replay"))
has_command = models.BooleanField(default=False, verbose_name=_("Command"))
2018-03-07 11:05:43 +00:00
terminal = models.ForeignKey(Terminal, null=True, on_delete=models.SET_NULL)
2018-04-23 03:40:30 +00:00
protocol = models.CharField(choices=PROTOCOL_CHOICES, default='ssh', max_length=8)
date_last_active = models.DateTimeField(verbose_name=_("Date last active"), default=timezone.now)
2018-04-23 03:40:30 +00:00
date_start = models.DateTimeField(verbose_name=_("Date start"), db_index=True, default=timezone.now)
date_end = models.DateTimeField(verbose_name=_("Date end"), null=True)
2017-11-14 01:44:16 +00:00
class Meta:
db_table = "terminal_session"
2017-12-04 08:41:00 +00:00
ordering = ["-date_start"]
2017-11-14 01:44:16 +00:00
def __str__(self):
return "{0.id} of {0.user} to {0.asset}".format(self)
class Task(models.Model):
NAME_CHOICES = (
("kill_session", "Kill Session"),
)
2017-11-23 06:08:01 +00:00
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
name = models.CharField(max_length=128, choices=NAME_CHOICES, verbose_name=_("Name"))
args = models.CharField(max_length=1024, verbose_name=_("Args"))
2018-03-07 11:05:43 +00:00
terminal = models.ForeignKey(Terminal, null=True, on_delete=models.SET_NULL)
2017-11-14 01:44:16 +00:00
is_finished = models.BooleanField(default=False)
2016-10-18 15:49:04 +00:00
date_created = models.DateTimeField(auto_now_add=True)
2017-11-14 01:44:16 +00:00
date_finished = models.DateTimeField(null=True)
2016-10-15 10:28:49 +00:00
class Meta:
2017-11-14 01:44:16 +00:00
db_table = "terminal_task"
2017-11-29 11:27:04 +00:00
class Command(AbstractSessionCommand):
2017-11-29 11:27:04 +00:00
class Meta:
db_table = "terminal_command"
2017-12-04 08:41:00 +00:00
ordering = ('-timestamp',)