Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug logging #226

Merged
merged 7 commits into from
Dec 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 0 additions & 40 deletions lib/tugboat/middleware/authentication_middleware.rb

This file was deleted.

15 changes: 11 additions & 4 deletions lib/tugboat/middleware/custom_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def call(env)

def filter(output)
if ENV['DEBUG'].to_i == 2
output = output.to_s.gsub(/client_id=[a-zA-Z0-9]*/,'client_id=[CLIENT-ID]')
output = output.to_s.gsub(/api_key=[a-zA-Z0-9]*/,'api_key=[API-KEY]')
output = output.to_s.gsub(/Bearer [a-zA-Z0-9]*/,'Bearer [TOKEN REDACTED]')
output = output.to_s.gsub(/_digitalocean2_session_v2=[a-zA-Z0-9%-]*/,'_digitalocean2_session_v2=[SESSION_COOKIE]')
else
output
Expand All @@ -52,15 +51,23 @@ def response_debug(env)
end

def debug_message(name, headers, body)
<<-MESSAGE.gsub(/^ +([^ ])/m, '\\1')

main_message = <<-MESSAGE.gsub(/^ +([^ ])/m, '\\1')
#{name} Headers:
----------------
#{format_headers(headers)}

#{name} Body:
-------------
#{body}
MESSAGE
main_message + pretty_body(body)
end

def pretty_body(body)
body_json = JSON.parse(body)
JSON.pretty_generate(body_json)
rescue JSON::ParserError => e
body
end

def format_headers(headers)
Expand Down
15 changes: 3 additions & 12 deletions lib/tugboat/middleware/inject_client.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
require 'barge'
require File.expand_path('../custom_logger', __FILE__)

module Tugboat
module Middleware
# Inject the digital ocean client into the environment
class InjectClient < Base

def tugboat_faraday
Faraday.new(:url => 'https://api.digitalocean.com/') do |faraday|
faraday.use AuthenticationMiddleware, @client_id, @api_key
faraday.use Faraday::Response::RaiseError
faraday.use CustomLogger if ENV['DEBUG']
faraday.request :url_encoded
faraday.response :rashify
faraday.response :json, :content_type => /\b(json|json-home)$/
faraday.adapter Faraday.default_adapter
end
end

def call(env)
# Sets the digital ocean client into the environment for use
# later.
@access_token = env["config"].access_token

env['barge'] = Barge::Client.new(:access_token => @access_token)

env['barge'].faraday.use CustomLogger if ENV['DEBUG']

@app.call(env)
end
end
Expand Down
32 changes: 26 additions & 6 deletions spec/cli/debug_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@
allow(ENV).to receive(:[]).with('HOME').and_return('/tmp/fake_home')
allow(ENV).to receive(:[]).with('DEBUG').and_return(1)
allow(ENV).to receive(:[]).with('http_proxy').and_return(nil)
allow(ENV).to receive(:[]).with('DO_API_TOKEN').and_return(nil)
end

it "gives full faraday logs" do
pending 'Debug flag not avaliable yet'
stub_request(:get, "https://api.digitalocean.com/v2/droplets?page=1&per_page=200").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer foo', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => fixture('show_droplets'), :headers => {})
stub_request(:get, "https://api.digitalocean.com/v2/droplets?page=1&per_page=1").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer foo', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => fixture('show_droplets'), :headers => {})

stub_request(:get, "https://api.digitalocean.com/v2/droplets?page=1&per_page=200").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer foo', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => fixture('show_droplets'), :headers => {})

@cli.droplets

expect($stdout.string).to include "Started GET request to: https://api.digitalocean.com/v2/droplets?page=1&per_page=200"
expect($stdout.string).to include "DEBUG -- : Request Headers:"

expect($stdout.string).to include "Bearer foo"
end
end

Expand All @@ -24,15 +34,25 @@
allow(ENV).to receive(:[]).with('HOME').and_return('/tmp/fake_home')
allow(ENV).to receive(:[]).with('DEBUG').and_return(2)
allow(ENV).to receive(:[]).with('http_proxy').and_return(nil)
allow(ENV).to receive(:[]).with('DO_API_TOKEN').and_return(nil)
end

it "gives full faraday logs with redacted API keys" do
pending 'Debug flag not avaliable yet'
stub_request(:get, "https://api.digitalocean.com/v2/droplets?page=1&per_page=200").
stub_request(:get, "https://api.digitalocean.com/v2/droplets?page=1&per_page=1").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer foo', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => fixture('show_droplets'), :headers => {})

stub_request(:get, "https://api.digitalocean.com/v2/droplets?page=1&per_page=200").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer foo', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(:status => 200, :body => fixture('show_droplets'), :headers => {})
@cli.droplets

expect($stdout.string).to include "Started GET request to: https://api.digitalocean.com/v2/droplets?page=1&per_page=200"
expect($stdout.string).to include "DEBUG -- : Request Headers:"

expect($stdout.string).to_not include "Bearer foo"
end
end

end

2 changes: 2 additions & 0 deletions spec/cli/env_variable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
allow(ENV).to receive(:[]).with('HOME').and_return('/tmp/fake_home')
allow(ENV).to receive(:[]).with('DO_API_TOKEN').and_return('env_variable')
allow(ENV).to receive(:[]).with('http_proxy').and_return(nil)
allow(ENV).to receive(:[]).with('DEBUG').and_return(nil)

@cli.verify
expect($stdout.string).to eq "Authentication with DigitalOcean was successful.\n"
Expand All @@ -26,6 +27,7 @@
allow(ENV).to receive(:[]).with('HOME').and_return('/tmp/fake_home')
allow(ENV).to receive(:[]).with('DO_API_TOKEN').and_return('')
allow(ENV).to receive(:[]).with('http_proxy').and_return(nil)
allow(ENV).to receive(:[]).with('DEBUG').and_return(nil)

@cli.verify
expect($stdout.string).to eq "Authentication with DigitalOcean was successful.\n"
Expand Down
2 changes: 1 addition & 1 deletion tugboat.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
gem.required_ruby_version = '>= 1.9.2'

gem.add_dependency 'thor', '~> 0.18.1'
gem.add_dependency 'barge', '~> 0.10.0'
gem.add_dependency 'barge', '~> 0.12.0'
gem.add_dependency 'middleware', '~> 0.1.0'

gem.add_development_dependency 'rake'
Expand Down