Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ssh.rb « support « qa « qa - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b2fbddce87d1c281587a6cd035f3c5d630fda902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true

require 'tempfile'
require 'etc'

module QA
  module Support
    class SSH
      include Scenario::Actable
      include Support::Run

      attr_accessor :known_hosts_file, :private_key_file, :key
      attr_reader :uri

      def initialize
        @private_key_file = Tempfile.new("id_#{SecureRandom.hex(8)}")
        @known_hosts_file = Tempfile.new("known_hosts_#{SecureRandom.hex(8)}")
      end

      def uri=(address)
        @uri = URI(address)
      end

      def setup(env: nil)
        File.binwrite(private_key_file, key.private_key)
        File.chmod(0700, private_key_file)

        keyscan_params = ['-T 60 -H']
        keyscan_params << "-p #{uri_port}" if uri_port
        keyscan_params << uri.host

        res = run("ssh-keyscan #{keyscan_params.join(' ')} >> #{known_hosts_file.path}", env: env, log_prefix: 'SSH: ')
        return res.response unless res.success?

        true
      end

      def delete
        private_key_file.close(true)
        known_hosts_file.close(true)
      end

      def reset_2fa_codes
        ssh_params = [uri.host]
        ssh_params << "-p #{uri_port}" if uri_port
        ssh_params << "2fa_recovery_codes"

        run("echo yes | ssh -i #{private_key_file.path} -o UserKnownHostsFile=#{known_hosts_file.path} #{git_user}@#{ssh_params.join(' ')}", log_prefix: 'SSH: ').to_s
      end

      private

      def uri_port
        use_typical_params? ? nil : uri.port
      end

      def git_user
        QA::Runtime::Env.running_in_ci? || use_typical_params? ? 'git' : Etc.getlogin
      end

      # Checks if typical parameters should be used. That means the SSH port will not be
      # needed because it's port 22, and the git user is named 'git'. We assume that
      # typical parameters should be used if the host URI includes a typical HTTP(S)
      # port (80 or 443)
      #
      # @return [Boolean] whether typical SSH port and git user parameters should be used
      def use_typical_params?
        [443, 80].include?(uri.port)
      end
    end
  end
end

QA::Support::SSH.prepend_mod_with("Support::SSH", namespace: QA)