diff options
| author | mokha <mokha@cisco.com> | 2018-05-24 16:51:51 -0600 |
|---|---|---|
| committer | mokha <mokha@cisco.com> | 2018-05-24 16:51:51 -0600 |
| commit | a5eb3ff39f3803dafe50e371892a19325f7fb568 (patch) | |
| tree | 70ba267385aef535f2029dbd86b75592d2a04603 | |
| parent | c0017d316402c2f5505b6376d287b726637aa877 (diff) | |
use net/hippie and support bearer token.
| -rw-r--r-- | ats-cli.gemspec | 1 | ||||
| -rw-r--r-- | lib/ats.rb | 2 | ||||
| -rw-r--r-- | lib/ats/amp4e/api.rb | 12 | ||||
| -rw-r--r-- | lib/ats/http_api.rb | 79 | ||||
| -rw-r--r-- | lib/ats/shiro/api.rb | 2 | ||||
| -rw-r--r-- | lib/ats/threat_grid/api.rb | 2 |
6 files changed, 13 insertions, 85 deletions
diff --git a/ats-cli.gemspec b/ats-cli.gemspec index 76c6c53..01a437c 100644 --- a/ats-cli.gemspec +++ b/ats-cli.gemspec @@ -25,6 +25,7 @@ Gem::Specification.new do |spec| spec.required_ruby_version = '>= 2.5.0' spec.add_dependency 'thor', '~> 0.20' + spec.add_dependency 'net-hippie', '0.1.8' spec.add_development_dependency "bundler", "~> 1.16" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec", "~> 3.0" @@ -1,11 +1,11 @@ require 'base64' require 'json' require 'logger' +require 'net/hippie' require 'net/http' require 'yaml' require 'ats/configuration' -require 'ats/http_api' require 'ats/version' require 'ats/amp4e/api' diff --git a/lib/ats/amp4e/api.rb b/lib/ats/amp4e/api.rb index 1664ec8..d6d00b5 100644 --- a/lib/ats/amp4e/api.rb +++ b/lib/ats/amp4e/api.rb @@ -7,14 +7,16 @@ module ATS 'User-Agent' => "RubyGems/ATS #{ATS::VERSION}", }.freeze - attr_reader :http, :port, :host, :scheme, :client_id, :client_secret + attr_reader :http, :port, :host, :scheme + attr_reader :bearer_token, :client_id, :client_secret def initialize(configuration:, debug: false) - @http = HttpAPI.new(headers: HEADERS, debug: debug) + @http = Net::Hippie::Client.new(headers: HEADERS, verify_mode: debug ? OpenSSL::SSL::VERIFY_NONE : nil) @configuration = configuration @port = configuration[:port] @scheme = configuration[:scheme] @host = configuration[:host] + @bearer_token = configuration[:bearer_token] @client_id = configuration[:client_id] @client_secret = configuration[:client_secret] end @@ -48,7 +50,11 @@ module ATS end def headers - { AUTHORIZATION: "Basic #{Base64.strict_encode64("#{client_id}:#{client_secret}")}" } + if bearer_token + { AUTHORIZATION: "Bearer #{bearer_token}" } + else + { AUTHORIZATION: "Basic #{Base64.strict_encode64("#{client_id}:#{client_secret}")}" } + end end end end diff --git a/lib/ats/http_api.rb b/lib/ats/http_api.rb deleted file mode 100644 index 0da1a87..0000000 --- a/lib/ats/http_api.rb +++ /dev/null @@ -1,79 +0,0 @@ -require 'openssl' - -module ATS - class HttpAPI - def initialize(headers: {}, debug: false) - @default_headers = headers - @debug = debug - end - - def execute(uri, request) - http_for(uri).request(request) - end - - def get(uri, headers: {}, body: {}) - request = get_for(uri, headers: headers, body: body) - response = execute(uri, request) - if block_given? - yield request, response - else - response - end - end - - def post(uri, headers: {}, body: {}) - request = post_for(uri, headers: headers, body: body) - response = execute(uri, request) - if block_given? - yield request, response - else - response - end - end - - def put(uri, headers: {}, body: {}) - request = put_for(uri, headers: headers, body: body) - response = execute(uri, request) - if block_given? - yield request, response - else - response - end - end - - private - - def http_for(uri) - http = Net::HTTP.new(uri.host, uri.port) - http.read_timeout = 30 - http.use_ssl = uri.scheme == "https" - if debug? - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - http.set_debug_output(ATS.logger) - end - http - end - - def post_for(uri, headers: {}, body: {}) - Net::HTTP::Post.new(uri.path, @default_headers.merge(headers)).tap do |x| - x.body = JSON.generate(body) - end - end - - def put_for(uri, headers: {}, body: {}) - Net::HTTP::Put.new(uri.path, @default_headers.merge(headers)).tap do |x| - x.body = JSON.generate(body) - end - end - - def get_for(uri, headers: {}, body: {}) - Net::HTTP::Get.new(uri.path, @default_headers.merge(headers)).tap do |x| - x.body = JSON.generate(body) - end - end - - def debug? - @debug - end - end -end diff --git a/lib/ats/shiro/api.rb b/lib/ats/shiro/api.rb index a818189..e148496 100644 --- a/lib/ats/shiro/api.rb +++ b/lib/ats/shiro/api.rb @@ -10,7 +10,7 @@ module ATS attr_reader :http, :port, :host, :scheme, :bearer_token def initialize(configuration:, debug: false) - @http = HttpAPI.new(headers: HEADERS, debug: debug) + @http = Net::Hippie::Client.new(headers: HEADERS, verify_mode: debug ? OpenSSL::SSL::VERIFY_NONE : nil) @configuration = configuration @port = configuration[:port] @scheme = configuration[:scheme] diff --git a/lib/ats/threat_grid/api.rb b/lib/ats/threat_grid/api.rb index 1f2fa83..4c71612 100644 --- a/lib/ats/threat_grid/api.rb +++ b/lib/ats/threat_grid/api.rb @@ -9,7 +9,7 @@ module ATS attr_reader :http, :port, :host, :scheme, :api_key def initialize(configuration:, debug: false) - @http = HttpAPI.new(headers: HEADERS, debug: debug) + @http = Net::Hippie::Client.new(headers: HEADERS, verify_mode: debug ? OpenSSL::SSL::VERIFY_NONE : nil) @configuration = configuration @port = configuration[:port] @scheme = configuration[:scheme] |
