rack_staging 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.
- data/README.mdown +51 -0
- data/Rakefile +25 -0
- data/lib/rack_staging.rb +49 -0
- data/test/staging_test.rb +78 -0
- data/test/test_helper.rb +12 -0
- metadata +62 -0
data/README.mdown
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Rack Staging
|
2
|
+
|
3
|
+
## Usage
|
4
|
+
|
5
|
+
## Why would I want to use this?
|
6
|
+
|
7
|
+
## Compatibility
|
8
|
+
|
9
|
+
## Contributions
|
10
|
+
|
11
|
+
* Glenn Gillen
|
12
|
+
|
13
|
+
## Status
|
14
|
+
|
15
|
+
In Development
|
16
|
+
|
17
|
+
## Bugs, Feature Requests, etc.
|
18
|
+
|
19
|
+
If you think you've found a bug or have a feature you'd like to contribute
|
20
|
+
here are the ways to let me know, in order of likelihood for a speedy
|
21
|
+
resolution:
|
22
|
+
|
23
|
+
* Fork the repo, add a test, implement the feature, send me a pull request
|
24
|
+
* Fork the repo, add a failing test, send me a pull request
|
25
|
+
* Create an issue on Github.
|
26
|
+
|
27
|
+
## License
|
28
|
+
|
29
|
+
Rack Staging is released under the MIT license.
|
30
|
+
|
31
|
+
Copyright (c) 2011 Glenn Gillen
|
32
|
+
|
33
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
34
|
+
of this software and associated documentation files (the "Software"), to deal
|
35
|
+
in the Software without restriction, including without limitation the rights
|
36
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
37
|
+
copies of the Software, and to permit persons to whom the Software is
|
38
|
+
furnished to do so, subject to the following conditions:
|
39
|
+
|
40
|
+
The above copyright notice and this permission notice shall be included in
|
41
|
+
all copies or substantial portions of the Software.
|
42
|
+
|
43
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
44
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
45
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
46
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
47
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
48
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
49
|
+
THE SOFTWARE.
|
50
|
+
ObjectBouncer
|
51
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rubygems/package_task'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
desc 'Test Rack::Staging'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
spec_data = File.open('rack_staging.gemspec').read
|
17
|
+
spec = nil
|
18
|
+
Thread.new do
|
19
|
+
spec = eval("#{spec_data}")
|
20
|
+
end.join
|
21
|
+
|
22
|
+
Gem::PackageTask.new(spec) do |pkg|
|
23
|
+
pkg.need_zip = false
|
24
|
+
pkg.need_tar = false
|
25
|
+
end
|
data/lib/rack_staging.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rack'
|
2
|
+
module Rack
|
3
|
+
class Staging
|
4
|
+
|
5
|
+
def initialize(app, staging_test = nil)
|
6
|
+
@app = app
|
7
|
+
@staging_test = staging_test
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
return @app.call(env) unless staging?(env)
|
12
|
+
return robots_txt if robots_txt?(env)
|
13
|
+
return unauthorized unless authorized?(env)
|
14
|
+
@app.call(env)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def staging?(env)
|
19
|
+
return @staging_test.call(env) if @staging_test
|
20
|
+
env["HTTP_HOST"] =~ /staging/ || ENV["STAGING"]
|
21
|
+
end
|
22
|
+
|
23
|
+
def robots_txt?(env)
|
24
|
+
env["PATH_INFO"] == "/robots.txt"
|
25
|
+
end
|
26
|
+
|
27
|
+
def robots_txt
|
28
|
+
body = <<-EOF
|
29
|
+
User-agent: *
|
30
|
+
Disallow: /
|
31
|
+
EOF
|
32
|
+
[ 200, {'Content-Type' => 'text/plain'}, [body] ]
|
33
|
+
end
|
34
|
+
|
35
|
+
def authorized?(env)
|
36
|
+
@auth ||= Rack::Auth::Basic::Request.new(env)
|
37
|
+
@auth.provided? &&
|
38
|
+
@auth.basic? &&
|
39
|
+
@auth.credentials &&
|
40
|
+
@auth.credentials == [ENV["STAGING_USER"], ENV["STAGING_PASS"]]
|
41
|
+
end
|
42
|
+
|
43
|
+
def unauthorized
|
44
|
+
[ 401, {'Content-Type' => 'text/plain'}, ["Unauthorized"] ]
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
$:.unshift(File.expand_path(File.dirname(__FILE__)))
|
2
|
+
require "test_helper"
|
3
|
+
|
4
|
+
class DummyApp
|
5
|
+
|
6
|
+
def call(env)
|
7
|
+
[ 200, {'Content-Type' => 'text/plain'}, ['this is from DummyApp'] ]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class RackStagingTest < Test::Unit::TestCase
|
12
|
+
|
13
|
+
def app
|
14
|
+
@app ||= Rack::Builder.new do
|
15
|
+
use Rack::Staging
|
16
|
+
run DummyApp.new
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_passes_through_to_other_middleware_if_not_staging
|
21
|
+
get "/"
|
22
|
+
assert_equal 200, last_response.status
|
23
|
+
assert_equal "this is from DummyApp", last_response.body
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_robots_txt_if_staging
|
27
|
+
header "HOST", "staging.example.org"
|
28
|
+
get "/robots.txt"
|
29
|
+
expected = <<-EOF
|
30
|
+
User-agent: *
|
31
|
+
Disallow: /
|
32
|
+
EOF
|
33
|
+
assert_equal expected, last_response.body
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_requires_authentication_if_staging
|
37
|
+
header "HOST", "staging.example.org"
|
38
|
+
get "/"
|
39
|
+
assert_equal 401, last_response.status
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_requires_authentication_if_env_var_set
|
43
|
+
ENV["STAGING"] = "true"
|
44
|
+
get "/"
|
45
|
+
assert_equal 401, last_response.status
|
46
|
+
ensure
|
47
|
+
ENV.delete("STAGING")
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_requires_authentication_if_proc_returns_true
|
51
|
+
@app = Rack::Builder.new do
|
52
|
+
use Rack::Staging, Proc.new{|env| env["HTTP_HOST"] =~ /foobar/ }
|
53
|
+
run DummyApp.new
|
54
|
+
end
|
55
|
+
header "HOST", "staging.example.org"
|
56
|
+
get "/"
|
57
|
+
assert_equal 200, last_response.status
|
58
|
+
|
59
|
+
header "HOST", "foobar.org"
|
60
|
+
get "/"
|
61
|
+
assert_equal 401, last_response.status
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_auths_based_on_env_settings
|
65
|
+
ENV["STAGING_USER"] = "top"
|
66
|
+
ENV["STAGING_PASS"] = "seecrets"
|
67
|
+
|
68
|
+
header "HOST", "staging.example.org"
|
69
|
+
get "/"
|
70
|
+
assert_equal 401, last_response.status
|
71
|
+
authorize "top", "seecrets"
|
72
|
+
get "/"
|
73
|
+
assert_equal 200, last_response.status
|
74
|
+
ensure
|
75
|
+
ENV.delete("STAGING_USER")
|
76
|
+
ENV.delete("STAGING_PASS")
|
77
|
+
end
|
78
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack_staging
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Glenn Gillen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-08-15 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Automatically protects your staging app from web crawlers and casual visitors.
|
18
|
+
email: me@glenngillen.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- README.mdown
|
27
|
+
- Rakefile
|
28
|
+
- lib/rack_staging.rb
|
29
|
+
- test/staging_test.rb
|
30
|
+
- test/test_helper.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/glenngillen/rack_staging
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- .
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.6.0
|
57
|
+
signing_key:
|
58
|
+
specification_version: 2
|
59
|
+
summary: Rack::Staging - Protects your staging apps from prying eyes.
|
60
|
+
test_files:
|
61
|
+
- test/staging_test.rb
|
62
|
+
- test/test_helper.rb
|