From f6c95e25aa714fe7cda9b67ae63035f905cb313c Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Wed, 26 Jan 2022 08:15:29 -0500 Subject: [PATCH 01/10] debug printouts Signed-off-by: Jose Borreguero --- django_remote_submission/tasks.py | 3 ++- django_remote_submission/wrapper/remote.py | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/django_remote_submission/tasks.py b/django_remote_submission/tasks.py index 3cceb4b..d7583d8 100644 --- a/django_remote_submission/tasks.py +++ b/django_remote_submission/tasks.py @@ -228,7 +228,8 @@ def submit_job_to_server(job_pk, password=None, public_key_filename=None, userna :param bool remote: Either runs this task locally on the host or in a remote server. """ - + print("DEBUG: Entering submit_job_to_server") + print(f"DEBUG: password={password}, public_key_filename={public_key_filename}") logger.debug("submit_job_to_server: %s", locals().keys()) wrapper_cls = RemoteWrapper if remote else LocalWrapper diff --git a/django_remote_submission/wrapper/remote.py b/django_remote_submission/wrapper/remote.py index cc2bb53..ebbd04d 100644 --- a/django_remote_submission/wrapper/remote.py +++ b/django_remote_submission/wrapper/remote.py @@ -121,6 +121,8 @@ class RemoteWrapper(object): private key file :return RemoteWrapper: returns itself """ + print("DEBUG: Entering wrapper.remote.connect()") + print(f"DEBUG: password={password} key_filename={key_filename}") if key_filename is not None: if not os.path.exists(str(key_filename)): raise ValueError(f"File {key_filename} does not exist") @@ -256,7 +258,8 @@ class RemoteWrapper(object): : raise ValueError: when no password and no stored public key are supplied :return SSHClient: an instace of the SSH tunnel """ - + print("DEBUG: entering wrapper.remote._start_client()") + print(f"DEBUG: password={password}") client = SSHClient() client.set_missing_host_key_policy(AutoAddPolicy()) @@ -281,10 +284,13 @@ class RemoteWrapper(object): six.raise_from(ValueError(f'incorrect password: {str(e)}'), e) else: logger.debug("Trying to connect with the public key") + print(f"DEBUG: self._key_filename={self._key_filename}") if self._key_filename is not None: try: logger.info("Connecting to %s with public key.", server_hostname) + print("DEBUG: Entering paramiko.client.connect()") + print(f"DEBUG: {server_hostname}, {server_port}, {username}, {self._key_filename}") client.connect( server_hostname, port=server_port, -- GitLab From 18e9c04dba20495b343c3a1a734264f5226b93d7 Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Wed, 26 Jan 2022 08:57:39 -0500 Subject: [PATCH 02/10] debug printouts through logger.error Signed-off-by: Jose Borreguero --- django_remote_submission/tasks.py | 4 ++-- django_remote_submission/wrapper/remote.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/django_remote_submission/tasks.py b/django_remote_submission/tasks.py index d7583d8..23194f3 100644 --- a/django_remote_submission/tasks.py +++ b/django_remote_submission/tasks.py @@ -228,8 +228,8 @@ def submit_job_to_server(job_pk, password=None, public_key_filename=None, userna :param bool remote: Either runs this task locally on the host or in a remote server. """ - print("DEBUG: Entering submit_job_to_server") - print(f"DEBUG: password={password}, public_key_filename={public_key_filename}") + logger.error("DEBUG: Entering submit_job_to_server") + logger.error(f"DEBUG: password={password}, public_key_filename={public_key_filename}") logger.debug("submit_job_to_server: %s", locals().keys()) wrapper_cls = RemoteWrapper if remote else LocalWrapper diff --git a/django_remote_submission/wrapper/remote.py b/django_remote_submission/wrapper/remote.py index ebbd04d..1f53d28 100644 --- a/django_remote_submission/wrapper/remote.py +++ b/django_remote_submission/wrapper/remote.py @@ -121,8 +121,8 @@ class RemoteWrapper(object): private key file :return RemoteWrapper: returns itself """ - print("DEBUG: Entering wrapper.remote.connect()") - print(f"DEBUG: password={password} key_filename={key_filename}") + logger.error("DEBUG: Entering wrapper.remote.connect()") + logger.error(f"DEBUG: password={password} key_filename={key_filename}") if key_filename is not None: if not os.path.exists(str(key_filename)): raise ValueError(f"File {key_filename} does not exist") @@ -284,13 +284,13 @@ class RemoteWrapper(object): six.raise_from(ValueError(f'incorrect password: {str(e)}'), e) else: logger.debug("Trying to connect with the public key") - print(f"DEBUG: self._key_filename={self._key_filename}") + logger.error(f"DEBUG: self._key_filename={self._key_filename}") if self._key_filename is not None: try: logger.info("Connecting to %s with public key.", server_hostname) - print("DEBUG: Entering paramiko.client.connect()") - print(f"DEBUG: {server_hostname}, {server_port}, {username}, {self._key_filename}") + logger.error("DEBUG: Entering paramiko.client.connect()") + logger.error(f"DEBUG: {server_hostname}, {server_port}, {username}, {self._key_filename}") client.connect( server_hostname, port=server_port, -- GitLab From 255f0f983ddbcabc5feae0d032fce9c8f41ed44d Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Wed, 26 Jan 2022 20:34:51 -0500 Subject: [PATCH 03/10] debug printouts through logger.error Signed-off-by: Jose Borreguero --- django_remote_submission/wrapper/remote.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/django_remote_submission/wrapper/remote.py b/django_remote_submission/wrapper/remote.py index 1f53d28..d8a9dc0 100644 --- a/django_remote_submission/wrapper/remote.py +++ b/django_remote_submission/wrapper/remote.py @@ -219,9 +219,12 @@ class RemoteWrapper(object): parameters and is called when new output appears on stderr. """ + logger.error("DEBUG: entering exec_command()") + logger.error(f"DEBUG: args={str(args)}; workdir={workdir}") chdir = self._make_command(['cd', workdir], None) run = self._make_command(args, timeout) command = '{} && {}'.format(chdir, run) + logger.error(f"DEBUG: run={run}; command={command}") logger.info('exec_command(command={!r})'.format(command)) transport = self._client.get_transport() @@ -307,8 +310,12 @@ class RemoteWrapper(object): return client def _make_command(self, args, timeout): + logger.error("DEBUG: entering _make_command()") + logger.error(f"DEBUG: args={str(args)}; timeout={timeout}") + for arg in args: # this loop only for debugging purposes + logger.error(f"DEBUG: arg={arg}, cmd_quote(arg)={cmd_quote(arg)}") command = ' '.join(cmd_quote(arg) for arg in args) - + logger.error(f"DEBUG: command={command}") if timeout is not None: command = 'timeout {}s {}'.format(timeout.total_seconds(), command) return command -- GitLab From 409091179d091a8fd49bccb1abf19aa950375f89 Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Thu, 27 Jan 2022 00:55:35 -0500 Subject: [PATCH 04/10] debug printouts through logger.error Signed-off-by: Jose Borreguero --- django_remote_submission/tasks.py | 2 ++ django_remote_submission/wrapper/remote.py | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/django_remote_submission/tasks.py b/django_remote_submission/tasks.py index 23194f3..4f8f71e 100644 --- a/django_remote_submission/tasks.py +++ b/django_remote_submission/tasks.py @@ -396,6 +396,8 @@ def copy_key_to_server(public_key_filename: Optional[Union[PosixPath, str]], :param bool remote: Either runs this task locally on the host or in a remote server. """ + logger.error("DEBUG: entering copy_key_to_server") + logger.error(f"public_key_filename={public_key_filename}, username={username}, password={password}") wrapper_cls = RemoteWrapper if remote else LocalWrapper wrapper = wrapper_cls(hostname=hostname, username=username, port=port) with wrapper.connect(password): diff --git a/django_remote_submission/wrapper/remote.py b/django_remote_submission/wrapper/remote.py index d8a9dc0..7f555fc 100644 --- a/django_remote_submission/wrapper/remote.py +++ b/django_remote_submission/wrapper/remote.py @@ -335,6 +335,8 @@ class RemoteWrapper(object): wrapper.close() """ + logger.error("DEBUG: entering deploy_key_if_it_does_not_exist") + logger.error(f"public_key_filename={public_key_filename}") assert os.path.exists(public_key_filename) if self._client is None: raise ValueError('Wrapper must be connected before deploy_key is called') @@ -342,7 +344,11 @@ class RemoteWrapper(object): with open(public_key_filename, 'rt') as f: key = f.read().strip() - self._client.exec_command('mkdir -p ~/.ssh/') + logger.error(f"DEBUG: Sending command mkdir -p ~/.ssh/") + stdin, stdout, stderr = self._client.exec_command('mkdir -p ~/.ssh/') + logger.error(f"DEBUG: {str(stdout.readlines())}") + logger.error(f"DEBUG: {str(stderr.readlines())}") + self._client.exec_command('chmod 700 ~/.ssh/') self._client.exec_command('chmod 644 ~/.ssh/authorized_keys') @@ -354,7 +360,11 @@ class RemoteWrapper(object): fi '''.format(cmd_quote(key.strip('\n')))) + logger.error("DEBUG: appending key to authorized keys") stdin, stdout, stderr = self._client.exec_command(command) + logger.error(f"DEBUG: {str(stdout.readlines())}") + logger.error(f"DEBUG: {str(stderr.readlines())}") + logger.debug(stdout.readlines()) logger.debug(stderr.readlines()) -- GitLab From 4ed97cc69440a921f47a9d2d6818f50650d293ad Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Thu, 27 Jan 2022 17:31:47 -0500 Subject: [PATCH 05/10] Register IdentitiFileModel in the admin page Signed-off-by: Jose Borreguero --- django_remote_submission/admin.py | 9 ++++++++- tests/unit/test_models.py | 3 +-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/django_remote_submission/admin.py b/django_remote_submission/admin.py index d80d7fc..07897f4 100644 --- a/django_remote_submission/admin.py +++ b/django_remote_submission/admin.py @@ -7,9 +7,10 @@ from django.utils.translation import ngettext_lazy as _ from django.shortcuts import render from django.http.response import HttpResponseRedirect -from .models import Server, Job, Log, Interpreter, Result +from .models import Server, Job, Log, Interpreter, Result, IdentityFileModel from .tasks import submit_job_to_server + @admin.register(Interpreter) class InterpreterAdmin(admin.ModelAdmin): """Manage interpreters with default admin interface.""" @@ -138,3 +139,9 @@ class LogAdmin(admin.ModelAdmin): """Manage logs with the default admin interface.""" pass + + +@admin.register(IdentityFileModel) +class IdentityFileModelAdmin(admin.ModelAdmin): + """Manage interpreters with default admin interface.""" + pass diff --git a/tests/unit/test_models.py b/tests/unit/test_models.py index 7f4b08c..3544c1f 100644 --- a/tests/unit/test_models.py +++ b/tests/unit/test_models.py @@ -8,8 +8,7 @@ test_django-remote-submission Tests for `django-remote-submission` models module. """ # package imports -from django_remote_submission.models import IdentityFileModel -from django_remote_submission.models import Interpreter, Log, Job, Result, Server +from django_remote_submission.models import IdentityFileModel, Interpreter, Log, Job, Result, Server from django_remote_submission.wrapper.remote import IdentityFile # third party imports -- GitLab From 9290b846d86c129cb01d01deb2f3512076eb7265 Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Thu, 27 Jan 2022 20:00:16 -0500 Subject: [PATCH 06/10] bugfix when input directory is of type "str" Signed-off-by: Jose Borreguero --- django_remote_submission/wrapper/remote.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/django_remote_submission/wrapper/remote.py b/django_remote_submission/wrapper/remote.py index 7f555fc..e21d7c7 100644 --- a/django_remote_submission/wrapper/remote.py +++ b/django_remote_submission/wrapper/remote.py @@ -35,7 +35,7 @@ logger = logging.getLogger(__name__) class IdentityFile: r"""Provide temporary RSA SSH keys""" def __init__(self, - sshdir: PosixPath = Path.home() / ".ssh", + sshdir: Union[str, PosixPath] = Path.home() / ".ssh", persistent: bool = False ) -> None: r""" @@ -43,8 +43,9 @@ class IdentityFile: :param persistent: unless set to `True, RSA files will be automatically deleted when the object is about to be garbage collected. """ - sshdir.mkdir(mode=700, parents=True, exist_ok=True) - _, name = tempfile.mkstemp(prefix="id_rsa_", dir=sshdir) + dirpath = Path(sshdir) if isinstance(sshdir, str) else sshdir + dirpath.mkdir(mode=700, parents=True, exist_ok=True) + _, name = tempfile.mkstemp(prefix="id_rsa_", dir=dirpath) self._private = Path(name) self._public = Path(name + ".pub") self._persistent = persistent -- GitLab From a96d9e847a6e47cecef76638a4a2b9b50c1e2a0f Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Mon, 31 Jan 2022 20:09:02 -0500 Subject: [PATCH 07/10] submission of jobs to the server requires the private, not the public, key Previously, id_rsa.pub was being passed which did not work but did not raise any errors because paramiko.client.connect automatically found private key ida_rsa under ~/.ssh/ Signed-off-by: Jose Borreguero --- django_remote_submission/tasks.py | 24 +++++++++++++----------- tests/test_tasks.py | 19 ++++++++++++++++++- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/django_remote_submission/tasks.py b/django_remote_submission/tasks.py index 4f8f71e..e4e02c4 100644 --- a/django_remote_submission/tasks.py +++ b/django_remote_submission/tasks.py @@ -210,7 +210,7 @@ class LogContainer(object): @shared_task -def submit_job_to_server(job_pk, password=None, public_key_filename=None, username=None, +def submit_job_to_server(job_pk, password=None, key_filename=None, username=None, timeout=None, log_policy=LogPolicy.LOG_LIVE, store_results=None, remote=True): """Submit a job to the remote server. @@ -219,7 +219,7 @@ def submit_job_to_server(job_pk, password=None, public_key_filename=None, userna :param int job_pk: the primary key of the :class:`models.Job` to submit :param str password: the password of the user submitting the job - :param public_key_filename: the path where it is. + :param key_filename: the path to the private key file :param str username: the username of the user submitting, if it is different from the owner of the job :param datetime.timedelta timeout: the timeout for running the job @@ -229,7 +229,7 @@ def submit_job_to_server(job_pk, password=None, public_key_filename=None, userna """ logger.error("DEBUG: Entering submit_job_to_server") - logger.error(f"DEBUG: password={password}, public_key_filename={public_key_filename}") + logger.error(f"DEBUG: password={password}, key_filename={key_filename}") logger.debug("submit_job_to_server: %s", locals().keys()) wrapper_cls = RemoteWrapper if remote else LocalWrapper @@ -250,7 +250,7 @@ def submit_job_to_server(job_pk, password=None, public_key_filename=None, userna log_policy=log_policy, ) - with wrapper.connect(password, public_key_filename): + with wrapper.connect(password, key_filename): wrapper.chdir(job.remote_directory) with wrapper.open(job.remote_filename, 'wt') as f: @@ -315,7 +315,7 @@ def submit_job_to_server(job_pk, password=None, public_key_filename=None, userna @shared_task -def copy_job_to_server(job_pk, password=None, public_key_filename=None, username=None, +def copy_job_to_server(job_pk, password=None, key_filename=None, username=None, timeout=None, log_policy=LogPolicy.LOG_LIVE, store_results=None, remote=True): """Copy a job file to the remote server. @@ -324,7 +324,7 @@ def copy_job_to_server(job_pk, password=None, public_key_filename=None, username :param int job_pk: the primary key of the :class:`models.Job` to submit :param str password: the password of the user submitting the job - :param public_key_filename: the path where it is. + :param key_filename: the path to the private key file :param str username: the username of the user submitting, if it is different from the owner of the job :param datetime.timedelta timeout: the timeout for running the job @@ -349,7 +349,7 @@ def copy_job_to_server(job_pk, password=None, public_key_filename=None, username port=job.server.port, ) - with wrapper.connect(password, public_key_filename): + with wrapper.connect(password, key_filename): wrapper.chdir(job.remote_directory) with wrapper.open(job.remote_filename, 'wt') as f: @@ -408,7 +408,8 @@ def copy_key_to_server(public_key_filename: Optional[Union[PosixPath, str]], @shared_task def delete_key_from_server(public_key_filename: Optional[Union[PosixPath, str]], username: str, - password: str, + password: Optional[str], + key_filename: Optional[str], hostname: str, port: int = 22, remote: bool = True @@ -418,15 +419,16 @@ def delete_key_from_server(public_key_filename: Optional[Union[PosixPath, str]], This can be used as a Celery task, if the library is installed and running. - :param public_key_filename: the path where it is. + :param public_key_filename: the path to the public key file :param username: the username of the user submitting - :param password: the password of the user submitting the job + :param key_filename: the password of the user submitting the job + :param password: the path to the private key file :param hostname: The hostname used to connect to the server :param port: The port to connect to for SSH (usually 22) :param remote: Either runs this task locally on the host or in a remote server. """ wrapper_cls = RemoteWrapper if remote else LocalWrapper wrapper = wrapper_cls(hostname=hostname, username=username, port=port) - with wrapper.connect(password): + with wrapper.connect(password=password, key_filename=key_filename): wrapper.delete_key(public_key_filename) return None diff --git a/tests/test_tasks.py b/tests/test_tasks.py index f67b3cb..5d6861f 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -526,7 +526,7 @@ def test_delete_key_old_way(env): @pytest.mark.remote_required @pytest.mark.django_db def test_deploy_and_delete_key(env): - r"""This is the new way of deploying and deleting the private key""" + r"""This is the new way of deploying and deleting the public key""" id_file = IdentityFile(sshdir=Path("/tmp")) # create temporary SSH identity file copy_key_to_server( @@ -549,10 +549,27 @@ def test_deploy_and_delete_key(env): with wrapper.connect(key_filename=id_file.private): pass + # delete the key from server passing the password for credentials delete_key_from_server( public_key_filename=id_file.public, username=env.remote_user, password=env.remote_password, + key_filename=None, + hostname=env.server_hostname, + port=env.server_port, + remote=runs_remotely, + ) + + # delete the key from server passing the private key file for credentials + copy_key_to_server(public_key_filename=id_file.public, username=env.remote_user, password=env.remote_password, + hostname=env.server_hostname, port=env.server_port, remote=runs_remotely) + with wrapper.connect(key_filename=id_file.private): + pass + delete_key_from_server( + public_key_filename=id_file.public, + username=env.remote_user, + password=None, + key_filename=id_file.private, hostname=env.server_hostname, port=env.server_port, remote=runs_remotely, -- GitLab From 2f43f3a0a488f1ff1bf0749e56953d4eb35fb949 Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Tue, 1 Feb 2022 06:32:01 -0500 Subject: [PATCH 08/10] overwrite IdentityFileModel.delete() Signed-off-by: Jose Borreguero --- django_remote_submission/models.py | 9 +++++++++ tests/unit/test_models.py | 18 +++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/django_remote_submission/models.py b/django_remote_submission/models.py index 3a4b8a6..d05a059 100644 --- a/django_remote_submission/models.py +++ b/django_remote_submission/models.py @@ -34,6 +34,9 @@ from model_utils import Choices from model_utils.fields import StatusField, AutoCreatedField from model_utils.models import TimeStampedModel +# standard imports +from pathlib import Path + # Thanks http://stackoverflow.com/a/7394475 class ListField(models.TextField): # noqa: D101 @@ -517,3 +520,9 @@ class IdentityFileModel(TimeStampedModel): r"""Override the save method to prevent updating the record, if extant""" kwargs["force_insert"] = True return super().save(*args, **kwargs) + + def delete(self, *args, **kwargs): + r"""Overwrite base delete method by first deleting the SSH key files""" + Path(self.public).unlink() + Path(self.private).unlink() + super().delete(*args, **kwargs) diff --git a/tests/unit/test_models.py b/tests/unit/test_models.py index 3544c1f..eade556 100644 --- a/tests/unit/test_models.py +++ b/tests/unit/test_models.py @@ -112,6 +112,7 @@ class TestIdentityFileModel: assert os.path.exists(record.private) assert os.path.exists(record.public) + @pytest.mark.django_db def test_create(self, user): id_file = IdentityFile(sshdir="/tmp") @@ -121,6 +122,9 @@ class TestIdentityFileModel: assert record.recipient == user assert os.path.exists(record.private) assert os.path.exists(record.public) + record.delete() + assert os.path.exists(record.private) is False + assert os.path.exists(record.public) is False @pytest.mark.django_db(transaction=True) def test_create_from_username(self, user): @@ -133,9 +137,6 @@ class TestIdentityFileModel: record = IdentityFileModel.objects.create_from_username(user.username) assert record.recipient == user assert Path(record.private).parent == Path.home() / ".ssh" - for file in [record.private, record.public]: - assert os.path.exists(file) - os.remove(file) # return the newly created IdentityFileModel record, do not instantiate another one record_2 = IdentityFileModel.objects.create_from_username(user.username) @@ -155,3 +156,14 @@ class TestIdentityFileModel: record.save() assert str(e.value) == "UNIQUE constraint failed: django_remote_submission_identityfilemodel.recipient_id" [os.remove(file) for file in [id_file.private, id_file.public]] + + @pytest.mark.django_db(transaction=True) + def test_delete(self, user): + r"""delete the record, along with the SSH key files""" + username = user.username + record = IdentityFileModel.objects.create_from_username(user.username) + record.delete() + assert os.path.exists(record.private) is False + assert os.path.exists(record.public) is False + # the IdentityFileModel instance is deleted, but not its associated recipient user + assert user.username == username -- GitLab From f5b04f6c7d24a779dbabb3f1678740ca88f356d6 Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Tue, 1 Feb 2022 08:22:59 -0500 Subject: [PATCH 09/10] remove DEBUG print statements Signed-off-by: Jose Borreguero --- django_remote_submission/tasks.py | 13 ----------- django_remote_submission/wrapper/remote.py | 27 +--------------------- 2 files changed, 1 insertion(+), 39 deletions(-) diff --git a/django_remote_submission/tasks.py b/django_remote_submission/tasks.py index e4e02c4..0b83da6 100644 --- a/django_remote_submission/tasks.py +++ b/django_remote_submission/tasks.py @@ -228,10 +228,6 @@ def submit_job_to_server(job_pk, password=None, key_filename=None, username=None :param bool remote: Either runs this task locally on the host or in a remote server. """ - logger.error("DEBUG: Entering submit_job_to_server") - logger.error(f"DEBUG: password={password}, key_filename={key_filename}") - logger.debug("submit_job_to_server: %s", locals().keys()) - wrapper_cls = RemoteWrapper if remote else LocalWrapper job = Job.objects.get(pk=job_pk) @@ -286,21 +282,14 @@ def submit_job_to_server(job_pk, password=None, key_filename=None, username=None results = [] for attr in file_attrs: - # logger.debug('Listing directory: {!r}'.format(attr)) - if attr is script_attr: continue - if attr.st_mtime < script_mtime: continue - if not is_matching(attr.filename, store_results): - # logger.debug('Listing directory: is_matching: {}'.format(attr.filename)) continue else: - # logger.debug('Listing directory: not is_matching: {}'.format(attr.filename)) pass - result = Result.objects.create( remote_filename=attr.filename, job=job, @@ -396,8 +385,6 @@ def copy_key_to_server(public_key_filename: Optional[Union[PosixPath, str]], :param bool remote: Either runs this task locally on the host or in a remote server. """ - logger.error("DEBUG: entering copy_key_to_server") - logger.error(f"public_key_filename={public_key_filename}, username={username}, password={password}") wrapper_cls = RemoteWrapper if remote else LocalWrapper wrapper = wrapper_cls(hostname=hostname, username=username, port=port) with wrapper.connect(password): diff --git a/django_remote_submission/wrapper/remote.py b/django_remote_submission/wrapper/remote.py index e21d7c7..8924c58 100644 --- a/django_remote_submission/wrapper/remote.py +++ b/django_remote_submission/wrapper/remote.py @@ -122,8 +122,6 @@ class RemoteWrapper(object): private key file :return RemoteWrapper: returns itself """ - logger.error("DEBUG: Entering wrapper.remote.connect()") - logger.error(f"DEBUG: password={password} key_filename={key_filename}") if key_filename is not None: if not os.path.exists(str(key_filename)): raise ValueError(f"File {key_filename} does not exist") @@ -220,12 +218,9 @@ class RemoteWrapper(object): parameters and is called when new output appears on stderr. """ - logger.error("DEBUG: entering exec_command()") - logger.error(f"DEBUG: args={str(args)}; workdir={workdir}") chdir = self._make_command(['cd', workdir], None) run = self._make_command(args, timeout) command = '{} && {}'.format(chdir, run) - logger.error(f"DEBUG: run={run}; command={command}") logger.info('exec_command(command={!r})'.format(command)) transport = self._client.get_transport() @@ -262,8 +257,6 @@ class RemoteWrapper(object): : raise ValueError: when no password and no stored public key are supplied :return SSHClient: an instace of the SSH tunnel """ - print("DEBUG: entering wrapper.remote._start_client()") - print(f"DEBUG: password={password}") client = SSHClient() client.set_missing_host_key_policy(AutoAddPolicy()) @@ -288,13 +281,9 @@ class RemoteWrapper(object): six.raise_from(ValueError(f'incorrect password: {str(e)}'), e) else: logger.debug("Trying to connect with the public key") - logger.error(f"DEBUG: self._key_filename={self._key_filename}") if self._key_filename is not None: try: - logger.info("Connecting to %s with public key.", - server_hostname) - logger.error("DEBUG: Entering paramiko.client.connect()") - logger.error(f"DEBUG: {server_hostname}, {server_port}, {username}, {self._key_filename}") + logger.info("Connecting to %s with public key.", server_hostname) client.connect( server_hostname, port=server_port, @@ -311,12 +300,7 @@ class RemoteWrapper(object): return client def _make_command(self, args, timeout): - logger.error("DEBUG: entering _make_command()") - logger.error(f"DEBUG: args={str(args)}; timeout={timeout}") - for arg in args: # this loop only for debugging purposes - logger.error(f"DEBUG: arg={arg}, cmd_quote(arg)={cmd_quote(arg)}") command = ' '.join(cmd_quote(arg) for arg in args) - logger.error(f"DEBUG: command={command}") if timeout is not None: command = 'timeout {}s {}'.format(timeout.total_seconds(), command) return command @@ -336,8 +320,6 @@ class RemoteWrapper(object): wrapper.close() """ - logger.error("DEBUG: entering deploy_key_if_it_does_not_exist") - logger.error(f"public_key_filename={public_key_filename}") assert os.path.exists(public_key_filename) if self._client is None: raise ValueError('Wrapper must be connected before deploy_key is called') @@ -345,10 +327,7 @@ class RemoteWrapper(object): with open(public_key_filename, 'rt') as f: key = f.read().strip() - logger.error(f"DEBUG: Sending command mkdir -p ~/.ssh/") stdin, stdout, stderr = self._client.exec_command('mkdir -p ~/.ssh/') - logger.error(f"DEBUG: {str(stdout.readlines())}") - logger.error(f"DEBUG: {str(stderr.readlines())}") self._client.exec_command('chmod 700 ~/.ssh/') self._client.exec_command('chmod 644 ~/.ssh/authorized_keys') @@ -361,11 +340,7 @@ class RemoteWrapper(object): fi '''.format(cmd_quote(key.strip('\n')))) - logger.error("DEBUG: appending key to authorized keys") stdin, stdout, stderr = self._client.exec_command(command) - logger.error(f"DEBUG: {str(stdout.readlines())}") - logger.error(f"DEBUG: {str(stderr.readlines())}") - logger.debug(stdout.readlines()) logger.debug(stderr.readlines()) -- GitLab From 6b77eb7afc54f95fe46f4e2b9fe1af4f3396993d Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Tue, 1 Feb 2022 08:25:44 -0500 Subject: [PATCH 10/10] correct flake8 redflags Signed-off-by: Jose Borreguero --- django_remote_submission/wrapper/remote.py | 3 +-- tests/unit/test_models.py | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/django_remote_submission/wrapper/remote.py b/django_remote_submission/wrapper/remote.py index 8924c58..61a5337 100644 --- a/django_remote_submission/wrapper/remote.py +++ b/django_remote_submission/wrapper/remote.py @@ -19,7 +19,6 @@ except ImportError: from pipes import quote as cmd_quote # standard imports -import datetime import logging import os from pathlib import Path, PosixPath @@ -327,7 +326,7 @@ class RemoteWrapper(object): with open(public_key_filename, 'rt') as f: key = f.read().strip() - stdin, stdout, stderr = self._client.exec_command('mkdir -p ~/.ssh/') + self._client.exec_command('mkdir -p ~/.ssh/') self._client.exec_command('chmod 700 ~/.ssh/') self._client.exec_command('chmod 644 ~/.ssh/authorized_keys') diff --git a/tests/unit/test_models.py b/tests/unit/test_models.py index eade556..4919dbb 100644 --- a/tests/unit/test_models.py +++ b/tests/unit/test_models.py @@ -112,7 +112,6 @@ class TestIdentityFileModel: assert os.path.exists(record.private) assert os.path.exists(record.public) - @pytest.mark.django_db def test_create(self, user): id_file = IdentityFile(sshdir="/tmp") -- GitLab