Skip to content

Commit 6cbf017

Browse files
authored
Merge pull request #1003 from iglov/master
Add a stdlib::ip_in_range() function
2 parents b7bd24a + 77a4b1f commit 6cbf017

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,17 @@ stdlib::extname('.profile') => ''
13991399
14001400
*Type*: rvalue.
14011401
1402+
#### `stdlib::ip_in_range`
1403+
1404+
A Puppet function to determine if an IPv4 address is within the IPv4 CIDR. Returns true if the ipaddress is within the given CIDRs.
1405+
1406+
```puppet
1407+
$ranges = ['192.168.0.0/24', '10.10.10.0/24']
1408+
$valid_ip = stdlib::ip_in_range('10.10.10.53', $ranges) # $valid_ip == true
1409+
```
1410+
1411+
*Type*: rvalue.
1412+
14021413
#### `fact`
14031414
14041415
Return the value of a given fact. Supports the use of dot-notation for referring to structured facts. If a fact requested does not exist, returns Undef.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Returns true if the ipaddress is within the given CIDRs
2+
#
3+
# @example ip_in_range(<IPv4 Address>, <IPv4 CIDR>)
4+
# stdlib::ip_in_range('10.10.10.53', '10.10.10.0/24') => true
5+
Puppet::Functions.create_function(:'stdlib::ip_in_range') do
6+
# @param [String] ipaddress The IP address to check
7+
# @param [Variant[String, Array]] range One CIDR or an array of CIDRs
8+
# defining the range(s) to check against
9+
#
10+
# @return [Boolean] True or False
11+
dispatch :ip_in_range do
12+
param 'String', :ipaddress
13+
param 'Variant[String, Array]', :range
14+
return_type 'Boolean'
15+
end
16+
17+
require 'ipaddr'
18+
19+
def ip_in_range(ipaddress, range)
20+
ip = IPAddr.new(ipaddress)
21+
22+
if range.is_a? Array
23+
ranges = range.map { |r| IPAddr.new(r) }
24+
ranges.any? { |rng| rng.include?(ip) }
25+
elsif range.is_a? String
26+
ranges = IPAddr.new(range)
27+
ranges.include?(ip)
28+
end
29+
end
30+
end

spec/functions/ip_in_range_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
3+
describe 'stdlib::ip_in_range' do
4+
describe 'signature validation' do
5+
it { is_expected.not_to eq(nil) }
6+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'stdlib::ip_in_range' expects 2 arguments, got none}) }
7+
it { is_expected.to run.with_params('one', 'two', '3').and_raise_error(ArgumentError, %r{'stdlib::ip_in_range' expects 2 arguments, got 3}) }
8+
it { is_expected.to run.with_params([], []).and_raise_error(ArgumentError, %r{'stdlib::ip_in_range' parameter 'ipaddress' expects a String value, got Array}) }
9+
it { is_expected.to run.with_params('1.1.1.1', 7).and_raise_error(ArgumentError, %r{'stdlib::ip_in_range' parameter 'range' expects a value of type String or Array, got Integer}) }
10+
end
11+
12+
describe 'basic validation inputs' do
13+
it { is_expected.to run.with_params('192.168.100.12', '192.168.100.0/24').and_return(true) }
14+
it { is_expected.to run.with_params('192.168.100.12', ['10.10.10.10/24', '192.168.100.0/24']).and_return(true) }
15+
it { is_expected.to run.with_params('10.10.10.10', '192.168.100.0/24').and_return(false) }
16+
end
17+
end

0 commit comments

Comments
 (0)