From 9535eaa8a8dcdaa60f4c6c6d0523ab9c890a6a7e Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Wed, 26 Aug 2020 12:51:20 +1000 Subject: [PATCH 1/2] Add a host block header only if adding more lines This way, if no lines are added, the config file doesn't end with a host block header. --- src/Service/SshConfig.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Service/SshConfig.php b/src/Service/SshConfig.php index d01e52407..670864354 100644 --- a/src/Service/SshConfig.php +++ b/src/Service/SshConfig.php @@ -71,12 +71,15 @@ public function configureSessionSsh() $lines[] = ''; } + + $hostBlock = ''; if ($domainWildcards) { - $lines[] = 'Host ' . implode(' ', $domainWildcards); + $hostBlock = 'Host ' . implode(' ', $domainWildcards); } $sessionIdentityFile = $this->sshKey->selectIdentity(); if ($sessionIdentityFile !== null) { + $lines[] = $hostBlock; $lines[] = '# This SSH key was detected as corresponding to the session:'; $lines[] = sprintf('IdentityFile %s', $sessionIdentityFile); $lines[] = ''; @@ -95,6 +98,7 @@ public function configureSessionSsh() if ($sessionIdentityFile === null && ($defaultFiles = $this->getUserDefaultSshIdentityFiles())) { $lines[] = '# Include SSH "default" identity files:'; foreach ($defaultFiles as $identityFile) { + $lines[] = $hostBlock; $lines[] = sprintf('IdentityFile %s', $identityFile); } $lines[] = ''; From cd5d307c41ff07e2a144943096b5fd7639684bca Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Wed, 26 Aug 2020 12:52:10 +1000 Subject: [PATCH 2/2] Move the no-lines-added test to the end Wait until we've added all the lines we're going to before we decide to remove the files. Previously, this would never happen because we unconditionally added a host block header to the lines - so in practice, this never caused any strange behaviour. --- src/Service/SshConfig.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Service/SshConfig.php b/src/Service/SshConfig.php index 670864354..e8489b322 100644 --- a/src/Service/SshConfig.php +++ b/src/Service/SshConfig.php @@ -85,15 +85,6 @@ public function configureSessionSsh() $lines[] = ''; } - $sessionSpecificFilename = $this->getSessionSshDir() . DIRECTORY_SEPARATOR . 'config'; - $includerFilename = $this->getCliSshDir() . DIRECTORY_SEPARATOR . 'session.config'; - if (empty($lines)) { - if (\file_exists($includerFilename) || \file_exists($sessionSpecificFilename)) { - $this->fs->remove([$includerFilename, $sessionSpecificFilename]); - } - return false; - } - // Add default files if there is no preferred session identity file. if ($sessionIdentityFile === null && ($defaultFiles = $this->getUserDefaultSshIdentityFiles())) { $lines[] = '# Include SSH "default" identity files:'; @@ -104,6 +95,15 @@ public function configureSessionSsh() $lines[] = ''; } + $sessionSpecificFilename = $this->getSessionSshDir() . DIRECTORY_SEPARATOR . 'config'; + $includerFilename = $this->getCliSshDir() . DIRECTORY_SEPARATOR . 'session.config'; + if (empty($lines)) { + if (\file_exists($includerFilename) || \file_exists($sessionSpecificFilename)) { + $this->fs->remove([$includerFilename, $sessionSpecificFilename]); + } + return false; + } + $this->writeSshIncludeFile($sessionSpecificFilename, $lines); $includerLines = [