rack-audit 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.md +48 -0
- data/Rakefile +2 -0
- data/lib/rack-audit.rb +89 -0
- data/lib/rack-audit/version.rb +5 -0
- data/rack-audit.gemspec +21 -0
- data/spec/rack-audit_spec.rb +17 -0
- data/spec/spec_helper.rb +7 -0
- metadata +106 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jason Hansen & Josh Lane
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Rack::Audit
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rack-audit'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rack-audit
|
18
|
+
|
19
|
+
## Releasing
|
20
|
+
|
21
|
+
gem bump -trv patch --key engineyard --host http://rubygems.engineyard.com
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Add into your `config.ru`:
|
26
|
+
|
27
|
+
if ENV['FORENSICD_URL']
|
28
|
+
require 'forensicology'
|
29
|
+
STDERR.puts "Using forensic"
|
30
|
+
use Rack::Forensicology, "Server (#{ENV['RAILS_ENV']})", ENV['FORENSICD_URL']
|
31
|
+
else
|
32
|
+
STDERR.puts "Not logging requests, set ENV['FORENSICD_URL'] to do so."
|
33
|
+
end
|
34
|
+
run Application
|
35
|
+
|
36
|
+
Insert via your Rails `config/application.rb`
|
37
|
+
|
38
|
+
if ENV['FORENSICD_URL']
|
39
|
+
config.middle.insert_after Airbrake::Rack, Rack::Audit, "Rails", ENV['FORENSICD_URL']
|
40
|
+
end
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/rack-audit.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# stdlib
|
2
|
+
require 'thread'
|
3
|
+
require 'net/http'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
# gems
|
7
|
+
require 'rack'
|
8
|
+
require 'uuidtools'
|
9
|
+
require 'multi_json'
|
10
|
+
|
11
|
+
class Rack::Audit
|
12
|
+
attr_reader :queue, :host, :port, :name
|
13
|
+
def initialize(app, name, uri)
|
14
|
+
@app = app
|
15
|
+
@queue = Queue.new
|
16
|
+
@uri = URI(uri)
|
17
|
+
@host = @uri.host
|
18
|
+
@port = @uri.port
|
19
|
+
@name = name
|
20
|
+
|
21
|
+
@consumer = Thread.new do
|
22
|
+
while true do
|
23
|
+
body = @queue.pop
|
24
|
+
begin
|
25
|
+
post(@uri, body)
|
26
|
+
rescue => e
|
27
|
+
STDERR.puts e.inspect
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def call(env)
|
34
|
+
uuid = UUIDTools::UUID.random_create.to_s
|
35
|
+
log_request(uuid, env)
|
36
|
+
response = @app.call(env)
|
37
|
+
log_response(uuid, response)
|
38
|
+
response
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def log_request(uuid, env)
|
44
|
+
request = Rack::Request.new(env)
|
45
|
+
loggable_request = {
|
46
|
+
:request_method => request.request_method,
|
47
|
+
:url => request.url,
|
48
|
+
:params => request.params,
|
49
|
+
:headers => Rack::Utils::HeaderHash.new(env).to_hash,
|
50
|
+
}
|
51
|
+
|
52
|
+
# FIXME: necessary?
|
53
|
+
request.body.rewind
|
54
|
+
|
55
|
+
queue << MultiJson.encode(
|
56
|
+
:uuid => uuid,
|
57
|
+
:request => loggable_request,
|
58
|
+
:time => Time.new.to_i,
|
59
|
+
:from => name
|
60
|
+
)
|
61
|
+
rescue => e
|
62
|
+
STDERR.puts e.inspect
|
63
|
+
ensure
|
64
|
+
true
|
65
|
+
end
|
66
|
+
|
67
|
+
def log_response(uuid, response)
|
68
|
+
status, headers, body = response
|
69
|
+
full_body = ''
|
70
|
+
body.each { |b| full_body += b.to_s; b.rewind if b.respond_to?(:rewind) }
|
71
|
+
queue << MultiJson.encode(
|
72
|
+
:uuid => uuid,
|
73
|
+
:response => [status, headers, full_body],
|
74
|
+
:time => Time.new.to_i,
|
75
|
+
:from => name
|
76
|
+
)
|
77
|
+
rescue => e
|
78
|
+
STDERR.puts e.inspect
|
79
|
+
ensure
|
80
|
+
true
|
81
|
+
end
|
82
|
+
|
83
|
+
def post(uri, body)
|
84
|
+
req = Net::HTTP::Post.new(uri.to_s)
|
85
|
+
req["content-type"] = "application/json"
|
86
|
+
req.body = body
|
87
|
+
Net::HTTP.start(host, port){|http| http.request(req)}
|
88
|
+
end
|
89
|
+
end
|
data/rack-audit.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rack-audit/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["EngineYard"]
|
6
|
+
gem.email = ["engineering@engineyard.com"]
|
7
|
+
gem.description = %q{Asynchronously send requests to another system}
|
8
|
+
gem.summary = %q{Asynchronously send requests to another system}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "rack-audit"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Rack::Audit::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'rack'
|
19
|
+
gem.add_dependency 'uuidtools'
|
20
|
+
gem.add_dependency 'multi_json'
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Rack::Audit do
|
4
|
+
def app
|
5
|
+
Rack::Builder.new do
|
6
|
+
use Rack::Audit, "test", "http://example.org"
|
7
|
+
run lambda{|env| [200, {"Content-Type" => "application/wtf"}, ["check out my body"]]}
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should perserve the request" do
|
12
|
+
response = get "/"
|
13
|
+
response.body.should == "check out my body"
|
14
|
+
response.headers.should == {"Content-Type" => "application/wtf", "Content-Length" => "17"}
|
15
|
+
response.status.should == 200
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-audit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- EngineYard
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-05 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: uuidtools
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
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: multi_json
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
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
|
+
description: Asynchronously send requests to another system
|
63
|
+
email:
|
64
|
+
- engineering@engineyard.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rspec
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/rack-audit.rb
|
76
|
+
- lib/rack-audit/version.rb
|
77
|
+
- rack-audit.gemspec
|
78
|
+
- spec/rack-audit_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
homepage: ''
|
81
|
+
licenses: []
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.8.24
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Asynchronously send requests to another system
|
104
|
+
test_files:
|
105
|
+
- spec/rack-audit_spec.rb
|
106
|
+
- spec/spec_helper.rb
|