wrenchmode-rack 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58f083348339efd1edfece7a5f8a2385e79058a9
4
+ data.tar.gz: 730b06f6d008933865c86c4752b70428a3fb9a3c
5
+ SHA512:
6
+ metadata.gz: 543dd1b8120bfccf7feff548bad8233287e827911442a5aaa7bbf4b26057a3b7187fc1bf260d84820e5d641d6cc660f2cc9e6a77c07d7f7d2cb846ee9e2a4f29
7
+ data.tar.gz: 2518bc795d9a5085a0006eaa8ad8369adf26e7880a674ccb386d2a632a0f0c7e90711996384030b89e5a01a54b78f640b453f5cb84292fcd6a08226511328756
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ wrenchmode-rack-*.gem
11
+ .ruby-version
12
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wrenchmode_rack.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Micah Wedemeyer
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,80 @@
1
+ # Wrenchmode Rack
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/wrenchmode-rack.svg)](https://badge.fury.io/rb/wrenchmode-rack)
4
+
5
+ This is a [Rack Middleware](http://rack.github.io/) for managing maintenance mode on your Ruby/Rack/Rails web application using [Wrenchmode](http://wrenchmode.com).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'wrenchmode-rack'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install wrenchmode-rack
22
+
23
+ ## Usage
24
+
25
+ ### In a Rails application
26
+
27
+ ```ruby
28
+ # config/environments/production.rb
29
+ config.middleware.use Wrenchmode::Rack, jwt: "your-long-jwt"
30
+
31
+ # If you want to test in staging prior to deploying to production.
32
+ # (Coming soon, still not implemented...)
33
+ # config/environments/staging.rb
34
+ config.middleware.use Wrenchmode::Rack, ignore_test_mode: false, jwt: "your-long-jwt"
35
+ ```
36
+
37
+ ### In a vanilla Rack application
38
+
39
+ ```ruby
40
+ # your-app.rb
41
+ require 'rubygems'
42
+ require 'bundler/setup'
43
+ Bundler.require(:default)
44
+
45
+ use Wrenchmode::Rack, jwt: "your-long-jwt"
46
+ ```
47
+
48
+ ## Advanced Configuration Options
49
+
50
+ You can also specify the following options to the middleware layer:
51
+
52
+ `force_open` - Set to true to force the middlware layer to allow all requests through, regardless of project status on Wrenchmode.com. Effectively disables the middleware. (Default false)
53
+
54
+ `ignore_test_mode` - (Coming soon...) Set to false to if you want the middleware to respond to a project that is in Test mode on Wrenchmode.com This can be useful if you want to test Wrenchmode in a development or staging environment prior to deploying to production. (Default true)
55
+
56
+ `disable_local_wrench` - (Coming soon...) Set to true if you want to disable LocalWrench mode, where the Wrenchmode page is served on your domain. Disabling it will instead force a redirect to the Wrenchmode.com domain. Note: Unless you explicitly want this behavior, it's best to leave this at the default. (Default false)
57
+
58
+ `check_delay_secs` - Change this to modify the rate at which the middleware polls Wrenchmode for updates. Unlikely that this needs anything faster than the default. (Default 5)
59
+
60
+ `logging` - Set to true in order to log information from the middleware layer to your logging facility. (Default false)
61
+
62
+ ## FAQ
63
+
64
+ ### Does every request to my server get proxied through Wrenchmode? Isn't that slow?
65
+
66
+ No. The middleware does not function as a proxy at all in that fashion. Instead, the middleware spins up a separate thread that periodically checks the Wrenchmode API for changes and updates its own internal state. In other words, the middleware adds zero performance impact on requests to your server.
67
+
68
+ ### What if the Wrenchmode service is down? Will my project be brought down as well?
69
+
70
+ No. The middleware is designed to fail open, meaning that if it encounters any errors or cannot contact the Wrenchmode API, it will automatically revert to "open" mode where it allows all requests to pass through normally to your server.
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/micahwedemeyer/wrenchmode-rack. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "wrenchmode_rack"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,207 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'ipaddr'
5
+
6
+ module Wrenchmode
7
+ class Rack
8
+ CLIENT_NAME = "wrenchmode-rack"
9
+ VERSION = '0.0.10'
10
+
11
+ SWITCH_URL_KEY = "switch_url"
12
+ TEST_MODE_KEY = "test_mode"
13
+ IS_SWITCHED_KEY = "is_switched"
14
+ IP_WHITELIST_KEY = "ip_whitelist"
15
+ REVERSE_PROXY_KEY = "reverse_proxy"
16
+
17
+ def initialize(app, opts = {})
18
+ @app = app
19
+
20
+ # Symbolize keys
21
+ opts = symbolize_keys(opts)
22
+ opts = {
23
+ force_open: false,
24
+ ignore_test_mode: true,
25
+ disable_local_wrench: false, # LocalWrench is our "brand name", want to avoid scaring people will talk of proxies
26
+ status_protocol: "https",
27
+ status_host: "api.wrenchmode.com",
28
+ status_path: "/api/projects/status",
29
+ check_delay_secs: 5,
30
+ logging: false,
31
+ read_timeout_secs: 3
32
+ }.merge(opts)
33
+
34
+ @jwt = opts[:jwt]
35
+ @ignore_test_mode = opts[:ignore_test_mode]
36
+ @disable_reverse_proxy = opts[:disable_local_wrench]
37
+ @force_open = opts[:force_open]
38
+ @status_url = "#{opts[:status_protocol]}://#{opts[:status_host]}#{opts[:status_path]}"
39
+ @check_delay_secs = opts[:check_delay_secs]
40
+ @logging = opts[:logging]
41
+ @read_timeout_secs = opts[:read_timeout_secs]
42
+ @ip_whitelist = []
43
+ @logger = nil
44
+
45
+ @enable_reverse_proxy = false
46
+
47
+ @made_contact = false
48
+ end
49
+
50
+ def call(env)
51
+ @logger = env['rack.logger'] if @logging && !@logger
52
+
53
+ unless @jwt
54
+ log("[Wrenchmode] No JWT specified so bypassing Wrenchmode. Please configure Wrenchmode with a JWT.", Logger::ERROR)
55
+ return @app.call(env)
56
+ end
57
+
58
+ # On startup, we need to give it a chance to make contact
59
+ @check_thread ||= start_check_thread()
60
+ sleep(0.01) while !@made_contact
61
+
62
+ should_display_wrenchmode = false
63
+ if @switched
64
+ req = ::Rack::Request.new(env)
65
+
66
+ should_display_wrenchmode = !@force_open
67
+ should_display_wrenchmode &&= !ip_whitelisted?(req)
68
+ end
69
+
70
+ if should_display_wrenchmode
71
+ if @enable_reverse_proxy
72
+ reverse_proxy
73
+ else
74
+ redirect
75
+ end
76
+ else
77
+ @app.call(env)
78
+ end
79
+ end
80
+
81
+ def update_status
82
+ json = fetch_status
83
+
84
+ @switch_url = json[SWITCH_URL_KEY]
85
+ test_mode = json[TEST_MODE_KEY] || false
86
+ @switched = json[IS_SWITCHED_KEY] && !(@ignore_test_mode && test_mode)
87
+ @ip_whitelist = json[IP_WHITELIST_KEY] || []
88
+
89
+ @enable_reverse_proxy = false
90
+ if json[REVERSE_PROXY_KEY] && !@disable_reverse_proxy
91
+ @enable_reverse_proxy = json[REVERSE_PROXY_KEY]["enabled"]
92
+ @reverse_proxy_config = symbolize_keys(json[REVERSE_PROXY_KEY])
93
+ end
94
+
95
+ rescue OpenURI::HTTPError => e
96
+ log("Wrenchmode Check HTTP Error: #{e.message}")
97
+ @switched = false
98
+ rescue JSON::JSONError => e
99
+ log("Wrenchmode Check JSON Error: #{e.message}")
100
+ @switched = false
101
+ rescue StandardError => e
102
+ log("Wrenchmode Check Unknown Error: #{e.message}")
103
+ @switched = false
104
+ ensure
105
+ @made_contact = true
106
+ end
107
+
108
+ private
109
+
110
+ def fetch_status
111
+ payload = JSON.generate(build_update_package)
112
+ body = nil
113
+
114
+ uri = URI.parse(@status_url)
115
+ use_ssl = uri.scheme == "https"
116
+ Net::HTTP.start(uri.host, uri.port, open_timeout: @read_timeout_secs, read_timeout: @read_timeout_secs, use_ssl: use_ssl) do |http|
117
+ response = http.post(uri, payload, post_headers)
118
+ body = response.read_body
119
+ end
120
+
121
+ JSON.parse(body)
122
+ end
123
+
124
+ def post_headers
125
+ {
126
+ "Content-Type" => "application/json",
127
+ "Accept" => "application/json",
128
+ "Authorization" => @jwt,
129
+ "User-Agent" => "#{CLIENT_NAME}-#{VERSION}"
130
+ }
131
+ end
132
+
133
+ def redirect
134
+ [
135
+ 302,
136
+ {'Location' => @switch_url, 'Content-Type' => 'text/html', 'Content-Length' => '0'},
137
+ []
138
+ ]
139
+ end
140
+
141
+ def reverse_proxy
142
+ [
143
+ @reverse_proxy_config[:http_status],
144
+ @reverse_proxy_config[:response_headers],
145
+ [@reverse_proxy_config[:response_body]]
146
+ ]
147
+ end
148
+
149
+ def start_check_thread
150
+ Thread.new do
151
+ while true do
152
+ update_status
153
+ sleep(@check_delay_secs)
154
+ end
155
+ end
156
+ end
157
+
158
+ def ip_whitelisted?(request)
159
+ return false unless request.ip
160
+ client_ip = IPAddr.new(request.ip)
161
+ @ip_whitelist.any? do |ip_address|
162
+ IPAddr.new(ip_address).include?(client_ip)
163
+ end
164
+ end
165
+
166
+ def build_update_package
167
+ {
168
+ hostname: guess_hostname,
169
+ ip_address: guess_ip_address,
170
+ pid: guess_pid,
171
+ client_name: CLIENT_NAME,
172
+ client_version: VERSION
173
+ }
174
+ end
175
+
176
+ def guess_pid
177
+ Process.pid
178
+ rescue StandardError => e
179
+ log("Wrenchmode error trying to guess PID: #{e.inspect}")
180
+ nil
181
+ end
182
+
183
+ def guess_hostname
184
+ Socket.gethostname
185
+ rescue StandardError => e
186
+ log("Wrenchmode error trying to guess the hostname: #{e.inspect}")
187
+ nil
188
+ end
189
+
190
+ def guess_ip_address
191
+ address = Socket.ip_address_list.find { |addr| addr.ipv4? && !addr.ipv4_loopback? && !addr.ipv4_private? }
192
+ address ? address.ip_address : nil
193
+ rescue StandardError => e
194
+ log("Wrenchmode error trying to guess the IP address: #{e.inspect}")
195
+ nil
196
+ end
197
+
198
+ def log(message, level = nil)
199
+ @logger.add(level || Logger::INFO, message) if @logging && @logger
200
+ end
201
+
202
+ def symbolize_keys(hash)
203
+ hash.each_with_object({}) { |(k,v), h| h[k.to_sym] = v }
204
+ end
205
+
206
+ end
207
+ end
data/lib/wrenchmode.rb ADDED
@@ -0,0 +1 @@
1
+ require 'wrenchmode/rack'
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wrenchmode'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wrenchmode-rack"
8
+ spec.version = Wrenchmode::Rack::VERSION
9
+ spec.authors = ["Micah Wedemeyer"]
10
+ spec.email = ["me@micahwedemeyer.com"]
11
+
12
+ spec.summary = "Rack middleware for using maintenance mode with Wrenchmode.com"
13
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "http://github.com/micahwedemeyer/wrenchmode-rack"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "rack", "~> 1.0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "pry", "~> 0.10.3"
28
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wrenchmode-rack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.10
5
+ platform: ruby
6
+ authors:
7
+ - Micah Wedemeyer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.10.3
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.10.3
83
+ description:
84
+ email:
85
+ - me@micahwedemeyer.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/wrenchmode.rb
101
+ - lib/wrenchmode/rack.rb
102
+ - wrenchmode-rack.gemspec
103
+ homepage: http://github.com/micahwedemeyer/wrenchmode-rack
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.5.1
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Rack middleware for using maintenance mode with Wrenchmode.com
127
+ test_files: []