diff options
| author | mokha <mokha@cisco.com> | 2018-03-17 17:48:49 -0600 |
|---|---|---|
| committer | mokha <mokha@cisco.com> | 2018-03-17 17:48:49 -0600 |
| commit | 554ab67d524a4293dca5a762cf063648a50ed2fb (patch) | |
| tree | 42f1b265929ed8f300432122ed36e376e8e146d6 | |
| parent | 6135bea4a511530dd68512997a808df7556238b7 (diff) | |
connect to session/whoami
| -rw-r--r-- | lib/ats/cli.rb | 5 | ||||
| -rw-r--r-- | lib/ats/cli/http_api.rb | 74 | ||||
| -rw-r--r-- | lib/ats/cli/threat_grid/api.rb | 22 | ||||
| -rw-r--r-- | lib/ats/cli/threatgrid.rb | 7 |
4 files changed, 103 insertions, 5 deletions
diff --git a/lib/ats/cli.rb b/lib/ats/cli.rb index 3e9b42a..c91fa83 100644 --- a/lib/ats/cli.rb +++ b/lib/ats/cli.rb @@ -1,10 +1,15 @@ require 'thor' +require 'logger' require 'ats/cli/threatgrid' require 'ats/cli/version' module ATS module CLI + def self.logger + @logger ||= Logger.new(STDOUT) + end + class Application < Thor desc 'threatgrid SUBCOMMAND ...ARGS', 'interact with the Threat Grid API' subcommand 'threatgrid', Threatgrid diff --git a/lib/ats/cli/http_api.rb b/lib/ats/cli/http_api.rb new file mode 100644 index 0000000..7a0b5b1 --- /dev/null +++ b/lib/ats/cli/http_api.rb @@ -0,0 +1,74 @@ +require 'json' +require 'net/http' + +module ATS + module CLI + class HttpAPI + def initialize(headers: {}) + @default_headers = headers + 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.is_a?(URI::HTTPS) + http.set_debug_output(ATS::CLI.logger) + 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 + end + end +end diff --git a/lib/ats/cli/threat_grid/api.rb b/lib/ats/cli/threat_grid/api.rb index 66f021d..f95500d 100644 --- a/lib/ats/cli/threat_grid/api.rb +++ b/lib/ats/cli/threat_grid/api.rb @@ -1,3 +1,5 @@ +require 'ats/cli/http_api' + module ATS module CLI module ThreatGrid @@ -7,11 +9,29 @@ module ATS 'User-Agent' => "ATS/CLI #{ATS::CLI::VERSION}", }.freeze - def initialize(api: HttpApi.new(headers: HEADERS), api_key:, api_host:) + attr_reader :http, :api_key, :api_host + + def initialize(api: HttpAPI.new(headers: HEADERS), api_key:, api_host:) @http = api @api_key = api_key @api_host = api_host end + + def whoami + http.get(build_uri("session/whoami"), body: default_payload) do |request, response| + JSON.parse(response.body, symbolize_names: true)[:data] + end + end + + private + + def build_uri(relative_url) + URI.parse("#{api_host}/api/v3/#{relative_url}") + end + + def default_payload + { api_key: api_key } + end end end end diff --git a/lib/ats/cli/threatgrid.rb b/lib/ats/cli/threatgrid.rb index 80cd988..62d1a28 100644 --- a/lib/ats/cli/threatgrid.rb +++ b/lib/ats/cli/threatgrid.rb @@ -8,8 +8,7 @@ module ATS desc 'whoami', 'whoami' def whoami - puts configuration.inspect - say api.whoami + say JSON.pretty_generate(api.whoami) end private @@ -19,11 +18,11 @@ module ATS end def api_key - configuration[:threatgrid][:api_key] + configuration['threatgrid']['api_key'] end def api_host - configuration[:threatgrid][:api_host] + configuration['threatgrid']['api_host'] end def configuration |
