Skip to content

Commit 494386a

Browse files
committed
(MODULES-6366) Add data types for IP validation
The types are from https://github.com/thrnio/puppet-ip which was released under the Apache-2.0 license.
1 parent b0d99ad commit 494386a

25 files changed

+545
-0
lines changed

README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,130 @@ Unacceptable input example:
465465
bob@example.com
466466
```
467467

468+
#### `Stdlib::IP::Address`
469+
470+
Matches any IP address, including both IPv4 and IPv6 addresses. It will
471+
match them either with or without an address prefix as used in CIDR
472+
format IPv4 addresses.
473+
474+
Examples:
475+
476+
```
477+
'127.0.0.1' =~ Stdlib::IP::Address # true
478+
'8.8.4.4' =~ Stdlib::IP::Address # true
479+
'10.1.240.4/24' =~ Stdlib::IP::Address # true
480+
'52.10.10.141' =~ Stdlib::IP::Address # true
481+
'192.168.1' =~ Stdlib::IP::Address # false
482+
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210' =~ Stdlib::IP::Address # true
483+
'FF01:0:0:0:0:0:0:101' =~ Stdlib::IP::Address # true
484+
'FF01::101' =~ Stdlib::IP::Address # true
485+
'FF01:0:0:0:0:0:0:101/32' =~ Stdlib::IP::Address # true
486+
'FF01::101/60' =~ Stdlib::IP::Address # true
487+
'::' =~ Stdlib::IP::Address # true
488+
'12AB::CD30:192.168.0.1' =~ Stdlib::IP::Address # true
489+
```
490+
491+
#### `Stdlib::IP::Address::V4`
492+
493+
Match any string consisting of an IPv4 address in the quad-dotted
494+
decimal format, with or without a CIDR prefix. It will not match any
495+
abbreviated form (e.g., 192.168.1) because these are poorly documented
496+
and inconsistently supported.
497+
498+
Examples:
499+
500+
```
501+
'127.0.0.1' =~ Stdlib::IP::Address::V4 # true
502+
'8.8.4.4' =~ Stdlib::IP::Address::V4 # true
503+
'10.1.240.4/24' =~ Stdlib::IP::Address::V4 # true
504+
'52.10.10.141' =~ Stdlib::IP::Address::V4 # true
505+
'192.168.1' =~ Stdlib::IP::Address::V4 # false
506+
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210' =~ Stdlib::IP::Address::V4 # false
507+
'12AB::CD30:192.168.0.1' =~ Stdlib::IP::Address::V4 # false
508+
```
509+
510+
#### `Stdlib::IP::Address::V6`
511+
512+
Match any string consistenting of an IPv6 address in any of the
513+
documented formats in RFC 2373, with or without an address prefix.
514+
515+
Examples:
516+
517+
```
518+
'127.0.0.1' =~ Stdlib::IP::Address::V6 # false
519+
'10.1.240.4/24' =~ Stdlib::IP::Address::V6 # true
520+
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210' =~ Stdlib::IP::Address::V6 # true
521+
'FF01:0:0:0:0:0:0:101' =~ Stdlib::IP::Address::V6 # true
522+
'FF01::101' =~ Stdlib::IP::Address::V6 # true
523+
'FF01:0:0:0:0:0:0:101/32' =~ Stdlib::IP::Address::V6 # true
524+
'FF01::101/60' =~ Stdlib::IP::Address::V6 # true
525+
'::' =~ Stdlib::IP::Address::V6 # true
526+
'12AB::CD30:192.168.0.1' =~ Stdlib::IP::Address::V6 # true
527+
```
528+
529+
#### `Stdlib::IP::Address::Nosubnet`
530+
531+
Match the same things as the `Stdlib::IP::Address` alias, except it will not
532+
match an address that includes an address prefix (e.g., it will match
533+
`192.168.0.6` but not `192.168.0.6/24`).
534+
535+
#### `Stdlib::IP::Address::V4::CIDR`
536+
537+
Match an IPv4 address in the CIDR format. It will only match if the
538+
address contains an address prefix (e.g., it will match `192.168.0.6/24`
539+
but not `192.168.0.6`).
540+
541+
#### `Stdlib::IP::Address::V4::Nosubnet`
542+
543+
Match an IPv4 address only if the address does not contain an address
544+
prefix (e.g., it will match `192.168.0.6` but not `192.168.0.6/24`).
545+
546+
#### `Stdlib::IP::Address::V6::Full`
547+
548+
Match an IPv6 address formatted in the "preferred form" as documented in
549+
section 2.2.1 of RFC 2373, with or without an address prefix as
550+
documented in section 2.3 of RFC 2373.
551+
552+
#### `Stdlib::IP::Address::V6::Alternate`
553+
554+
Match an IPv6 address formatted in the "alternative form" allowing for
555+
representing the last two 16-bit pieces of the address with a
556+
quad-dotted decimal, as documented in section 2.2.1 of RFC 2373. It will
557+
match addresses with or without an address prefix as documented in
558+
section 2.3 of RFC 2373.
559+
560+
#### `Stdlib::IP::Address::V6::Compressed`
561+
562+
Match an IPv6 address which may contain `::` used to compress zeros as
563+
documented in section 2.2.2 of RFC 2373. It will match addresses with
564+
or without an address prefix as documented in section 2.3 of RFC 2373.
565+
566+
#### `Stdlib::IP::Address::V6::Nosubnet`
567+
568+
Alias to allow `Stdlib::IP::Address::V6::Nosubnet::Full`,
569+
`Stdlib::IP::Address::V6::Nosubnet::Alternate` and
570+
`Stdlib::IP::Address::V6::Nosubnet::Compressed`.
571+
572+
#### `Stdlib::IP::Address::V6::Nosubnet::Full`
573+
574+
Match an IPv6 address formatted in the "preferred form" as documented in
575+
section 2.2.1 of RFC 2373. It will not match addresses with address
576+
prefix as documented in section 2.3 of RFC 2373.
577+
578+
#### `Stdlib::IP::Address::V6::Nosubnet::Alternate`
579+
580+
Match an IPv6 address formatted in the "alternative form" allowing for
581+
representing the last two 16-bit pieces of the address with a
582+
quad-dotted decimal, as documented in section 2.2.1 of RFC 2373. It will
583+
only match addresses without an address prefix as documented in section
584+
2.3 of RFC 2373.
585+
586+
#### `Stdlib::IP::Address::V6::Nosubnet::Compressed`
587+
588+
Match an IPv6 address which may contain `::` used to compress zeros as
589+
documented in section 2.2.2 of RFC 2373. It will only match addresses
590+
without an address prefix as documented in section 2.3 of RFC 2373.
591+
468592
### Facts
469593
470594
#### `package_provider`
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'spec_helper'
2+
3+
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4+
describe 'Stdlib::IP::Address::Nosubnet' do
5+
describe 'accepts ipv4 and ipv6 addresses without subnets' do
6+
[
7+
'224.0.0.0',
8+
'255.255.255.255',
9+
'0.0.0.0',
10+
'192.88.99.0',
11+
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
12+
'fa76:8765:34ac:0823:ab76:eee9:0987:1111',
13+
'127.0.0.1',
14+
'8.8.4.4',
15+
'52.10.10.141',
16+
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
17+
'FF01:0:0:0:0:0:0:101',
18+
'FF01::101',
19+
'::',
20+
'12AB::CD30:192.168.0.1',
21+
].each do |value|
22+
describe value.inspect do
23+
it { is_expected.to allow_value(value) }
24+
end
25+
end
26+
end
27+
28+
describe 'rejects other values' do
29+
[
30+
'10.1.240.4/24',
31+
'FF01:0:0:0:0:0:0:101/32',
32+
'FF01::101/60',
33+
'nope',
34+
'77',
35+
'4.4.4',
36+
'2001:0db8:85a3:000000:0000:8a2e:0370:7334',
37+
].each do |value|
38+
describe value.inspect do
39+
it { is_expected.not_to allow_value(value) }
40+
end
41+
end
42+
end
43+
end
44+
end

spec/type_aliases/ip_address_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'spec_helper'
2+
3+
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4+
describe 'Stdlib::IP::Address' do
5+
describe 'accepts ipv4 and ipv6 addresses' do
6+
[
7+
'224.0.0.0',
8+
'255.255.255.255',
9+
'0.0.0.0',
10+
'192.88.99.0',
11+
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
12+
'fa76:8765:34ac:0823:ab76:eee9:0987:1111',
13+
'127.0.0.1',
14+
'8.8.4.4',
15+
'10.1.240.4/24',
16+
'52.10.10.141',
17+
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
18+
'FF01:0:0:0:0:0:0:101',
19+
'FF01::101',
20+
'FF01:0:0:0:0:0:0:101/32',
21+
'FF01::101/60',
22+
'::',
23+
'12AB::CD30:192.168.0.1',
24+
].each do |value|
25+
describe value.inspect do
26+
it { is_expected.to allow_value(value) }
27+
end
28+
end
29+
end
30+
31+
describe 'rejects other values' do
32+
[
33+
'nope',
34+
'77',
35+
'4.4.4',
36+
'2001:0db8:85a3:000000:0000:8a2e:0370:7334',
37+
].each do |value|
38+
describe value.inspect do
39+
it { is_expected.not_to allow_value(value) }
40+
end
41+
end
42+
end
43+
end
44+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'spec_helper'
2+
3+
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4+
describe 'Stdlib::IP::Address::V4::Nosubnet' do
5+
describe 'accepts ipv4 addresses without subnets' do
6+
[
7+
'127.0.0.1',
8+
'8.8.4.4',
9+
'52.10.10.141',
10+
].each do |value|
11+
describe value.inspect do
12+
it { is_expected.to allow_value(value) }
13+
end
14+
end
15+
end
16+
17+
describe 'rejects other values' do
18+
[
19+
'10.1.240.4/24',
20+
'192.168.1',
21+
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
22+
'12AB::CD30:192.168.0.1',
23+
].each do |value|
24+
describe value.inspect do
25+
it { is_expected.not_to allow_value(value) }
26+
end
27+
end
28+
end
29+
end
30+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'spec_helper'
2+
3+
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4+
describe 'Stdlib::IP::Address::V4' do
5+
describe 'accepts ipv4 addresses' do
6+
[
7+
'127.0.0.1',
8+
'8.8.4.4',
9+
'10.1.240.4/24',
10+
'52.10.10.141',
11+
].each do |value|
12+
describe value.inspect do
13+
it { is_expected.to allow_value(value) }
14+
end
15+
end
16+
end
17+
18+
describe 'rejects other values' do
19+
[
20+
'192.168.1',
21+
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
22+
'12AB::CD30:192.168.0.1',
23+
].each do |value|
24+
describe value.inspect do
25+
it { is_expected.not_to allow_value(value) }
26+
end
27+
end
28+
end
29+
end
30+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'spec_helper'
2+
3+
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4+
describe 'Stdlib::IP::Address::V6::Alternative' do
5+
describe 'accepts ipv6 addresses in alternative format' do
6+
[
7+
'0:0:0:0:0:0:13.1.68.3',
8+
'0:0:0:0:0:FFFF:129.144.52.38',
9+
'0:0:0:0:0:FFFF:129.144.52.38/60',
10+
].each do |value|
11+
describe value.inspect do
12+
it { is_expected.to allow_value(value) }
13+
end
14+
end
15+
end
16+
17+
describe 'rejects other values' do
18+
[
19+
'nope',
20+
'127.0.0.1',
21+
].each do |value|
22+
describe value.inspect do
23+
it { is_expected.not_to allow_value(value) }
24+
end
25+
end
26+
end
27+
end
28+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'spec_helper'
2+
3+
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4+
describe 'Stdlib::IP::Address::V6::Compressed' do
5+
describe 'accepts ipv6 addresses in compressed format' do
6+
[
7+
'::13.1.68.3',
8+
'::FFFF:129.144.52.38',
9+
'1080::8:800:200C:417A',
10+
'1080::8:800:200C:417A/60',
11+
'FF01::101',
12+
'::1',
13+
'::',
14+
].each do |value|
15+
describe value.inspect do
16+
it { is_expected.to allow_value(value) }
17+
end
18+
end
19+
end
20+
21+
describe 'rejects other values' do
22+
[
23+
'nope',
24+
'127.0.0.1',
25+
'FEDC::BA98:7654:3210::3210',
26+
].each do |value|
27+
describe value.inspect do
28+
it { is_expected.not_to allow_value(value) }
29+
end
30+
end
31+
end
32+
end
33+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'spec_helper'
2+
3+
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
4+
describe 'Stdlib::IP::Address::V6::Full' do
5+
describe 'accepts ipv6 addresses in full format' do
6+
[
7+
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
8+
'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210/60',
9+
'1080:0:0:0:8:800:200C:417A',
10+
].each do |value|
11+
describe value.inspect do
12+
it { is_expected.to allow_value(value) }
13+
end
14+
end
15+
end
16+
17+
describe 'rejects other values' do
18+
[
19+
'nope',
20+
'127.0.0.1',
21+
].each do |value|
22+
describe value.inspect do
23+
it { is_expected.not_to allow_value(value) }
24+
end
25+
end
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)