Skip to content

Commit ca550c1

Browse files
committed
move gem + code
1 parent 4415254 commit ca550c1

File tree

14 files changed

+290
-1
lines changed

14 files changed

+290
-1
lines changed

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--format documentation
2+
--color

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sudo: false
2+
language: ruby
3+
rvm:
4+
- 2.3.1
5+
before_install: gem install bundler -v 1.13.6

CODE_OF_CONDUCT.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, gender identity and expression, level of experience,
9+
nationality, personal appearance, race, religion, or sexual identity and
10+
orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at [email protected]. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at [http://contributor-covenant.org/version/1/4][version]
72+
73+
[homepage]: http://contributor-covenant.org
74+
[version]: http://contributor-covenant.org/version/1/4/

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in rack-request-object-logger.gemspec
4+
gemspec

README.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,76 @@
11
# rack-request-object-logger
2-
Log HTTP requests via Rack stack to a object. Can use any object.
2+
3+
**Human description:** I created this to log all HTTP requests from my Rails application into MySQL database automatically. Then process via ElasticSearch.
4+
5+
**General concept:** Log HTTP requests via Rack stack to an object. Can use any object, because logger uses dependency injection in setup. Be independent of Rails.
6+
7+
Don't be confused with no commits in months or years. Rack middlewares rarely change. They just work for years.
8+
9+
## Install gem
10+
11+
```
12+
gem install rack-request-object-logger
13+
```
14+
15+
## Gemfile
16+
17+
```ruby
18+
gem 'rack-request-object-logger'
19+
```
20+
21+
## Example - logging to SQL database in Rails
22+
23+
generate a model for storage
24+
25+
```
26+
$ bin/rails g model Sql::HttpRequest uuid:string headers:text
27+
28+
```
29+
30+
add JSON serialization
31+
```
32+
# app/models/sql/http_request.rb
33+
class Sql::HttpRequest < ApplicationRecord
34+
serialize :headers, JSON
35+
end
36+
37+
```
38+
39+
add automatic logging via initializer
40+
```
41+
# config/initializers/rack_middlewares.rb
42+
43+
Rails.application.config.middleware.use(RackRequestObjectLogger, Sql::HttpRequest)
44+
```
45+
46+
## Rails integration/awareness
47+
48+
The logger sets the UUID of request to match the request ID set by Rails.
49+
50+
## Security considerations
51+
52+
The middleware stores all HTTP headers, but strips all *active_dispatch*, *warden* and other stuff. That means HTTP basic auth credentials are stored and also data in query string.
53+
54+
I've seen applications sending sensitive data in GET and even POST requests in a query string. Don't do that. Use POST body. Or modify the middleware to filter out them.
55+
56+
## Author & License
57+
58+
Copyright 2016 Ivan Stana, licensed under Apache 2.0 license. Enjoy.
59+
60+
## Development
61+
62+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
63+
64+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
65+
66+
## Contributing
67+
68+
Bug reports and pull requests are welcome on GitHub at https://github.com/istana/rack-request-object-logger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
69+
70+
## Alternatives
71+
72+
There may be better alternatives for you:
73+
74+
- http://bogomips.org/clogger/
75+
- http://www.rubydoc.info/github/rack/rack/Rack/CommonLogger
76+
- https://github.com/mattt/rack-http-logger

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

bin/console

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "rack/request/object/logger"
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

bin/setup

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

lib/rack-request-object-logger.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'rack'
2+
require 'securerandom'
3+
4+
class RackRequestObjectLogger
5+
def initialize(app, request_model)
6+
@app = app
7+
@model = request_model
8+
end
9+
10+
def call(env)
11+
request = Rack::Request.new(env)
12+
# this may not be sufficient with warden/devise/other gems which put stuff into env
13+
# also need to filter figaro stuff, stack settings from AWS
14+
# TODO: pick only HTTP_* and the fields with ip address and CGI
15+
# TODO: respect Rails request id
16+
# TODO: filter sensitive params
17+
# TODO: generally consider to whitelist fields instead of blacklist_
18+
data = request.env.reject{|header, value| !value.is_a?(String) || header =~ /\Aactive_dispatch/ }
19+
20+
m = @model.new
21+
# Rails+Puma adds request.uuid, but cannot use here
22+
# https://github.com/anveo/rack-request-id
23+
m.uuid = SecureRandom.uuid
24+
m.headers = data
25+
26+
if !m.save
27+
# TODO logger or something
28+
# I'm too lazy
29+
end
30+
31+
@app.call(env)
32+
end
33+
end

lib/rack/request/object/logger.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require "rack/request/object/logger/version"
2+
3+
module Rack
4+
module Request
5+
module Object
6+
module Logger
7+
# Your code goes here...
8+
end
9+
end
10+
end
11+
end

0 commit comments

Comments
 (0)