From 9be5ce07f583e90dbc2102519cb2ab9eb7240740 Mon Sep 17 00:00:00 2001 From: Aidaho Date: Wed, 3 May 2023 19:14:13 +0300 Subject: [PATCH] v6.3.11.0 Changelog: https://roxy-wi.org/changelog#6_3_11 --- api/api_funct.py | 9 +++++---- app/modules/server/ssh.py | 15 +++++---------- app/options.py | 5 ++++- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/api/api_funct.py b/api/api_funct.py index 218ef43d..0b12022b 100644 --- a/api/api_funct.py +++ b/api/api_funct.py @@ -793,11 +793,12 @@ def upload_ssh_key(): groups = sql.select_groups(id=group_id) for group in groups: user_group = group.name - if ssh_mod.upload_ssh_key(name, user_group, key): + try: + ssh_mod.upload_ssh_key(f'{name}_{user_group}', user_group, key) data = {'status': 'done'} return dict(data) - else: - data = {'status': 'error: check all fields'} + except Exception as e: + data = {'status': f'{e}'} return dict(data) @@ -815,7 +816,7 @@ def create_server(): login, group_id, role_id = sql.get_username_groupid_from_api_token(token) try: - if server_mod.create_server(hostname, ip, group_id, virt, 1, master_id, cred_id, port, desc, 0, 0, 0, 0, '1', role_id=role_id, token=token): + if server_mod.create_server(hostname, ip, group_id, virt, 1, master_id, cred_id, port, desc, 0, 0, 0, 0, role_id=role_id, token=token): data = {'status': 'done'} roxywi_common.logging(ip, f'A new server {hostname} has been created', roxywi=1, keep_history=1, service='server') return dict(data) diff --git a/app/modules/server/ssh.py b/app/modules/server/ssh.py index 52396cb7..a4a74d8a 100644 --- a/app/modules/server/ssh.py +++ b/app/modules/server/ssh.py @@ -92,18 +92,15 @@ def create_ssh_cread_api(name: str, enable: str, group: str, username: str, pass def upload_ssh_key(name: str, user_group: str, key: str) -> bool: if '..' in name: - print('error: nice try') - return False + raise Exception('error: nice try') if name == '': - print('error: please select credentials first') - return False + raise Exception('error: please select credentials first') try: key = paramiko.pkey.load_private_key(key) except Exception as e: - print(f'error: Cannot save SSH key file: {e}') - return False + 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/' @@ -126,8 +123,7 @@ def upload_ssh_key(name: str, user_group: str, key: str) -> bool: try: key.write_private_key_file(ssh_keys) except Exception as e: - print(f'error: Cannot save SSH key file: {e}') - return False + raise Exception(f'error: Cannot save SSH key file: {e}') else: print(f'success: SSH key has been saved into: {ssh_keys}') @@ -135,10 +131,9 @@ def upload_ssh_key(name: str, user_group: str, key: str) -> bool: os.chmod(ssh_keys, 0o600) except IOError as e: roxywi_common.logging('Roxy-WI server', e.args[0], roxywi=1) - return False + raise Exception(f'error: something went wrong: {e}') roxywi_common.logging("Roxy-WI server", f"A new SSH cert has been uploaded {ssh_keys}", roxywi=1, login=1) - return True def update_ssh_key() -> None: diff --git a/app/options.py b/app/options.py index 9b0cd949..e795f149 100644 --- a/app/options.py +++ b/app/options.py @@ -847,7 +847,10 @@ if form.getvalue('ssh_cert'): name = common.checkAjaxInput(form.getvalue('name')) key = form.getvalue('ssh_cert') - ssh_mod.upload_ssh_key(name, user_group, key) + try: + ssh_mod.upload_ssh_key(name, user_group, key) + except Exception as e: + print(e) if form.getvalue('new_receiver'): import modules.alerting.alerting as alerting