AWS-IP
view release on metacpan or search on metacpan
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
Appendix: How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to humanity, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.
To do so, attach the following notices to the program. It is safest to
attach them to the start of each source file to most effectively convey
the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
"HTTP::Tiny" : "0",
"IO::Socket::SSL" : "1.56",
"JSON::XS" : "0",
"Net::CIDR::Set" : "0",
"Net::SSLeay" : "1.49",
"constant" : "0",
"strict" : "0",
"warnings" : "0"
}
},
"test" : {
"requires" : {
"Test::More" : "0"
}
}
},
"release_status" : "stable",
"version" : 0.04,
"x_serialization_backend" : "JSON::PP version 2.27400_02"
}
Makefile.PL view on Meta::CPAN
"JSON::XS" => 0,
"Net::CIDR::Set" => 0,
"Net::SSLeay" => "1.49",
"constant" => 0,
"strict" => 0,
"warnings" => 0
},
"TEST_REQUIRES" => {
"Test::More" => 0
},
"test" => {
"TESTS" => "t/*.t"
}
);
my %FallbackPrereqs = (
"Cache::File" => 0,
"Carp" => 0,
"File::Temp" => 0,
"HTTP::Tiny" => 0,
=head2 new ($cache_timeout_secs, [$cache_path])
Creates a new AWS::IP object and sets up the cache. Requires an number for the cache timeout seconds. Optionally takes a cache path argument. If no cache path is supplied, AWS::IP will use a random temp directory. If you want to reuse the cache over ...
=cut
=head2 ip_is_aws ($ip, [$service])
Boolean method to test if an ip address is from AWS. Optionally takes a service name (AMAZON|EC2|CLOUDFRONT|ROUTE53|ROUTE53_HEALTHCHECKS) and restricts the check to AWS ip addresses for that service.
If you are checking more than one ip address, it's more efficient to pull the CIDRs you want, then use L<Net::CIDR::Set> to test if the ips are present in the CIDRs (see example in SYNOPSIS).
=cut
=head2 get_raw_data
Returns the entire raw IP dataset as a Perl data structure.
=cut
Returns an arrayref of the services (Amazon, EC2 etc) in the AWS IP address data.
=cut
=head2 SEE ALSO
L<AWS::Networks> - is similar to this module but does not provide cacheing.
Amazon's L<page|http://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html> on AWS IP ranges.
=cut
lib/AWS/IP.pm view on Meta::CPAN
bless {
cache => Cache::File->new( cache_root => ($cache_path || tempdir()),
lock_level => Cache::File::LOCK_LOCAL(),
default_expires => "$cache_timeout_secs sec"),
}, $class;
}
=head2 ip_is_aws ($ip, [$service])
Boolean method to test if an ip address is from AWS. Optionally takes a service name (AMAZON|EC2|CLOUDFRONT|ROUTE53|ROUTE53_HEALTHCHECKS) and restricts the check to AWS ip addresses for that service.
If you are checking more than one ip address, it's more efficient to pull the CIDRs you want, then use L<Net::CIDR::Set> to test if the ips are present in the CIDRs (see example in SYNOPSIS).
=cut
sub ip_is_aws
{
my ($self, $ip, $service) = @_;
croak 'Error must supply an ip address' unless $ip;
my $ip_ranges;
lib/AWS/IP.pm view on Meta::CPAN
{
$services{ $_->{service} } = 1;
}
[ keys %services ];
}
=head2 SEE ALSO
L<AWS::Networks> - is similar to this module but does not provide cacheing.
Amazon's L<page|http://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html> on AWS IP ranges.
=cut
sub _refresh_cache
{
my ($self) = @_;
my $response = HTTP::Tiny->new->get('https://ip-ranges.amazonaws.com/ip-ranges.json');
use strict;
use warnings;
use Test::More;
my $test_data = do {
open my $test_fh, '<', 't/ip-ranges.json' or die $!;
local $/;
<$test_fh>;
};
# loads ok
use_ok('AWS::IP', 'load module');
ok my $aws = AWS::IP->new(600), 'constructor';
ok $aws->_refresh_cache_from_string($test_data), 'refresh cache';
# ip range checks
ok $aws->ip_is_aws('50.19.0.1'), 'ip 50.19.0.1 is found in AWS range';
ok $aws->ip_is_aws('54.239.98.0', 'AMAZON'), 'ip 54.239.98.0 is found in AMAZON AWS range';
ok !$aws->ip_is_aws('54.239.98.0', 'EC2'), 'ip 54.239.98.0 is not found in EC2 AWS range';
# counts
is 383, @{$aws->get_cidrs}, '383 CIDRs are present';
is 5, @{$aws->get_services}, '5 services are present';
is 12, @{$aws->get_regions}, '12 regions are present';
# cache expiry
ok my $aws_2 = AWS::IP->new(1), 'constructor with 1 second cache';
ok $aws->_refresh_cache_from_string($test_data), 'refresh cache';
sleep(2); # let cache expire
ok !$aws_2->{cache}->entry('AWS_IPS')->exists, 'Entry no longer exists';
ok !$aws_2->{cache}->entry('AWS_IPS')->get, 'Data is no longer cached';
done_testing;
( run in 0.315 second using v1.01-cache-2.11-cpan-87723dcf8b7 )