Module: Ronin::DB

Defined in:
lib/ronin/db.rb,
lib/ronin/db/cli.rb,
lib/ronin/db/home.rb,
lib/ronin/db/root.rb,
lib/ronin/db/tasks.rb,
lib/ronin/db/version.rb,
lib/ronin/db/exceptions.rb,
lib/ronin/db/cli/command.rb,
lib/ronin/db/config_file.rb,
lib/ronin/db/cli/printing.rb,
lib/ronin/db/cli/deletable.rb,
lib/ronin/db/cli/importable.rb,
lib/ronin/db/cli/modifiable.rb,
lib/ronin/db/cli/ruby_shell.rb,
lib/ronin/db/cli/uri_methods.rb,
lib/ronin/db/cli/commands/add.rb,
lib/ronin/db/cli/commands/asn.rb,
lib/ronin/db/cli/commands/ips.rb,
lib/ronin/db/cli/commands/irb.rb,
lib/ronin/db/cli/commands/edit.rb,
lib/ronin/db/cli/commands/list.rb,
lib/ronin/db/cli/commands/oses.rb,
lib/ronin/db/cli/commands/urls.rb,
lib/ronin/db/cli/model_command.rb,
lib/ronin/db/cli/commands/certs.rb,
lib/ronin/db/cli/commands/creds.rb,
lib/ronin/db/cli/commands/hosts.rb,
lib/ronin/db/cli/commands/ports.rb,
lib/ronin/db/cli/commands/emails.rb,
lib/ronin/db/cli/commands/people.rb,
lib/ronin/db/cli/commands/remove.rb,
lib/ronin/db/cli/commands/migrate.rb,
lib/ronin/db/cli/database_options.rb,
lib/ronin/db/cli/commands/services.rb,
lib/ronin/db/cli/commands/software.rb,
lib/ronin/db/cli/commands/passwords.rb,
lib/ronin/db/cli/commands/web_vulns.rb,
lib/ronin/db/cli/commands/completion.rb,
lib/ronin/db/cli/commands/open_ports.rb,
lib/ronin/db/cli/commands/phone_numbers.rb,
lib/ronin/db/cli/commands/street_addresses.rb

Overview

Manages the Ronin database.

Defined Under Namespace

Modules: ConfigFile, Home Classes: CLI, InvalidConfig, Tasks, UnknownDatabase

Constant Summary collapse

ROOT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Path to ronin-db root directory.

File.expand_path(File.join(__dir__,'..','..','..'))
VERSION =

ronin-db version

'0.2.0'

Class Method Summary collapse

Class Method Details

.configHash{Symbol => Hash}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

ronin-db database(s) configuration.

Returns:

  • (Hash{Symbol => Hash})


47
48
49
# File 'lib/ronin/db.rb', line 47

def self.config
  @config ||= ConfigFile.load
end

.connect(database = :default, pool: nil, migrate: nil, load_models: true) ⇒ Object

Connects to the Database.

Examples:

Connect to the default database (~/.local/share/ronin-db/default.sqlite3):

DB.connect

Connect to a specific database from the configuration file (~/.config/ronin-db/databases.yml):

DB.connect(:my_database)

Connect to an arbitrary database:

Db.connect({adapter: 'sqlite3', database: '/path/to/database.sqlite3'})

Parameters:

  • database (Symbol, String, Hash) (defaults to: :default)

    The optional database name, String URL, or Hash of database information.

  • pool (Integer, nil) (defaults to: nil)

    Sets the connection pool for the database.

  • migrate (Boolean) (defaults to: nil)

    Specifies whether to hard or lazy migrate the database.

  • load_models (Boolean) (defaults to: true)

    Specifies whether to load all models or just connect to the database.

Raises:

  • (UnknownDatabase)

    The database name was not listed in the config file.

  • (ArgumentError)

    The given database was not a Symbol, String, or a Hash.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ronin/db.rb', line 108

def self.connect(database=:default, pool: nil,
                                    migrate: nil,
                                    load_models: true)
  config = case database
           when Hash   then database
           when String then {url: database}
           when Symbol
             self.config.fetch(database) do
               raise(UnknownDatabase,"unknown database: #{database.inspect}")
             end
           else
             raise(ArgumentError,"#{self}.#{__method__} only accepts a Symbol or a Hash")
           end

  # set/override the connection pool setting
  config = config.merge(pool: pool) if pool

  # load activerecord
  require 'active_record'

  # connect to the database
  ActiveRecord::Base.establish_connection(config)

  # migrate the database if necessary
  if migrate == true then migrate!
  else                    self.migrate
  end

  if load_models
    # require and connect all models
    require 'ronin/db/models'
    Models.connect
  end

  return true
end

.logger=(new_logger) ⇒ Object

Sets up the Database logger.



35
36
37
38
# File 'lib/ronin/db.rb', line 35

def self.logger=(new_logger)
  require 'active_record'
  ActiveRecord::Base.logger = new_logger
end

.migrateObject

Only migrate the database if the database is empty, otherwise warn the user that there are pending migrations.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ronin/db.rb', line 63

def self.migrate
  require 'ronin/db/migrations'

  if Migrations.current_version == 0
    # auto-run the migrations when the database is empty
    Migrations.migrate
  elsif Migrations.needs_migration?
    # warn the user that there are pending migrations, instead of
    # auto-running migrations each time
    warn "WARNING: Database requires migrating!"
  end
end

.migrate!Object

Migrate the database up and apply any pending migrations.



54
55
56
57
# File 'lib/ronin/db.rb', line 54

def self.migrate!
  require 'ronin/db/migrations'
  Migrations.migrate
end