Skip to content

Commit 49b0f9e

Browse files
committed
Require MTU passed as positive integer; coerce when parsing
1 parent a4279eb commit 49b0f9e

File tree

3 files changed

+8
-51
lines changed

3 files changed

+8
-51
lines changed

lib/puppet/provider/network_config/interfaces.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def self.parse_file(_filename, contents)
226226
case key # rubocop:disable Metrics/BlockNesting
227227
when 'address' then Instance[name].ipaddress = val
228228
when 'netmask' then Instance[name].netmask = val
229-
when 'mtu' then Instance[name].mtu = val
229+
when 'mtu' then Instance[name].mtu = val.to_i
230230
else Instance[name].options[key] << val
231231
end
232232
end

lib/puppet/type/network_config.rb

+3-10
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,8 @@
8686
desc 'The Maximum Transmission Unit size to use for the interface'
8787
validate do |value|
8888
# reject floating point and negative integers
89-
# XXX this lets 1500.0 pass
90-
if value.is_a? Numeric
91-
unless value.integer?
92-
raise ArgumentError, "#{value} is not a valid mtu (must be a positive integer)"
93-
end
94-
else
95-
unless value =~ %r{^\d+$}
96-
raise ArgumentError, "#{value} is not a valid mtu (must be a positive integer)"
97-
end
89+
unless value.is_a?(Numeric) && value.integer?
90+
raise ArgumentError, "#{value} is not a valid mtu (must be a positive integer)"
9891
end
9992

10093
# Intel 82598 & 82599 chips support MTUs up to 16110; is there any
@@ -106,7 +99,7 @@
10699
# is 42 with a 802.1q header and 46 without.
107100
min_mtu = 42
108101
max_mtu = 65_536
109-
unless (min_mtu..max_mtu).cover?(value.to_i)
102+
unless min_mtu <= value && value <= max_mtu
110103
raise ArgumentError, "#{value} is not in the valid mtu range (#{min_mtu} .. #{max_mtu})"
111104
end
112105
end

spec/unit/type/network_config_spec.rb

+4-40
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,14 @@
140140
end
141141

142142
describe 'mtu' do
143-
it 'validates a tiny mtu size' do
144-
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '42')
145-
end
146-
147143
it 'validates a tiny mtu size as a number' do
148144
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 42)
149145
end
150146

151-
it 'validates a normal mtu size' do
152-
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '1500')
153-
end
154-
155147
it 'validates a normal mtu size as a number' do
156148
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 1500)
157149
end
158150

159-
it 'validates a large mtu size' do
160-
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '16384')
161-
end
162-
163151
it 'validates a large mtu size as a number' do
164152
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 16_384)
165153
end
@@ -170,63 +158,39 @@
170158
end.to raise_error(%r{must be a positive integer})
171159
end
172160

173-
it 'fails on values < 42' do
174-
expect do
175-
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '41')
176-
end.to raise_error(%r{is not in the valid mtu range})
177-
end
178-
179161
it 'fails on numeric values < 42' do
180162
expect do
181163
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 41)
182164
end.to raise_error(%r{is not in the valid mtu range})
183165
end
184166

185-
it 'fails on zero' do
186-
expect do
187-
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '0')
188-
end.to raise_error(%r{is not in the valid mtu range})
189-
end
190-
191167
it 'fails on numeric zero' do
192168
expect do
193169
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 0)
194170
end.to raise_error(%r{is not in the valid mtu range})
195171
end
196172

197-
it 'fails on values > 65536' do
198-
expect do
199-
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '65537')
200-
end.to raise_error(%r{is not in the valid mtu range})
201-
end
202-
203173
it 'fails on numeric values > 65536' do
204174
expect do
205175
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 65_537)
206176
end.to raise_error(%r{is not in the valid mtu range})
207177
end
208178

209-
it 'fails on negative values' do
210-
expect do
211-
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '-1500')
212-
end.to raise_error(%r{is not a valid mtu})
213-
end
214-
215179
it 'fails on negative numbers' do
216180
expect do
217181
Puppet::Type.type(:network_config).new(name: 'yay', mtu: -1500)
218182
end.to raise_error(%r{is not in the valid mtu range})
219183
end
220184

221-
it 'fails on non-integer values' do
185+
it 'fails on numeric non-integer values' do
222186
expect do
223-
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '1500.1')
187+
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 1500.1)
224188
end.to raise_error(%r{must be a positive integer})
225189
end
226190

227-
it 'fails on numeric non-integer values' do
191+
it 'fails on integers passed as strings' do
228192
expect do
229-
Puppet::Type.type(:network_config).new(name: 'yay', mtu: 1500.1)
193+
Puppet::Type.type(:network_config).new(name: 'yay', mtu: '1500.1')
230194
end.to raise_error(%r{must be a positive integer})
231195
end
232196
end

0 commit comments

Comments
 (0)