train 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: b9584715b895615e0a5f3462971856c35b43898d1329b18657b8fee5a07375de
4
- data.tar.gz: 2f293c8a2620a662a361e2b1d09d2b2979dc817484bc369eca30b5fc36b568ab
3
+ metadata.gz: 90cfe2b9d2a7ff8e247729ed8f7e7fac0ad3ced66eb98de9265b00170ae3e609
4
+ data.tar.gz: cd9c481520c62e3ffe6c5bef2553a16f0b2685d312cb68517101a221a35136b7
5
5
  SHA512:
6
- metadata.gz: c9e5eccbc7975365d13bb2eb4eb451520ed0f2a48949218d679d02983560cf95f2ecf9321121fbf563fbc29d8246c1da469fd990b4debb6df94535b7477816d5
7
- data.tar.gz: 3d1b0e300b67d64268e7cb2657c495332dd0cbfedfe264f4ea7e25db126b97a558256d0664917892a3273a8517c17f8993b0bd4cda49994c9bbe1cd3d18e4c40
6
+ metadata.gz: c66b8d1b3d903b1efd079a49ff0a6007a247468c3a46df4b00b594cf78b1342a6ac138aea119752a49c8c3e196291cbabd9bf10da146d0176cde25345ea5aa17
7
+ data.tar.gz: 3054b59b175c6fe70dc08099cf505092fb860c3c3323ed9c77c2c7a67a5d27a4f95c811238caaa3014fdd673626a77a2d02da583c953415b9a38013ab6accdde
@@ -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
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: json