From 019478f534b574b59b12a9ce68c0a17264efbbd3 Mon Sep 17 00:00:00 2001 From: Aidaho Date: Fri, 3 May 2024 17:19:16 +0300 Subject: [PATCH] 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. --- app/modules/server/ssh.py | 40 ++------------------------------------- 1 file changed, 2 insertions(+), 38 deletions(-) diff --git a/app/modules/server/ssh.py b/app/modules/server/ssh.py index 6744222e..14db2cc6 100644 --- a/app/modules/server/ssh.py +++ b/app/modules/server/ssh.py @@ -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/'