Method: Train.unpack_target_from_uri

Defined in:
lib/train.rb

.unpack_target_from_uri(uri_string, opts = {}) ⇒ Object

Given a string that looks like a URI, unpack connection credentials. The name of the desired transport is always taken from the ‘scheme’ slot of the URI; the remaining portion of the URI is parsed as if it were an HTTP URL, and then the URL components are stored in the credentials hash. It is up to the transport to interpret the fields in a sensible way for that transport. New transport authors are encouraged to use transport://credset format (see inspec/inspec/issues/3661) rather than inventing a new field mapping.



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
# File 'lib/train.rb', line 89

def self.unpack_target_from_uri(uri_string, opts = {}) # rubocop: disable Metrics/AbcSize
  creds = {}
  return creds if uri_string.empty?

  # split up the target's host/scheme configuration
  uri = parse_uri(uri_string)
  unless uri.host.nil? and uri.scheme.nil?
    creds[:backend]  ||= uri.scheme
    creds[:host]     ||= uri.hostname
    creds[:port]     ||= uri.port
    creds[:user]     ||= uri.user
    creds[:path]     ||= uri.path
    creds[:password] ||=
      if opts[:www_form_encoded_password] && !uri.password.nil?
        URI.decode_www_form_component(uri.password)
      else
        uri.password
      end
  end

  # ensure path is nil, if its empty; e.g. required to reset defaults for winrm # TODO: move logic into winrm plugin
  creds[:path] = nil if !creds[:path].nil? && creds[:path].to_s.empty?

  # compact! is available in ruby 2.4+
  # TODO: rewrite next line using compact! once we drop support for ruby 2.3
  creds = creds.delete_if { |_, value| value.nil? }

  # return the updated config
  creds
end