Skip to content

Commit

Permalink
Ensure UUID of command is shared when redacted
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lazyatom authored and mattbrictson committed Feb 21, 2019
1 parent df110b2 commit a703af2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ appear at the top.
## [Unreleased][]

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

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

Expand Down
7 changes: 2 additions & 5 deletions lib/sshkit/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Command

Failed = Class.new(SSHKit::StandardError)

attr_reader :command, :args, :options, :started_at, :started, :exit_status, :full_stdout, :full_stderr
attr_reader :command, :args, :options, :started_at, :started, :exit_status, :full_stdout, :full_stderr, :uuid

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

def complete?
Expand All @@ -41,10 +42,6 @@ def started=(new_started)
@started = new_started
end

def uuid
@uuid ||= Digest::SHA1.hexdigest(SecureRandom.random_bytes(10))[0..7]
end

def success?
exit_status.nil? ? false : exit_status.to_i == 0
end
Expand Down
5 changes: 5 additions & 0 deletions test/unit/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,10 @@ def test_command_raises_command_failed_error_when_non_zero_exit
assert_equal "whoami exit status: 1\nwhoami stdout: Nothing written\nwhoami stderr: Nothing written\n", error.message
end

def test_shares_same_uuid_before_and_after_redaction
command = Command.new(:whoami)
command_with_redaction = command.with_redaction
assert_equal command.uuid, command_with_redaction.uuid, "UUID should be stable before and after redaction"
end
end
end

0 comments on commit a703af2

Please sign in to comment.