ohai 18.0.14 → 18.0.26

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +28 -28
  3. data/LICENSE +201 -201
  4. data/bin/ohai +25 -25
  5. data/lib/ohai/application.rb +189 -189
  6. data/lib/ohai/common/dmi.rb +167 -163
  7. data/lib/ohai/config.rb +51 -51
  8. data/lib/ohai/dsl/plugin/versionvii.rb +203 -203
  9. data/lib/ohai/dsl/plugin.rb +232 -232
  10. data/lib/ohai/dsl.rb +22 -22
  11. data/lib/ohai/exception.rb +36 -36
  12. data/lib/ohai/hints.rb +68 -68
  13. data/lib/ohai/loader.rb +178 -178
  14. data/lib/ohai/log.rb +34 -34
  15. data/lib/ohai/mash.rb +22 -22
  16. data/lib/ohai/mixin/alibaba_metadata.rb +86 -86
  17. data/lib/ohai/mixin/azure_metadata.rb +111 -111
  18. data/lib/ohai/mixin/chef_utils_wiring.rb +52 -52
  19. data/lib/ohai/mixin/command.rb +4 -4
  20. data/lib/ohai/mixin/constant_helper.rb +55 -55
  21. data/lib/ohai/mixin/dmi_decode.rb +54 -54
  22. data/lib/ohai/mixin/do_metadata.rb +48 -48
  23. data/lib/ohai/mixin/ec2_metadata.rb +256 -256
  24. data/lib/ohai/mixin/gce_metadata.rb +83 -83
  25. data/lib/ohai/mixin/http_helper.rb +64 -64
  26. data/lib/ohai/mixin/network_helper.rb +65 -63
  27. data/lib/ohai/mixin/os.rb +128 -128
  28. data/lib/ohai/mixin/scaleway_metadata.rb +51 -51
  29. data/lib/ohai/mixin/seconds_to_human.rb +52 -52
  30. data/lib/ohai/mixin/shell_out.rb +51 -51
  31. data/lib/ohai/mixin/softlayer_metadata.rb +74 -74
  32. data/lib/ohai/mixin/string.rb +31 -31
  33. data/lib/ohai/mixin/train_helpers.rb +36 -36
  34. data/lib/ohai/mixin/which.rb +39 -39
  35. data/lib/ohai/plugin_config.rb +47 -47
  36. data/lib/ohai/plugins/aix/kernel.rb +50 -50
  37. data/lib/ohai/plugins/aix/memory.rb +37 -37
  38. data/lib/ohai/plugins/aix/network.rb +142 -142
  39. data/lib/ohai/plugins/aix/platform.rb +30 -30
  40. data/lib/ohai/plugins/aix/uptime.rb +54 -54
  41. data/lib/ohai/plugins/aix/virtualization.rb +154 -154
  42. data/lib/ohai/plugins/alibaba.rb +72 -72
  43. data/lib/ohai/plugins/azure.rb +154 -154
  44. data/lib/ohai/plugins/bsd/virtualization.rb +121 -121
  45. data/lib/ohai/plugins/c.rb +178 -178
  46. data/lib/ohai/plugins/chef.rb +50 -50
  47. data/lib/ohai/plugins/cloud.rb +357 -357
  48. data/lib/ohai/plugins/command.rb +26 -26
  49. data/lib/ohai/plugins/cpu.rb +635 -626
  50. data/lib/ohai/plugins/darwin/hardware.rb +99 -99
  51. data/lib/ohai/plugins/darwin/memory.rb +62 -62
  52. data/lib/ohai/plugins/darwin/network.rb +207 -207
  53. data/lib/ohai/plugins/darwin/platform.rb +38 -38
  54. data/lib/ohai/plugins/darwin/virtualization.rb +93 -93
  55. data/lib/ohai/plugins/digital_ocean.rb +67 -67
  56. data/lib/ohai/plugins/dmi.rb +134 -134
  57. data/lib/ohai/plugins/docker.rb +58 -58
  58. data/lib/ohai/plugins/dragonflybsd/memory.rb +60 -60
  59. data/lib/ohai/plugins/dragonflybsd/network.rb +128 -128
  60. data/lib/ohai/plugins/dragonflybsd/platform.rb +28 -28
  61. data/lib/ohai/plugins/ec2.rb +148 -155
  62. data/lib/ohai/plugins/elixir.rb +36 -36
  63. data/lib/ohai/plugins/erlang.rb +60 -60
  64. data/lib/ohai/plugins/eucalyptus.rb +86 -86
  65. data/lib/ohai/plugins/filesystem.rb +751 -751
  66. data/lib/ohai/plugins/fips.rb +36 -35
  67. data/lib/ohai/plugins/freebsd/memory.rb +60 -60
  68. data/lib/ohai/plugins/freebsd/network.rb +128 -128
  69. data/lib/ohai/plugins/freebsd/platform.rb +28 -28
  70. data/lib/ohai/plugins/gce.rb +89 -89
  71. data/lib/ohai/plugins/go.rb +34 -34
  72. data/lib/ohai/plugins/groovy.rb +38 -38
  73. data/lib/ohai/plugins/grub2.rb +40 -40
  74. data/lib/ohai/plugins/habitat.rb +73 -73
  75. data/lib/ohai/plugins/haskell.rb +96 -96
  76. data/lib/ohai/plugins/hostname.rb +133 -133
  77. data/lib/ohai/plugins/init_package.rb +26 -26
  78. data/lib/ohai/plugins/java.rb +78 -78
  79. data/lib/ohai/plugins/kernel.rb +292 -292
  80. data/lib/ohai/plugins/keys.rb +27 -27
  81. data/lib/ohai/plugins/languages.rb +26 -26
  82. data/lib/ohai/plugins/libvirt.rb +114 -114
  83. data/lib/ohai/plugins/linode.rb +73 -73
  84. data/lib/ohai/plugins/linux/block_device.rb +48 -48
  85. data/lib/ohai/plugins/linux/hostnamectl.rb +34 -34
  86. data/lib/ohai/plugins/linux/interrupts.rb +84 -84
  87. data/lib/ohai/plugins/linux/ipc.rb +52 -52
  88. data/lib/ohai/plugins/linux/livepatch.rb +38 -38
  89. data/lib/ohai/plugins/linux/lsb.rb +46 -46
  90. data/lib/ohai/plugins/linux/lspci.rb +76 -76
  91. data/lib/ohai/plugins/linux/machineid.rb +36 -36
  92. data/lib/ohai/plugins/linux/mdadm.rb +120 -120
  93. data/lib/ohai/plugins/linux/memory.rb +106 -106
  94. data/lib/ohai/plugins/linux/network.rb +879 -879
  95. data/lib/ohai/plugins/linux/os_release.rb +38 -38
  96. data/lib/ohai/plugins/linux/platform.rb +314 -313
  97. data/lib/ohai/plugins/linux/selinux.rb +69 -69
  98. data/lib/ohai/plugins/linux/sessions.rb +54 -54
  99. data/lib/ohai/plugins/linux/sysctl.rb +39 -39
  100. data/lib/ohai/plugins/linux/systemd_paths.rb +36 -36
  101. data/lib/ohai/plugins/linux/tc.rb +61 -61
  102. data/lib/ohai/plugins/linux/virtualization.rb +300 -300
  103. data/lib/ohai/plugins/lua.rb +39 -39
  104. data/lib/ohai/plugins/mono.rb +50 -50
  105. data/lib/ohai/plugins/netbsd/memory.rb +99 -99
  106. data/lib/ohai/plugins/netbsd/network.rb +122 -122
  107. data/lib/ohai/plugins/netbsd/platform.rb +28 -28
  108. data/lib/ohai/plugins/network.rb +186 -186
  109. data/lib/ohai/plugins/nodejs.rb +40 -40
  110. data/lib/ohai/plugins/ohai.rb +29 -29
  111. data/lib/ohai/plugins/ohai_time.rb +26 -26
  112. data/lib/ohai/plugins/openbsd/memory.rb +99 -99
  113. data/lib/ohai/plugins/openbsd/network.rb +122 -122
  114. data/lib/ohai/plugins/openbsd/platform.rb +28 -28
  115. data/lib/ohai/plugins/openstack.rb +84 -84
  116. data/lib/ohai/plugins/os.rb +55 -55
  117. data/lib/ohai/plugins/packages.rb +234 -234
  118. data/lib/ohai/plugins/passwd.rb +104 -104
  119. data/lib/ohai/plugins/perl.rb +45 -45
  120. data/lib/ohai/plugins/php.rb +52 -52
  121. data/lib/ohai/plugins/platform.rb +29 -29
  122. data/lib/ohai/plugins/powershell.rb +82 -82
  123. data/lib/ohai/plugins/ps.rb +35 -35
  124. data/lib/ohai/plugins/python.rb +43 -43
  125. data/lib/ohai/plugins/rackspace.rb +177 -177
  126. data/lib/ohai/plugins/root_group.rb +41 -41
  127. data/lib/ohai/plugins/rpm.rb +121 -119
  128. data/lib/ohai/plugins/ruby.rb +66 -66
  129. data/lib/ohai/plugins/rust.rb +34 -34
  130. data/lib/ohai/plugins/scala.rb +38 -38
  131. data/lib/ohai/plugins/scaleway.rb +58 -58
  132. data/lib/ohai/plugins/scsi.rb +52 -52
  133. data/lib/ohai/plugins/shard.rb +142 -142
  134. data/lib/ohai/plugins/shells.rb +32 -32
  135. data/lib/ohai/plugins/softlayer.rb +48 -48
  136. data/lib/ohai/plugins/solaris2/dmi.rb +191 -191
  137. data/lib/ohai/plugins/solaris2/memory.rb +32 -32
  138. data/lib/ohai/plugins/solaris2/network.rb +192 -192
  139. data/lib/ohai/plugins/solaris2/platform.rb +58 -58
  140. data/lib/ohai/plugins/solaris2/virtualization.rb +90 -90
  141. data/lib/ohai/plugins/ssh_host_key.rb +84 -84
  142. data/lib/ohai/plugins/sysconf.rb +46 -46
  143. data/lib/ohai/plugins/timezone.rb +25 -25
  144. data/lib/ohai/plugins/train.rb +35 -35
  145. data/lib/ohai/plugins/uptime.rb +95 -95
  146. data/lib/ohai/plugins/virtualbox.rb +197 -197
  147. data/lib/ohai/plugins/vmware.rb +94 -94
  148. data/lib/ohai/plugins/windows/dmi.rb +95 -95
  149. data/lib/ohai/plugins/windows/drivers.rb +52 -52
  150. data/lib/ohai/plugins/windows/memory.rb +39 -39
  151. data/lib/ohai/plugins/windows/network.rb +222 -222
  152. data/lib/ohai/plugins/windows/platform.rb +34 -34
  153. data/lib/ohai/plugins/windows/system_enclosure.rb +29 -29
  154. data/lib/ohai/plugins/windows/virtualization.rb +45 -45
  155. data/lib/ohai/plugins/zpools.rb +94 -94
  156. data/lib/ohai/provides_map.rb +208 -208
  157. data/lib/ohai/runner.rb +128 -128
  158. data/lib/ohai/system.rb +258 -258
  159. data/lib/ohai/train_transport.rb +29 -29
  160. data/lib/ohai/util/file_helper.rb +6 -6
  161. data/lib/ohai/util/ip_helper.rb +56 -56
  162. data/lib/ohai/util/win32.rb +47 -47
  163. data/lib/ohai/version.rb +23 -23
  164. data/lib/ohai.rb +23 -23
  165. data/ohai.gemspec +35 -35
  166. metadata +3 -3
@@ -1,256 +1,256 @@
1
- # frozen_string_literal: true
2
- #
3
- # Author:: Tim Dysinger (<tim@dysinger.net>)
4
- # Author:: Benjamin Black (<bb@chef.io>)
5
- # Author:: Christopher Brown (<cb@chef.io>)
6
- # Copyright:: Copyright (c) Chef Software Inc.
7
- # License:: Apache License, Version 2.0
8
- #
9
- # Licensed under the Apache License, Version 2.0 (the "License");
10
- # you may not use this file except in compliance with the License.
11
- # You may obtain a copy of the License at
12
- #
13
- # http://www.apache.org/licenses/LICENSE-2.0
14
- #
15
- # Unless required by applicable law or agreed to in writing, software
16
- # distributed under the License is distributed on an "AS IS" BASIS,
17
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
- # See the License for the specific language governing permissions and
19
- # limitations under the License.
20
-
21
- require "net/http" unless defined?(Net::HTTP)
22
-
23
- module Ohai
24
- module Mixin
25
- ##
26
- # This code parses the EC2 Instance Metadata API to provide details
27
- # of the running instance.
28
- #
29
- # Earlier version of this code assumed a specific version of the
30
- # metadata API was available. Unfortunately the API versions
31
- # supported by a particular instance are determined at instance
32
- # launch and are not extended over the life of the instance. As such
33
- # the earlier code would fail depending on the age of the instance.
34
- #
35
- # The updated code probes the instance metadata endpoint for
36
- # available versions, determines the most advanced version known to
37
- # work and executes the metadata retrieval using that version.
38
- #
39
- # If no compatible version is found, an empty hash is returned.
40
- #
41
- module Ec2Metadata
42
-
43
- EC2_METADATA_ADDR ||= "169.254.169.254"
44
- EC2_SUPPORTED_VERSIONS ||= %w{ 1.0
45
- 2007-01-19
46
- 2007-03-01
47
- 2007-08-29
48
- 2007-10-10
49
- 2007-12-15
50
- 2008-02-01
51
- 2008-09-01
52
- 2009-04-04
53
- 2011-01-01
54
- 2011-05-01
55
- 2012-01-12
56
- 2014-02-25
57
- 2014-11-05
58
- 2015-10-20
59
- 2016-04-19
60
- 2016-06-30
61
- 2016-09-02
62
- 2018-03-28
63
- 2018-08-17
64
- 2018-09-24
65
- 2019-10-01
66
- 2020-10-27
67
- 2021-01-03
68
- 2021-03-23
69
- 2021-07-15 }.freeze
70
- EC2_ARRAY_VALUES ||= %w{security-groups local_ipv4s}.freeze
71
- EC2_ARRAY_DIR ||= %w{network/interfaces/macs}.freeze
72
- EC2_JSON_DIR ||= %w{iam}.freeze
73
-
74
- #
75
- # The latest metadata version in EC2_SUPPORTED_VERSIONS that this instance supports
76
- # in AWS supported metadata versions are determined at instance start so we need to be
77
- # cautious here in case an instance has been running for a long time
78
- #
79
- # @return [String] the version
80
- #
81
- def best_api_version
82
- @api_version ||= begin
83
- logger.trace("Mixin EC2: Fetching http://#{EC2_METADATA_ADDR}/ to determine the latest supported metadata release")
84
- response = http_client.get("/", { 'X-aws-ec2-metadata-token': v2_token })
85
- if response.code == "404"
86
- logger.trace("Mixin EC2: Received HTTP 404 from metadata server while determining API version, assuming 'latest'")
87
- return "latest"
88
- elsif response.code != "200"
89
- raise "Mixin EC2: Unable to determine EC2 metadata version (returned #{response.code} response)"
90
- end
91
- # NOTE: Sorting the list of versions may have unintended consequences in
92
- # non-EC2 environments. It appears to be safe in EC2 as of 2013-04-12.
93
- versions = response.body.split("\n").sort
94
- until versions.empty? || EC2_SUPPORTED_VERSIONS.include?(versions.last)
95
- pv = versions.pop
96
- logger.trace("Mixin EC2: EC2 lists metadata version: #{pv} not yet supported by Ohai") unless pv == "latest"
97
- end
98
- logger.trace("Mixin EC2: Latest supported EC2 metadata version: #{versions.last}")
99
- if versions.empty?
100
- raise "Mixin EC2: Unable to determine EC2 metadata version (no supported entries found)"
101
- end
102
-
103
- versions.last
104
- end
105
- end
106
-
107
- # a net/http client with a timeout of 10s and a keepalive of 10s
108
- #
109
- # @return [Net::HTTP]
110
- def http_client
111
- @conn ||= Net::HTTP.start(EC2_METADATA_ADDR).tap do |h|
112
- h.read_timeout = 10
113
- h.keep_alive_timeout = 10
114
- end
115
- end
116
-
117
- #
118
- # Fetch an API token for use querying AWS IMDSv2 or return nil if no token if found
119
- # AWS like systems (think OpenStack) will not respond with a token here
120
- #
121
- # @return [NilClass, String] API token or nil
122
- #
123
- def v2_token
124
- @v2_token ||= begin
125
- request = http_client.put("/latest/api/token", nil, { 'X-aws-ec2-metadata-token-ttl-seconds': "60" })
126
- if request.code == "404" # not on AWS
127
- nil
128
- else
129
- request.body
130
- end
131
- end
132
- end
133
-
134
- # Get metadata for a given path and API version
135
- #
136
- # Typically, a 200 response is expected for valid metadata.
137
- # On certain instance types, traversing the provided metadata path
138
- # produces a 404 for some unknown reason. In that event, return
139
- # `nil` and continue the run instead of failing it.
140
- def metadata_get(id, api_version)
141
- path = "/#{api_version}/meta-data/#{id}"
142
- logger.trace("Mixin EC2: Fetching http://#{EC2_METADATA_ADDR}#{path}")
143
- response = http_client.get(path, { 'X-aws-ec2-metadata-token': v2_token })
144
- case response.code
145
- when "200"
146
- response.body
147
- when "404"
148
- logger.trace("Mixin EC2: Encountered 404 response retrieving EC2 metadata path: #{path} ; continuing.")
149
- nil
150
- else
151
- raise "Mixin EC2: Encountered error retrieving EC2 metadata (#{path} returned #{response.code} response)"
152
- end
153
- end
154
-
155
- def fetch_metadata(id = "", api_version = nil)
156
- metadata = {}
157
- retrieved_metadata = metadata_get(id, best_api_version)
158
- if retrieved_metadata
159
- retrieved_metadata.split("\n").each do |o|
160
- key = expand_path("#{id}#{o}")
161
- if key[-1..-1] != "/"
162
- metadata[metadata_key(key)] =
163
- if EC2_ARRAY_VALUES.include? key
164
- retr_meta = metadata_get(key, best_api_version)
165
- retr_meta ? retr_meta.split("\n") : retr_meta
166
- else
167
- metadata_get(key, best_api_version)
168
- end
169
- elsif (!key.eql?(id)) && (!key.eql?("/"))
170
- name = key[0..-2]
171
- sym = metadata_key(name)
172
- if EC2_ARRAY_DIR.include?(name)
173
- metadata[sym] = fetch_dir_metadata(key, best_api_version)
174
- elsif EC2_JSON_DIR.include?(name)
175
- metadata[sym] = fetch_json_dir_metadata(key, best_api_version)
176
- else
177
- fetch_metadata(key, best_api_version).each { |k, v| metadata[k] = v }
178
- end
179
- end
180
- end
181
- metadata
182
- end
183
- end
184
-
185
- def fetch_dir_metadata(id, api_version)
186
- metadata = {}
187
- retrieved_metadata = metadata_get(id, api_version)
188
- if retrieved_metadata
189
- retrieved_metadata.split("\n").each do |o|
190
- key = expand_path(o)
191
- if key[-1..-1] != "/"
192
- retr_meta = metadata_get("#{id}#{key}", api_version)
193
- metadata[metadata_key(key)] = retr_meta || ""
194
- elsif !key.eql?("/")
195
- metadata[key[0..-2]] = fetch_dir_metadata("#{id}#{key}", api_version)
196
- end
197
- end
198
- metadata
199
- end
200
- end
201
-
202
- def fetch_json_dir_metadata(id, api_version)
203
- metadata = {}
204
- retrieved_metadata = metadata_get(id, api_version)
205
- if retrieved_metadata
206
- retrieved_metadata.split("\n").each do |o|
207
- key = expand_path(o)
208
- if key[-1..-1] != "/"
209
- retr_meta = metadata_get("#{id}#{key}", api_version)
210
- data = retr_meta || ""
211
- json = String(data)
212
- parser = FFI_Yajl::Parser.new
213
- metadata[metadata_key(key)] = parser.parse(json)
214
- elsif !key.eql?("/")
215
- metadata[key[0..-2]] = fetch_json_dir_metadata("#{id}#{key}", api_version)
216
- end
217
- end
218
- metadata
219
- end
220
- end
221
-
222
- def fetch_userdata
223
- logger.trace("Mixin EC2: Fetching http://#{EC2_METADATA_ADDR}/#{best_api_version}/user-data/")
224
- response = http_client.get("/#{best_api_version}/user-data/", { 'X-aws-ec2-metadata-token': v2_token })
225
- response.code == "200" ? response.body : nil
226
- end
227
-
228
- def fetch_dynamic_data
229
- @fetch_dynamic_data ||= begin
230
- response = http_client.get("/#{best_api_version}/dynamic/instance-identity/document/", { 'X-aws-ec2-metadata-token': v2_token })
231
-
232
- if json?(response.body) && response.code == "200"
233
- FFI_Yajl::Parser.parse(response.body)
234
- else
235
- {}
236
- end
237
- end
238
- end
239
-
240
- private
241
-
242
- def expand_path(file_name)
243
- path = file_name.gsub(/\=.*$/, "/")
244
- # ignore "./" and "../"
245
- path.gsub(%r{/\.\.?(?:/|$)}, "/")
246
- .sub(%r{^\.\.?(?:/|$)}, "")
247
- .sub(/^$/, "/")
248
- end
249
-
250
- def metadata_key(key)
251
- key.gsub(%r{\-|/}, "_")
252
- end
253
-
254
- end
255
- end
256
- end
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Tim Dysinger (<tim@dysinger.net>)
4
+ # Author:: Benjamin Black (<bb@chef.io>)
5
+ # Author:: Christopher Brown (<cb@chef.io>)
6
+ # Copyright:: Copyright (c) Chef Software Inc.
7
+ # License:: Apache License, Version 2.0
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+
21
+ require "net/http" unless defined?(Net::HTTP)
22
+
23
+ module Ohai
24
+ module Mixin
25
+ ##
26
+ # This code parses the EC2 Instance Metadata API to provide details
27
+ # of the running instance.
28
+ #
29
+ # Earlier version of this code assumed a specific version of the
30
+ # metadata API was available. Unfortunately the API versions
31
+ # supported by a particular instance are determined at instance
32
+ # launch and are not extended over the life of the instance. As such
33
+ # the earlier code would fail depending on the age of the instance.
34
+ #
35
+ # The updated code probes the instance metadata endpoint for
36
+ # available versions, determines the most advanced version known to
37
+ # work and executes the metadata retrieval using that version.
38
+ #
39
+ # If no compatible version is found, an empty hash is returned.
40
+ #
41
+ module Ec2Metadata
42
+
43
+ EC2_METADATA_ADDR ||= "169.254.169.254"
44
+ EC2_SUPPORTED_VERSIONS ||= %w{ 1.0
45
+ 2007-01-19
46
+ 2007-03-01
47
+ 2007-08-29
48
+ 2007-10-10
49
+ 2007-12-15
50
+ 2008-02-01
51
+ 2008-09-01
52
+ 2009-04-04
53
+ 2011-01-01
54
+ 2011-05-01
55
+ 2012-01-12
56
+ 2014-02-25
57
+ 2014-11-05
58
+ 2015-10-20
59
+ 2016-04-19
60
+ 2016-06-30
61
+ 2016-09-02
62
+ 2018-03-28
63
+ 2018-08-17
64
+ 2018-09-24
65
+ 2019-10-01
66
+ 2020-10-27
67
+ 2021-01-03
68
+ 2021-03-23
69
+ 2021-07-15 }.freeze
70
+ EC2_ARRAY_VALUES ||= %w{security-groups local_ipv4s}.freeze
71
+ EC2_ARRAY_DIR ||= %w{network/interfaces/macs}.freeze
72
+ EC2_JSON_DIR ||= %w{iam}.freeze
73
+
74
+ #
75
+ # The latest metadata version in EC2_SUPPORTED_VERSIONS that this instance supports
76
+ # in AWS supported metadata versions are determined at instance start so we need to be
77
+ # cautious here in case an instance has been running for a long time
78
+ #
79
+ # @return [String] the version
80
+ #
81
+ def best_api_version
82
+ @api_version ||= begin
83
+ logger.trace("Mixin EC2: Fetching http://#{EC2_METADATA_ADDR}/ to determine the latest supported metadata release")
84
+ response = http_client.get("/", { 'X-aws-ec2-metadata-token': v2_token })
85
+ if response.code == "404"
86
+ logger.trace("Mixin EC2: Received HTTP 404 from metadata server while determining API version, assuming 'latest'")
87
+ return "latest"
88
+ elsif response.code != "200"
89
+ raise "Mixin EC2: Unable to determine EC2 metadata version (returned #{response.code} response)"
90
+ end
91
+ # NOTE: Sorting the list of versions may have unintended consequences in
92
+ # non-EC2 environments. It appears to be safe in EC2 as of 2013-04-12.
93
+ versions = response.body.split("\n").sort
94
+ until versions.empty? || EC2_SUPPORTED_VERSIONS.include?(versions.last)
95
+ pv = versions.pop
96
+ logger.trace("Mixin EC2: EC2 lists metadata version: #{pv} not yet supported by Ohai") unless pv == "latest"
97
+ end
98
+ logger.trace("Mixin EC2: Latest supported EC2 metadata version: #{versions.last}")
99
+ if versions.empty?
100
+ raise "Mixin EC2: Unable to determine EC2 metadata version (no supported entries found)"
101
+ end
102
+
103
+ versions.last
104
+ end
105
+ end
106
+
107
+ # a net/http client with a timeout of 10s and a keepalive of 10s
108
+ #
109
+ # @return [Net::HTTP]
110
+ def http_client
111
+ @conn ||= Net::HTTP.start(EC2_METADATA_ADDR).tap do |h|
112
+ h.read_timeout = 10
113
+ h.keep_alive_timeout = 10
114
+ end
115
+ end
116
+
117
+ #
118
+ # Fetch an API token for use querying AWS IMDSv2 or return nil if no token if found
119
+ # AWS like systems (think OpenStack) will not respond with a token here
120
+ #
121
+ # @return [NilClass, String] API token or nil
122
+ #
123
+ def v2_token
124
+ @v2_token ||= begin
125
+ request = http_client.put("/latest/api/token", nil, { 'X-aws-ec2-metadata-token-ttl-seconds': "60" })
126
+ if request.code == "404" # not on AWS
127
+ nil
128
+ else
129
+ request.body
130
+ end
131
+ end
132
+ end
133
+
134
+ # Get metadata for a given path and API version
135
+ #
136
+ # Typically, a 200 response is expected for valid metadata.
137
+ # On certain instance types, traversing the provided metadata path
138
+ # produces a 404 for some unknown reason. In that event, return
139
+ # `nil` and continue the run instead of failing it.
140
+ def metadata_get(id, api_version)
141
+ path = "/#{api_version}/meta-data/#{id}"
142
+ logger.trace("Mixin EC2: Fetching http://#{EC2_METADATA_ADDR}#{path}")
143
+ response = http_client.get(path, { 'X-aws-ec2-metadata-token': v2_token })
144
+ case response.code
145
+ when "200"
146
+ response.body
147
+ when "404"
148
+ logger.trace("Mixin EC2: Encountered 404 response retrieving EC2 metadata path: #{path} ; continuing.")
149
+ nil
150
+ else
151
+ raise "Mixin EC2: Encountered error retrieving EC2 metadata (#{path} returned #{response.code} response)"
152
+ end
153
+ end
154
+
155
+ def fetch_metadata(id = "", api_version = nil)
156
+ metadata = {}
157
+ retrieved_metadata = metadata_get(id, best_api_version)
158
+ if retrieved_metadata
159
+ retrieved_metadata.split("\n").each do |o|
160
+ key = expand_path("#{id}#{o}")
161
+ if key[-1..-1] != "/"
162
+ metadata[metadata_key(key)] =
163
+ if EC2_ARRAY_VALUES.include? key
164
+ retr_meta = metadata_get(key, best_api_version)
165
+ retr_meta ? retr_meta.split("\n") : retr_meta
166
+ else
167
+ metadata_get(key, best_api_version)
168
+ end
169
+ elsif (!key.eql?(id)) && (!key.eql?("/"))
170
+ name = key[0..-2]
171
+ sym = metadata_key(name)
172
+ if EC2_ARRAY_DIR.include?(name)
173
+ metadata[sym] = fetch_dir_metadata(key, best_api_version)
174
+ elsif EC2_JSON_DIR.include?(name)
175
+ metadata[sym] = fetch_json_dir_metadata(key, best_api_version)
176
+ else
177
+ fetch_metadata(key, best_api_version).each { |k, v| metadata[k] = v }
178
+ end
179
+ end
180
+ end
181
+ metadata
182
+ end
183
+ end
184
+
185
+ def fetch_dir_metadata(id, api_version)
186
+ metadata = {}
187
+ retrieved_metadata = metadata_get(id, api_version)
188
+ if retrieved_metadata
189
+ retrieved_metadata.split("\n").each do |o|
190
+ key = expand_path(o)
191
+ if key[-1..-1] != "/"
192
+ retr_meta = metadata_get("#{id}#{key}", api_version)
193
+ metadata[metadata_key(key)] = retr_meta || ""
194
+ elsif !key.eql?("/")
195
+ metadata[key[0..-2]] = fetch_dir_metadata("#{id}#{key}", api_version)
196
+ end
197
+ end
198
+ metadata
199
+ end
200
+ end
201
+
202
+ def fetch_json_dir_metadata(id, api_version)
203
+ metadata = {}
204
+ retrieved_metadata = metadata_get(id, api_version)
205
+ if retrieved_metadata
206
+ retrieved_metadata.split("\n").each do |o|
207
+ key = expand_path(o)
208
+ if key[-1..-1] != "/"
209
+ retr_meta = metadata_get("#{id}#{key}", api_version)
210
+ data = retr_meta || ""
211
+ json = String(data)
212
+ parser = FFI_Yajl::Parser.new
213
+ metadata[metadata_key(key)] = parser.parse(json)
214
+ elsif !key.eql?("/")
215
+ metadata[key[0..-2]] = fetch_json_dir_metadata("#{id}#{key}", api_version)
216
+ end
217
+ end
218
+ metadata
219
+ end
220
+ end
221
+
222
+ def fetch_userdata
223
+ logger.trace("Mixin EC2: Fetching http://#{EC2_METADATA_ADDR}/#{best_api_version}/user-data/")
224
+ response = http_client.get("/#{best_api_version}/user-data/", { 'X-aws-ec2-metadata-token': v2_token })
225
+ response.code == "200" ? response.body : nil
226
+ end
227
+
228
+ def fetch_dynamic_data
229
+ @fetch_dynamic_data ||= begin
230
+ response = http_client.get("/#{best_api_version}/dynamic/instance-identity/document/", { 'X-aws-ec2-metadata-token': v2_token })
231
+
232
+ if json?(response.body) && response.code == "200"
233
+ FFI_Yajl::Parser.parse(response.body)
234
+ else
235
+ {}
236
+ end
237
+ end
238
+ end
239
+
240
+ private
241
+
242
+ def expand_path(file_name)
243
+ path = file_name.gsub(/\=.*$/, "/")
244
+ # ignore "./" and "../"
245
+ path.gsub(%r{/\.\.?(?:/|$)}, "/")
246
+ .sub(%r{^\.\.?(?:/|$)}, "")
247
+ .sub(/^$/, "/")
248
+ end
249
+
250
+ def metadata_key(key)
251
+ key.gsub(%r{\-|/}, "_")
252
+ end
253
+
254
+ end
255
+ end
256
+ end