2015-11-05 14:47:45 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
import random
|
2015-11-14 13:13:02 +00:00
|
|
|
|
import os.path
|
2015-11-25 08:01:07 +00:00
|
|
|
|
import shutil
|
|
|
|
|
from paramiko import SSHException
|
2015-11-15 04:38:57 +00:00
|
|
|
|
from paramiko.rsakey import RSAKey
|
2015-11-18 07:15:08 +00:00
|
|
|
|
from jumpserver.api import mkdir
|
2015-11-14 13:13:02 +00:00
|
|
|
|
from uuid import uuid4
|
2015-11-25 06:59:57 +00:00
|
|
|
|
from jumpserver.api import CRYPTOR
|
2015-11-28 13:02:23 +00:00
|
|
|
|
from os import makedirs
|
|
|
|
|
|
2015-11-26 09:23:16 +00:00
|
|
|
|
from tempfile import NamedTemporaryFile
|
2015-11-14 13:13:02 +00:00
|
|
|
|
|
2015-11-15 15:30:37 +00:00
|
|
|
|
from jumpserver.settings import KEY_DIR
|
2015-11-14 13:13:02 +00:00
|
|
|
|
|
2015-11-05 14:47:45 +00:00
|
|
|
|
|
|
|
|
|
def get_rand_pass():
|
|
|
|
|
"""
|
|
|
|
|
get a reandom password.
|
|
|
|
|
"""
|
2015-11-25 06:59:57 +00:00
|
|
|
|
CRYPTOR.gen_rand_pass(20)
|
2015-11-05 14:47:45 +00:00
|
|
|
|
|
2015-11-14 13:13:02 +00:00
|
|
|
|
|
2015-11-05 14:47:45 +00:00
|
|
|
|
def updates_dict(*args):
|
|
|
|
|
"""
|
|
|
|
|
surport update multi dict
|
|
|
|
|
"""
|
|
|
|
|
result = {}
|
|
|
|
|
for d in args:
|
|
|
|
|
result.update(d)
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
2015-11-25 08:01:07 +00:00
|
|
|
|
def gen_keys(key="", key_path_dir=""):
|
2015-11-14 13:13:02 +00:00
|
|
|
|
"""
|
|
|
|
|
在KEY_DIR下创建一个 uuid命名的目录,
|
|
|
|
|
并且在该目录下 生产一对秘钥
|
|
|
|
|
:return: 返回目录名(uuid)
|
|
|
|
|
"""
|
2015-11-15 15:30:37 +00:00
|
|
|
|
key_basename = "key-" + uuid4().hex
|
2015-11-25 08:01:07 +00:00
|
|
|
|
if not key_path_dir:
|
|
|
|
|
key_path_dir = os.path.join(KEY_DIR, 'role_key', key_basename)
|
2015-11-14 13:13:02 +00:00
|
|
|
|
private_key = os.path.join(key_path_dir, 'id_rsa')
|
|
|
|
|
public_key = os.path.join(key_path_dir, 'id_rsa.pub')
|
2015-11-25 08:01:07 +00:00
|
|
|
|
mkdir(key_path_dir, mode=0755)
|
|
|
|
|
if not key:
|
|
|
|
|
key = RSAKey.generate(2048)
|
|
|
|
|
key.write_private_key_file(private_key)
|
|
|
|
|
else:
|
|
|
|
|
key_file = os.path.join(key_path_dir, 'id_rsa')
|
|
|
|
|
with open(key_file, 'w') as f:
|
|
|
|
|
f.write(key)
|
|
|
|
|
f.close()
|
|
|
|
|
with open(key_file) as f:
|
|
|
|
|
try:
|
|
|
|
|
key = RSAKey.from_private_key(f)
|
|
|
|
|
except SSHException:
|
|
|
|
|
shutil.rmtree(key_path_dir, ignore_errors=True)
|
|
|
|
|
raise SSHException
|
2015-11-21 11:20:11 +00:00
|
|
|
|
os.chmod(private_key, 0644)
|
2015-11-14 13:13:02 +00:00
|
|
|
|
|
2015-11-15 04:38:57 +00:00
|
|
|
|
with open(public_key, 'w') as content_file:
|
|
|
|
|
for data in [key.get_name(),
|
|
|
|
|
" ",
|
|
|
|
|
key.get_base64(),
|
|
|
|
|
" %s@%s" % ("jumpserver", os.uname()[1])]:
|
|
|
|
|
content_file.write(data)
|
2015-11-14 13:13:02 +00:00
|
|
|
|
return key_path_dir
|
|
|
|
|
|
|
|
|
|
|
2015-11-05 14:47:45 +00:00
|
|
|
|
if __name__ == "__main__":
|
2015-11-14 13:13:02 +00:00
|
|
|
|
print gen_keys()
|
2015-11-05 14:47:45 +00:00
|
|
|
|
|
|
|
|
|
|