stubby 0.0.2 → 0.0.3

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.md CHANGED
@@ -129,6 +129,19 @@ interfaces and will shut down the stubby server.
129
129
  To contribute a stub, just add your stub to the list above and issue a pull
130
130
  request.
131
131
 
132
+ ## Environment Verification
133
+
134
+ It may be useful to deny requests that aren't routed by a system using stubby,
135
+ or for a site accessed with stubby to display some information about the
136
+ environment requested.
137
+
138
+ The HTTP and HTTPS extensions both append request headers to proxied
139
+ requests. A STUBBY_ENV header contains the name of the stubby environment.
140
+
141
+ Additionally, stubby requests send a STUBBY_KEY header which contains a hash
142
+ that should be unique over the stubby user and the instruction that the trigger
143
+ executed. If you configure your application to track STUBBY_KEY values, you can
144
+ whitelist requests to a stubby system.
132
145
 
133
146
  ## Stubbing
134
147
 
@@ -69,65 +69,61 @@ module Extensions
69
69
 
70
70
  adapter "http-proxy" do
71
71
  url.scheme = "http"
72
-
73
- if url.path.empty?
74
- # Proxy all requests, preserve incoming path
75
- out = url.dup
76
- out.path = request.path
77
- request = HTTPI::Request.new
78
- request.url = out.to_s
79
- else
80
- # Proxy to the given path
81
- request = HTTPI::Request.new
82
- request.url = url.to_s
83
- end
84
-
85
- response = HTTPI.get(request)
86
- response.headers.delete "transfer-encoding"
87
- response.headers.delete "connection"
88
-
89
- status(response.code)
90
- headers(response.headers)
91
- body(response.body)
72
+ run_proxy
92
73
 
93
74
  end
94
75
 
95
76
  adapter "https-proxy" do
96
77
  url.scheme = "https"
78
+ run_proxy
79
+ end
80
+
81
+ %w(get post put patch delete options link unlink).each do |method|
82
+ send(method, //) do
83
+ run_handler
84
+ end
85
+ end
86
+
87
+ private
88
+ def run_handler
89
+ if instruction.nil?
90
+ not_found
91
+ elsif adapter=self.class.adapters[url.scheme]
92
+ instance_eval &adapter
93
+ else
94
+ instance_eval &self.class.adapters["default"]
95
+ end
96
+ end
97
97
 
98
+ def run_proxy
98
99
  if url.path.empty?
99
100
  # Proxy all requests, preserve incoming path
100
101
  out = url.dup
101
102
  out.path = request.path
102
- request = HTTPI::Request.new
103
- request.url = out.to_s
103
+ r = HTTPI::Request.new
104
+ r.url = out.to_s
104
105
  else
105
106
  # Proxy to the given path
106
- request = HTTPI::Request.new
107
- request.url = url.to_s
107
+ r = HTTPI::Request.new
108
+ r.url = url.to_s
108
109
  end
109
110
 
110
- response = HTTPI.get(request)
111
+ r.headers["STUBBY_ENV"] = settings.stubby_session.environment
112
+ r.headers["STUBBY_KEY"] = settings.stubby_session.key(instruction)
113
+ r.headers["STUBBY_USER"] = settings.stubby_session.user_key
114
+
115
+ request.body.rewind
116
+ r.body = request.body.read
117
+
118
+ response = HTTPI.request(request.request_method.downcase.to_sym, r)
111
119
  response.headers.delete "transfer-encoding"
112
120
  response.headers.delete "connection"
113
-
121
+
114
122
  status(response.code)
115
123
  headers(response.headers)
116
124
  body(response.body)
117
-
118
125
  end
119
126
 
120
- get(//) do
121
- if instruction.nil?
122
- not_found
123
- elsif adapter=self.class.adapters[url.scheme]
124
- instance_eval &adapter
125
- else
126
- instance_eval &self.class.adapters["default"]
127
- end
128
- end
129
-
130
- private
131
127
  def forbidden
132
128
  [403, "Forbidden"]
133
129
  end
data/lib/stubby/master.rb CHANGED
@@ -151,6 +151,18 @@ module Stubby
151
151
  @config.environment = environment
152
152
  end
153
153
 
154
+ def key(identifier)
155
+ Digest::MD5.hexdigest(user_key + identifier)
156
+ end
157
+
158
+ def user_key
159
+ @user_key ||= read_key
160
+ end
161
+
162
+ def environment
163
+ @config.environment
164
+ end
165
+
154
166
  def run!(options={})
155
167
  run_network do
156
168
  run_master do
@@ -160,6 +172,22 @@ module Stubby
160
172
  end
161
173
 
162
174
  private
175
+ def read_key
176
+ File.read(keyfile)
177
+ rescue
178
+ generate_key
179
+ end
180
+
181
+ def generate_key
182
+ SecureRandom.hex(50).tap do |key|
183
+ File.write(keyfile, key)
184
+ end
185
+ end
186
+
187
+ def keyfile
188
+ File.expand_path("~/.stubby/key")
189
+ end
190
+
163
191
  def run_network
164
192
  assume_network_interface
165
193
  yield
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stubby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: