diff --git a/api-v3-client-examples/README.md b/api-v3-client-examples/README.md new file mode 100644 index 0000000..e212fb9 --- /dev/null +++ b/api-v3-client-examples/README.md @@ -0,0 +1,15 @@ +# api-v3-client-example + +## Overview +The examples provided outline how to auto generate libraries for the Nexpose/InsightVM RESTful api using Swagger +Codegen. The setup_workspace.rb script is meant to quickly get started generating the files and sample scripts +leveraging the generated library have been place in the samples/ directory. + +## Use +Below are the very basic steps for getting started. Feel free to experiment and see what best fits your needs: +1. Ensure Java is installed and on path; required for Swagger Codegen library +2. Update setup_workspace.rb with the appropriate Nexpose/InsightVM console URL +3. Run setup_workspace.rb +4. Update samples/settings.yml with console connection details (URL, Username, Password) +5. Profit! + \ No newline at end of file diff --git a/api-v3-client-examples/ruby/config.json b/api-v3-client-examples/ruby/config.json new file mode 100644 index 0000000..7894b0c --- /dev/null +++ b/api-v3-client-examples/ruby/config.json @@ -0,0 +1,7 @@ +{ + "gemName": "rapid7_vm_console", + "moduleName": "Rapid7VmConsole", + "gemVersion": "3.0.0-6.5.0", + "gemSummary": "A ruby wrapper for the Rapid7 Nexpose and InsightVM Console API v3", + "gemLicense": "MIT" +} \ No newline at end of file diff --git a/api-v3-client-examples/ruby/samples/credential/create_shared_credential.rb b/api-v3-client-examples/ruby/samples/credential/create_shared_credential.rb new file mode 100644 index 0000000..35cb90c --- /dev/null +++ b/api-v3-client-examples/ruby/samples/credential/create_shared_credential.rb @@ -0,0 +1,50 @@ +#!/usr/bin/env ruby + +# Add lib to load path to use client without installing gem +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) + +if File.exist?(File.join(lib_dir, 'rapid7_vm_console.rb')) + $LOAD_PATH.unshift lib_dir +else + abort('ABORT: rapid7_vm_console.rb not on path or gem not installed') +end + +require 'base64' +require 'yaml' + +# Generated swagger client +require 'rapid7_vm_console' + +settings = YAML.load_file('../settings.yml') + +config = Rapid7VmConsole::Configuration.new() +config.scheme = 'https' +config.host=settings[:console][:host] +config.username = settings[:console][:user] +config.password = settings[:console][:pass] +config.verify_ssl = settings[:console][:verify_ssl] +config.verify_ssl_host = settings[:console][:verify_ssl_host] +if settings[:console][:debug] + config.logger = Logger.new(STDOUT) + config.debugging = true +end + +client = Rapid7VmConsole::ApiClient.new(config) +client.default_headers['Authorization'] = "Basic #{Base64.strict_encode64("#{config.username}:#{config.password}")}" + +credential_client = Rapid7VmConsole::CredentialApi.new(client) +site_client = Rapid7VmConsole::SiteApi.new(client) + +site_in_scope = 'Example' +site_id = site_client.get_sites.resources.select{|site| site.name.eql?(site_in_scope)}.first.id + +begin + account = Rapid7VmConsole::SharedCredentialAccount.new(service: 'ssh', username: 'username', password: 'password') + shared_credential = Rapid7VmConsole::SharedCredential.new(account: account, description: 'Example SSH Credential', + name: 'Example SSH', siteAssignment: 'specific-sites', sites: [site_id]) + puts credential_client.create_shared_credential({:param0 => shared_credential}) +rescue Rapid7VmConsole::ApiError => re + puts re.response_body +rescue Exception => e + puts e +end \ No newline at end of file diff --git a/api-v3-client-examples/ruby/samples/credential/list_shared_credentials.rb b/api-v3-client-examples/ruby/samples/credential/list_shared_credentials.rb new file mode 100644 index 0000000..6905251 --- /dev/null +++ b/api-v3-client-examples/ruby/samples/credential/list_shared_credentials.rb @@ -0,0 +1,44 @@ +#!/usr/bin/env ruby + +# Add lib to load path to use client without installing gem +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) + +if File.exist?(File.join(lib_dir, 'rapid7_vm_console.rb')) + $LOAD_PATH.unshift lib_dir +else + abort('ABORT: rapid7_vm_console.rb not on path or gem not installed') +end + +require 'base64' +require 'yaml' + +# Generated swagger client +require 'rapid7_vm_console' + +settings = YAML.load_file('../settings.yml') + +config = Rapid7VmConsole::Configuration.new() +config.scheme = 'https' +config.host=settings[:console][:host] +config.username = settings[:console][:user] +config.password = settings[:console][:pass] +config.verify_ssl = settings[:console][:verify_ssl] +config.verify_ssl_host = settings[:console][:verify_ssl_host] +if settings[:console][:debug] + config.logger = Logger.new(STDOUT) + config.debugging = true +end + +client = Rapid7VmConsole::ApiClient.new(config) +client.default_headers['Authorization'] = "Basic #{Base64.strict_encode64("#{config.username}:#{config.password}")}" + +credential_client = Rapid7VmConsole::CredentialApi.new(client) + +begin + shared_credentials = credential_client.get_shared_credentials + puts shared_credentials.resources +rescue Rapid7VmConsole::ApiError => re + puts re.response_body +rescue Exception => e + puts e +end \ No newline at end of file diff --git a/api-v3-client-examples/ruby/samples/credential/list_site_credentials.rb b/api-v3-client-examples/ruby/samples/credential/list_site_credentials.rb new file mode 100644 index 0000000..0d469ff --- /dev/null +++ b/api-v3-client-examples/ruby/samples/credential/list_site_credentials.rb @@ -0,0 +1,52 @@ +#!/usr/bin/env ruby + +# Add lib to load path to use client without installing gem +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) + +if File.exist?(File.join(lib_dir, 'rapid7_vm_console.rb')) + $LOAD_PATH.unshift lib_dir +else + abort('ABORT: rapid7_vm_console.rb not on path or gem not installed') +end + +require 'base64' +require 'yaml' + +# Generated swagger client +require 'rapid7_vm_console' + +settings = YAML.load_file('../settings.yml') + +config = Rapid7VmConsole::Configuration.new() +config.scheme = 'https' +config.host=settings[:console][:host] +config.username = settings[:console][:user] +config.password = settings[:console][:pass] +config.verify_ssl = settings[:console][:verify_ssl] +config.verify_ssl_host = settings[:console][:verify_ssl_host] +if settings[:console][:debug] + config.logger = Logger.new(STDOUT) + config.debugging = true +end + +client = Rapid7VmConsole::ApiClient.new(config) +client.default_headers['Authorization'] = "Basic #{Base64.strict_encode64("#{config.username}:#{config.password}")}" + +site_client = Rapid7VmConsole::SiteApi.new(client) + +site_in_scope = 'Example' +site_id = site_client.get_sites.resources.select{|site| site.name.eql?(site_in_scope)}.first.id + +begin + site_credentials = site_client.get_site_credentials(site_id) + puts 'Site Credentials:' + puts site_credentials.resources.empty? ? 'None' : site_credentials.resources + + site_shared_credentials = site_client.get_site_shared_credentials(site_id) + puts 'Site Shared Credentials:' + puts site_shared_credentials.resources.empty? ? 'None' : site_shared_credentials.resources.each{|cred| cred.name} +rescue Rapid7VmConsole::ApiError => re + puts re.response_body +rescue Exception => e + puts e +end \ No newline at end of file diff --git a/api-v3-client-examples/ruby/samples/report/create_sql_report.rb b/api-v3-client-examples/ruby/samples/report/create_sql_report.rb new file mode 100644 index 0000000..06552fb --- /dev/null +++ b/api-v3-client-examples/ruby/samples/report/create_sql_report.rb @@ -0,0 +1,54 @@ +#!/usr/bin/env ruby + +# Add lib to load path to use client without installing gem +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) + +if File.exist?(File.join(lib_dir, 'rapid7_vm_console.rb')) + $LOAD_PATH.unshift lib_dir +else + abort('ABORT: rapid7_vm_console.rb not on path or gem not installed') +end + +require 'base64' +require 'yaml' + +# Generated swagger client +require 'rapid7_vm_console' + +settings = YAML.load_file('../settings.yml') + +config = Rapid7VmConsole::Configuration.new() +config.scheme = 'https' +config.host=settings[:console][:host] +config.username = settings[:console][:user] +config.password = settings[:console][:pass] +config.verify_ssl = settings[:console][:verify_ssl] +config.verify_ssl_host = settings[:console][:verify_ssl_host] +if settings[:console][:debug] + config.logger = Logger.new(STDOUT) + config.debugging = true +end + +client = Rapid7VmConsole::ApiClient.new(config) +client.default_headers['Authorization'] = "Basic #{Base64.strict_encode64("#{config.username}:#{config.password}")}" + +report_name = 'Example Assets Query' +report_query = <<~SQL + select * from dim_asset +SQL + +report_client = Rapid7VmConsole::ReportApi.new(client) +site_client = Rapid7VmConsole::SiteApi.new(client) + +site_in_scope = 'Example' +site_id = site_client.get_sites.resources.select{|site| site.name.eql?(site_in_scope)}.first.id + +scope = Rapid7VmConsole::ReportConfigScopeResource.new({sites: [site_id]}) +report_config = Rapid7VmConsole::Report.new(name: report_name, format: 'sql-query', scope: scope, query: report_query, version: '2.3.0') + +begin + response = report_client.create_report({:param0 => report_config}) + puts response +rescue Exception => e + puts e.response_body +end \ No newline at end of file diff --git a/api-v3-client-examples/ruby/samples/report/list_reports.rb b/api-v3-client-examples/ruby/samples/report/list_reports.rb new file mode 100644 index 0000000..c15fced --- /dev/null +++ b/api-v3-client-examples/ruby/samples/report/list_reports.rb @@ -0,0 +1,50 @@ +#!/usr/bin/env ruby + +# Add lib to load path to use client without installing gem +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) + +if File.exist?(File.join(lib_dir, 'rapid7_vm_console.rb')) + $LOAD_PATH.unshift lib_dir +else + abort('ABORT: rapid7_vm_console.rb not on path or gem not installed') +end + +require 'base64' +require 'yaml' + +# Generated swagger client +require 'rapid7_vm_console' + +settings = YAML.load_file('../settings.yml') + +config = Rapid7VmConsole::Configuration.new() +config.scheme = 'https' +config.host=settings[:console][:host] +config.username = settings[:console][:user] +config.password = settings[:console][:pass] +config.verify_ssl = settings[:console][:verify_ssl] +config.verify_ssl_host = settings[:console][:verify_ssl_host] +if settings[:console][:debug] + config.logger = Logger.new(STDOUT) + config.debugging = true +end + +client = Rapid7VmConsole::ApiClient.new(config) +client.default_headers['Authorization'] = "Basic #{Base64.strict_encode64("#{config.username}:#{config.password}")}" + +report_client = Rapid7VmConsole::ReportApi.new(client) + +reports = [] +catch (:reports_fetched) do + opts = {page: 0, size: 10} + + while true + reportsPage = report_client.get_reports(opts) + reports.concat(reportsPage.resources) + + opts[:page] += 1 + throw(:reports_fetched) if reportsPage.page.total_pages <= opts[:page] + end +end + +puts reports \ No newline at end of file diff --git a/api-v3-client-examples/ruby/samples/report/run_report.rb b/api-v3-client-examples/ruby/samples/report/run_report.rb new file mode 100644 index 0000000..2aaaa49 --- /dev/null +++ b/api-v3-client-examples/ruby/samples/report/run_report.rb @@ -0,0 +1,77 @@ +#!/usr/bin/env ruby + +# Add lib to load path to use client without installing gem +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) + +if File.exist?(File.join(lib_dir, 'rapid7_vm_console.rb')) + $LOAD_PATH.unshift lib_dir +else + abort('ABORT: rapid7_vm_console.rb not on path or gem not installed') +end + +require 'base64' +require 'yaml' + +# Generated swagger client +require 'rapid7_vm_console' + +settings = YAML.load_file('../settings.yml') + +config = Rapid7VmConsole::Configuration.new() +config.scheme = 'https' +config.host=settings[:console][:host] +config.username = settings[:console][:user] +config.password = settings[:console][:pass] +config.verify_ssl = settings[:console][:verify_ssl] +config.verify_ssl_host = settings[:console][:verify_ssl_host] +if settings[:console][:debug] + config.logger = Logger.new(STDOUT) + config.debugging = true +end + +client = Rapid7VmConsole::ApiClient.new(config) +client.default_headers['Authorization'] = "Basic #{Base64.strict_encode64("#{config.username}:#{config.password}")}" + +report_name = 'Example Assets Query' +report_client = Rapid7VmConsole::ReportApi.new(client) + +done = false +opts = {page: 0, size: 10} + +until done + reportsPage = report_client.get_reports() + + reportsPage.resources.each do |report| + if report.name.eql?(report_name) + puts "Found Report Configuration [#{report.name}]; Generating Report..." + + # Generate Report + report_instance = report_client.generate_report(report.id) + + # Check report status + report_done = false + + until report_done + report_instance_status = report_client.get_report_instance(report.id,report_instance.id) + + if %w(aborted failed complete).include?(report_instance_status.status) + report_done = true + + # Download report + report_contents = report_client.download_report(report.id,report_instance.id) + # Save contents or process + puts report_contents + else + sleep(5) + end + + puts "Report Name [#{report.name}] status: #{report_instance_status.status}" + end + + done = true + end + end + + opts[:page] += 1 + done = true if reportsPage.page.total_pages <= opts[:page] +end \ No newline at end of file diff --git a/api-v3-client-examples/ruby/samples/scan/list_site_scans.rb b/api-v3-client-examples/ruby/samples/scan/list_site_scans.rb new file mode 100644 index 0000000..6331ea5 --- /dev/null +++ b/api-v3-client-examples/ruby/samples/scan/list_site_scans.rb @@ -0,0 +1,53 @@ +#!/usr/bin/env ruby + +# Add lib to load path to use client without installing gem +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) + +if File.exist?(File.join(lib_dir, 'rapid7_vm_console.rb')) + $LOAD_PATH.unshift lib_dir +else + abort('ABORT: rapid7_vm_console.rb not on path or gem not installed') +end + +require 'base64' +require 'yaml' + +# Generated swagger client +require 'rapid7_vm_console' + +settings = YAML.load_file('../settings.yml') + +config = Rapid7VmConsole::Configuration.new() +config.scheme = 'https' +config.host=settings[:console][:host] +config.username = settings[:console][:user] +config.password = settings[:console][:pass] +config.verify_ssl = settings[:console][:verify_ssl] +config.verify_ssl_host = settings[:console][:verify_ssl_host] +if settings[:console][:debug] + config.logger = Logger.new(STDOUT) + config.debugging = true +end + +client = Rapid7VmConsole::ApiClient.new(config) +client.default_headers['Authorization'] = "Basic #{Base64.strict_encode64("#{config.username}:#{config.password}")}" + +scan_client = Rapid7VmConsole::ScanApi.new(client) +site_client = Rapid7VmConsole::SiteApi.new(client) + +site_in_scope = 'Example' +site_id = site_client.get_sites.resources.select{|site| site.name.eql?(site_in_scope)}.first.id + +done = false + +opts = {page: 0, size: 10} +until done + scans_page = scan_client.get_site_scans(site_id, opts) + + scans_page.resources.each do |scan| + puts "Scan Name: #{scan.scan_name}; Link: #{scan.links.select{|link| link.rel.eql?('self')}.first.href}" + end + + opts[:page] += 1 + done = true if scans_page.page.total_pages <= opts[:page] +end \ No newline at end of file diff --git a/api-v3-client-examples/ruby/samples/settings.yml b/api-v3-client-examples/ruby/samples/settings.yml new file mode 100644 index 0000000..b2a1538 --- /dev/null +++ b/api-v3-client-examples/ruby/samples/settings.yml @@ -0,0 +1,8 @@ +--- +:console: + :host: 'localhost:3780' + :user: 'nxadmin' + :pass: 'nxpassword' + :verify_ssl: false + :verify_ssl_host: false + :debug: false diff --git a/api-v3-client-examples/ruby/samples/site/create_site.rb b/api-v3-client-examples/ruby/samples/site/create_site.rb new file mode 100644 index 0000000..06798d2 --- /dev/null +++ b/api-v3-client-examples/ruby/samples/site/create_site.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby + +# Add lib to load path to use client without installing gem +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) + +if File.exist?(File.join(lib_dir, 'rapid7_vm_console.rb')) + $LOAD_PATH.unshift lib_dir +else + abort('ABORT: rapid7_vm_console.rb not on path or gem not installed') +end + +require 'base64' +require 'yaml' + +# Generated swagger client +require 'rapid7_vm_console' + +settings = YAML.load_file('../settings.yml') + +config = Rapid7VmConsole::Configuration.new() +config.scheme = 'https' +config.host=settings[:console][:host] +config.username = settings[:console][:user] +config.password = settings[:console][:pass] +config.verify_ssl = settings[:console][:verify_ssl] +config.verify_ssl_host = settings[:console][:verify_ssl_host] +if settings[:console][:debug] + config.logger = Logger.new(STDOUT) + config.debugging = true +end + +# Create api client and set Authorization header for basic auth +client = Rapid7VmConsole::ApiClient.new(config) +client.default_headers['Authorization'] = "Basic #{Base64.strict_encode64("#{config.username}:#{config.password}")}" + +# Define list of included addresses +addresses = Rapid7VmConsole::IncludedScanTargets.new({:addresses => ['10.3.23.200','10.3.23.201','10.3.23.202']}) +# Set list of addresses as target for static site +assets = Rapid7VmConsole::StaticSite.new({:includedTargets => addresses}) +# Set scan scope for site +scan = Rapid7VmConsole::ScanScope.new({:assets => assets}) + +# Define attributes and create new site +attributes = {:description => 'Example Description', :name => 'Example', :scan => scan} +site = Rapid7VmConsole::SiteCreateResource.new(attributes) + +# Create Site +begin + # Save site with api + site_client = Rapid7VmConsole::SiteApi.new(client) + site_reference = site_client.create_site({:param0 => site}) + + puts "Site ID: #{site_reference.id}" + puts "Site link: #{site_reference.links.select{|link|link.rel.eql?('Site')}.first.href}" +rescue Rapid7VmConsole::ApiError => re + puts re.response_body +rescue Exception => e + puts e +end diff --git a/api-v3-client-examples/ruby/samples/site/create_site_schedule.rb b/api-v3-client-examples/ruby/samples/site/create_site_schedule.rb new file mode 100644 index 0000000..21ff3b4 --- /dev/null +++ b/api-v3-client-examples/ruby/samples/site/create_site_schedule.rb @@ -0,0 +1,73 @@ +#!/usr/bin/env ruby + +# Add lib to load path to use client without installing gem +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) + +if File.exist?(File.join(lib_dir, 'rapid7_vm_console.rb')) + $LOAD_PATH.unshift lib_dir +else + abort('ABORT: rapid7_vm_console.rb not on path or gem not installed') +end + +require 'base64' +require 'time' +require 'yaml' + +# Generated swagger client +require 'rapid7_vm_console' + +settings = YAML.load_file('../settings.yml') + +config = Rapid7VmConsole::Configuration.new() +config.scheme = 'https' +config.host=settings[:console][:host] +config.username = settings[:console][:user] +config.password = settings[:console][:pass] +config.verify_ssl = settings[:console][:verify_ssl] +config.verify_ssl_host = settings[:console][:verify_ssl_host] +if settings[:console][:debug] + config.logger = Logger.new(STDOUT) + config.debugging = true +end + +client = Rapid7VmConsole::ApiClient.new(config) +client.default_headers['Authorization'] = "Basic #{Base64.strict_encode64("#{config.username}:#{config.password}")}" + +site_client = Rapid7VmConsole::SiteApi.new(client) + +done = false +site_in_scope = 'Example' + +opts = {page: 0, size: 10} +until done + sites_page = site_client.get_sites(opts) + + sites_page.resources.each do |site| + if site.name.eql?(site_in_scope) + puts "Found Site [#{site.name}]; Creating Schedule..." + + # Create Scan Schedule + attributes = { :scanName => 'Scripted Schedule', + :start => Time.new(2018, 6, 10, 8, 00, 00, "-05:00").iso8601, + :onScanRepeat => 'restart-scan', + :scanEngineId => 1, + :scanTemplateId => 'discovery', + :enabled => true } + site_schedule = Rapid7VmConsole::ScanSchedule.new(attributes) + + begin + puts site_client.create_site_scan_schedule(site.id, {:param0 => site_schedule.to_hash}) + rescue Rapid7VmConsole::ApiError => re + puts re.response_body + rescue Exception => e + puts e + end + + done = true + end + end + + + opts[:page] += 1 + done = true if sites_page.page.total_pages <= opts[:page] +end \ No newline at end of file diff --git a/api-v3-client-examples/ruby/samples/site/list_sites.rb b/api-v3-client-examples/ruby/samples/site/list_sites.rb new file mode 100644 index 0000000..dfdda39 --- /dev/null +++ b/api-v3-client-examples/ruby/samples/site/list_sites.rb @@ -0,0 +1,57 @@ +#!/usr/bin/env ruby + +# Add lib to load path to use client without installing gem +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) + +if File.exist?(File.join(lib_dir, 'rapid7_vm_console.rb')) + $LOAD_PATH.unshift lib_dir +else + abort('ABORT: rapid7_vm_console.rb not on path or gem not installed') +end + +require 'base64' +require 'yaml' + +# Generated swagger client +require 'rapid7_vm_console' + +settings = YAML.load_file('../settings.yml') + +config = Rapid7VmConsole::Configuration.new() +config.scheme = 'https' +config.host=settings[:console][:host] +config.username = settings[:console][:user] +config.password = settings[:console][:pass] +config.verify_ssl = settings[:console][:verify_ssl] +config.verify_ssl_host = settings[:console][:verify_ssl_host] +if settings[:console][:debug] + config.logger = Logger.new(STDOUT) + config.debugging = true +end + +client = Rapid7VmConsole::ApiClient.new(config) +client.default_headers['Authorization'] = "Basic #{Base64.strict_encode64("#{config.username}:#{config.password}")}" + +site_client = Rapid7VmConsole::SiteApi.new(client) + +done = false + +opts = {page: 0, size: 10} +until done + begin + sites_page = site_client.get_sites(opts) + rescue Rapid7VmConsole::ApiError => re + puts re.response_body + exit + rescue Exception => e + puts e + exit + end + + sites_page.resources.each do |site| + puts "Site Name: #{site.name}; Link: #{site.links.select{|link| link.rel.eql?('self')}.first.href}" + end + + opts[:page] += 1 + done = true if sites_page.page.total_pages <= opts[:page] +end \ No newline at end of file diff --git a/api-v3-client-examples/ruby/samples/site/list_sites_schedules.rb b/api-v3-client-examples/ruby/samples/site/list_sites_schedules.rb new file mode 100644 index 0000000..8a709c6 --- /dev/null +++ b/api-v3-client-examples/ruby/samples/site/list_sites_schedules.rb @@ -0,0 +1,61 @@ +#!/usr/bin/env ruby + +# Add lib to load path to use client without installing gem +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) + +if File.exist?(File.join(lib_dir, 'rapid7_vm_console.rb')) + $LOAD_PATH.unshift lib_dir +else + abort('ABORT: rapid7_vm_console.rb not on path or gem not installed') +end + +require 'base64' +require 'yaml' + +# Generated swagger client +require 'rapid7_vm_console' + +settings = YAML.load_file('../settings.yml') + +config = Rapid7VmConsole::Configuration.new() +config.scheme = 'https' +config.host=settings[:console][:host] +config.username = settings[:console][:user] +config.password = settings[:console][:pass] +config.verify_ssl = settings[:console][:verify_ssl] +config.verify_ssl_host = settings[:console][:verify_ssl_host] +if settings[:console][:debug] + config.logger = Logger.new(STDOUT) + config.debugging = true +end + +client = Rapid7VmConsole::ApiClient.new(config) +client.default_headers['Authorization'] = "Basic #{Base64.strict_encode64("#{config.username}:#{config.password}")}" + +site_client = Rapid7VmConsole::SiteApi.new(client) + +done = false + +opts = {page: 0, size: 10} +until done + sites_page = site_client.get_sites(opts) + + sites_page.resources.each do |site| + puts "Site: #{site.name}" + site_client.get_site_scan_schedules(site.id).resources.each do |site_schedule| + puts " Schedule Name: #{site_schedule.scan_name}" + puts " -- Template: #{site_schedule.scan_template_id}; Enabled: #{site_schedule.enabled}" + puts " -- Repeat: #{site_schedule.repeat.nil? ? 'None' : site_schedule.repeat.every}" + + schedule = site_client.get_site_scan_schedule(site.id, site_schedule.id) + if schedule.assets.nil? + puts " -- Scope: ALL" + else + puts " -- Scope: Sub #{schedule.assets}" + end + end + end + + opts[:page] += 1 + done = true if sites_page.page.total_pages <= opts[:page] +end \ No newline at end of file diff --git a/api-v3-client-examples/ruby/samples/site/scan_site.rb b/api-v3-client-examples/ruby/samples/site/scan_site.rb new file mode 100644 index 0000000..9c4a254 --- /dev/null +++ b/api-v3-client-examples/ruby/samples/site/scan_site.rb @@ -0,0 +1,75 @@ +#!/usr/bin/env ruby + +# Add lib to load path to use client without installing gem +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) + +if File.exist?(File.join(lib_dir, 'rapid7_vm_console.rb')) + $LOAD_PATH.unshift lib_dir +else + abort('ABORT: rapid7_vm_console.rb not on path or gem not installed') +end + +require 'base64' +require 'yaml' + +# Generated swagger client +require 'rapid7_vm_console' + +settings = YAML.load_file('../settings.yml') + +config = Rapid7VmConsole::Configuration.new() +config.scheme = 'https' +config.host=settings[:console][:host] +config.username = settings[:console][:user] +config.password = settings[:console][:pass] +config.verify_ssl = settings[:console][:verify_ssl] +config.verify_ssl_host = settings[:console][:verify_ssl_host] +if settings[:console][:debug] + config.logger = Logger.new(STDOUT) + config.debugging = true +end + +client = Rapid7VmConsole::ApiClient.new(config) +client.default_headers['Authorization'] = "Basic #{Base64.strict_encode64("#{config.username}:#{config.password}")}" + +site_client = Rapid7VmConsole::SiteApi.new(client) + +done = false +site_to_scan = 'Example' + +opts = {page: 0, size: 10} +until done + sitesPage = site_client.get_sites(opts) + + sitesPage.resources.each do |site| + if site.name.eql?(site_to_scan) + puts "Found Site [#{site.name}]; Starting Scan..." + + # Run Scan + scan_client = Rapid7VmConsole::ScanApi.new(client) + scan_opts = {name: "Example Scan #{DateTime.now}", templateId: 'discovery'} + scan_reference = scan_client.start_scan(site.id, {param1: scan_opts}) + + # Check Scan Status + scan_done = false + + until scan_done + scan_status = scan_client.get_scan(scan_reference.id) + + if %w(aborted finished stopped error).include?(scan_status.status) + scan_done = true + else + sleep(10) + end + + puts "Scan Name [#{scan_status.scan_name}] status: #{scan_status.status}" + end + + done = true + end + end + + + opts[:page] += 1 + done = true if sitesPage.page.total_pages <= opts[:page] +end \ No newline at end of file diff --git a/api-v3-client-examples/ruby/setup_workspace.rb b/api-v3-client-examples/ruby/setup_workspace.rb new file mode 100644 index 0000000..b8c2f87 --- /dev/null +++ b/api-v3-client-examples/ruby/setup_workspace.rb @@ -0,0 +1,62 @@ +# setup_workspace.rb +# +# Purpose: Generation of Ruby based API client for the Rapid7 Nexpose and InsightVM v3 API +# +# Requirements: Java install and on PATH +# +# Configuration: Update CONSOLE_URL parameter with appropriate value +# +# Output: Generation of api and models based on Rapid7 Nexpose and InsightVM Swagger file +# + +require 'fileutils' +require 'json' +require 'net/http' +require 'net/https' + +CONSOLE_URL = 'https://localhost:3780' +CODEGEN_JAR_NAME = 'swagger-codegen-cli' +CODEGEN_JAR_VERSION = '2.3.0' + +# Download swagger codegen jar +uri = URI("http://central.maven.org/maven2/io/swagger/#{CODEGEN_JAR_NAME}/#{CODEGEN_JAR_VERSION}/#{CODEGEN_JAR_NAME}-#{CODEGEN_JAR_VERSION}.jar") + +Net::HTTP.start(uri.host, uri.port) do |http| + request = Net::HTTP::Get.new uri + + http.request request do |response| + open "#{CODEGEN_JAR_NAME}-#{CODEGEN_JAR_VERSION}.jar", 'w' do |io| + response.read_body do |chunk| + io.write chunk + end + end + end +end + +# Download swagger file +console_swagger_path = "#{CONSOLE_URL}/api/3/json" +swagger_file = 'console-swagger.json' + +uri = URI(console_swagger_path) + +Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http| + request = Net::HTTP::Get.new uri + + http.request request do |response| + open swagger_file, 'w' do |io| + response.read_body do |chunk| + io.write chunk + end + end + end +end + +# Remove previous directories from generation +dirs = %w(lib docs spec) + +dirs.each do |dir| + FileUtils.rm_rf(dir) +end + +# Generate library +system("java -jar #{CODEGEN_JAR_NAME}-#{CODEGEN_JAR_VERSION}.jar generate -i #{swagger_file} -l ruby -o ./ -c config.json")