mirror of https://github.com/Aidaho12/haproxy-wi
v7.2.5.0: Simplify SSH key handling in server module
This commit simplifies the SSH key handling functionality in the server module. Unnecessary definitions and methods for checking and handling different key types have been removed. Now, paramiko's standard load_private_key method is directly used to load the SSH keys, which has streamlined the code and made it more maintainable.pull/381/head
parent
b66c4d0793
commit
019478f534
|
@ -2,7 +2,6 @@ import os
|
|||
from cryptography.fernet import Fernet
|
||||
|
||||
import paramiko
|
||||
from paramiko import RSAKey, DSSKey, ECDSAKey, Ed25519Key, PKey
|
||||
from flask import render_template, request
|
||||
|
||||
import app.modules.db.cred as cred_sql
|
||||
|
@ -118,36 +117,7 @@ def create_ssh_cread_api(name: str, enable: str, group: str, username: str, pass
|
|||
roxywi_common.handle_exceptions(e, 'Roxy-WI server', f'Cannot create SSH credentials {name}', roxywi=1)
|
||||
|
||||
|
||||
def get_key_class_name(uploaded_key, passphrase):
|
||||
for pkey_class in (ECDSAKey, RSAKey, DSSKey, Ed25519Key):
|
||||
try:
|
||||
key = pkey_class.from_private_key(uploaded_key, passphrase)
|
||||
class_name = str(pkey_class).split('.')[-1].rstrip(">'")
|
||||
print(class_name)
|
||||
|
||||
# return class_name
|
||||
return key
|
||||
|
||||
except Exception as e:
|
||||
print("An exception occurred: {}".format(e))
|
||||
pass
|
||||
|
||||
|
||||
def proper_method_call(filepath, passphrase):
|
||||
key_class_name = get_key_class_name(filepath, passphrase)
|
||||
print('key_class_name',key_class_name)
|
||||
key_class = getattr(paramiko, key_class_name)
|
||||
key_method = getattr(key_class, "from_private_key")
|
||||
print('filepath',filepath)
|
||||
try:
|
||||
key = key_method(filepath, passphrase)
|
||||
except Exception as e:
|
||||
raise Exception(f'something went wrong: {e}')
|
||||
return key
|
||||
|
||||
|
||||
def upload_ssh_key(name: str, user_group: str, key: str, passphrase: str) -> str:
|
||||
import io
|
||||
if '..' in name:
|
||||
raise Exception('error: nice try')
|
||||
|
||||
|
@ -155,15 +125,9 @@ def upload_ssh_key(name: str, user_group: str, key: str, passphrase: str) -> str
|
|||
raise Exception('error: please select credentials first')
|
||||
|
||||
try:
|
||||
print('key1',key)
|
||||
key = io.StringIO(key)
|
||||
print('key2',key)
|
||||
# key = paramiko.pkey.load_private_key(key, password=passphrase)
|
||||
key = paramiko.Ed25519Key.from_private_key(key, password=passphrase)
|
||||
# key = get_key_class_name(key, passphrase)
|
||||
# key = proper_method_call(key, passphrase)
|
||||
key = paramiko.pkey.load_private_key(key, password=passphrase)
|
||||
except Exception as e:
|
||||
raise Exception(f'error: Cannot read SSH key: {e}')
|
||||
raise Exception(f'error: Cannot save SSH key file: {e}')
|
||||
|
||||
lib_path = get_config.get_config_var('main', 'lib_path')
|
||||
full_dir = f'{lib_path}/keys/'
|
||||
|
|
Loading…
Reference in New Issue