-
Notifications
You must be signed in to change notification settings - Fork 583
[#20862] Add functions to validate ipv4 and ipv6 addresses #158
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Puppet::Parser::Functions | ||
|
||
newfunction(:validate_ipv4_address, :doc => <<-ENDHEREDOC | ||
Validate that all values passed are valid IPv4 addresses. | ||
Fail compilation if any value fails this check. | ||
|
||
The following values will pass: | ||
|
||
$my_ip = "1.2.3.4" | ||
validate_ipv4_address($my_ip) | ||
validate_bool("8.8.8.8", "172.16.0.1", $my_ip) | ||
|
||
The following values will fail, causing compilation to abort: | ||
|
||
$some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ] | ||
validate_ipv4_address($some_array) | ||
|
||
ENDHEREDOC | ||
) do |args| | ||
|
||
require "ipaddr" | ||
rescuable_exceptions = [ ArgumentError ] | ||
|
||
if defined?(IPAddr::InvalidAddressError) | ||
rescuable_exceptions << IPAddr::InvalidAddressError | ||
end | ||
|
||
unless args.length > 0 then | ||
raise Puppet::ParseError, ("validate_ipv4_address(): wrong number of arguments (#{args.length}; must be > 0)") | ||
end | ||
|
||
args.each do |arg| | ||
unless arg.is_a?(String) | ||
raise Puppet::ParseError, "#{arg.inspect} is not a string." | ||
end | ||
|
||
begin | ||
unless IPAddr.new(arg).ipv4? | ||
raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address." | ||
end | ||
rescue *rescuable_exceptions | ||
raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address." | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Puppet::Parser::Functions | ||
|
||
newfunction(:validate_ipv6_address, :doc => <<-ENDHEREDOC | ||
Validate that all values passed are valid IPv6 addresses. | ||
Fail compilation if any value fails this check. | ||
|
||
The following values will pass: | ||
|
||
$my_ip = "3ffe:505:2" | ||
validate_ipv6_address(1) | ||
validate_ipv6_address($my_ip) | ||
validate_bool("fe80::baf6:b1ff:fe19:7507", $my_ip) | ||
|
||
The following values will fail, causing compilation to abort: | ||
|
||
$some_array = [ true, false, "garbage string", "1.2.3.4" ] | ||
validate_ipv6_address($some_array) | ||
|
||
ENDHEREDOC | ||
) do |args| | ||
|
||
require "ipaddr" | ||
rescuable_exceptions = [ ArgumentError ] | ||
|
||
if defined?(IPAddr::InvalidAddressError) | ||
rescuable_exceptions << IPAddr::InvalidAddressError | ||
end | ||
|
||
unless args.length > 0 then | ||
raise Puppet::ParseError, ("validate_ipv6_address(): wrong number of arguments (#{args.length}; must be > 0)") | ||
end | ||
|
||
args.each do |arg| | ||
unless arg.is_a?(String) | ||
raise Puppet::ParseError, "#{arg.inspect} is not a string." | ||
end | ||
|
||
begin | ||
unless IPAddr.new(arg).ipv6? | ||
raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address." | ||
end | ||
rescue *rescuable_exceptions | ||
raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address." | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
64 changes: 64 additions & 0 deletions
64
spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#! /usr/bin/env/ruby -S rspec | ||
|
||
require "spec_helper" | ||
|
||
describe Puppet::Parser::Functions.function(:validate_ipv4_address) do | ||
let(:scope) { PuppetlabsSpec::PuppetInternals.scope } | ||
|
||
describe "when calling validate_ipv4_address from puppet" do | ||
describe "when given IPv4 address strings" do | ||
it "should compile with one argument" do | ||
Puppet[:code] = "validate_ipv4_address('1.2.3.4')" | ||
scope.compiler.compile | ||
end | ||
|
||
it "should compile with multiple arguments" do | ||
Puppet[:code] = "validate_ipv4_address('1.2.3.4', '5.6.7.8')" | ||
scope.compiler.compile | ||
end | ||
end | ||
|
||
describe "when given an IPv6 address" do | ||
it "should not compile" do | ||
Puppet[:code] = "validate_ipv4_address('3ffe:505')" | ||
expect { | ||
scope.compiler.compile | ||
}.to raise_error(Puppet::ParseError, /not a valid IPv4 address/) | ||
end | ||
end | ||
|
||
describe "when given other strings" do | ||
it "should not compile" do | ||
Puppet[:code] = "validate_ipv4_address('hello', 'world')" | ||
expect { | ||
scope.compiler.compile | ||
}.to raise_error(Puppet::ParseError, /not a valid IPv4 address/) | ||
end | ||
end | ||
|
||
describe "when given numbers" do | ||
it "should not compile" do | ||
Puppet[:code] = "validate_ipv4_address(1, 2)" | ||
expect { | ||
scope.compiler.compile | ||
}.to raise_error(Puppet::ParseError, /is not a valid IPv4 address/) | ||
end | ||
end | ||
|
||
describe "when given booleans" do | ||
it "should not compile" do | ||
Puppet[:code] = "validate_ipv4_address(true, false)" | ||
expect { | ||
scope.compiler.compile | ||
}.to raise_error(Puppet::ParseError, /is not a string/) | ||
end | ||
end | ||
|
||
it "should not compile when no arguments are passed" do | ||
Puppet[:code] = "validate_ipv4_address()" | ||
expect { | ||
scope.compiler.compile | ||
}.to raise_error(Puppet::ParseError, /wrong number of arguments/) | ||
end | ||
end | ||
end |
67 changes: 67 additions & 0 deletions
67
spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#! /usr/bin/env/ruby -S rspec | ||
|
||
require "spec_helper" | ||
|
||
describe Puppet::Parser::Functions.function(:validate_ipv6_address) do | ||
let(:scope) { PuppetlabsSpec::PuppetInternals.scope } | ||
|
||
describe "when calling validate_ipv6_address from puppet" do | ||
describe "when given IPv6 address strings" do | ||
it "should compile with one argument" do | ||
Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::')" | ||
scope.compiler.compile | ||
end | ||
|
||
it "should compile with multiple arguments" do | ||
Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::', '3ffe:0505:0001::')" | ||
scope.compiler.compile | ||
end | ||
end | ||
|
||
describe "when given an ipv4 address" do | ||
it "should not compile" do | ||
Puppet[:code] = "validate_ipv6_address('1.2.3.4')" | ||
expect { | ||
scope.compiler.compile | ||
}.to raise_error(Puppet::ParseError, /not a valid IPv6 address/) | ||
end | ||
end | ||
|
||
describe "when given other strings" do | ||
it "should not compile" do | ||
Puppet[:code] = "validate_ipv6_address('hello', 'world')" | ||
expect { | ||
scope.compiler.compile | ||
}.to raise_error(Puppet::ParseError, /not a valid IPv6 address/) | ||
end | ||
end | ||
|
||
# 1.8.7 is EOL'd and also absolutely insane about ipv6 | ||
unless RUBY_VERSION == '1.8.7' | ||
describe "when given numbers" do | ||
it "should not compile" do | ||
Puppet[:code] = "validate_ipv6_address(1, 2)" | ||
expect { | ||
scope.compiler.compile | ||
}.to raise_error(Puppet::ParseError, /not a valid IPv6 address/) | ||
end | ||
end | ||
end | ||
|
||
describe "when given booleans" do | ||
it "should not compile" do | ||
Puppet[:code] = "validate_ipv6_address(true, false)" | ||
expect { | ||
scope.compiler.compile | ||
}.to raise_error(Puppet::ParseError, /is not a string/) | ||
end | ||
end | ||
|
||
it "should not compile when no arguments are passed" do | ||
Puppet[:code] = "validate_ipv6_address()" | ||
expect { | ||
scope.compiler.compile | ||
}.to raise_error(Puppet::ParseError, /wrong number of arguments/) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the behavior of ruby 1.8.7 and ipv6? Do we need to add a note about this in the method documentation?