diff options
author | Patrick Steinhardt <ps@pks.im> | 2022-02-28 13:38:00 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-03-01 14:26:20 +0300 |
commit | f1126927dddcfdf3e8f2004cf10bff48196f8544 (patch) | |
tree | 37c7cf88ddaa8b1e92fcd7e950ad0b72d3b8f39e | |
parent | b6e1f3ce3799d61cb7cdbd67952d82c126f44c4f (diff) |
ruby: Drop more unused code
This commit drops more code that is not being used anymore in the Gitaly
side car.
-rw-r--r-- | ruby/lib/gitaly_server.rb | 2 | ||||
-rw-r--r-- | ruby/lib/gitlab/git.rb | 36 | ||||
-rw-r--r-- | ruby/lib/gitlab/git/operation_service.rb | 32 | ||||
-rw-r--r-- | ruby/lib/gitlab/git/path_helper.rb | 28 | ||||
-rw-r--r-- | ruby/lib/gitlab/git/popen.rb | 67 | ||||
-rw-r--r-- | ruby/lib/gitlab/git/ssh_auth.rb | 95 | ||||
-rw-r--r-- | ruby/lib/praefect/transaction.rb | 27 | ||||
-rw-r--r-- | ruby/spec/lib/gitlab/git/popen_spec.rb | 73 | ||||
-rw-r--r-- | ruby/spec/lib/gitlab/git/ssh_auth_spec.rb | 112 | ||||
-rw-r--r-- | ruby/spec/lib/gitlab/git_spec.rb | 41 |
10 files changed, 0 insertions, 513 deletions
diff --git a/ruby/lib/gitaly_server.rb b/ruby/lib/gitaly_server.rb index 2e4b49805..b3d65705f 100644 --- a/ruby/lib/gitaly_server.rb +++ b/ruby/lib/gitaly_server.rb @@ -10,8 +10,6 @@ require_relative 'gitaly_server/wiki_service.rb' require_relative 'gitaly_server/health_service.rb' require_relative 'gitaly_server/feature_flags.rb' -require_relative 'praefect/transaction.rb' - module GitalyServer STORAGE_PATH_HEADER = 'gitaly-storage-path'.freeze REPO_PATH_HEADER = 'gitaly-repo-path'.freeze diff --git a/ruby/lib/gitlab/git.rb b/ruby/lib/gitlab/git.rb index 5ac9a6e0d..ec2c97e3e 100644 --- a/ruby/lib/gitlab/git.rb +++ b/ruby/lib/gitlab/git.rb @@ -49,10 +49,7 @@ module Gitlab BaseError = Class.new(StandardError) CommandError = Class.new(BaseError) CommitError = Class.new(BaseError) - OSError = Class.new(BaseError) - UnknownRef = Class.new(BaseError) PreReceiveError = Class.new(BaseError) - PatchError = Class.new(BaseError) class << self include Gitlab::EncodingHelper @@ -66,23 +63,6 @@ module Gitlab self.ref_name(ref) if self.branch_ref?(ref) end - def committer_hash(email:, name:, timestamp: nil) - return if email.nil? || name.nil? - - # Git strips newlines and angle brackets silently. - # libgit2/Rugged doesn't, but aborts if angle brackets are present. - # See upstream issue https://github.com/libgit2/libgit2/issues/5342 - email = email.delete("\n<>") - name = name.delete("\n<>") - time = timestamp ? Time.at(timestamp.seconds, timestamp.nanos, :nsec, in: "+00:00") : Time.now - - { - email: email, - name: name, - time: time - } - end - def tag_name(ref) ref = ref.to_s self.ref_name(ref) if self.tag_ref?(ref) @@ -103,22 +83,6 @@ module Gitlab def version Gitlab::Git::Version.git_version end - - def diff_line_code(file_path, new_line_position, old_line_position) - "#{Digest::SHA1.hexdigest(file_path)}_#{old_line_position}_#{new_line_position}" - end - - def shas_eql?(sha1, sha2) - return false if sha1.nil? || sha2.nil? - return false unless sha1.class == sha2.class - - # If either of the shas is below the minimum length, we cannot be sure - # that they actually refer to the same commit because of hash collision. - length = [sha1.length, sha2.length].min - return false if length < Gitlab::Git::Commit::MIN_SHA_LENGTH - - sha1[0, length] == sha2[0, length] - end end end end diff --git a/ruby/lib/gitlab/git/operation_service.rb b/ruby/lib/gitlab/git/operation_service.rb index b10ffeb44..0ab311694 100644 --- a/ruby/lib/gitlab/git/operation_service.rb +++ b/ruby/lib/gitlab/git/operation_service.rb @@ -56,38 +56,6 @@ module Gitlab end end - # Yields the given block (which should return a commit) and - # writes it to the ref while also executing hooks for it. - # The ref is _always_ overwritten (nothing is taken from its - # previous state). - # - # Returns the generated commit. - # - # ref - The target ref path we're committing to. - # from_ref - The ref we're taking the HEAD commit from. - def commit_ref(ref, source_sha, from_ref:) - update_autocrlf_option - - target_sha = from_ref.target - repository.write_ref(ref, target_sha) - - # Make commit - newrev = yield - - unless newrev - error = "Failed to create merge commit for source_sha #{source_sha} and" \ - " target_sha #{target_sha} at #{ref}" - - raise Gitlab::Git::CommitError.new(error) - end - - oldrev = from_ref.target - - update_ref(ref, newrev, oldrev) - - newrev - end - private # Returns [newrev, should_run_after_create, should_run_after_create_branch] diff --git a/ruby/lib/gitlab/git/path_helper.rb b/ruby/lib/gitlab/git/path_helper.rb deleted file mode 100644 index 8624fb408..000000000 --- a/ruby/lib/gitlab/git/path_helper.rb +++ /dev/null @@ -1,28 +0,0 @@ -# Gitaly note: JV: no RPC's here. - -module Gitlab - module Git - class PathHelper - InvalidPath = Class.new(StandardError) - - class << self - def normalize_path!(filename) - return unless filename - - # Strip all leading slashes so that //foo -> foo - filename = filename.sub(%r{\A/*}, '') - - # Expand relative paths (e.g. foo/../bar) - filename = Pathname.new(filename) - filename.relative_path_from(Pathname.new('')) - - filename.each_filename do |segment| - raise InvalidPath, 'Path cannot include directory traversal' if segment == '..' - end - - filename.to_s - end - end - end - end -end diff --git a/ruby/lib/gitlab/git/popen.rb b/ruby/lib/gitlab/git/popen.rb index 6d4ef34d9..5d8218eb1 100644 --- a/ruby/lib/gitlab/git/popen.rb +++ b/ruby/lib/gitlab/git/popen.rb @@ -46,73 +46,6 @@ module Gitlab [cmd_output, cmd_status] end - - def popen_with_timeout(cmd, timeout, path, vars = {}) - raise "System commands must be given as an array of strings" unless cmd.is_a?(Array) - - path ||= Dir.pwd - vars['PWD'] = path - - FileUtils.mkdir_p(path) unless File.directory?(path) - - rout, wout = IO.pipe - rerr, werr = IO.pipe - - pid = Process.spawn(vars, *cmd, out: wout, err: werr, chdir: path, pgroup: true) - # stderr and stdout pipes can block if stderr/stdout aren't drained: https://bugs.ruby-lang.org/issues/9082 - # Mimic what Ruby does with capture3: https://github.com/ruby/ruby/blob/1ec544695fa02d714180ef9c34e755027b6a2103/lib/open3.rb#L257-L273 - out_reader = Thread.new { rout.read } - err_reader = Thread.new { rerr.read } - - begin - # close write ends so we could read them - wout.close - werr.close - - status = process_wait_with_timeout(pid, timeout) - - cmd_output = out_reader.value - cmd_output << err_reader.value # Copying the behaviour of `popen` which merges stderr into output - - [cmd_output, status.exitstatus] - rescue Timeout::Error => e - kill_process_group_for_pid(pid) - - raise e - ensure - wout.close unless wout.closed? - werr.close unless werr.closed? - - # rout is shared with out_reader. To prevent an exception in that - # thread, kill the thread before closing rout. The same goes for rerr - # below. - out_reader.kill - rout.close - - err_reader.kill - rerr.close - end - end - - def process_wait_with_timeout(pid, timeout) - deadline = timeout.seconds.from_now - wait_time = 0.01 - - while deadline > Time.now - sleep(wait_time) - _, status = Process.wait2(pid, Process::WNOHANG) - - return status unless status.nil? - end - - raise Timeout::Error, "Timeout waiting for process ##{pid}" - end - - def kill_process_group_for_pid(pid) - Process.kill("KILL", -pid) - Process.wait(pid) - rescue Errno::ESRCH - end end end end diff --git a/ruby/lib/gitlab/git/ssh_auth.rb b/ruby/lib/gitlab/git/ssh_auth.rb deleted file mode 100644 index f6f21959e..000000000 --- a/ruby/lib/gitlab/git/ssh_auth.rb +++ /dev/null @@ -1,95 +0,0 @@ -require 'shellwords' - -module Gitlab - module Git - # SshAuth writes custom identity and known_hosts files to temporary files - # and builds a `GIT_SSH_COMMAND` environment variable to allow git - # operations over SSH to take advantage of them. - # - # To use: - # SshAuth.from_gitaly(request).setup do |env| - # # Run commands here with the provided environment - # end - class SshAuth - class Option - def initialize(key, value) - if key.include?('=') || needs_escape?(key) - raise ArgumentError, "invalid SSH config key: #{key.inspect}" - end - - if needs_escape?(value) - raise ArgumentError, "invalid SSH config value: #{value.inspect}" - end - - @key = key - @value = value - end - - def to_s - "-o#{@key}=#{@value}" - end - - private - - def needs_escape?(str) - Shellwords.shellescape(str) != str - end - end - - attr_reader :ssh_key, :known_hosts - - def self.from_gitaly(request) - new(request.ssh_key, request.known_hosts) - end - - def initialize(ssh_key, known_hosts) - @ssh_key = ssh_key - @known_hosts = known_hosts - end - - def setup - options = [] - - if ssh_key.present? - key_file = write_tempfile('gitlab-shell-key-file', 0o400, ssh_key) - - options << Option.new('IdentityFile', key_file.path) - options << Option.new('IdentitiesOnly', 'yes') - end - - if known_hosts.present? - known_hosts_file = write_tempfile('gitlab-shell-known-hosts', 0o400, known_hosts) - - options << Option.new('StrictHostKeyChecking', 'yes') - options << Option.new('CheckHostIP', 'no') - options << Option.new('UserKnownHostsFile', known_hosts_file.path) - end - - yield custom_environment(options) - ensure - key_file&.close! - known_hosts_file&.close! - end - - private - - def write_tempfile(name, mode, data) - Tempfile.open(name) do |tempfile| - tempfile.chmod(mode) - tempfile.write(data) - - # Return the tempfile instance so it can be unlinked - tempfile - end - end - - # Constructs an environment that will make SSH, as invoked by git, respect - # the given options. To achieve this, we use the GIT_SSH_COMMAND envvar. - def custom_environment(options) - return {} unless options.present? - - { 'GIT_SSH_COMMAND' => %(ssh #{options.join(' ')}) } - end - end - end -end diff --git a/ruby/lib/praefect/transaction.rb b/ruby/lib/praefect/transaction.rb deleted file mode 100644 index 3231f9212..000000000 --- a/ruby/lib/praefect/transaction.rb +++ /dev/null @@ -1,27 +0,0 @@ -module Praefect - class Transaction - TRANSACTION_METADATA_KEY = "gitaly-reference-transaction".freeze - TRANSACTION_PAYLOAD_KEY = "transaction".freeze - - MissingPraefectMetadataError = Class.new(StandardError) - - def self.from_metadata(metadata) - transaction_metadata = metadata[TRANSACTION_METADATA_KEY] - return new(nil) unless transaction_metadata - - transaction = JSON.parse(Base64.decode64(transaction_metadata)) - - new(transaction) - end - - def initialize(transaction) - @transaction = transaction - end - - def payload - { - TRANSACTION_PAYLOAD_KEY => @transaction - }.reject { |_, v| v.nil? } - end - end -end diff --git a/ruby/spec/lib/gitlab/git/popen_spec.rb b/ruby/spec/lib/gitlab/git/popen_spec.rb index 22739e9c5..6272b7c17 100644 --- a/ruby/spec/lib/gitlab/git/popen_spec.rb +++ b/ruby/spec/lib/gitlab/git/popen_spec.rb @@ -105,77 +105,4 @@ describe 'Gitlab::Git::Popen' do end end end - - context 'popen_with_timeout' do - let(:timeout) { 1.second } - - context 'no timeout' do - context 'zero status' do - let(:result) { klass.new.popen_with_timeout(%w(ls), timeout, path) } - let(:status) { result.last } - - it { expect(status).to be_zero } - end - - context 'non-zero status' do - let(:result) { klass.new.popen_with_timeout(%w(cat NOTHING), timeout, path) } - let(:output) { result.first } - let(:status) { result.last } - - it { expect(status).to eq(1) } - it { expect(output).to include('No such file or directory') } - end - - context 'unsafe string command' do - it 'raises an error when it gets called with a string argument' do - expect { klass.new.popen_with_timeout('ls', timeout, path) }.to raise_error(RuntimeError) - end - end - end - - context 'timeout' do - context 'timeout' do - it "raises a Timeout::Error" do - expect { klass.new.popen_with_timeout(%w(sleep 1000), timeout, path) }.to raise_error(Timeout::Error) - end - - it "handles processes that do not shutdown correctly" do - expect { klass.new.popen_with_timeout(['bash', '-c', "trap -- '' SIGTERM; sleep 1000"], timeout, path) }.to raise_error(Timeout::Error) - end - end - - context 'timeout period' do - let(:time_taken) do - begin - start = Time.now - klass.new.popen_with_timeout(%w(sleep 1000), timeout, path) - rescue - Time.now - start - end - end - - it { expect(time_taken).to be >= timeout } - end - - context 'clean up' do - let(:instance) { klass.new } - - it 'kills the child process' do - expect(instance).to receive(:kill_process_group_for_pid).and_wrap_original do |m, *args| - # is the PID, and it should not be running at this point - m.call(*args) - - pid = args.first - begin - Process.getpgid(pid) - raise "The child process should have been killed" - rescue Errno::ESRCH - end - end - - expect { instance.popen_with_timeout(['bash', '-c', "trap -- '' SIGTERM; sleep 1000"], timeout, path) }.to raise_error(Timeout::Error) - end - end - end - end end diff --git a/ruby/spec/lib/gitlab/git/ssh_auth_spec.rb b/ruby/spec/lib/gitlab/git/ssh_auth_spec.rb deleted file mode 100644 index d7c3c05d9..000000000 --- a/ruby/spec/lib/gitlab/git/ssh_auth_spec.rb +++ /dev/null @@ -1,112 +0,0 @@ -require 'spec_helper' - -describe Gitlab::Git::SshAuth do - describe Gitlab::Git::SshAuth::Option do - it 'invalid keys' do - ['foo=bar', 'foo bar', "foo\nbar", %(foo'bar)].each do |key| - expect { described_class.new(key, 'zzz') }.to raise_error(ArgumentError) - end - end - - it 'invalid values' do - ['foo bar', "foo\nbar", %(foo'bar)].each do |value| - expect { described_class.new('zzz', value) }.to raise_error(ArgumentError) - end - end - end - - describe '.from_gitaly' do - it 'initializes based on ssh_key and known_hosts in the request' do - result = described_class.from_gitaly(double(ssh_key: 'SSH KEY', known_hosts: 'KNOWN HOSTS')) - - expect(result.class).to eq(described_class) - expect(result.ssh_key).to eq('SSH KEY') - expect(result.known_hosts).to eq('KNOWN HOSTS') - end - end - - describe '#setup' do - subject { described_class.new(ssh_key, known_hosts).setup { |env| env } } - - context 'no credentials' do - let(:ssh_key) { nil } - let(:known_hosts) { nil } - - it 'writes no tempfiles' do - expect(Tempfile).not_to receive(:new) - - is_expected.to eq({}) - end - end - - context 'just the SSH key' do - let(:ssh_key) { 'Fake SSH key' } - let(:known_hosts) { nil } - - it 'writes the SSH key file' do - ssh_key_file = stub_tempfile('/tmpfiles/keyFile', 'gitlab-shell-key-file', chmod: 0o400) - - is_expected.to eq(build_env(ssh_key_file: ssh_key_file.path)) - - expect(ssh_key_file.string).to eq(ssh_key) - end - end - - context 'just the known_hosts file' do - let(:ssh_key) { nil } - let(:known_hosts) { 'Fake known_hosts data' } - - it 'writes the known_hosts file and script' do - known_hosts_file = stub_tempfile('/tmpfiles/knownHosts', 'gitlab-shell-known-hosts', chmod: 0o400) - - is_expected.to eq(build_env(known_hosts_file: known_hosts_file.path)) - - expect(known_hosts_file.string).to eq(known_hosts) - end - end - - context 'SSH key and known_hosts file' do - let(:ssh_key) { 'Fake SSH key' } - let(:known_hosts) { 'Fake known_hosts data' } - - it 'writes SSH key, known_hosts and script files' do - ssh_key_file = stub_tempfile('id_rsa', 'gitlab-shell-key-file', chmod: 0o400) - known_hosts_file = stub_tempfile('known_hosts', 'gitlab-shell-known-hosts', chmod: 0o400) - - is_expected.to eq(build_env(ssh_key_file: ssh_key_file.path, known_hosts_file: known_hosts_file.path)) - - expect(ssh_key_file.string).to eq(ssh_key) - expect(known_hosts_file.string).to eq(known_hosts) - end - end - end - - def build_env(ssh_key_file: nil, known_hosts_file: nil) - opts = [] - - if ssh_key_file - opts << "-oIdentityFile=#{ssh_key_file}" - opts << '-oIdentitiesOnly=yes' - end - - if known_hosts_file - opts << '-oStrictHostKeyChecking=yes' - opts << '-oCheckHostIP=no' - opts << "-oUserKnownHostsFile=#{known_hosts_file}" - end - - { 'GIT_SSH_COMMAND' => %(ssh #{opts.join(' ')}) } - end - - def stub_tempfile(name, filename, chmod:) - file = StringIO.new - - allow(file).to receive(:path).and_return(name) - - expect(Tempfile).to receive(:new).with(filename).and_return(file) - expect(file).to receive(:chmod).with(chmod) - expect(file).to receive(:close!) - - file - end -end diff --git a/ruby/spec/lib/gitlab/git_spec.rb b/ruby/spec/lib/gitlab/git_spec.rb deleted file mode 100644 index 2128225a5..000000000 --- a/ruby/spec/lib/gitlab/git_spec.rb +++ /dev/null @@ -1,41 +0,0 @@ -require 'spec_helper' - -describe Gitlab::Git do - let(:committer_email) { 'user@example.org' } - let(:committer_name) { 'John Doe' } - - describe 'committer_hash' do - it "returns a hash containing the given email and name" do - committer_hash = described_class.committer_hash(email: committer_email, name: committer_name) - - expect(committer_hash[:email]).to eq(committer_email) - expect(committer_hash[:name]).to eq(committer_name) - expect(committer_hash[:time]).to be_a(Time) - end - - context 'when email is nil' do - it "returns nil" do - committer_hash = described_class.committer_hash(email: nil, name: committer_name) - - expect(committer_hash).to be_nil - end - end - - context 'when name is nil' do - it "returns nil" do - committer_hash = described_class.committer_hash(email: committer_email, name: nil) - - expect(committer_hash).to be_nil - end - end - - context 'when email or name contains angle brackets or newlines' do - it 'strips invalid characters' do - committer_hash = described_class.committer_hash(email: "<foo>\n", name: "f<o>\no") - - expect(committer_hash[:email]).to eq('foo') - expect(committer_hash[:name]).to eq('foo') - end - end - end -end |