Skip to content

Port Cloudflare Updates #3

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 11 commits into from
Aug 27, 2014
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.gem
/*.gem
*.lock
.bundle
vendor/gems
/bin
13 changes: 13 additions & 0 deletions config/cloudflare-ips.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
199.27.128.0/21
173.245.48.0/20
103.21.244.0/22
103.22.200.0/22
103.31.4.0/22
141.101.64.0/18
108.162.192.0/18
190.93.240.0/20
188.114.96.0/20
197.234.240.0/22
198.41.128.0/17
162.158.0.0/15
104.16.0.0/12
3 changes: 2 additions & 1 deletion github-pages-health-check.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Gem::Specification.new do |s|
s.license = "MIT"
s.files = [
"lib/github-pages-health-check.rb",
"lib/github-pages-health-check/version.rb"
"lib/github-pages-health-check/version.rb",
"lib/github-pages-health-check/cloudflare.rb"
]

s.add_dependency("net-dns", "~> 0.6")
Expand Down
6 changes: 3 additions & 3 deletions lib/github-pages-health-check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
require 'net/dns/resolver'
require 'ipaddr'
require 'public_suffix'
require 'singleton'
require_relative 'github-pages-health-check/version'
require_relative 'github-pages-health-check/cloudflare'

class GitHubPages
class HealthCheck
Expand All @@ -18,14 +20,12 @@ class InvalidCNAME < StandardError; end
199.27.73.133
]

CLOUDFLARE_v4 = IPAddr.new("108.162.192.0/18")

def initialize(domain)
@domain = domain
end

def cloudflare_ip?
dns.all? { |answer| answer.class == Net::DNS::RR::A && CLOUDFLARE_v4.include?(answer.address.to_s) }
dns.all? { |answer| answer.class == Net::DNS::RR::A && CloudFlare.controls_ip?(answer.address) }
end

# Returns an array of DNS answers
Expand Down
37 changes: 37 additions & 0 deletions lib/github-pages-health-check/cloudflare.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class GitHubPages
class HealthCheck
class CloudFlare
include Singleton

CONFIG_PATH = File.expand_path("../../config/cloudflare-ips.txt", File.dirname(__FILE__))

# Public: Does cloudflare control this address?
def self.controls_ip?(address)
instance.controls_ip?(address)
end

# Internal: Create a new cloudflare info instance.
def initialize(options = {})
@path = options.fetch(:path) { CONFIG_PATH }
end

# Internal: The path of the config file.
attr_reader :path

# Internal: Does cloudflare control this address?
def controls_ip?(address)
ranges.any? { |range| range.include?(address) }
end

# Internal: The IP address ranges that cloudflare controls.
def ranges
@ranges ||= load_ranges
end

# Internal: Load IPAddr ranges from #path
def load_ranges
File.read(path).lines.map { |line| IPAddr.new(line.chomp) }
end
end
end
end
2 changes: 1 addition & 1 deletion lib/github-pages-health-check/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class GitHubPages
class HealthCheck
VERSION = '0.0.2'
VERSION = '0.0.3'
end
end
11 changes: 10 additions & 1 deletion script/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@

set -ex

bundle install
cd "$(dirname "$0")/.."

if [ -f .ruby-version ] ; then
export RBENV_VERSION=$(cat .ruby-version)
fi

#set ruby version, see https://github.com/github/pages-jekyll/pull/17
export PATH=/usr/share/rbenv/shims:$PATH BUNDLE_GEMFILE=$(pwd)/Gemfile
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems weird to be in bootstrap. If this is just for CI, maybe move the boostrap in cibuild so it's after the env updates?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but without that, it bootstraps into global ruby, which is 1.8.x. Pulled from the Pages bootstrap.


bundle install --path vendor/gems --binstubs --local --standalone --clean "$@"
17 changes: 17 additions & 0 deletions script/check-cloudflare-ips
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash -e

script/update-cloudflare-ips >/dev/null 2>&1

# `git diff --quiet` suppresses output and sets a return code
# 0 - no changes
# 1 - changes
if git diff -w --quiet --cached config/cloudflare-ips.txt
then
echo CloudFlare IP list is up-to-date.
exit 0
else
echo git reset config/cloudflare-ips.txt
git reset --quiet config/cloudflare-ips.txt
echo '*** CloudFlare IP list is out of date! Run script/update-cloudflare-ips!'
exit 1
fi
16 changes: 15 additions & 1 deletion script/cibuild
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,18 @@

set -ex

bundle exec rspec
script/bootstrap

if [ -L $0 ]; then
root=$(cd "$(dirname "$(readlink "$0")")"/.. && pwd)
else
root=$(cd "$(dirname "$0")"/.. && pwd)
fi

export PATH="$root/bin:/usr/share/rbenv/shims:$PATH"

export RBENV_VERSION=$(cat $root/.ruby-version)
export BUNDLE_GEMFILE="$root/Gemfile"

bundle exec bin/rspec
script/check-cloudflare-ips
14 changes: 14 additions & 0 deletions script/update-cloudflare-ips
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash -e
#/ Usage script/update-cloudflare-ips
#/ updates config/cloudflare-ips.txt

source=https://www.cloudflare.com/ips-v4
dest=config/cloudflare-ips.txt

echo '====>' Downloading $source
curl --silent --fail $source | tee $dest
echo # cover for a missing newline

echo '====>' git add $dest
git diff -w $dest
git add --verbose $dest
50 changes: 50 additions & 0 deletions spec/github_pages_health_check_cloudflare_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'spec_helper'
require 'json'
require 'tempfile'
require 'ipaddr'

describe(GitHubPages::HealthCheck::CloudFlare) do

let(:instance) { GitHubPages::HealthCheck::CloudFlare.send(:new, :path => ipaddr_path) }
let(:tempfile) { Tempfile.new("pages-jekyll-alarmist-cloudflare-ips").tap { |f| f.sync = true } }
let(:ipaddr_path) { tempfile.path }

context "default" do
let(:instance) { GitHubPages::HealthCheck::CloudFlare.instance }

it "loads the default config" do
path = File.expand_path(instance.path)
expected = File.expand_path("../config/cloudflare-ips.txt", File.dirname(__FILE__))
expect(path).to eql(expected)
end
end

context "no config file" do
before { tempfile.unlink }

it "raises an error" do
expect { instance.ranges }.to raise_error
end
end

context "parses config" do
before { tempfile.write("199.27.128.0/21\n173.245.48.0/20") }

it "has two IPs" do
expect(instance.ranges.size).to eql(2)
end

it "loads the IP addresses" do
expect(instance.ranges).to include(IPAddr.new("199.27.128.0/21"))
expect(instance.ranges).to include(IPAddr.new("173.245.48.0/20"))
end

it("controls? 199.27.128.55") { expect(instance.controls_ip?(IPAddr.new("199.27.128.55"))).to be_truthy }
it("controls? 173.245.48.55") { expect(instance.controls_ip?(IPAddr.new("173.245.48.55"))).to be_truthy }
it("controls? 200.27.128.55") { expect(instance.controls_ip?(IPAddr.new("200.27.128.55"))).to be_falsey }
end

it "works as a singleton" do
expect(GitHubPages::HealthCheck::CloudFlare.controls_ip?("108.162.196.20")).to be(true)
end
end
Binary file added vendor/cache/coderay-1.1.0.gem
Binary file not shown.
Binary file added vendor/cache/diff-lcs-1.2.5.gem
Binary file not shown.
Binary file added vendor/cache/method_source-0.8.2.gem
Binary file not shown.
Binary file added vendor/cache/net-dns-0.8.0.gem
Binary file not shown.
Binary file added vendor/cache/pry-0.10.0.gem
Binary file not shown.
Binary file added vendor/cache/public_suffix-1.4.5.gem
Binary file not shown.
Binary file added vendor/cache/rspec-3.0.0.gem
Binary file not shown.
Binary file added vendor/cache/rspec-core-3.0.4.gem
Binary file not shown.
Binary file added vendor/cache/rspec-expectations-3.0.4.gem
Binary file not shown.
Binary file added vendor/cache/rspec-mocks-3.0.4.gem
Binary file not shown.
Binary file added vendor/cache/rspec-support-3.0.4.gem
Binary file not shown.
Binary file added vendor/cache/slop-3.5.0.gem
Binary file not shown.