-
Notifications
You must be signed in to change notification settings - Fork 267
/
Rakefile
277 lines (234 loc) · 8.57 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
require 'rspec/core'
require 'rspec/core/rake_task'
require 'json'
require 'yaml'
require 'fileutils'
require 'sequel'
# system database configuration
require_relative 'lib/system/database'
include Intrigue::Core::System::Database
# Config files
$intrigue_basedir = File.dirname(__FILE__)
$intrigue_environment = ENV["INTRIGUE_ENV"] || "development"
$intrigueio_api_key = ENV["INTRIGUEIO_API_KEY"]
$intrigueio_api_hostname = ENV["INTRIGUEIO_API_HOSTNAME"] || "https://api.intrigue.io"
# Configuration and scripts
procfile_file = "#{$intrigue_basedir}/Procfile"
config_directory = "#{$intrigue_basedir}/config"
puma_config_file = "#{$intrigue_basedir}/config/puma.rb"
god_config_file = "#{$intrigue_basedir}/util/god/intrigue.rb"
system_config_file = "#{$intrigue_basedir}/config/config.json"
database_config_file = "#{$intrigue_basedir}/config/database.yml"
sidekiq_config_file = "#{$intrigue_basedir}/config/sidekiq.yml"
redis_config_file = "#{$intrigue_basedir}/config/redis.yml"
control_script = "#{$intrigue_basedir}/util/control.sh"
all_config_files = [
procfile_file,
puma_config_file,
system_config_file,
database_config_file,
sidekiq_config_file,
redis_config_file
]
desc "Clean"
task :clean do
puts "[+] Cleaning up!"
all_config_files.each do |c|
FileUtils.mv c, "#{c}.backup"
end
end
desc "System Cleanup"
task :cleanup do
FileUtils.rm procfile_file, :force => true
FileUtils.rm puma_config_file, :force => true
FileUtils.rm god_config_file, :force => true
FileUtils.rm system_config_file, :force => true
FileUtils.rm database_config_file, :force => true
FileUtils.rm sidekiq_config_file, :force => true
FileUtils.rm redis_config_file, :force => true
FileUtils.rm "#{config_directory}/server.key", :force => true
FileUtils.rm "#{config_directory}/server.crt", :force => true
end
desc "System Update"
task :update do
puts "[+] Downloading latest data files..."
Dir.chdir("#{$intrigue_basedir}/data/"){ puts %x["./get_latest.sh"] }
end
def _get_global_entities
uri = "#{$intrigueio_api_hostname}/api/system/entities/global/entities/?key=#{$intrigueio_api_key}"
begin
puts "[+] Making request for global entities!"
response = RestClient.get(uri)
# handle missing data
return -1 unless response && response.length > 0
j = JSON.parse(response.body)
rescue JSON::ParserError => e
puts "[+] Unable to parse json: #{e}"
return -1
end
j
end
desc "Load Global Namespace"
task :load_global_namespace do
require_relative 'core'
# First, always pull, and load in the global entities
puts "[+] Getting global entities intel from Intrigue.io API"
global_entities = _get_global_entities
# wait a while and try again if we didnt get an bootstrap list
until global_entities && global_entities.kind_of?(Hash)
wait_time = rand(100)
puts "[ ] Trying again after #{wait_time} to get global entities"
sleep wait_time
global_entities = _get_global_entities
# check it first
if global_entities == -1
puts "[-] unable to get global entities intel, retrying..."
next
end
end
# LOAD IT IN
puts "[+] Loading in entities intel from Intrigue.io API"
Intrigue::Core::Model::GlobalEntity.load_global_namespace(global_entities)
global_entities = nil
puts "[+] Done pulling global namespace"
end
desc "System Setup"
task :setup do
puts "[+] Setup initiated!"
## Copy puma config into place
puts "[+] Copying Procfile...."
unless File.exist? procfile_file
puts "[+] Creating.... #{procfile_file}"
FileUtils.cp "#{procfile_file}.default", procfile_file
end
## Copy puma config into place
puts "[+] Copying puma config...."
unless File.exist? puma_config_file
puts "[+] Creating.... #{puma_config_file}"
FileUtils.cp "#{puma_config_file}.default", puma_config_file
end
## Copy puma config into place
puts "[+] Copying god config...."
unless File.exist? god_config_file
puts "[+] Creating.... #{god_config_file}"
FileUtils.cp "#{god_config_file}.default", god_config_file
end
## Copy system config into place
puts "[+] Copying system config...."
if File.exist? system_config_file
puts "[ ] File already exists, skipping: #{system_config_file}"
config = JSON.parse(File.read(system_config_file))
system_password = config["credentials"]["password"]
else
puts "[+] Creating system config: #{system_config_file}"
FileUtils.cp "#{system_config_file}.default", system_config_file
# Set up our password
config = JSON.parse(File.read(system_config_file))
if config["credentials"]["password"] == "AUTOGENERATED"
if ENV["INTRIGUE_PASS"] # if we were passed on the CLI
config["credentials"]["username"] = ENV["INTRIGUE_USER"] || "intrigue"
config["credentials"]["password"] = ENV["INTRIGUE_PASS"]
else # generate a new password
system_password = "#{(0...11).map { ('a'..'z').to_a[rand(26)] }.join}"
puts "[+] GENERATING NEW SYSTEM PASSWORD!"
config["credentials"]["password"] = system_password # Save it
end
system_password = config["credentials"]["password"]
end
# Set up our api_key
if config["credentials"]["api_key"] == "AUTOGENERATED"
if ENV["INTRIGUE_API_KEY"] # if we were passed on the CLI
config["credentials"]["api_key"] = ENV["INTRIGUE_API_KEY"]
else
# generate a new api_key
api_key = "#{(0...11).map { ('a'..'z').to_a[rand(26)] }.join}"
puts "[+] GENERATING NEW SYSTEM API KEY!"
config["credentials"]["api_key"] = api_key # Save it
end
else
api_key = config["credentials"]["api_key"]
end
# write the password / api key s
File.open(system_config_file,"w").puts JSON.pretty_generate(config)
end
# Create SSL Cert
unless (File.exist?("#{$intrigue_basedir}/config/server.key") || File.exist?("#{$intrigue_basedir}/config/server.crt"))
puts "[+] Generating A new self-signed SSL Certificate..."
Dir.chdir("#{$intrigue_basedir}/config/"){
subject_name = "/C=GB/ST=London/L=London/O=Global Security/OU=IT Department/CN=intrigue.local"
command = "openssl req -subj '#{subject_name}' -new -newkey rsa:2048 -sha256 -days 365 -nodes -x509 -addext 'extendedKeyUsage = serverAuth' -keyout server.key -out server.crt > /dev/null 2>&1"
puts `#{command}`
}
else
puts "[+] SSL Certificate exists..."
end
## Copy database config into place
puts "[+] Copying database config."
unless File.exist? database_config_file
puts "[+] Creating.... #{database_config_file}"
FileUtils.cp "#{database_config_file}.default", database_config_file
# Set up our password
config = YAML.load_file(database_config_file)
config["development"]["password"] = system_password
config["production"]["password"] = system_password
File.open(database_config_file,"w").puts YAML.dump config
end
## Copy redis config into place
puts "[+] Copying redis config."
unless File.exist? redis_config_file
puts "[+] Creating.... #{redis_config_file}"
FileUtils.cp "#{redis_config_file}.default", redis_config_file
end
## Place sidekiq task worker configs into place
puts "[+] Setting up sidekiq config...."
worker_configs = [
sidekiq_config_file
]
worker_configs.each do |wc|
unless File.exist?(wc)
puts "[+] Copying: #{wc}.default"
FileUtils.cp "#{wc}.default", wc
end
end
# end worker config placement
puts "[+] Downloading latest data files..."
Dir.chdir("#{$intrigue_basedir}/data/"){ puts %x["./get_latest.sh"] }
# Print it
puts
puts "[+] ==================================="
puts "[+] SYSTEM USERNAME: intrigue"
puts "[+] SYSTEM PASSWORD: #{system_password}"
puts "[+] ==================================="
puts
puts "[+] Complete!"
end
namespace :db do
desc "Prints current schema version"
task :version do
setup_database
version = if $db.tables.include?(:schema_info)
$db[:schema_info].first[:version]
end || 0
puts "[+] Schema Version: #{version}"
end
desc "Perform migration up to latest migration available"
task :migrate do
setup_database
::Sequel::Migrator.run($db, "db")
Rake::Task['db:version'].execute
end
desc "Perform rollback to specified target or full rollback as default"
task :rollback, :target do |t, args|
setup_database
args.with_defaults(:target => 0)
::Sequel::Migrator.run($db, "db", :target => args[:target].to_i)
Rake::Task['db:version'].execute
end
desc "Perform migration reset (full rollback and migration)"
task :reset do
setup_database
::Sequel::Migrator.run($db, "db", :target => 0)
::Sequel::Migrator.run($db, "db")
Rake::Task['db:version'].execute
end
end