Commit 298ec2ae authored by Ash McKenzie's avatar Ash McKenzie

Add private attr_readers

For :uri, :username, :passwordm, :ssh_cmd, :known_hosts_file and :private_key_file
parent c40010b3
...@@ -23,12 +23,12 @@ module QA ...@@ -23,12 +23,12 @@ module QA
def username=(name) def username=(name)
@username = name @username = name
@uri.user = name uri.user = name
end end
def password=(pass) def password=(pass)
@password = pass @password = pass
@uri.password = CGI.escape(pass).gsub('+', '%20') uri.password = CGI.escape(pass).gsub('+', '%20')
end end
def use_default_credentials def use_default_credentials
...@@ -42,7 +42,7 @@ module QA ...@@ -42,7 +42,7 @@ module QA
end end
def clone(opts = '') def clone(opts = '')
run_and_redact_credentials(build_git_command("git clone #{opts} #{@uri} ./")) run_and_redact_credentials(build_git_command("git clone #{opts} #{uri} ./"))
end end
def checkout(branch_name) def checkout(branch_name)
...@@ -82,7 +82,7 @@ module QA ...@@ -82,7 +82,7 @@ module QA
end end
def push_changes(branch = 'master') def push_changes(branch = 'master')
output, _ = run_and_redact_credentials(build_git_command("git push #{@uri} #{branch}")) output, _ = run_and_redact_credentials(build_git_command("git push #{uri} #{branch}"))
output output
end end
...@@ -93,31 +93,33 @@ module QA ...@@ -93,31 +93,33 @@ module QA
def use_ssh_key(key) def use_ssh_key(key)
@private_key_file = Tempfile.new("id_#{SecureRandom.hex(8)}") @private_key_file = Tempfile.new("id_#{SecureRandom.hex(8)}")
File.binwrite(@private_key_file, key.private_key) File.binwrite(private_key_file, key.private_key)
File.chmod(0700, @private_key_file) File.chmod(0700, private_key_file)
@known_hosts_file = Tempfile.new("known_hosts_#{SecureRandom.hex(8)}") @known_hosts_file = Tempfile.new("known_hosts_#{SecureRandom.hex(8)}")
keyscan_params = ['-H'] keyscan_params = ['-H']
keyscan_params << "-p #{@uri.port}" if @uri.port keyscan_params << "-p #{uri.port}" if uri.port
keyscan_params << @uri.host keyscan_params << uri.host
run_and_redact_credentials("ssh-keyscan #{keyscan_params.join(' ')} >> #{@known_hosts_file.path}") run_and_redact_credentials("ssh-keyscan #{keyscan_params.join(' ')} >> #{known_hosts_file.path}")
configure_ssh_command("ssh -i #{@private_key_file.path} -o UserKnownHostsFile=#{@known_hosts_file.path}") configure_ssh_command("ssh -i #{private_key_file.path} -o UserKnownHostsFile=#{known_hosts_file.path}")
end end
def delete_ssh_key def delete_ssh_key
return unless @private_key_file return unless private_key_file
@private_key_file.close(true) private_key_file.close(true)
@known_hosts_file.close(true) known_hosts_file.close(true)
end end
def build_git_command(command_str) def build_git_command(command_str)
[@ssh_cmd, command_str].compact.join(' ') [ssh_cmd, command_str].compact.join(' ')
end end
private private
attr_reader :uri, :username, :password, :ssh_cmd, :known_hosts_file, :private_key_file
# Since the remote URL contains the credentials, and git occasionally # Since the remote URL contains the credentials, and git occasionally
# outputs the URL. Note that stderr is redirected to stdout. # outputs the URL. Note that stderr is redirected to stdout.
def run_and_redact_credentials(command) def run_and_redact_credentials(command)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment