From b936ec6edbefefab731ad74763e1aa3b568406e0 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Mon, 5 Feb 2024 17:55:04 +0100 Subject: [PATCH 1/2] Ask for SSH password using getpass --- pysqa/ext/remote.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/pysqa/ext/remote.py b/pysqa/ext/remote.py index ea94619..ffbb1c1 100644 --- a/pysqa/ext/remote.py +++ b/pysqa/ext/remote.py @@ -1,6 +1,7 @@ # coding: utf-8 # Copyright (c) Jan Janssen +import getpass import json import os import warnings @@ -25,12 +26,18 @@ def __init__(self, config, directory="~/.queues", execute_command=execute_comman ) if "ssh_key" in config.keys(): self._ssh_key = os.path.abspath(os.path.expanduser(config["ssh_key"])) + self._ssh_ask_for_password = False else: self._ssh_key = None if "ssh_password" in config.keys(): self._ssh_password = config["ssh_password"] + self._ssh_ask_for_password = False else: self._ssh_password = None + if "ssh_ask_for_password" in config.keys(): + self._ssh_ask_for_password = config["ssh_ask_for_password"] + else: + self._ssh_ask_for_password = False if "ssh_key_passphrase" in config.keys(): self._ssh_key_passphrase = config["ssh_key_passphrase"] else: @@ -221,7 +228,7 @@ def _transfer_files(self, file_dict, sftp=None, transfer_back=False): def _open_ssh_connection(self): ssh = paramiko.SSHClient() ssh.load_host_keys(self._ssh_known_hosts) - if self._ssh_key is not None and self._ssh_key_passphrase is not None: + if self._ssh_key is not None and self._ssh_key_passphrase is not None and not self._ssh_ask_for_password: ssh.connect( hostname=self._ssh_host, port=self._ssh_port, @@ -229,7 +236,7 @@ def _open_ssh_connection(self): key_filename=self._ssh_key, passphrase=self._ssh_key_passphrase, ) - elif self._ssh_key is not None: + elif self._ssh_key is not None and not self._ssh_ask_for_password: ssh.connect( hostname=self._ssh_host, port=self._ssh_port, @@ -240,6 +247,7 @@ def _open_ssh_connection(self): self._ssh_password is not None and self._ssh_authenticator_service is None and not self._ssh_two_factor_authentication + and not self._ssh_ask_for_password ): ssh.connect( hostname=self._ssh_host, @@ -247,6 +255,13 @@ def _open_ssh_connection(self): username=self._ssh_username, password=self._ssh_password, ) + elif self._ssh_ask_for_password and not self._ssh_two_factor_authentication: + ssh.connect( + hostname=self._ssh_host, + port=self._ssh_port, + username=self._ssh_username, + password=getpass.getpass(prompt='SSH Password: ', stream=None), + ) elif ( self._ssh_password is not None and self._ssh_authenticator_service is not None @@ -287,6 +302,16 @@ def authentication(title, instructions, prompt_list): ssh._transport.auth_interactive_dumb( username=self._ssh_username, handler=None, submethods="" ) + elif self._ssh_ask_for_password and self._ssh_two_factor_authentication: + ssh.connect( + hostname=self._ssh_host, + port=self._ssh_port, + username=self._ssh_username, + password=getpass.getpass(prompt='SSH Password: ', stream=None), + ) + ssh._transport.auth_interactive_dumb( + username=self._ssh_username, handler=None, submethods="" + ) else: raise ValueError("Un-supported authentication method.") From 31be4431279a3f54f3d0048b759a3273f00f5552 Mon Sep 17 00:00:00 2001 From: pyiron-runner Date: Mon, 5 Feb 2024 16:56:18 +0000 Subject: [PATCH 2/2] Format black --- pysqa/ext/remote.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pysqa/ext/remote.py b/pysqa/ext/remote.py index ffbb1c1..d36f4f1 100644 --- a/pysqa/ext/remote.py +++ b/pysqa/ext/remote.py @@ -228,7 +228,11 @@ def _transfer_files(self, file_dict, sftp=None, transfer_back=False): def _open_ssh_connection(self): ssh = paramiko.SSHClient() ssh.load_host_keys(self._ssh_known_hosts) - if self._ssh_key is not None and self._ssh_key_passphrase is not None and not self._ssh_ask_for_password: + if ( + self._ssh_key is not None + and self._ssh_key_passphrase is not None + and not self._ssh_ask_for_password + ): ssh.connect( hostname=self._ssh_host, port=self._ssh_port, @@ -260,7 +264,7 @@ def _open_ssh_connection(self): hostname=self._ssh_host, port=self._ssh_port, username=self._ssh_username, - password=getpass.getpass(prompt='SSH Password: ', stream=None), + password=getpass.getpass(prompt="SSH Password: ", stream=None), ) elif ( self._ssh_password is not None @@ -307,7 +311,7 @@ def authentication(title, instructions, prompt_list): hostname=self._ssh_host, port=self._ssh_port, username=self._ssh_username, - password=getpass.getpass(prompt='SSH Password: ', stream=None), + password=getpass.getpass(prompt="SSH Password: ", stream=None), ) ssh._transport.auth_interactive_dumb( username=self._ssh_username, handler=None, submethods=""