Class: Aspera::Cli::Http

Inherits:
Object
  • Object
show all
Extended by:
Option::Declarator
Defined in:
lib/aspera/cli/http.rb

Overview

Encapsulates all HTTP/S and TLS runtime configuration options. Extracted from Plugins::Config so it can be referenced independently via Context#http_config without coupling to the plugin machinery.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Option::Declarator

declare_options, option, option_specs, register_option_spec

Constructor Details

#initialize ⇒ Http

Returns a new instance of Http.



60
61
62
63
64
65
66
67
68
# File 'lib/aspera/cli/http.rb', line 60

def initialize
  @insecure              = false
  @warn_insecure         = true
  @ignore_cert_host_port = []
  @http_options          = {}
  @ssl_warned_urls       = []
  @certificate_store     = nil
  @certificate_paths     = nil
end

Instance Attribute Details

#http_options ⇒ Object

Returns the value of attribute http_options.



71
72
73
# File 'lib/aspera/cli/http.rb', line 71

def http_options
  @http_options
end

#ignore_cert_host_port ⇒ Object

Returns the value of attribute ignore_cert_host_port.



71
72
73
# File 'lib/aspera/cli/http.rb', line 71

def ignore_cert_host_port
  @ignore_cert_host_port
end

#insecure ⇒ Object

Returns the value of attribute insecure.



70
71
72
# File 'lib/aspera/cli/http.rb', line 70

def insecure
  @insecure
end

#warn_insecure ⇒ Object

Returns the value of attribute warn_insecure.



70
71
72
# File 'lib/aspera/cli/http.rb', line 70

def warn_insecure
  @warn_insecure
end

Class Method Details

.duration_seconds(value) ⇒ Numeric

Returns Number of seconds (integer, unless the string has milliseconds).

Parameters:

  • value (Numeric, String) —

    Duration: number of seconds, or string, e.g. 12h, 1d, 1h 30m, 0.5

Returns:

  • (Numeric) —

    Number of seconds (integer, unless the string has milliseconds)



43
44
45
46
47
# File 'lib/aspera/cli/http.rb', line 43

def duration_seconds(value)
  return value if value.is_a?(Numeric)
  milliseconds = Units::Duration.new(value).to_unit('ms')
  (milliseconds % 1000).zero? ? milliseconds / 1000 : milliseconds / 1000.0
end

Instance Method Details

#http_proxy=(value) ⇒ Object


Proxy



115
116
117
118
# File 'lib/aspera/cli/http.rb', line 115

def http_proxy=(value)
  URI.parse(value)
  ENV['http_proxy'] = value
end

#ignore_cert?(address, port) ⇒ Boolean

Should the certificate be ignored for this host/port? Also logs a warning the first time (if warn_insecure is set).

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/aspera/cli/http.rb', line 136

def ignore_cert?(address, port)
  endpoint    = [address, port].freeze
  ignore_cert = @insecure || @ignore_cert_host_port.any?(endpoint)
  if ignore_cert && @warn_insecure
    base_url = "https://#{address}:#{port}"
    unless @ssl_warned_urls.include?(base_url)
      Log.log.warn { "Ignoring certificate for: #{base_url}. Do not deactivate certificate verification in production." }
      @ssl_warned_urls.push(base_url)
    end
  end
  Log.log.debug { "ignore cert? #{endpoint} -> #{ignore_cert}" }
  ignore_cert
end

#trusted_cert_locations ⇒ Object

Return cert file paths (computes OS defaults lazily if never set).



198
199
200
201
202
203
204
205
206
207
# File 'lib/aspera/cli/http.rb', line 198

def trusted_cert_locations
  if @certificate_paths.nil?
    self.trusted_cert_locations = [SpecialValues::DEF]
    locations = @certificate_paths
    # Restore to "lazy" state so next call recomputes if store was reset
    @certificate_paths = @certificate_store = nil
    return locations
  end
  @certificate_paths
end

#trusted_cert_locations=(path_list) ⇒ nil

Add files, folders or the default OS locations to the cert store. Replace the certificate store with the given locations

Parameters:

  • path_list (Array<String>) —

    list of file/folder paths to add to the certificate store

  • path_list (Array<String>) —

    files or folders, or DEF for system default; empty: system default

Returns:

  • (nil)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/aspera/cli/http.rb', line 159

def trusted_cert_locations=(path_list)
  Aspera.assert_type(path_list, Array) { 'cert locations' }
  # Empty: lazily initialized with system default
  @certificate_store = @certificate_paths = nil
  return if path_list.empty?
  Log.log.debug('Creating SSL Cert store')
  @certificate_store = OpenSSL::X509::Store.new
  @certificate_paths = []
  path_list.each do |path|
    paths_to_add = [path]
    Log.log.debug { "Adding cert location: #{path}" }
    if path.eql?(SpecialValues::DEF)
      @certificate_store.set_default_paths
      paths_to_add = [OpenSSL::X509::DEFAULT_CERT_DIR]
      paths_to_add.push(ENV.fetch(OpenSSL::X509::DEFAULT_CERT_FILE_ENV, OpenSSL::X509::DEFAULT_CERT_FILE)) unless defined?(JRUBY_VERSION)
      paths_to_add.select! { |f| File.exist?(f) }
    elsif File.file?(path)
      @certificate_store.add_file(path)
    elsif File.directory?(path)
      @certificate_store.add_path(path)
    else
      raise "No such file or folder: #{path}"
    end
    paths_to_add.each do |p|
      pp = [File.realpath(p)]
      if File.directory?(p)
        pp = Dir.children(p)
          .map { |e| File.realpath(File.join(p, e)) }
          .select { |entry| File.file?(entry) }
          .select { |entry| CERT_EXT.any? { |ext| entry.end_with?(ext) } }
      end
      @certificate_paths.concat(pp)
    end
  end
  @certificate_paths.uniq!
  nil
end

#update_session(http_session) ⇒ nil

Parameters:

  • http_session (Net::HTTP) —

    HTTP session to configure

Returns:

  • (nil)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/aspera/cli/http.rb', line 216

def update_session(http_session)
  http_session.set_debug_output(LineLogger.new(:trace2)) if Log.instance.logger.trace2?
  http_session.verify_mode = SELF_SIGNED_CERT if http_session.use_ssl? && ignore_cert?(http_session.address, http_session.port)
  http_session.cert_store = @certificate_store if @certificate_store
  Log.log.debug { "Using cert store #{http_session.cert_store} (#{@certificate_store})" } unless http_session.cert_store.nil?
  @http_options.each do |k, v|
    method = "#{k}=".to_sym
    if http_session.respond_to?(method)
      http_session.send(method, v)
    else
      Log.log.error { "Unknown HTTP session attribute: #{k}" }
    end
  end
  nil
end