Commit c1437353 authored by ibuler's avatar ibuler

Add ssh-key-gen function

parent 43af5383
...@@ -96,7 +96,7 @@ TEMPLATES = [ ...@@ -96,7 +96,7 @@ TEMPLATES = [
}, },
] ]
WSGI_APPLICATION = 'jumpserver.wsgi.application' # WSGI_APPLICATION = 'jumpserver.wsgi.application'
# Database # Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases # https://docs.djangoproject.com/en/1.10/ref/settings/#databases
...@@ -230,9 +230,6 @@ MEDIA_URL = '/media/' ...@@ -230,9 +230,6 @@ MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/') + '/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/') + '/'
# Custom User Auth model
AUTH_USER_MODEL = 'users.User'
# Use django-bootstrap-form to format template, input max width arg # Use django-bootstrap-form to format template, input max width arg
BOOTSTRAP_COLUMN_COUNT = 11 BOOTSTRAP_COLUMN_COUNT = 11
...@@ -275,3 +272,7 @@ WS4REDIS_PREFIX = 'demo' ...@@ -275,3 +272,7 @@ WS4REDIS_PREFIX = 'demo'
SESSION_ENGINE = 'redis_sessions.session' SESSION_ENGINE = 'redis_sessions.session'
SESSION_REDIS_PREFIX = 'session' SESSION_REDIS_PREFIX = 'session'
# Custom User Auth model
AUTH_USER_MODEL = 'users.User'
...@@ -10,9 +10,8 @@ from django.db import models ...@@ -10,9 +10,8 @@ from django.db import models
from django.contrib.auth.models import AbstractUser, Permission from django.contrib.auth.models import AbstractUser, Permission
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
from rest_framework.authtoken.models import Token
from django.db import IntegrityError from django.db import IntegrityError
from rest_framework.authtoken.models import Token
# class Role(models.Model): # class Role(models.Model):
...@@ -250,5 +249,8 @@ def generate_fake(): ...@@ -250,5 +249,8 @@ def generate_fake():
@receiver(post_save, sender=settings.AUTH_USER_MODEL) @receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs): def create_auth_token(sender, instance=None, created=False, **kwargs):
if created: if created:
try:
Token.objects.create(user=instance) Token.objects.create(user=instance)
except IntegrityError:
pass
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
</td> </td>
<td class="text-center"> <td class="text-center">
<a href="{% url 'users:user-edit' pk=user.id %}" class="btn btn-xs btn-info">编辑</a> <a href="{% url 'users:user-edit' pk=user.id %}" class="btn btn-xs btn-info">编辑</a>
<a href="{% url 'users:user-delete' pk=user.id %}" class="btn btn-xs btn-danger del {% if user.username == 'admin' %} disabled {% endif %}">删除</a> <a href="{% url 'users:user-delete' pk=user.id %}" class="btn btn-xs btn-danger del {% if user.id == request.user.id or user.username == 'admin' %} disabled {% endif %}">删除</a>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
......
# ~*~ coding: utf-8 ~*~ # ~*~ coding: utf-8 ~*~
# #
import os
import logging
from paramiko.rsakey import RSAKey
from django.contrib.auth.mixins import UserPassesTestMixin from django.contrib.auth.mixins import UserPassesTestMixin
from django.urls import reverse_lazy from django.urls import reverse_lazy
try:
import cStringIO as StringIO
except ImportError:
import StringIO
logger = logging.getLogger('jumpserver')
class AdminUserRequiredMixin(UserPassesTestMixin): class AdminUserRequiredMixin(UserPassesTestMixin):
login_url = reverse_lazy('users:login') login_url = reverse_lazy('users:login')
def test_func(self): def test_func(self):
return self.request.user.is_staff return self.request.user.is_staff
def ssh_key_gen(length=2048, password=None, username='root', hostname=None):
"""Generate user ssh private and public key
Use paramiko RSAKey generate it.
"""
if hostname is None:
hostname = os.uname()[1]
f = StringIO.StringIO()
try:
logger.debug('Begin to generate ssh private key ...')
private_key_obj = RSAKey.generate(length)
private_key_obj.write_private_key(f, password=password)
private_key = f.getvalue()
public_key = "%(key_type)s %(key_content)s %(username)s@%(hostname)s" % {
'key_type': private_key_obj.get_name(),
'key_content': private_key_obj.get_base64(),
'username': username,
'hostname': hostname,
}
logger.debug('Finish to generate ssh private key ...')
return private_key, public_key
except IOError:
raise IOError('These is error when generate ssh key.')
...@@ -13,3 +13,4 @@ wcwidth==0.1.7 ...@@ -13,3 +13,4 @@ wcwidth==0.1.7
websocket-client==0.37.0 websocket-client==0.37.0
djangorestframework==3.4.5 djangorestframework==3.4.5
ForgeryPy==0.1 ForgeryPy==0.1
paramiko==2.0.2
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment