train-core 1.7.1 → 1.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/train.rb +61 -46
  3. data/lib/train/version.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 971b7658559761fe6c861b6e67e7666eb30ac38beb2c599e914c4a3d9ef881f5
4
- data.tar.gz: f2afc7e3be801fc8f0a0e1eadd786a9e58403a2f8fb07330b5537c71963d06b3
3
+ metadata.gz: 2aa1b81d26ddba4a2a0f074d650fc9a9289afa7c2399b5c8f6a15e8c928cc431
4
+ data.tar.gz: de2f1c194c79a4bab63a26610c6bc8472d1275e00ff53155bf070925187f48f4
5
5
  SHA512:
6
- metadata.gz: 83e66837187261cdbcf49ead7fbeca0b1e923c1751f1d1fb53478baff494923099710785000c8714fafb9f77942f083fa2f33eae20d5f9da87f2e22bac617222
7
- data.tar.gz: d95ab6664a99c59f3e23b001e4fd9d16dbd46c6c72fc63116d75bf4ae677061282e9bc1f6b715c25be5f8e61307f554c1a0068db617c45c2876b750b0769bf0c
6
+ metadata.gz: 04a84767760b02d28af78b7b1f8c6318e4f59a23832a4486d6d236cdba47955b5cca72e3bd104279fc4a6405cd982db0420c2b6c4d7ce84b26e16b1196a27a4d
7
+ data.tar.gz: e2f9f46e1015fc25de047eadd5c1f5d694fd93659c28d5e4e8b274128b1b6de3cb50c043fa8f63373ecfc7081d65f45032af088d77c18b212aae609d27d52549
@@ -61,53 +61,61 @@ module Train
61
61
  raise ex
62
62
  end
63
63
 
64
- # Resolve target configuration in URI-scheme into
65
- # all respective fields and merge with existing configuration.
66
- # e.g. ssh://bob@remote => backend: ssh, user: bob, host: remote
67
- def self.target_config(config = nil) # rubocop:disable Metrics/AbcSize
68
- conf = config.nil? ? {} : config.dup
69
- conf = symbolize_keys(conf)
70
-
71
- group_keys_and_keyfiles(conf)
64
+ # Legacy code to unpack a series of items from an incoming Hash
65
+ # Inspec::Config.unpack_train_credentials now handles this in most cases that InSpec needs
66
+ # If you need to unpack a URI, use unpack_target_from_uri
67
+ # TODO: deprecate; can't issue a warning because train doesn't have a logger until the connection is setup (See base_connection.rb)
68
+ def self.target_config(config = nil)
69
+ conf = config.dup
70
+ # Symbolize keys
71
+ conf.keys.each do |key|
72
+ unless key.is_a? Symbol
73
+ conf[key.to_sym] = conf.delete(key)
74
+ end
75
+ end
72
76
 
77
+ group_keys_and_keyfiles(conf) # TODO: move logic into SSH plugin
73
78
  return conf if conf[:target].to_s.empty?
79
+ unpack_target_from_uri(conf[:target], conf).merge(conf)
80
+ end
81
+
82
+ # Given a string that looks like a URI, unpack connection credentials.
83
+ # The name of the desired transport is always taken from the 'scheme' slot of the URI;
84
+ # the remaining portion of the URI is parsed as if it were an HTTP URL, and then
85
+ # the URL components are stored in the credentials hash. It is up to the transport
86
+ # to interpret the fields in a sensible way for that transport.
87
+ # New transport authors are encouraged to use transport://credset format (see
88
+ # inspec/inspec/issues/3661) rather than inventing a new field mapping.
89
+ def self.unpack_target_from_uri(uri_string, opts = {}) # rubocop: disable Metrics/AbcSize
90
+ creds = {}
91
+ return creds if uri_string.empty?
74
92
 
75
93
  # split up the target's host/scheme configuration
76
- uri = parse_uri(conf[:target].to_s)
94
+ uri = parse_uri(uri_string)
77
95
  unless uri.host.nil? and uri.scheme.nil?
78
- conf[:backend] ||= uri.scheme
79
- conf[:host] ||= uri.hostname
80
- conf[:port] ||= uri.port
81
- conf[:user] ||= uri.user
82
- conf[:path] ||= uri.path
83
- conf[:password] ||=
84
- if conf[:www_form_encoded_password] && !uri.password.nil?
96
+ creds[:backend] ||= uri.scheme
97
+ creds[:host] ||= uri.hostname
98
+ creds[:port] ||= uri.port
99
+ creds[:user] ||= uri.user
100
+ creds[:path] ||= uri.path
101
+ creds[:password] ||=
102
+ if opts[:www_form_encoded_password] && !uri.password.nil?
85
103
  URI.decode_www_form_component(uri.password)
86
104
  else
87
105
  uri.password
88
106
  end
89
107
  end
90
108
 
91
- # ensure path is nil, if its empty; e.g. required to reset defaults for winrm
92
- conf[:path] = nil if !conf[:path].nil? && conf[:path].to_s.empty?
109
+ # ensure path is nil, if its empty; e.g. required to reset defaults for winrm # TODO: move logic into winrm plugin
110
+ creds[:path] = nil if !creds[:path].nil? && creds[:path].to_s.empty?
93
111
 
94
- # return the updated config
95
- conf
96
- end
112
+ # compact! is available in ruby 2.4+
113
+ # TODO: rewrite next line using compact! once we drop support for ruby 2.3
114
+ creds = creds.delete_if { |_, value| value.nil? }
97
115
 
98
- # Takes a map of key-value pairs and turns all keys into symbols. For this
99
- # to work, only keys are supported that can be turned into symbols.
100
- # Example: { 'a' => 123 } ==> { a: 123 }
101
- #
102
- # @param map [Hash]
103
- # @return [Hash] new map with all keys being symbols
104
- def self.symbolize_keys(map)
105
- map.each_with_object({}) do |(k, v), acc|
106
- acc[k.to_sym] = v
107
- acc
108
- end
116
+ # return the updated config
117
+ creds
109
118
  end
110
- private_class_method :symbolize_keys
111
119
 
112
120
  # Parse a URI. Supports empty URI's with paths, e.g. `mock://`
113
121
  #
@@ -127,34 +135,41 @@ module Train
127
135
  raise Train::UserError, e
128
136
  end
129
137
 
130
- u = URI.parse(string)
131
- u.host = nil
132
- u
138
+ uri = URI.parse(string)
139
+ uri.host = nil
140
+ uri
133
141
  end
134
142
  private_class_method :parse_uri
135
143
 
136
- def self.validate_backend(conf, default = :local)
137
- return default if conf.nil?
138
- res = conf[:backend]
144
+ # Examine the given credential information, and if all is well,
145
+ # return the transport name.
146
+ # TODO: this actually does no validation of the credential options whatsoever
147
+ def self.validate_backend(credentials, default_transport_name = 'local')
148
+ return default_transport_name if credentials.nil?
149
+ transport_name = credentials[:backend]
139
150
 
140
- if (res.nil? || res == 'localhost') && conf[:sudo]
151
+ # TODO: Determine if it is ever possible (or supported) for transport_name to be 'localhost'
152
+ # TODO: After inspec/inspec/pull/3750 is merged, should be able to remove nil from the list
153
+ if credentials[:sudo] && [nil, 'local', 'localhost'].include?(transport_name)
141
154
  fail Train::UserError, 'Sudo is only valid when running against a remote host. '\
142
155
  'To run this locally with elevated privileges, run the command with `sudo ...`.'
143
156
  end
144
157
 
145
- return res if !res.nil?
158
+ return transport_name if !transport_name.nil?
146
159
 
147
- if !conf[:target].nil?
160
+ if !credentials[:target].nil?
161
+ # We should not get here, because if target_uri unpacking was successful,
162
+ # it would have set credentials[:backend]
148
163
  fail Train::UserError, 'Cannot determine backend from target '\
149
- "configuration #{conf[:target].inspect}. Valid example: ssh://192.168.0.1."
164
+ "configuration #{credentials[:target]}. Valid example: ssh://192.168.0.1"
150
165
  end
151
166
 
152
- if !conf[:host].nil?
167
+ if !credentials[:host].nil?
153
168
  fail Train::UserError, 'Host configured, but no backend was provided. Please '\
154
- 'specify how you want to connect. Valid example: ssh://192.168.0.1.'
169
+ 'specify how you want to connect. Valid example: ssh://192.168.0.1'
155
170
  end
156
171
 
157
- conf[:backend] = default
172
+ credentials[:backend] = default_transport_name
158
173
  end
159
174
 
160
175
  def self.group_keys_and_keyfiles(conf)
@@ -3,5 +3,5 @@
3
3
  # Author:: Dominik Richter (<dominik.richter@gmail.com>)
4
4
 
5
5
  module Train
6
- VERSION = '1.7.1'.freeze
6
+ VERSION = '1.7.2'.freeze
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: train-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.1
4
+ version: 1.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dominik Richter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-26 00:00:00.000000000 Z
11
+ date: 2019-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mixlib-shellout