sshkit-sudo-next 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/sshkit/sudo/command.rb +17 -0
- data/lib/sshkit/sudo/password_sending_interaction_handler.rb +31 -11
- data/lib/sshkit/sudo/stream_redactor.rb +50 -0
- data/lib/sshkit/sudo/sudo_netssh.rb +86 -46
- data/lib/sshkit/sudo/version.rb +1 -1
- data/sshkit-sudo-next.gemspec +4 -5
- metadata +8 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a92ec3b6dbb3ef5951d570a4bf270a7a3e9a3d3469c8d450aa8634ff55413d15
|
|
4
|
+
data.tar.gz: 180082aeebbe9e8e7f2fd21600948dbd65c4d9f40cacc19cd78c5793b050d858
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cfdd4da51d358cb29ba2e7599d986bc164a396c48bc28c1c55a0d8d111b6db152df07262855cacc53f1ead2cca0cdb26571f826517f6177a3684390e3073e377
|
|
7
|
+
data.tar.gz: 66ca3f13ed1efbf7d09fd98ed023d55180bfee885adec5a3d1e09fc2af83780e8067533267fe35b76542c9e67bdbe777fc5797c88959e784bddf87ecd114e5be
|
data/lib/sshkit/sudo/command.rb
CHANGED
|
@@ -10,5 +10,22 @@ module SSHKit
|
|
|
10
10
|
return yield if env_string.empty?
|
|
11
11
|
"( export #{env_string} ; #{yield} )"
|
|
12
12
|
end
|
|
13
|
+
|
|
14
|
+
# The SudoNetssh backend stores redacted output, while the interaction handler must see the raw
|
|
15
|
+
# output (Command#on_stdout/on_stderr would pass it the redacted one).
|
|
16
|
+
#
|
|
17
|
+
def interact(stream_name, data, channel)
|
|
18
|
+
call_interaction_handler(stream_name, data, channel)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def store_stdout(data)
|
|
22
|
+
@stdout = data
|
|
23
|
+
@full_stdout += data
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def store_stderr(data)
|
|
27
|
+
@stderr = data
|
|
28
|
+
@full_stderr += data
|
|
29
|
+
end
|
|
13
30
|
end
|
|
14
31
|
end
|
|
@@ -1,28 +1,48 @@
|
|
|
1
1
|
module SSHKit
|
|
2
2
|
module Sudo
|
|
3
|
-
#
|
|
3
|
+
# Sends the password when a sudo prompt is the last thing a command printed.
|
|
4
4
|
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
5
|
+
# Output is matched per command/stream over a rolling tail, so prompts split across SSH chunks
|
|
6
|
+
# are still recognized. The match is anchored at the end of the output (sudo waits for input
|
|
7
|
+
# after printing the prompt), so ordinary output mentioning "password" (e.g. Chef's
|
|
8
|
+
# "Disabling password aging … [15:19:27…") doesn't trigger a send; a spurious send would be
|
|
9
|
+
# echoed by the PTY into the output.
|
|
9
10
|
#
|
|
10
11
|
class PasswordSendingInteractionHandler
|
|
11
12
|
WRONG_PASSWORD_MESSAGE_REGEX = /Sorry.*\stry\sagain/
|
|
12
|
-
PASSWORD_PROMPT_REGEX = /[Pp]assword.*:/
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
# Classic sudo, and sudo-rs. Custom prompts (`sudo -p`, PAM's "Password:") can be configured via
|
|
15
|
+
# the backend's `password_prompt_regex`; prefer patterns that can't match ordinary output.
|
|
16
|
+
DEFAULT_PASSWORD_PROMPT_REGEX = /\[sudo\] password for [^:\s]+:|\[sudo: authenticate\] Password:/
|
|
17
|
+
|
|
18
|
+
TAIL_SIZE = 256
|
|
19
|
+
|
|
20
|
+
def initialize(servers_password, prompt_regex: nil)
|
|
15
21
|
@servers_password = servers_password
|
|
22
|
+
# Anchored at the end of the output, also for custom patterns.
|
|
23
|
+
@prompt_regex = /(?:#{prompt_regex || DEFAULT_PASSWORD_PROMPT_REGEX})[ \t]*\z/
|
|
24
|
+
@tails = {}
|
|
16
25
|
end
|
|
17
26
|
|
|
18
27
|
def on_data(command, stream_name, data, channel)
|
|
19
|
-
|
|
28
|
+
key = [command.object_id, stream_name]
|
|
29
|
+
tail = (@tails[key] || "".b) + data.b
|
|
30
|
+
tail = tail.byteslice(-TAIL_SIZE, TAIL_SIZE) if tail.bytesize > TAIL_SIZE
|
|
31
|
+
text = tail.dup.force_encoding(Encoding::UTF_8).scrub
|
|
32
|
+
|
|
33
|
+
raise "Wrong password!" if text =~ WRONG_PASSWORD_MESSAGE_REGEX
|
|
20
34
|
|
|
21
|
-
if
|
|
22
|
-
|
|
23
|
-
channel.send_data(
|
|
35
|
+
if text =~ @prompt_regex
|
|
36
|
+
@tails.delete(key)
|
|
37
|
+
channel.send_data(@servers_password)
|
|
38
|
+
else
|
|
39
|
+
@tails[key] = tail
|
|
24
40
|
end
|
|
25
41
|
end
|
|
42
|
+
|
|
43
|
+
def forget(command)
|
|
44
|
+
@tails.delete_if { |(command_id, _), _| command_id == command.object_id }
|
|
45
|
+
end
|
|
26
46
|
end
|
|
27
47
|
end
|
|
28
48
|
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module SSHKit
|
|
2
|
+
module Sudo
|
|
3
|
+
# Replaces a secret in a chunked byte stream, including occurrences split across chunks: a
|
|
4
|
+
# trailing fragment that could be the start of the secret is held back until the next chunk
|
|
5
|
+
# (or #flush) decides it.
|
|
6
|
+
#
|
|
7
|
+
class StreamRedactor
|
|
8
|
+
REPLACEMENT = "[REDACTED]".b
|
|
9
|
+
|
|
10
|
+
def initialize(secret)
|
|
11
|
+
@secret = secret.to_s.b
|
|
12
|
+
@pending = "".b
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def redact(data)
|
|
16
|
+
return data if @secret.empty?
|
|
17
|
+
|
|
18
|
+
rest = @pending + data.b
|
|
19
|
+
redacted = "".b
|
|
20
|
+
|
|
21
|
+
while (index = rest.index(@secret))
|
|
22
|
+
redacted << rest.byteslice(0, index) << REPLACEMENT
|
|
23
|
+
rest = rest.byteslice(index + @secret.bytesize, rest.bytesize)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
held = held_back_size(rest)
|
|
27
|
+
@pending = rest.byteslice(rest.bytesize - held, held)
|
|
28
|
+
|
|
29
|
+
redacted << rest.byteslice(0, rest.bytesize - held)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# A fragment pending at the end of the stream may be a truncated echo of the secret.
|
|
33
|
+
#
|
|
34
|
+
def flush
|
|
35
|
+
pending, @pending = @pending, "".b
|
|
36
|
+
pending.empty? ? pending : REPLACEMENT.dup
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def held_back_size(buffer)
|
|
42
|
+
[@secret.bytesize - 1, buffer.bytesize].min.downto(1) do |size|
|
|
43
|
+
return size if buffer.end_with?(@secret.byteslice(0, size))
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
0
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require 'sshkit'
|
|
2
2
|
require_relative 'password_sending_interaction_handler'
|
|
3
|
+
require_relative 'stream_redactor'
|
|
3
4
|
require_relative 'command'
|
|
4
5
|
|
|
5
6
|
module SSHKit
|
|
@@ -8,12 +9,11 @@ module SSHKit
|
|
|
8
9
|
# can be implemented, if needed, as Configuration attributes.
|
|
9
10
|
#
|
|
10
11
|
class SudoNetssh < Netssh
|
|
11
|
-
PASSWORD_PROMPT_REGEX = /\[sudo\] password for \S+\:/
|
|
12
12
|
|
|
13
13
|
SKIP_STDOUT_LOGGING_PATTERNS = [
|
|
14
14
|
/^\r\n$/,
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
# Skipping this removes context from the wrong password prompt, but it's still understandable.
|
|
16
|
+
SSHKit::Sudo::PasswordSendingInteractionHandler::DEFAULT_PASSWORD_PROMPT_REGEX,
|
|
17
17
|
]
|
|
18
18
|
|
|
19
19
|
class << self
|
|
@@ -30,7 +30,7 @@ module SSHKit
|
|
|
30
30
|
#
|
|
31
31
|
def config
|
|
32
32
|
@config ||= Class.new(Netssh::Configuration) do
|
|
33
|
-
attr_accessor :owner, :servers_password, :commands_log
|
|
33
|
+
attr_accessor :owner, :servers_password, :commands_log, :password_prompt_regex
|
|
34
34
|
end.new
|
|
35
35
|
end
|
|
36
36
|
end
|
|
@@ -38,14 +38,20 @@ module SSHKit
|
|
|
38
38
|
def initialize(*args, &block)
|
|
39
39
|
super
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
# A single snapshot, so that the sent and the redacted passwords can't diverge.
|
|
42
|
+
@password = normalize_password(self.class.config.servers_password)
|
|
43
|
+
@prompt_regex = self.class.config.password_prompt_regex ||
|
|
44
|
+
SSHKit::Sudo::PasswordSendingInteractionHandler::DEFAULT_PASSWORD_PROMPT_REGEX
|
|
45
|
+
@interaction_handler = SSHKit::Sudo::PasswordSendingInteractionHandler.new(
|
|
46
|
+
@password + "\n", prompt_regex: @prompt_regex
|
|
47
|
+
)
|
|
42
48
|
end
|
|
43
49
|
|
|
44
50
|
def capture(*args)
|
|
45
51
|
# To ensure that we clean out the sudo part when the results are returned,
|
|
46
52
|
# otherwise the commands will be corrupt.
|
|
47
53
|
#
|
|
48
|
-
super.gsub(
|
|
54
|
+
super.gsub(/(?:#{@prompt_regex})[ \t]*/, '')
|
|
49
55
|
end
|
|
50
56
|
|
|
51
57
|
# Required because the uploaded file is owned by the SSH user, not the owner.
|
|
@@ -64,6 +70,30 @@ module SSHKit
|
|
|
64
70
|
|
|
65
71
|
private
|
|
66
72
|
|
|
73
|
+
# The configured value may carry trailing newlines (once, one was required).
|
|
74
|
+
#
|
|
75
|
+
def normalize_password(value)
|
|
76
|
+
password = value.to_s.sub(/[\r\n]+\z/, '')
|
|
77
|
+
raise ArgumentError, "The servers password can't contain newlines" if password =~ /[\r\n]/
|
|
78
|
+
|
|
79
|
+
password
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def handle_stdout(cmd, data)
|
|
83
|
+
return if data.empty?
|
|
84
|
+
|
|
85
|
+
cmd.store_stdout(data)
|
|
86
|
+
skip_stdout_logging = SKIP_STDOUT_LOGGING_PATTERNS.any? { |pattern| data =~ pattern }
|
|
87
|
+
output.log_command_data(cmd, :stdout, data) unless skip_stdout_logging
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def handle_stderr(cmd, data)
|
|
91
|
+
return if data.empty?
|
|
92
|
+
|
|
93
|
+
cmd.store_stderr(data)
|
|
94
|
+
output.log_command_data(cmd, :stderr, data)
|
|
95
|
+
end
|
|
96
|
+
|
|
67
97
|
def command(args, options)
|
|
68
98
|
options[:interaction_handler] ||= @interaction_handler
|
|
69
99
|
|
|
@@ -93,50 +123,60 @@ module SSHKit
|
|
|
93
123
|
output.log_command_start(cmd)
|
|
94
124
|
cmd.started = true
|
|
95
125
|
exit_status = nil
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
output.log_command_data(cmd, :stdout, data) unless skip_stdout_logging
|
|
110
|
-
end
|
|
111
|
-
chan.on_extended_data do |ch, _type, data|
|
|
112
|
-
cmd.on_stderr(ch, data)
|
|
113
|
-
output.log_command_data(cmd, :stderr, data)
|
|
126
|
+
# Defense in depth: the password must never reach logs, captures or exception messages,
|
|
127
|
+
# even if echoed back (PTY echo), so output is redacted before SSHKit stores or logs it.
|
|
128
|
+
# The interaction handler is fed the raw output, on the live channel.
|
|
129
|
+
stdout_redactor = SSHKit::Sudo::StreamRedactor.new(@password)
|
|
130
|
+
stderr_redactor = SSHKit::Sudo::StreamRedactor.new(@password)
|
|
131
|
+
begin
|
|
132
|
+
with_ssh do |ssh|
|
|
133
|
+
ssh.open_channel do |chan|
|
|
134
|
+
chan.request_pty
|
|
135
|
+
prepared_command = cmd.to_command
|
|
136
|
+
|
|
137
|
+
if self.class.config.commands_log
|
|
138
|
+
IO.write(self.class.config.commands_log, prepared_command + "\n", mode: 'a')
|
|
114
139
|
end
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
#
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
#
|
|
134
|
-
#
|
|
140
|
+
|
|
141
|
+
chan.exec prepared_command do |_ch, _success|
|
|
142
|
+
chan.on_data do |ch, data|
|
|
143
|
+
cmd.interact(:stdout, data, ch)
|
|
144
|
+
handle_stdout(cmd, stdout_redactor.redact(data))
|
|
145
|
+
end
|
|
146
|
+
chan.on_extended_data do |ch, _type, data|
|
|
147
|
+
cmd.interact(:stderr, data, ch)
|
|
148
|
+
handle_stderr(cmd, stderr_redactor.redact(data))
|
|
149
|
+
end
|
|
150
|
+
chan.on_request("exit-status") do |_ch, data|
|
|
151
|
+
exit_status = data.read_long
|
|
152
|
+
end
|
|
153
|
+
#chan.on_request("exit-signal") do |ch, data|
|
|
154
|
+
# # TODO: This gets called if the program is killed by a signal
|
|
155
|
+
# # might also be a worthwhile thing to report
|
|
156
|
+
# exit_signal = data.read_string.to_i
|
|
157
|
+
# warn ">>> " + exit_signal.inspect
|
|
158
|
+
# output.log_command_killed(cmd, exit_signal)
|
|
159
|
+
#end
|
|
160
|
+
chan.on_open_failed do |_ch|
|
|
161
|
+
# TODO: What do do here?
|
|
162
|
+
# I think we should raise something
|
|
163
|
+
end
|
|
164
|
+
chan.on_process do |_ch|
|
|
165
|
+
# TODO: I don't know if this is useful
|
|
166
|
+
end
|
|
167
|
+
chan.on_eof do |_ch|
|
|
168
|
+
# TODO: chan sends EOF before the exit status has been
|
|
169
|
+
# writtend
|
|
170
|
+
end
|
|
135
171
|
end
|
|
172
|
+
chan.wait
|
|
136
173
|
end
|
|
137
|
-
|
|
174
|
+
ssh.loop
|
|
138
175
|
end
|
|
139
|
-
|
|
176
|
+
handle_stdout(cmd, stdout_redactor.flush)
|
|
177
|
+
handle_stderr(cmd, stderr_redactor.flush)
|
|
178
|
+
ensure
|
|
179
|
+
@interaction_handler.forget(cmd)
|
|
140
180
|
end
|
|
141
181
|
# Set exit_status and log the result upon completion
|
|
142
182
|
if exit_status
|
data/lib/sshkit/sudo/version.rb
CHANGED
data/sshkit-sudo-next.gemspec
CHANGED
|
@@ -6,12 +6,11 @@ require 'sshkit/sudo/version'
|
|
|
6
6
|
Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "sshkit-sudo-next"
|
|
8
8
|
spec.version = SSHKit::Sudo::VERSION
|
|
9
|
-
spec.date = "2026-
|
|
9
|
+
spec.date = "2026-06-11"
|
|
10
10
|
spec.authors = ["Saverio Miroddi"]
|
|
11
11
|
spec.email = ["saverio.pub2@gmail.com"]
|
|
12
12
|
spec.summary = %q{SSHKit extension, for sudo operation with password input.}
|
|
13
|
-
spec.
|
|
14
|
-
spec.homepage = ""
|
|
13
|
+
spec.homepage = "https://github.com/64kramsystem/sshkit-sudo-next"
|
|
15
14
|
spec.license = "MIT"
|
|
16
15
|
|
|
17
16
|
spec.files = `git ls-files -z`.split("\x0")
|
|
@@ -19,9 +18,9 @@ Gem::Specification.new do |spec|
|
|
|
19
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
20
19
|
spec.require_paths = ["lib"]
|
|
21
20
|
|
|
22
|
-
spec.required_ruby_version = ">=
|
|
21
|
+
spec.required_ruby_version = ">= 2.5"
|
|
23
22
|
|
|
24
|
-
spec.add_dependency "sshkit", "~> 1.
|
|
23
|
+
spec.add_dependency "sshkit", "~> 1.25.0"
|
|
25
24
|
spec.add_development_dependency "bundler", "~> 1.7"
|
|
26
25
|
spec.add_development_dependency "rake", "~> 10.0"
|
|
27
26
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sshkit-sudo-next
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Saverio Miroddi
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-
|
|
10
|
+
date: 2026-06-11 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: sshkit
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - "~>"
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 1.
|
|
18
|
+
version: 1.25.0
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - "~>"
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: 1.
|
|
25
|
+
version: 1.25.0
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: bundler
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -51,7 +51,6 @@ dependencies:
|
|
|
51
51
|
- - "~>"
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
53
|
version: '10.0'
|
|
54
|
-
description: SSHKit extension, for sudo operation with password input.
|
|
55
54
|
email:
|
|
56
55
|
- saverio.pub2@gmail.com
|
|
57
56
|
executables: []
|
|
@@ -66,10 +65,11 @@ files:
|
|
|
66
65
|
- lib/sshkit/sudo.rb
|
|
67
66
|
- lib/sshkit/sudo/command.rb
|
|
68
67
|
- lib/sshkit/sudo/password_sending_interaction_handler.rb
|
|
68
|
+
- lib/sshkit/sudo/stream_redactor.rb
|
|
69
69
|
- lib/sshkit/sudo/sudo_netssh.rb
|
|
70
70
|
- lib/sshkit/sudo/version.rb
|
|
71
71
|
- sshkit-sudo-next.gemspec
|
|
72
|
-
homepage:
|
|
72
|
+
homepage: https://github.com/64kramsystem/sshkit-sudo-next
|
|
73
73
|
licenses:
|
|
74
74
|
- MIT
|
|
75
75
|
metadata: {}
|
|
@@ -80,14 +80,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
80
80
|
requirements:
|
|
81
81
|
- - ">="
|
|
82
82
|
- !ruby/object:Gem::Version
|
|
83
|
-
version:
|
|
83
|
+
version: '2.5'
|
|
84
84
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
85
|
requirements:
|
|
86
86
|
- - ">="
|
|
87
87
|
- !ruby/object:Gem::Version
|
|
88
88
|
version: '0'
|
|
89
89
|
requirements: []
|
|
90
|
-
rubygems_version: 4.0.
|
|
90
|
+
rubygems_version: 4.0.20
|
|
91
91
|
specification_version: 4
|
|
92
92
|
summary: SSHKit extension, for sudo operation with password input.
|
|
93
93
|
test_files: []
|