Skip to content

Commit cdc55cd

Browse files
authored
Fixes #32606 - Reimplement known host key removal
Ported from theforeman/foreman_remote_execution 0d5078ccbbfcc51d1dfa3097b727d9e8415506ea
1 parent 1f69209 commit cdc55cd

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

lib/smart_proxy_remote_execution_ssh/plugin.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Plugin < Proxy::Plugin
2727
require 'smart_proxy_remote_execution_ssh/dispatcher'
2828
require 'smart_proxy_remote_execution_ssh/log_filter'
2929
require 'smart_proxy_remote_execution_ssh/runners'
30+
require 'smart_proxy_remote_execution_ssh/utils'
3031

3132
Proxy::RemoteExecution::Ssh.validate!
3233

lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def initialize(options, user_method, suspended_action: nil)
121121
@local_working_dir = options.fetch(:local_working_dir, settings.local_working_dir)
122122
@remote_working_dir = options.fetch(:remote_working_dir, settings.remote_working_dir)
123123
@cleanup_working_dirs = options.fetch(:cleanup_working_dirs, settings.cleanup_working_dirs)
124+
@first_execution = options.fetch(:first_execution, false)
124125
@user_method = user_method
125126
end
126127

@@ -148,6 +149,7 @@ def self.build(options, suspended_action:)
148149
end
149150

150151
def start
152+
Proxy::RemoteExecution::Utils.prune_known_hosts!(@host, @ssh_port, logger) if @first_execution
151153
prepare_start
152154
script = initialization_script
153155
logger.debug("executing script:\n#{indent_multiline(script)}")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'open3'
2+
3+
module Proxy::RemoteExecution
4+
module Utils
5+
class << self
6+
def prune_known_hosts!(hostname, port, logger = Logger.new($stdout))
7+
return if Net::SSH::KnownHosts.search_for(hostname).empty?
8+
9+
target = if port == 22
10+
hostname
11+
else
12+
"[#{hostname}]:#{port}"
13+
end
14+
15+
Open3.popen3('ssh-keygen', '-R', target) do |_stdin, stdout, _stderr, wait_thr|
16+
wait_thr.join
17+
stdout.read
18+
end
19+
rescue Errno::ENOENT => e
20+
logger.warn("Could not remove #{hostname} from know_hosts: #{e}")
21+
end
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)