rack-entrance 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/README.md +26 -0
- data/lib/rack/entrance/version.rb +11 -0
- data/lib/rack/entrance.rb +26 -0
- data/spec/lib/rack/entrance_spec.rb +45 -0
- data/spec/spec_helper.rb +2 -0
- metadata +135 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2013 Bukowskis
|
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,26 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/bukowskis/rack-entrance.png)](https://travis-ci.org/bukowskis/rack-entrance)
|
2
|
+
|
3
|
+
# Entrace
|
4
|
+
|
5
|
+
A tiny middleware that determines whether this request was from the internal network or not.
|
6
|
+
|
7
|
+
# Installation
|
8
|
+
|
9
|
+
```bash
|
10
|
+
gem install rack-entrance
|
11
|
+
````
|
12
|
+
|
13
|
+
# Usage
|
14
|
+
|
15
|
+
#### Add it to your middleware stack
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
ENV['ENTRANCE_INTERNAL_IPS'] = "127.0.0.1,192.0.2.21"
|
19
|
+
use Rack::Entrance
|
20
|
+
````
|
21
|
+
|
22
|
+
#### Use it in your Controllers
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
request.env['entrance.internal'] #=> true/false
|
26
|
+
```
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class Entrance
|
5
|
+
|
6
|
+
def initialize(app, options = {})
|
7
|
+
@app = app
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
request = Rack::Request.new(env)
|
12
|
+
ip = env["action_dispatch.remote_ip"] || request.ip
|
13
|
+
request.env['entrance.internal'] = internal? ip
|
14
|
+
@app.call env
|
15
|
+
end
|
16
|
+
|
17
|
+
def internal?(ip)
|
18
|
+
internal_ips.include? ip
|
19
|
+
end
|
20
|
+
|
21
|
+
def internal_ips
|
22
|
+
@internal_ips ||= ENV['ENTRANCE_INTERNAL_IPS'].to_s.split(',')
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Rack::Entrance do
|
4
|
+
include Rack::Test::Methods
|
5
|
+
|
6
|
+
let(:inner_app) do
|
7
|
+
lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['All good!']] }
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:app) { Rack::Entrance.new(inner_app) }
|
11
|
+
|
12
|
+
context 'internal request' do
|
13
|
+
before do
|
14
|
+
ENV['ENTRANCE_INTERNAL_IPS'] = '127.0.0.1'
|
15
|
+
get '/'
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'sets internal to true' do
|
19
|
+
last_request.env['entrance.internal'].should be_true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'external request' do
|
24
|
+
before do
|
25
|
+
ENV['ENTRANCE_INTERNAL_IPS'] = '192.0.2.21,203.0.113.255'
|
26
|
+
get '/'
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'sets internal to false' do
|
30
|
+
last_request.env['entrance.internal'].should be_false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'no env variable set' do
|
35
|
+
before do
|
36
|
+
ENV['ENTRANCE_INTERNAL_IPS'] = nil
|
37
|
+
get '/'
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'sets internal to false' do
|
41
|
+
last_request.env['entrance.internal'].should be_false
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-entrance
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- bukowskis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rack-test
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: guard-rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rb-fsevent
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: A tiny middleware that determines whether this request was from the internal
|
95
|
+
network or not.
|
96
|
+
email:
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- lib/rack/entrance/version.rb
|
102
|
+
- lib/rack/entrance.rb
|
103
|
+
- spec/lib/rack/entrance_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
- README.md
|
106
|
+
- LICENSE
|
107
|
+
homepage: https://github.com/bukowskis/rack-entrance
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options:
|
112
|
+
- --encoding
|
113
|
+
- UTF-8
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ! '>='
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 1.8.23
|
131
|
+
signing_key:
|
132
|
+
specification_version: 3
|
133
|
+
summary: A tiny middleware that determines whether this request was from the internal
|
134
|
+
network or not.
|
135
|
+
test_files: []
|