File: ssh_connect.rb

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (144 lines) | stat: -rw-r--r-- 4,916 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
module VagrantPlugins
  module Kernel_V2
    class SSHConnectConfig < Vagrant.plugin("2", :config)
      DEFAULT_SSH_CONNECT_TIMEOUT = 15

      attr_accessor :host
      attr_accessor :port
      attr_accessor :config
      attr_accessor :connect_timeout
      attr_accessor :private_key_path
      attr_accessor :username
      attr_accessor :password
      attr_accessor :insert_key
      attr_accessor :keys_only
      attr_accessor :paranoid
      attr_accessor :verify_host_key
      attr_accessor :compression
      attr_accessor :dsa_authentication
      attr_accessor :extra_args
      attr_accessor :remote_user
      attr_accessor :disable_deprecated_algorithms

      def initialize
        @host             = UNSET_VALUE
        @port             = UNSET_VALUE
        @config           = UNSET_VALUE
        @connect_timeout  = UNSET_VALUE
        @private_key_path = UNSET_VALUE
        @username         = UNSET_VALUE
        @password         = UNSET_VALUE
        @insert_key       = UNSET_VALUE
        @keys_only        = UNSET_VALUE
        @paranoid         = UNSET_VALUE
        @verify_host_key  = UNSET_VALUE
        @compression      = UNSET_VALUE
        @dsa_authentication = UNSET_VALUE
        @extra_args       = UNSET_VALUE
        @remote_user      = UNSET_VALUE
        @disable_deprecated_algorithms = UNSET_VALUE
      end

      def finalize!
        @host             = nil if @host == UNSET_VALUE
        @port             = nil if @port == UNSET_VALUE
        @private_key_path = nil if @private_key_path == UNSET_VALUE
        @username         = nil if @username == UNSET_VALUE
        @password         = nil if @password == UNSET_VALUE
        @insert_key       = true if @insert_key == UNSET_VALUE
        @keys_only        = true if @keys_only == UNSET_VALUE
        @paranoid         = false if @paranoid == UNSET_VALUE
        @verify_host_key  = :never if @verify_host_key == UNSET_VALUE
        @compression      = true if @compression == UNSET_VALUE
        @dsa_authentication = true if @dsa_authentication == UNSET_VALUE
        @extra_args       = nil if @extra_args == UNSET_VALUE
        @config           = nil if @config == UNSET_VALUE
        @disable_deprecated_algorithms = false if @disable_deprecated_algorithms == UNSET_VALUE
        @connect_timeout  = DEFAULT_SSH_CONNECT_TIMEOUT if @connect_timeout == UNSET_VALUE

        if @private_key_path && !@private_key_path.is_a?(Array)
          @private_key_path = [@private_key_path]
        end

        if @remote_user == UNSET_VALUE
          if @username
            @remote_user = @username
          else
            @remote_user = nil
          end
        end

        if @paranoid
          @verify_host_key = @paranoid
        end

        # Values for verify_host_key changed in 5.0.0 of net-ssh. If old value
        # detected, update with new value
        case @verify_host_key
        when true
          @verify_host_key = :accepts_new_or_local_tunnel
        when false
          @verify_host_key = :never
        when :very
          @verify_host_key = :accept_new
        when :secure
          @verify_host_key = :always
        end

        # Attempt to convert timeout to integer value
        # If we can't convert the connect timeout into an integer or
        # if the value is less than 1, set it to the default value
        begin
          @connect_timeout = @connect_timeout.to_i
        rescue
          # ignore
        end
      end

      # NOTE: This is _not_ a valid config validation method, since it
      # returns an _array_ of strings rather than a Hash. This is meant to
      # be used with a subclass that handles this.
      #
      # @return [Array<String>]
      def validate(machine)
        errors = _detected_errors

        if @private_key_path
          @private_key_path.each do |raw_path|
            path = File.expand_path(raw_path, machine.env.root_path)
            if !File.file?(path)
              errors << I18n.t(
                "vagrant.config.ssh.private_key_missing",
                path: raw_path)
            end
          end
        end

        if @config
          config_path = File.expand_path(@config, machine.env.root_path)
          if !File.file?(config_path)
            errors << I18n.t(
              "vagrant.config.ssh.ssh_config_missing",
              path: @config)
          end
        end

        if @paranoid
          machine.env.ui.warn(I18n.t("vagrant.config.ssh.paranoid_deprecated"))
        end

        if !@connect_timeout.is_a?(Integer)
          errors << I18n.t(
            "vagrant.config.ssh.connect_timeout_invalid_type",
            given: @connect_timeout.class.name)
        elsif @connect_timeout < 1
          errors << I18n.t(
            "vagrant.config.ssh.connect_timeout_invalid_value",
            given: @connect_timeout.to_s)
        end

        errors
      end
    end
  end
end