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

scripts/nexpose-apiv3-examples - examples scripts using generated library #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions api-v3-client-examples/README.md
Original file line number Diff line number Diff line change
@@ -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!

7 changes: 7 additions & 0 deletions api-v3-client-examples/ruby/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions api-v3-client-examples/ruby/samples/report/create_sql_report.rb
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions api-v3-client-examples/ruby/samples/report/list_reports.rb
Original file line number Diff line number Diff line change
@@ -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
77 changes: 77 additions & 0 deletions api-v3-client-examples/ruby/samples/report/run_report.rb
Original file line number Diff line number Diff line change
@@ -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
Loading