ohai 14.4.0 → 14.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ohai/plugins/windows/network.rb +67 -5
- data/lib/ohai/version.rb +1 -1
- data/spec/unit/plugins/windows/network_spec.rb +137 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58700e6a6ab915d4ec1351041bc74a4f2312ff07d9db58fb225eeaf78a9046fc
|
4
|
+
data.tar.gz: 8c5c2b886c709bee3f3e20979181c424805fbf28e7da3db004d4d5b40b3edc6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 449a50927ade9ebbf3f52899952cf1e9c98670ed99ec6797d4a7d92240772f421dbcb45a2641efc2994c2291d72d502aaab1e4de04f776eb356151d8090908bb
|
7
|
+
data.tar.gz: d98210bde7491883d659dcd0bf40e1179f2340e18f63ed2324e12949e51f41003213578f0895d1e18a961ec32f6c621fdf1440463023ca6a3eeb5f423cf021a8
|
@@ -53,6 +53,65 @@ Ohai.plugin(:Network) do
|
|
53
53
|
data
|
54
54
|
end
|
55
55
|
|
56
|
+
# Returns interface code for an interface
|
57
|
+
#
|
58
|
+
# Interface Index (if present, Index otherwise) will be converted in hexadecimal format
|
59
|
+
#
|
60
|
+
# @param int_idx [String or nil] the interface index of interface
|
61
|
+
# @param idx [String] the index of interface
|
62
|
+
#
|
63
|
+
# @return [String]
|
64
|
+
#
|
65
|
+
# @example Interface Code when interface index is present
|
66
|
+
# plugin.interface_code("1", "1") #=> "ox1"
|
67
|
+
# @example Interface Code when interface index is not present
|
68
|
+
# plugin.interface_code(nil, "2") #=> "ox2"
|
69
|
+
#
|
70
|
+
def interface_code(int_idx, idx)
|
71
|
+
sprintf("0x%x", (int_idx || idx)).downcase
|
72
|
+
end
|
73
|
+
|
74
|
+
# Returns IPV4 address from list of addresses containing IPV4 and IPV6 formats
|
75
|
+
#
|
76
|
+
# @param addresses [Array<String>] List of addresses
|
77
|
+
#
|
78
|
+
# @return [String]
|
79
|
+
#
|
80
|
+
# @example When list contains both IPV4 and IPV6 formats
|
81
|
+
# plugin.prefer_ipv4([IPV4, IPV6]) #=> "IPV4"
|
82
|
+
# @example When list contains only IPV6 format
|
83
|
+
# plugin.prefer_ipv4([IPV6]) #=> "IPV6"
|
84
|
+
#
|
85
|
+
def prefer_ipv4(addresses)
|
86
|
+
return nil unless addresses.is_a?(Array)
|
87
|
+
addresses.find { |ip| IPAddress.valid_ipv4?(ip) } ||
|
88
|
+
addresses.find { |ip| IPAddress.valid_ipv6?(ip) }
|
89
|
+
end
|
90
|
+
|
91
|
+
# Selects default interface and returns its information
|
92
|
+
#
|
93
|
+
# @note Interface with least metric value should be prefered as default_route
|
94
|
+
#
|
95
|
+
# @param configuration [Mash] Configuration of interfaces as iface_config
|
96
|
+
# [<interface_index> => {<interface_configurations>}]
|
97
|
+
#
|
98
|
+
# @return [Hash<:index, :interface_index, :default_ip_gateway, :ip_connection_metric>]
|
99
|
+
#
|
100
|
+
def favored_default_route(configuration)
|
101
|
+
return nil unless configuration.is_a?(Hash)
|
102
|
+
config = configuration.dup
|
103
|
+
|
104
|
+
config.inject([]) do |arr, (k, v)|
|
105
|
+
if v["default_ip_gateway"]
|
106
|
+
arr << { index: v["index"],
|
107
|
+
interface_index: v["interface_index"],
|
108
|
+
default_ip_gateway: prefer_ipv4(v["default_ip_gateway"]),
|
109
|
+
ip_connection_metric: v["ip_connection_metric"] }
|
110
|
+
end
|
111
|
+
arr
|
112
|
+
end.min_by { |r| r[:ip_connection_metric] }
|
113
|
+
end
|
114
|
+
|
56
115
|
collect_data(:windows) do
|
57
116
|
|
58
117
|
require "wmi-lite/wmi"
|
@@ -70,6 +129,7 @@ Ohai.plugin(:Network) do
|
|
70
129
|
iface_config[i] = Mash.new unless iface_config[i]
|
71
130
|
iface_config[i][:ip_address] ||= []
|
72
131
|
iface_config[i][:ip_address] << adapter["IPAddress"]
|
132
|
+
|
73
133
|
adapter.wmi_ole_object.properties_.each do |p|
|
74
134
|
if iface_config[i][p.name.wmi_underscore.to_sym].nil?
|
75
135
|
iface_config[i][p.name.wmi_underscore.to_sym] = adapter[p.name.downcase]
|
@@ -89,7 +149,7 @@ Ohai.plugin(:Network) do
|
|
89
149
|
|
90
150
|
iface_instance.each_key do |i|
|
91
151
|
if iface_instance[i][:name] && iface_config[i] && iface_config[i][:ip_address][0]
|
92
|
-
cint =
|
152
|
+
cint = interface_code(iface_instance[i][:interface_index], iface_instance[i][:index])
|
93
153
|
iface[cint] = Mash.new
|
94
154
|
iface[cint][:configuration] = iface_config[i]
|
95
155
|
iface[cint][:instance] = iface_instance[i]
|
@@ -97,6 +157,7 @@ Ohai.plugin(:Network) do
|
|
97
157
|
iface[cint][:counters] = Mash.new
|
98
158
|
iface[cint][:addresses] = Mash.new
|
99
159
|
iface[cint][:configuration][:ip_address] = iface[cint][:configuration][:ip_address].flatten
|
160
|
+
|
100
161
|
iface[cint][:configuration][:ip_address].each_index do |ip_index|
|
101
162
|
ip = iface[cint][:configuration][:ip_address][ip_index]
|
102
163
|
ip_and_subnet = ip.dup
|
@@ -127,13 +188,14 @@ Ohai.plugin(:Network) do
|
|
127
188
|
iface[cint][:type] = iface[cint][:instance][:adapter_type] if iface[cint][:instance][:adapter_type]
|
128
189
|
iface[cint][:arp] = {}
|
129
190
|
iface[cint][:encapsulation] = windows_encaps_lookup(iface[cint][:instance][:adapter_type]) if iface[cint][:instance][:adapter_type]
|
130
|
-
if !iface[cint][:configuration][:default_ip_gateway].nil? && iface[cint][:configuration][:default_ip_gateway].size > 0
|
131
|
-
network[:default_gateway] = iface[cint][:configuration][:default_ip_gateway].first
|
132
|
-
network[:default_interface] = cint
|
133
|
-
end
|
134
191
|
end
|
135
192
|
end
|
136
193
|
|
194
|
+
if (route = favored_default_route(iface_config))
|
195
|
+
network[:default_gateway] = route[:default_ip_gateway]
|
196
|
+
network[:default_interface] = interface_code(route[:interface_index], route[:index])
|
197
|
+
end
|
198
|
+
|
137
199
|
cint = nil
|
138
200
|
so = shell_out("arp -a")
|
139
201
|
if so.exitstatus == 0
|
data/lib/ohai/version.rb
CHANGED
@@ -0,0 +1,137 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Nimesh Pathi <nimesh.patni@msystechnologies.com>
|
3
|
+
# Copyright:: Copyright (c) 2018 Chef Software, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require_relative "../../../spec_helper.rb"
|
20
|
+
require "ipaddress"
|
21
|
+
|
22
|
+
describe Ohai::System, "Windows Network Plugin" do
|
23
|
+
let(:plugin) { get_plugin("windows/network") }
|
24
|
+
|
25
|
+
before(:each) do
|
26
|
+
allow(plugin).to receive(:collect_os).and_return(:windows)
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#interface_code" do
|
30
|
+
let(:interface_idx) { 1 }
|
31
|
+
let(:index) { 2 }
|
32
|
+
context "when interface index is given" do
|
33
|
+
it "Returns a valid string having hexadecimal interface_index" do
|
34
|
+
index = nil
|
35
|
+
expect(plugin.interface_code(interface_idx, index)).to eq("0x1")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
context "when interface index is not given" do
|
39
|
+
it "Returns a valid string having hexadecimal index" do
|
40
|
+
interface_idx = nil
|
41
|
+
expect(plugin.interface_code(interface_idx, index)).to eq("0x2")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#prefer_ipv4" do
|
47
|
+
let(:inet4) { "192.168.1.1" }
|
48
|
+
let(:inet6) { "fe80::2fe:c8ff:fef5:c88f" }
|
49
|
+
context "When Array is not passed" do
|
50
|
+
it "Returns nil" do
|
51
|
+
expect(plugin.prefer_ipv4("Invalid")).to be_nil
|
52
|
+
end
|
53
|
+
end
|
54
|
+
context "When no address is passed in Array" do
|
55
|
+
it "Returns nil" do
|
56
|
+
expect(plugin.prefer_ipv4([])).to be_nil
|
57
|
+
end
|
58
|
+
end
|
59
|
+
context "Preferred chances of IPV4 address" do
|
60
|
+
it "Returns the address when only IPV4 address is passed" do
|
61
|
+
expect(plugin.prefer_ipv4([inet4])).to eq(inet4)
|
62
|
+
end
|
63
|
+
it "Returns the address when IPV6 is also present at latter place" do
|
64
|
+
expect(plugin.prefer_ipv4([inet4, inet6])).to eq(inet4)
|
65
|
+
end
|
66
|
+
it "Returns the address when IPV6 is also present at former place" do
|
67
|
+
expect(plugin.prefer_ipv4([inet6, inet4])).to eq(inet4)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
context "Preferred chances of IPV6 address" do
|
71
|
+
it "Returns the address when only IPV6 address is passed" do
|
72
|
+
expect(plugin.prefer_ipv4([inet4])).to eq(inet4)
|
73
|
+
end
|
74
|
+
it "Does not return the address if IPV4 is also present at former place" do
|
75
|
+
expect(plugin.prefer_ipv4([inet4, inet6])).not_to eq(inet6)
|
76
|
+
end
|
77
|
+
it "Does not return the address if IPV4 is also present at latter place" do
|
78
|
+
expect(plugin.prefer_ipv4([inet6, inet4])).not_to eq(inet6)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "#favored_default_route" do
|
84
|
+
let(:interface1) do
|
85
|
+
{ "index" => 1,
|
86
|
+
"interface_index" => 1,
|
87
|
+
"ip_connection_metric" => 10,
|
88
|
+
"default_ip_gateway" => ["fe80::2fe:c8ff:fef5:c88f", "192.168.1.1"] }
|
89
|
+
end
|
90
|
+
let(:iface_config) { { 1 => interface1 } }
|
91
|
+
context "When a hash is not passed" do
|
92
|
+
it "Returns nil" do
|
93
|
+
expect(plugin.favored_default_route("Invalid")).to be_nil
|
94
|
+
end
|
95
|
+
end
|
96
|
+
context "When no interface is passed in Hash" do
|
97
|
+
it "Returns nil" do
|
98
|
+
expect(plugin.favored_default_route({})).to be_nil
|
99
|
+
end
|
100
|
+
end
|
101
|
+
context "When an interface configuration is passed" do
|
102
|
+
context "without default_ip_gateway" do
|
103
|
+
it "Returns nil" do
|
104
|
+
interface1["default_ip_gateway"] = nil
|
105
|
+
expect(plugin.favored_default_route(iface_config)).to be_nil
|
106
|
+
end
|
107
|
+
end
|
108
|
+
context "with default_ip_gateway" do
|
109
|
+
it "Returns a hash with details" do
|
110
|
+
expect(plugin.favored_default_route(iface_config)).to be_a(Hash)
|
111
|
+
expect(plugin.favored_default_route(iface_config)).not_to be_empty
|
112
|
+
end
|
113
|
+
it "Returns the default_gateway in IPV4 format" do
|
114
|
+
expect(plugin.favored_default_route(iface_config)).to include(default_ip_gateway: "192.168.1.1")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
context "When multiple interfaces are passed" do
|
119
|
+
let(:interface2) do
|
120
|
+
{ "index" => 2,
|
121
|
+
"interface_index" => 3,
|
122
|
+
"ip_connection_metric" => 20,
|
123
|
+
"default_ip_gateway" => ["192.168.1.2"] }
|
124
|
+
end
|
125
|
+
let(:iface_config) do
|
126
|
+
{ 1 => interface1,
|
127
|
+
2 => interface2 }
|
128
|
+
end
|
129
|
+
it "Returns the default route as least metric interface" do
|
130
|
+
expect(plugin.favored_default_route(iface_config)).to include(interface_index: 1)
|
131
|
+
end
|
132
|
+
it "Returns its default_gateway in IPV4 format" do
|
133
|
+
expect(plugin.favored_default_route(iface_config)).to include(default_ip_gateway: "192.168.1.1")
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ohai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 14.4.
|
4
|
+
version: 14.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Jacob
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: systemu
|
@@ -514,6 +514,7 @@ files:
|
|
514
514
|
- spec/unit/plugins/windows/fips_spec.rb
|
515
515
|
- spec/unit/plugins/windows/kernel_spec.rb
|
516
516
|
- spec/unit/plugins/windows/memory_spec.rb
|
517
|
+
- spec/unit/plugins/windows/network_spec.rb
|
517
518
|
- spec/unit/plugins/windows/system_enclosure_spec.rb
|
518
519
|
- spec/unit/plugins/windows/uptime_spec.rb
|
519
520
|
- spec/unit/plugins/windows/virtualization_spec.rb
|