Skip to content
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

[#20862] Add functions to validate ipv4 and ipv6 addresses #158

Merged
merged 4 commits into from
Jun 7, 2013
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/puppet/parser/functions/validate_ipv4_address.rb
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
49 changes: 49 additions & 0 deletions lib/puppet/parser/functions/validate_ipv6_address.rb
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 spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb
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 spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb
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
Copy link
Contributor

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?

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