CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/metasploit/framework/require.rb
Views: 1904
1
# @note needs to use explicit nesting. so this file can be loaded directly without loading 'metasploit/framework', this
2
# file can be used prior to Bundler.require.
3
module Metasploit
4
module Framework
5
# Extension to `Kernel#require` behavior.
6
module Require
7
#
8
# Module Methods
9
#
10
11
# Tries to require `name`. If a `LoadError` occurs, then `without_warning` is printed to standard error using
12
# `Kernel#warn`, along with instructions for reinstalling the bundle. If a `LoadError` does not occur, then
13
# `with_block` is called.
14
#
15
# @param name [String] the name of the library to `Kernel#require`.
16
# @param without_warning [String] warning to print if `name` cannot be required.
17
# @yield block to run when `name` requires successfully
18
# @yieldreturn [void]
19
# @return [void]
20
def self.optionally(name, without_warning)
21
begin
22
require name
23
rescue LoadError
24
warn without_warning
25
warn "Bundle installed '--without #{Bundler.settings.without.join(' ')}'"
26
warn "To clear the without option do `bundle install --without ''` " \
27
"(the --without flag with an empty string) or " \
28
"`rm -rf .bundle` to remove the .bundle/config manually and " \
29
"then `bundle install`"
30
else
31
if block_given?
32
yield
33
end
34
end
35
end
36
37
# Tries to `require 'active_record/railtie'` to define the activerecord Rails initializers and rake tasks.
38
#
39
# @example Optionally requiring 'active_record/railtie'
40
# require 'metasploit/framework/require'
41
#
42
# class MyClass
43
# def setup
44
# if database_enabled
45
# Metasploit::Framework::Require.optionally_active_record_railtie
46
# end
47
# end
48
# end
49
#
50
# @return [void]
51
def self.optionally_active_record_railtie
52
if ::Rails.application.config.paths['config/database'].any?
53
optionally(
54
'active_record/railtie',
55
'activerecord not in the bundle, so database support will be disabled.'
56
)
57
else
58
warn 'Could not find database.yml, so database support will be disabled.'
59
end
60
end
61
62
# Tries to `require 'metasploit/credential'` and include `Metasploit::Credential::Creation` in the
63
# `including_module`.
64
#
65
# @param including_module [Module] `Class` or `Module` that wants to `include Metasploit::Credential::Creation`.
66
# @return [void]
67
def self.optionally_include_metasploit_credential_creation(including_module)
68
optionally(
69
'metasploit/credential',
70
"metasploit-credential not in the bundle, so Metasploit::Credential creation will fail for #{including_module.name}"
71
) do
72
including_module.send(:include, Metasploit::Credential::Creation)
73
end
74
end
75
76
# Tries to require gems necessary for using a database with the framework.
77
#
78
# @example
79
# Metasploit::Framework::Require.optionally_require_metasploit_db_gems
80
#
81
# @return [void]
82
def self.optionally_require_metasploit_db_gem_engines
83
optionally(
84
'metasploit/credential',
85
'metasploit-credential not in the bundle',
86
) do
87
require 'metasploit/credential/engine'
88
end
89
90
optionally(
91
'metasploit_data_models',
92
'metasploit_data_models not in the bundle'
93
) do
94
require 'metasploit_data_models/engine'
95
end
96
end
97
98
#
99
# Instance Methods
100
#
101
102
# Tries to `require 'metasploit/credential/creation'` and include it in this `Class` or `Module`.
103
#
104
# @example Using in a `Module`
105
# require 'metasploit/framework/require'
106
#
107
# module MyModule
108
# extend Metasploit::Framework::Require
109
#
110
# optionally_include_metasploit_credential_creation
111
# end
112
#
113
# @return [void]
114
def optionally_include_metasploit_credential_creation
115
Metasploit::Framework::Require.optionally_include_metasploit_credential_creation(self)
116
end
117
end
118
end
119
end
120
121