Skip to content

Commit a703af2

Browse files
lazyatommattbrictson
authored andcommitted
Ensure UUID of command is shared when redacted
In logging, the command UUID displayed in the "command start" phase was sometimes different to that displayed with the output of the command. This is because the backend can call `.with_redaction` on a command when logging the start, and this causes the command to be duplicated. However, if the `uuid` method hadn't been called before that point, the copy command would generate its own UUID, distinct from the original command. This change avoids that by eagerly calculating the UUID when the command is created, so that any copied objects share the same UUID.
1 parent df110b2 commit a703af2

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ appear at the top.
66
## [Unreleased][]
77

88
* Your contribution here!
9+
* [#455](https://github.com/capistrano/sshkit/pull/455): Ensure UUID of commands are stable in logging - [@lazyatom](https://github.com/lazyatom)
910

1011
## [1.18.2][] (2019-02-03)
1112

lib/sshkit/command.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Command
99

1010
Failed = Class.new(SSHKit::StandardError)
1111

12-
attr_reader :command, :args, :options, :started_at, :started, :exit_status, :full_stdout, :full_stderr
12+
attr_reader :command, :args, :options, :started_at, :started, :exit_status, :full_stdout, :full_stderr, :uuid
1313

1414
# Initialize a new Command object
1515
#
@@ -25,6 +25,7 @@ def initialize(*args)
2525
@args = args
2626
@options.symbolize_keys!
2727
@stdout, @stderr, @full_stdout, @full_stderr = String.new, String.new, String.new, String.new
28+
@uuid = Digest::SHA1.hexdigest(SecureRandom.random_bytes(10))[0..7]
2829
end
2930

3031
def complete?
@@ -41,10 +42,6 @@ def started=(new_started)
4142
@started = new_started
4243
end
4344

44-
def uuid
45-
@uuid ||= Digest::SHA1.hexdigest(SecureRandom.random_bytes(10))[0..7]
46-
end
47-
4845
def success?
4946
exit_status.nil? ? false : exit_status.to_i == 0
5047
end

test/unit/test_command.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,10 @@ def test_command_raises_command_failed_error_when_non_zero_exit
245245
assert_equal "whoami exit status: 1\nwhoami stdout: Nothing written\nwhoami stderr: Nothing written\n", error.message
246246
end
247247

248+
def test_shares_same_uuid_before_and_after_redaction
249+
command = Command.new(:whoami)
250+
command_with_redaction = command.with_redaction
251+
assert_equal command.uuid, command_with_redaction.uuid, "UUID should be stable before and after redaction"
252+
end
248253
end
249254
end

0 commit comments

Comments
 (0)