Skip to content

(MODULES-6366) Add data types for IP validation #872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,129 @@ Unacceptable input example:
foobar2001:db8::1
```

#### `Stdlib::IP::Address`

Matches any IP address, including both IPv4 and IPv6 addresses. It will
match them either with or without an address prefix as used in CIDR
format IPv4 addresses.

Examples:

```
'127.0.0.1' =~ Stdlib::IP::Address # true
'8.8.4.4' =~ Stdlib::IP::Address # true
'10.1.240.4/24' =~ Stdlib::IP::Address # true
'52.10.10.141' =~ Stdlib::IP::Address # true
'192.168.1' =~ Stdlib::IP::Address # false
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210' =~ Stdlib::IP::Address # true
'FF01:0:0:0:0:0:0:101' =~ Stdlib::IP::Address # true
'FF01::101' =~ Stdlib::IP::Address # true
'FF01:0:0:0:0:0:0:101/32' =~ Stdlib::IP::Address # true
'FF01::101/60' =~ Stdlib::IP::Address # true
'::' =~ Stdlib::IP::Address # true
'12AB::CD30:192.168.0.1' =~ Stdlib::IP::Address # true
```

#### `Stdlib::IP::Address::V4`

Match any string consisting of an IPv4 address in the quad-dotted
decimal format, with or without a CIDR prefix. It will not match any
abbreviated form (e.g., 192.168.1) because these are poorly documented
and inconsistently supported.

Examples:

```
'127.0.0.1' =~ Stdlib::IP::Address::V4 # true
'8.8.4.4' =~ Stdlib::IP::Address::V4 # true
'10.1.240.4/24' =~ Stdlib::IP::Address::V4 # true
'52.10.10.141' =~ Stdlib::IP::Address::V4 # true
'192.168.1' =~ Stdlib::IP::Address::V4 # false
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210' =~ Stdlib::IP::Address::V4 # false
'12AB::CD30:192.168.0.1' =~ Stdlib::IP::Address::V4 # false
```

#### `Stdlib::IP::Address::V6`

Match any string consistenting of an IPv6 address in any of the
documented formats in RFC 2373, with or without an address prefix.

Examples:

```
'127.0.0.1' =~ Stdlib::IP::Address::V6 # false
'10.1.240.4/24' =~ Stdlib::IP::Address::V6 # false
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210' =~ Stdlib::IP::Address::V6 # true
'FF01:0:0:0:0:0:0:101' =~ Stdlib::IP::Address::V6 # true
'FF01::101' =~ Stdlib::IP::Address::V6 # true
'FF01:0:0:0:0:0:0:101/32' =~ Stdlib::IP::Address::V6 # true
'FF01::101/60' =~ Stdlib::IP::Address::V6 # true
'::' =~ Stdlib::IP::Address::V6 # true
'12AB::CD30:192.168.0.1' =~ Stdlib::IP::Address::V6 # true
```

#### `Stdlib::IP::Address::Nosubnet`

Match the same things as the `Stdlib::IP::Address` alias, except it will not
match an address that includes an address prefix (e.g., it will match
`192.168.0.6` but not `192.168.0.6/24`).

#### `Stdlib::IP::Address::V4::CIDR`

Match an IPv4 address in the CIDR format. It will only match if the
address contains an address prefix (e.g., it will match `192.168.0.6/24`
but not `192.168.0.6`).

#### `Stdlib::IP::Address::V4::Nosubnet`

Match an IPv4 address only if the address does not contain an address
prefix (e.g., it will match `192.168.0.6` but not `192.168.0.6/24`).

#### `Stdlib::IP::Address::V6::Full`

Match an IPv6 address formatted in the "preferred form" as documented in
section 2.2.1 of RFC 2373, with or without an address prefix as
documented in section 2.3 of RFC 2373.

#### `Stdlib::IP::Address::V6::Alternate`

Match an IPv6 address formatted in the "alternative form" allowing for
representing the last two 16-bit pieces of the address with a
quad-dotted decimal, as documented in section 2.2.1 of RFC 2373. It will
match addresses with or without an address prefix as documented in
section 2.3 of RFC 2373.

#### `Stdlib::IP::Address::V6::Compressed`

Match an IPv6 address which may contain `::` used to compress zeros as
documented in section 2.2.2 of RFC 2373. It will match addresses with
or without an address prefix as documented in section 2.3 of RFC 2373.

#### `Stdlib::IP::Address::V6::Nosubnet`

Alias to allow `Stdlib::IP::Address::V6::Nosubnet::Full`,
`Stdlib::IP::Address::V6::Nosubnet::Alternate` and
`Stdlib::IP::Address::V6::Nosubnet::Compressed`.

#### `Stdlib::IP::Address::V6::Nosubnet::Full`

Match an IPv6 address formatted in the "preferred form" as documented in
section 2.2.1 of RFC 2373. It will not match addresses with address
prefix as documented in section 2.3 of RFC 2373.

#### `Stdlib::IP::Address::V6::Nosubnet::Alternate`

Match an IPv6 address formatted in the "alternative form" allowing for
representing the last two 16-bit pieces of the address with a
quad-dotted decimal, as documented in section 2.2.1 of RFC 2373. It will
only match addresses without an address prefix as documented in section
2.3 of RFC 2373.

#### `Stdlib::IP::Address::V6::Nosubnet::Compressed`

Match an IPv6 address which may contain `::` used to compress zeros as
documented in section 2.2.2 of RFC 2373. It will only match addresses
without an address prefix as documented in section 2.3 of RFC 2373.

### Facts

Expand Down
44 changes: 44 additions & 0 deletions spec/type_aliases/ip_address_nosubnet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'spec_helper'

if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
describe 'Stdlib::IP::Address::Nosubnet' do
describe 'accepts ipv4 and ipv6 addresses without subnets' do
[
'224.0.0.0',
'255.255.255.255',
'0.0.0.0',
'192.88.99.0',
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
'fa76:8765:34ac:0823:ab76:eee9:0987:1111',
'127.0.0.1',
'8.8.4.4',
'52.10.10.141',
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
'FF01:0:0:0:0:0:0:101',
'FF01::101',
'::',
'12AB::CD30:192.168.0.1',
].each do |value|
describe value.inspect do
it { is_expected.to allow_value(value) }
end
end
end

describe 'rejects other values' do
[
'10.1.240.4/24',
'FF01:0:0:0:0:0:0:101/32',
'FF01::101/60',
'nope',
'77',
'4.4.4',
'2001:0db8:85a3:000000:0000:8a2e:0370:7334',
].each do |value|
describe value.inspect do
it { is_expected.not_to allow_value(value) }
end
end
end
end
end
44 changes: 44 additions & 0 deletions spec/type_aliases/ip_address_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'spec_helper'

if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
describe 'Stdlib::IP::Address' do
describe 'accepts ipv4 and ipv6 addresses' do
[
'224.0.0.0',
'255.255.255.255',
'0.0.0.0',
'192.88.99.0',
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
'fa76:8765:34ac:0823:ab76:eee9:0987:1111',
'127.0.0.1',
'8.8.4.4',
'10.1.240.4/24',
'52.10.10.141',
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
'FF01:0:0:0:0:0:0:101',
'FF01::101',
'FF01:0:0:0:0:0:0:101/32',
'FF01::101/60',
'::',
'12AB::CD30:192.168.0.1',
].each do |value|
describe value.inspect do
it { is_expected.to allow_value(value) }
end
end
end

describe 'rejects other values' do
[
'nope',
'77',
'4.4.4',
'2001:0db8:85a3:000000:0000:8a2e:0370:7334',
].each do |value|
describe value.inspect do
it { is_expected.not_to allow_value(value) }
end
end
end
end
end
30 changes: 30 additions & 0 deletions spec/type_aliases/ip_address_v4_nosubnet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'

if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
describe 'Stdlib::IP::Address::V4::Nosubnet' do
describe 'accepts ipv4 addresses without subnets' do
[
'127.0.0.1',
'8.8.4.4',
'52.10.10.141',
].each do |value|
describe value.inspect do
it { is_expected.to allow_value(value) }
end
end
end

describe 'rejects other values' do
[
'10.1.240.4/24',
'192.168.1',
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
'12AB::CD30:192.168.0.1',
].each do |value|
describe value.inspect do
it { is_expected.not_to allow_value(value) }
end
end
end
end
end
30 changes: 30 additions & 0 deletions spec/type_aliases/ip_address_v4_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'

if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
describe 'Stdlib::IP::Address::V4' do
describe 'accepts ipv4 addresses' do
[
'127.0.0.1',
'8.8.4.4',
'10.1.240.4/24',
'52.10.10.141',
].each do |value|
describe value.inspect do
it { is_expected.to allow_value(value) }
end
end
end

describe 'rejects other values' do
[
'192.168.1',
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
'12AB::CD30:192.168.0.1',
].each do |value|
describe value.inspect do
it { is_expected.not_to allow_value(value) }
end
end
end
end
end
28 changes: 28 additions & 0 deletions spec/type_aliases/ip_address_v6_alternative_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
describe 'Stdlib::IP::Address::V6::Alternative' do
describe 'accepts ipv6 addresses in alternative format' do
[
'0:0:0:0:0:0:13.1.68.3',
'0:0:0:0:0:FFFF:129.144.52.38',
'0:0:0:0:0:FFFF:129.144.52.38/60',
].each do |value|
describe value.inspect do
it { is_expected.to allow_value(value) }
end
end
end

describe 'rejects other values' do
[
'nope',
'127.0.0.1',
].each do |value|
describe value.inspect do
it { is_expected.not_to allow_value(value) }
end
end
end
end
end
31 changes: 31 additions & 0 deletions spec/type_aliases/ip_address_v6_compressed_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'

if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
describe 'Stdlib::IP::Address::V6::Compressed' do
describe 'accepts ipv6 addresses in compressed format' do
[
'1080::8:800:200C:417A',
'1080::8:800:200C:417A/60',
'FF01::101',
'::1',
'::',
].each do |value|
describe value.inspect do
it { is_expected.to allow_value(value) }
end
end
end

describe 'rejects other values' do
[
'nope',
'127.0.0.1',
'FEDC::BA98:7654:3210::3210',
].each do |value|
describe value.inspect do
it { is_expected.not_to allow_value(value) }
end
end
end
end
end
28 changes: 28 additions & 0 deletions spec/type_aliases/ip_address_v6_full_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
describe 'Stdlib::IP::Address::V6::Full' do
describe 'accepts ipv6 addresses in full format' do
[
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210/60',
'1080:0:0:0:8:800:200C:417A',
].each do |value|
describe value.inspect do
it { is_expected.to allow_value(value) }
end
end
end

describe 'rejects other values' do
[
'nope',
'127.0.0.1',
].each do |value|
describe value.inspect do
it { is_expected.not_to allow_value(value) }
end
end
end
end
end
Loading