aws-ip-ranges 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c225665cec6c7205ff46219d77132011653a20cc5bfac94b3d74ad8ac9bbc424
4
+ data.tar.gz: be8c5ff22db519157d8a1d1d345a296347e0e307aa79ea2b2ef6f7784d7fd46c
5
+ SHA512:
6
+ metadata.gz: 275c52bcda6397d5ec9dcb9852fe53856c1858b39379ef521c8c2ed2bd7eb8204556185dc13b4d81285ee9199e9ba7ea73f3088d091d3e44fbaaf1b29806e7fc
7
+ data.tar.gz: 443e52964abfa8b5a899542c84e7d869abe14909f21eba4315d977da68c7c6deedc1de136ab4893869beea1f82743d4e2540e758ca201ed4702838fcca033c46
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Naming/FileName:
17
+ Exclude:
18
+ - lib/aws-ip-ranges.rb
19
+
20
+ Metrics/ClassLength:
21
+ Exclude:
22
+ - test/**/*.rb
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.6.9
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ ## [Unreleased]
2
+
3
+ - Add CLI and API to fetch and filter AWS IP ranges
4
+
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in aws-ip-ranges.gemspec
6
+ gemspec
7
+
8
+ gem "minitest", "~> 5.0"
9
+ gem "rack", "~> 2.2"
10
+ gem "rake", "~> 13.0"
11
+ gem "rubocop", "~> 1.7"
12
+ gem "sinatra", "~> 2.2"
13
+ gem "webrick", "~> 1.7"
14
+
15
+ gem "pry-byebug", "~> 3.9"
data/Gemfile.lock ADDED
@@ -0,0 +1,78 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ aws-ip-ranges (0.1.0)
5
+ dry-cli (~> 0.7.0)
6
+ faraday (~> 2.0)
7
+ faraday-retry (~> 1.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ast (2.4.2)
13
+ byebug (11.1.3)
14
+ coderay (1.1.3)
15
+ dry-cli (0.7.0)
16
+ faraday (2.2.0)
17
+ faraday-net_http (~> 2.0)
18
+ ruby2_keywords (>= 0.0.4)
19
+ faraday-net_http (2.0.1)
20
+ faraday-retry (1.0.3)
21
+ method_source (1.0.0)
22
+ minitest (5.15.0)
23
+ mustermann (1.1.1)
24
+ ruby2_keywords (~> 0.0.1)
25
+ parallel (1.22.1)
26
+ parser (3.1.1.0)
27
+ ast (~> 2.4.1)
28
+ pry (0.13.1)
29
+ coderay (~> 1.1)
30
+ method_source (~> 1.0)
31
+ pry-byebug (3.9.0)
32
+ byebug (~> 11.0)
33
+ pry (~> 0.13.0)
34
+ rack (2.2.3)
35
+ rack-protection (2.2.0)
36
+ rack
37
+ rainbow (3.1.1)
38
+ rake (13.0.6)
39
+ regexp_parser (2.2.1)
40
+ rexml (3.2.5)
41
+ rubocop (1.26.1)
42
+ parallel (~> 1.10)
43
+ parser (>= 3.1.0.0)
44
+ rainbow (>= 2.2.2, < 4.0)
45
+ regexp_parser (>= 1.8, < 3.0)
46
+ rexml
47
+ rubocop-ast (>= 1.16.0, < 2.0)
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (>= 1.4.0, < 3.0)
50
+ rubocop-ast (1.16.0)
51
+ parser (>= 3.1.1.0)
52
+ ruby-progressbar (1.11.0)
53
+ ruby2_keywords (0.0.5)
54
+ sinatra (2.2.0)
55
+ mustermann (~> 1.0)
56
+ rack (~> 2.2)
57
+ rack-protection (= 2.2.0)
58
+ tilt (~> 2.0)
59
+ tilt (2.0.10)
60
+ unicode-display_width (2.1.0)
61
+ webrick (1.7.0)
62
+
63
+ PLATFORMS
64
+ x86_64-darwin-20
65
+ x86_64-linux
66
+
67
+ DEPENDENCIES
68
+ aws-ip-ranges!
69
+ minitest (~> 5.0)
70
+ pry-byebug (~> 3.9)
71
+ rack (~> 2.2)
72
+ rake (~> 13.0)
73
+ rubocop (~> 1.7)
74
+ sinatra (~> 2.2)
75
+ webrick (~> 1.7)
76
+
77
+ BUNDLED WITH
78
+ 2.3.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Happydemics
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # `aws-ip-ranges`
2
+
3
+ Retrieve AWS IP ranges with ease.
4
+
5
+ ## Installation
6
+
7
+ Run:
8
+
9
+ ```sh
10
+ gem install aws-ip-ranges
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Code
16
+
17
+ ```ruby
18
+ AwsIpRanges.fetch(service: ['cloudfront', 's3']) # => [#<IPAddr: IPv4:3.2.34.0/255.255.255.192>, #<IPAddr: IPv4:3.5.140.0/255.255.252.0>]
19
+ AwsIpRanges.fetch(region: 'eu-west-3')
20
+ AwsIpRanges.fetch(only_ipv4: true)
21
+ AwsIpRanges.fetch(only_ipv6: true)
22
+ ```
23
+
24
+ ### CLI
25
+
26
+ Execute:
27
+
28
+ ```sh
29
+ $ aws-ip-ranges list
30
+ 35.172.155.192/27
31
+ 35.172.155.96/27
32
+ 44.192.134.240/28
33
+ ...
34
+ 2600:1f70:c000:400::/56
35
+ ```
36
+
37
+ See the help for more usage:
38
+
39
+ ```sh
40
+ Command:
41
+ aws-ip-ranges list
42
+
43
+ Usage:
44
+ aws-ip-ranges list
45
+
46
+ Description:
47
+ List all AWS IP ranges
48
+
49
+ Options:
50
+ --[no-]only-ipv6 # Whether to include only IPv6 ranges., default: false
51
+ --[no-]only-ipv4 # Whether to include only IPv4 ranges., default: false
52
+ --service=VALUE1,VALUE2,.. # Which services to include. By default, includes all.
53
+ --region=VALUE1,VALUE2,.. # Which regions to include. By default, includes all.
54
+ --help, -h # Print this help
55
+
56
+ Examples:
57
+ aws-ip-ranges list # Prints all AWS IP ranges
58
+ aws-ip-ranges list --only-ipv4 # Prints all AWS IPv4 ranges
59
+ aws-ip-ranges list --only-ipv6 --service=cloudfront,global # Prints AWS Cloudfront and Global IPv6 ranges
60
+ aws-ip-ranges list --only-ipv6 --service=s3 --region=eu-west-1 # Prints AWS S3 IPv6 ranges in the eu-west-1 region
61
+ ```
62
+
63
+ ## Development
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
68
+
69
+ ## Contributing
70
+
71
+ We are not accepting outside contribution on this library at the moment.
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "aws/ip/ranges"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/aws-ip-ranges ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "dry/cli"
5
+
6
+ require "aws_ip_ranges"
7
+
8
+ module AwsIpRanges
9
+ module CLI
10
+ # rubocop:disable Style/Documentation
11
+ module Commands
12
+ extend Dry::CLI::Registry
13
+
14
+ class Version < Dry::CLI::Command
15
+ desc "Print version"
16
+
17
+ def call(*)
18
+ puts AwsIpRanges::VERSION
19
+ end
20
+ end
21
+
22
+ class List < Dry::CLI::Command
23
+ desc "List all AWS IP ranges"
24
+
25
+ option :only_ipv6, type: :boolean, default: false, desc: "Whether to include only IPv6 ranges."
26
+ option :only_ipv4, type: :boolean, default: false, desc: "Whether to include only IPv4 ranges."
27
+ option :service, type: :array, default: nil, desc: "Which services to include. By default, includes all."
28
+ option :region, type: :array, default: nil, desc: "Which regions to include. By default, includes all."
29
+
30
+ example [
31
+ " # Prints all AWS IP ranges",
32
+ "--only-ipv4 # Prints all AWS IPv4 ranges",
33
+ "--only-ipv6 --service=cloudfront,global # Prints AWS Cloudfront and Global IPv6 ranges",
34
+ "--only-ipv6 --service=s3 --region=eu-west-1 # Prints AWS S3 IPv6 ranges in the eu-west-1 region"
35
+ ]
36
+
37
+ def call(only_ipv6: nil, only_ipv4: nil, service: nil, region: nil, **)
38
+ check_ip_version(only_ipv4, only_ipv6)
39
+
40
+ base_url = ENV["AWS_IP_RANGE_HOST_URL"] || AwsIpRanges::DEFAULT_HOST_URL
41
+ ips = AwsIpRanges.fetch(only_ipv6: only_ipv6, only_ipv4: only_ipv4, service: service, region: region,
42
+ base_url: base_url)
43
+ ips.each do |ip|
44
+ puts "#{ip}/#{ip.prefix}"
45
+ end
46
+ rescue StandardError => e
47
+ error 2, "Failed to load IP ranges: #{e}"
48
+ end
49
+
50
+ private
51
+
52
+ def check_ip_version(only_ipv4, only_ipv6)
53
+ return if !only_ipv4 || !only_ipv6
54
+
55
+ error(1, '"aws-ip-ranges list" cannot be called with both --only-ipv4 and --only-ipv6 options')
56
+ end
57
+
58
+ def error(status, msg)
59
+ warn "ERROR: #{msg}"
60
+ exit status
61
+ end
62
+ end
63
+
64
+ register "version", Version, aliases: ["v", "-v", "--version"]
65
+ register "list", List
66
+ end
67
+ # rubocop:enable Style/Documentation
68
+ end
69
+ end
70
+
71
+ Dry::CLI.new(AwsIpRanges::CLI::Commands).call
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "aws_ip_ranges"
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AwsIpRanges
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "faraday/net_http"
5
+ require "faraday/retry"
6
+
7
+ require "aws_ip_ranges/version"
8
+
9
+ Faraday.default_adapter = :net_http
10
+
11
+ #
12
+ # Retrieves the IP ranges from AWS.
13
+ #
14
+ module AwsIpRanges
15
+ DEFAULT_HOST_URL = "https://ip-ranges.amazonaws.com/"
16
+
17
+ class << self
18
+ #
19
+ # Fetches the latest list of IP ranges from AWS.
20
+ #
21
+ # @param [true,false] only_ipv6 whether to return only IP v6 ranges. Defaults to `false`.
22
+ # @param [true,false] only_ipv4 whether to return only IP v4 ranges. Defaults to `false`.
23
+ # @param [Array<String>,String] region a list of region to filter the ip ranges.
24
+ # @param [Array<String>,String] service a list of services to filter the ip ranges.
25
+ #
26
+ # @return [Array<IPAddr>] a list of IP ranges from AWS services.
27
+ #
28
+ def fetch(only_ipv6: false, only_ipv4: false, region: nil, service: nil, base_url: DEFAULT_HOST_URL)
29
+ ip_ranges = fetch_ip_ranges(base_url, only_ipv6: only_ipv6, only_ipv4: only_ipv4)
30
+ ip_ranges = filter_by_region(ip_ranges, region)
31
+ ip_ranges = filter_by_service(ip_ranges, service)
32
+ ip_ranges.map do |ip_range|
33
+ IPAddr.new(ip_range["ip_prefix"])
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def fetch_ip_ranges(base_url, only_ipv6:, only_ipv4:)
40
+ resp = http_client(base_url).get("/ip-ranges.json")
41
+ aws_ip_ranges_config = JSON.parse(resp.body)
42
+
43
+ ip_ranges = []
44
+ ip_ranges.concat(aws_ip_ranges_config["prefixes"]) if only_ipv4 || !only_ipv6
45
+ ip_ranges.concat(map_ipv6(aws_ip_ranges_config["ipv6_prefixes"])) if only_ipv6 || !only_ipv4
46
+ ip_ranges
47
+ end
48
+
49
+ def filter_by_region(ips, regions)
50
+ filter_by_key(ips, "region", regions)
51
+ end
52
+
53
+ def filter_by_service(ips, services)
54
+ filter_by_key(ips, "service", services)
55
+ end
56
+
57
+ def filter_by_key(ips, key, values)
58
+ return ips if values.nil?
59
+
60
+ values = Array(values)
61
+ values = values.map(&:downcase)
62
+ ips.filter { |config| values.include?(config[key].downcase) }
63
+ end
64
+
65
+ def map_ipv6(prefixes)
66
+ prefixes.each { |prefix| prefix["ip_prefix"] = prefix.delete("ipv6_prefix") }
67
+ end
68
+
69
+ def http_client(base_url)
70
+ Faraday.new(url: base_url) do |faraday|
71
+ faraday.options.timeout = 10
72
+ faraday.request :retry,
73
+ max: 5,
74
+ interval: 1,
75
+ interval_randomness: 0.5,
76
+ backoff_factor: 2
77
+ faraday.response :raise_error
78
+ faraday.adapter Faraday.default_adapter
79
+ end
80
+ end
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws-ip-ranges
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yohan Robert
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-10-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-cli
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday-retry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ description: Retrieve AWS IP ranges with ease.
56
+ email:
57
+ - yohan.robert@happydemics.com
58
+ executables:
59
+ - aws-ip-ranges
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".rubocop.yml"
64
+ - ".tool-versions"
65
+ - CHANGELOG.md
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - exe/aws-ip-ranges
74
+ - lib/aws-ip-ranges.rb
75
+ - lib/aws_ip_ranges.rb
76
+ - lib/aws_ip_ranges/version.rb
77
+ homepage: https://github.com/happydemics/aws-ip-ranges
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/happydemics/aws-ip-ranges
82
+ source_code_uri: https://github.com/happydemics/aws-ip-ranges
83
+ changelog_uri: https://github.com/happydemics/aws-ip-ranges/tree/master/CHANGELOG.md
84
+ rubygems_mfa_required: 'true'
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.6.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.0.3.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Retrieve AWS IP ranges with ease.
104
+ test_files: []