r7insight 2.7.6 → 3.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,37 +1,58 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'pathname'
3
5
 
4
- logger = Le.new('185a71d5-32a7-4007-a12d-3295c75a10e2', 'eu')
5
- logger.info("ssl")
6
-
7
- describe Le::Host::HTTP do
6
+ logger = R7Insight.new('185a71d5-32a7-4007-a12d-3295c75a10e2', 'eu')
7
+ logger.info('ssl')
8
8
 
9
+ describe R7Insight::Host::CONNECTION do
9
10
  let(:token) { '11111111-2222-3333-aaaa-bbbbbbbbbbbb' }
10
- let(:region) {'eu'}
11
+ let(:region) { 'eu' }
11
12
  let(:local) { false }
12
13
  let(:debug) { false }
13
14
  let(:ssl) { false }
14
15
  let(:udp) { nil }
15
16
 
16
- let(:datahub_endpoint) { ["", 10000]}
17
- let(:host_id) {""}
18
- let(:custom_host) {[false, ""]}
19
- let(:endpoint) {false}
20
-
21
-
22
- let(:host) { Le::Host::HTTP.new(token, region, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp, endpoint) }
23
-
24
- let(:logger_console) { host.instance_variable_get(:@logger_console) }
25
- let(:logger_console_dev) { logger_console.instance_variable_get(:@logdev).dev }
26
-
27
- specify { host.must_be_instance_of Le::Host::HTTP }
28
- specify {host.region.must_equal 'eu'}
29
- specify { host.local.must_equal false }
30
- specify { host.debug.must_equal false }
31
- specify { host.ssl.must_equal false }
32
- specify { host.udp_port.must_equal nil }
33
- specify {host_id.must_equal ""}
34
- specify {custom_host.must_equal [false, ""]}
35
-
36
-
17
+ let(:datahub_endpoint) { ['', 10_000] }
18
+ let(:host_id) { '' }
19
+ let(:custom_host) { [false, ''] }
20
+ let(:endpoint) { false }
21
+
22
+ let(:host) do
23
+ R7Insight::Host::CONNECTION.new(token, region, local,
24
+ debug, ssl,
25
+ datahub_endpoint,
26
+ host_id, custom_host,
27
+ udp, endpoint)
28
+ end
29
+
30
+ let(:logger_console) { host.instance_variable_get(:@logger_console) }
31
+
32
+ describe 'host' do
33
+ it 'is an instance of CONNECTION' do
34
+ assert_instance_of(R7Insight::Host::CONNECTION, host)
35
+ end
36
+ it 'region is expected value' do
37
+ assert_equal(host.region, 'eu')
38
+ end
39
+ it 'local is expected value' do
40
+ refute(host.local)
41
+ end
42
+ it 'debug is expected value' do
43
+ refute(host.debug)
44
+ end
45
+ it 'ssl is expected value' do
46
+ refute(host.ssl)
47
+ end
48
+ it 'udp port is expected value' do
49
+ assert_nil(host.udp_port)
50
+ end
51
+ it 'id is expected value' do
52
+ assert_equal(host_id, '')
53
+ end
54
+ it 'custom_host is expected value' do
55
+ assert_equal(custom_host, [false, ''])
56
+ end
57
+ end
37
58
  end
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'minitest/autorun'
2
- require 'le'
4
+ require 'r7_insight'
metadata CHANGED
@@ -1,77 +1,94 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: r7insight
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.6
4
+ version: 3.0.4
5
5
  platform: ruby
6
6
  authors:
7
- - Mark Lacomber
8
- - Stephen Hynes
7
+ - Rapid7
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2017-12-06 00:00:00.000000000 Z
11
+ date: 2020-05-28 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- name: bundler
14
+ name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
16
  requirements:
18
- - - ">="
17
+ - - "~>"
19
18
  - !ruby/object:Gem::Version
20
- version: '0'
19
+ version: 6.0.0
21
20
  type: :development
22
21
  prerelease: false
23
22
  version_requirements: !ruby/object:Gem::Requirement
24
23
  requirements:
25
- - - ">="
24
+ - - "~>"
26
25
  - !ruby/object:Gem::Version
27
- version: '0'
26
+ version: 6.0.0
28
27
  - !ruby/object:Gem::Dependency
29
- name: rake
28
+ name: bundler
30
29
  requirement: !ruby/object:Gem::Requirement
31
30
  requirements:
32
- - - ">="
31
+ - - "~>"
33
32
  - !ruby/object:Gem::Version
34
- version: '0'
33
+ version: 2.0.0
35
34
  type: :development
36
35
  prerelease: false
37
36
  version_requirements: !ruby/object:Gem::Requirement
38
37
  requirements:
39
- - - ">="
38
+ - - "~>"
40
39
  - !ruby/object:Gem::Version
41
- version: '0'
40
+ version: 2.0.0
42
41
  - !ruby/object:Gem::Dependency
43
42
  name: minitest
44
43
  requirement: !ruby/object:Gem::Requirement
45
44
  requirements:
46
- - - ">="
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.12.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.12.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
47
60
  - !ruby/object:Gem::Version
48
- version: '0'
61
+ version: 13.0.0
49
62
  type: :development
50
63
  prerelease: false
51
64
  version_requirements: !ruby/object:Gem::Requirement
52
65
  requirements:
53
- - - ">="
66
+ - - "~>"
54
67
  - !ruby/object:Gem::Version
55
- version: '0'
56
- description: "\n\n"
57
- email: support@rapid7.com
68
+ version: 13.0.0
69
+ description: " Rapid7 Insight Platform Ruby library for logging\n"
70
+ email: InsightOpsTeam@rapid7.com
58
71
  executables: []
59
72
  extensions: []
60
73
  extra_rdoc_files: []
61
74
  files:
62
75
  - ".gitignore"
76
+ - ".travis.yml"
77
+ - CONTRIBUTING.md
63
78
  - Gemfile
64
- - LE.gemspec
79
+ - Gemfile.lock
65
80
  - LICENSE
81
+ - Makefile
66
82
  - README.md
67
83
  - Rakefile
68
- - lib/le.rb
69
- - lib/le/host.rb
70
- - lib/le/host/connection.rb
84
+ - lib/r7_insight.rb
85
+ - lib/r7_insight/host.rb
86
+ - lib/r7_insight/host/connection.rb
87
+ - r7insight.gemspec
71
88
  - test/fixtures/log/.gitignore
72
89
  - test/host_spec.rb
73
90
  - test/http_spec.rb
74
- - test/le_spec.rb
91
+ - test/r7insight_spec.rb
75
92
  - test/region.rb
76
93
  - test/spec_helper.rb
77
94
  homepage: https://github.com/rapid7/r7insight_ruby
@@ -93,15 +110,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
110
  - !ruby/object:Gem::Version
94
111
  version: '0'
95
112
  requirements: []
96
- rubyforge_project:
97
- rubygems_version: 2.5.2
113
+ rubygems_version: 3.0.8
98
114
  signing_key:
99
115
  specification_version: 4
100
- summary: InsightOps logging plugin
116
+ summary: Rapid7 Insight Platform logging plugin
101
117
  test_files:
102
118
  - test/fixtures/log/.gitignore
103
119
  - test/host_spec.rb
104
120
  - test/http_spec.rb
105
- - test/le_spec.rb
121
+ - test/r7insight_spec.rb
106
122
  - test/region.rb
107
123
  - test/spec_helper.rb
data/LE.gemspec DELETED
@@ -1,29 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'le'
5
-
6
- Gem::Specification.new do |gem|
7
- gem.name = "r7insight"
8
- gem.version = "2.7.6"
9
- gem.date = Time.now
10
- gem.summary = "InsightOps logging plugin"
11
- gem.licenses = ["MIT"]
12
- gem.description =<<EOD
13
-
14
-
15
- EOD
16
-
17
- gem.authors = ["Mark Lacomber", "Stephen Hynes"]
18
- gem.email = "support@rapid7.com"
19
- gem.homepage = "https://github.com/rapid7/r7insight_ruby"
20
- gem.files = `git ls-files`.split($/)
21
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
22
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
23
- gem.require_paths = ["lib"]
24
-
25
- gem.add_development_dependency "bundler"
26
- gem.add_development_dependency "rake"
27
- gem.add_development_dependency 'minitest'
28
-
29
- end
@@ -1,315 +0,0 @@
1
- require 'socket'
2
- require 'openssl'
3
- require 'thread'
4
- require 'timeout'
5
- require 'uri'
6
-
7
- module Le
8
- module Host
9
- class CONNECTION
10
- DATA_ENDPOINT = '.data.logs.insight.rapid7.com'
11
- DATA_PORT_UNSECURE = 80
12
- DATA_PORT_SECURE = 443
13
- API_SSL_PORT = 20000
14
- SHUTDOWN_COMMAND = "DIE!DIE!" # magic command string for async worker to shutdown
15
- SHUTDOWN_MAX_WAIT = 10 # max seconds to wait for queue to clear on shutdown
16
- SHUTDOWN_WAIT_STEP = 0.2 # sleep duration (seconds) while waiting to shutdown
17
-
18
-
19
- include Le::Host::InstanceMethods
20
- #! attr_accessor :token, :queue, :started, :thread, :conn, :local, :debug, :ssl, :datahub_enabled, :dathub_ip, :datahub_port, :host_id, :custom_host, :host_name_enabled, :host_name
21
- attr_accessor :token, :region,:queue, :started, :thread, :conn, :local, :debug, :ssl, :datahub_enabled, :datahub_ip, :datahub_port, :datahub_endpoint, :host_id, :host_name_enabled, :host_name, :custom_host, :udp_port, :use_data_endpoint
22
-
23
-
24
- def initialize(token, region, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint)
25
- if local
26
- device = if local.class <= TrueClass
27
- if defined?(Rails)
28
- Rails.root.join("log","#{Rails.env}.log")
29
- else
30
- STDOUT
31
- end
32
- else
33
- local
34
- end
35
- @logger_console = Logger.new(device)
36
- end
37
-
38
- @region = region
39
- @local = !!local
40
- @debug= debug
41
- @ssl = ssl
42
- @udp_port = udp_port
43
- @use_data_endpoint = use_data_endpoint
44
-
45
- @datahub_endpoint = datahub_endpoint
46
- if !@datahub_endpoint[0].empty?
47
- @datahub_enabled=true
48
- @datahub_ip="#{@datahub_endpoint[0]}"
49
- @datahub_port=@datahub_endpoint[1]
50
- else
51
- @datahub_enabled=false
52
- end
53
-
54
-
55
- if (@datahub_enabled && @ssl)
56
- puts ("\n\nYou Cannot have DataHub and SSL enabled at the same time. Please set SSL value to false in your environment.rb file or used Token-Based logging by leaving the Datahub IP address blank. Exiting application. \n\n")
57
- exit
58
- end
59
-
60
- #check if region was specified
61
- if region.empty?
62
- puts ("\n\nYou need to specify a region. Options: eu, us")
63
- end
64
-
65
- #check if DataHub is enabled... if datahub is not enabled, set the token to the token's parameter. If DH is enabled, make the token empty.
66
- if (!datahub_enabled)
67
- @token = token
68
- else
69
- @token = ''
70
-
71
- #! NOTE THIS @datahub_port conditional MAY NEED TO BE CHANGED IF SSL CAN'T WORK WITH DH
72
- @datahub_port = @datahub_port.empty? ? API_SSL_PORT : datahub_port
73
- @datahub_ip = datahub_ip
74
- end
75
-
76
- @host_name_enabled=custom_host[0];
77
- @host_name= custom_host[1];
78
-
79
-
80
- # Check if host_id is empty -- if not assign, if so, make it an empty string.
81
- if !host_id.empty?
82
- @host_id = host_id
83
- @host_id = "host_id=#{host_id}"
84
- else
85
- @host_id=''
86
- end
87
-
88
-
89
-
90
- #assign host_name, if no host name is given and host_name_enabled = true... assign a host_name based on the machine name.
91
- if @host_name_enabled
92
- if host_name.empty?
93
- @host_name=Socket.gethostname
94
- end
95
-
96
- @host_name="host_name=#{@host_name}"
97
- end
98
-
99
-
100
- @queue = Queue.new
101
- @started = false
102
- @thread = nil
103
-
104
- if @debug
105
- self.init_debug
106
- end
107
- at_exit { shutdown! }
108
- end
109
-
110
- def init_debug
111
- filePath = "r7insightGem.log"
112
- if File.exist?('log/')
113
- filePath = "log/r7insightGem.log"
114
- end
115
- @debug_logger = Logger.new(filePath)
116
- end
117
-
118
- def dbg(message)
119
- if @debug
120
- @debug_logger.add(Logger::Severity::DEBUG, message)
121
- end
122
- end
123
-
124
- def write(message)
125
-
126
- if !host_id.empty?
127
- message = "#{message} #{ host_id }"
128
- end
129
-
130
- if host_name_enabled
131
- message="#{message} #{ host_name }"
132
- end
133
-
134
- if @local
135
- @logger_console.add(Logger::Severity::UNKNOWN, message)
136
- end
137
-
138
- if message.scan(/\n/).empty?
139
- @queue << "#{ @token } #{ message } \n"
140
- else
141
- @queue << "#{ message.gsub(/^/, "#{ @token } [#{ random_message_id }]") }\n"
142
- end
143
-
144
-
145
- if @started
146
- check_async_thread
147
- else
148
- start_async_thread
149
- end
150
- end
151
-
152
- def start_async_thread
153
- @thread = Thread.new { run() }
154
- dbg "LE: Asynchronous socket writer started"
155
- @started = true
156
- end
157
-
158
- def check_async_thread
159
- if not(@thread && @thread.alive?)
160
- @thread = Thread.new { run() }
161
- end
162
- end
163
-
164
- def close
165
- dbg "LE: Closing asynchronous socket writer"
166
- @started = false
167
- end
168
-
169
- def openConnection
170
- dbg "LE: Reopening connection to Logentries API server"
171
-
172
-
173
- if @use_data_endpoint
174
- host = @region + DATA_ENDPOINT
175
-
176
- if @ssl
177
- port = DATA_PORT_SECURE
178
- else
179
- port = DATA_PORT_UNSECURE
180
- end
181
- else
182
- if @udp_port
183
- host = @region + DATA_ENDPOINT
184
- port = @udp_port
185
- elsif @datahub_enabled
186
- host = @datahub_ip
187
- port = @datahub_port
188
- else
189
- host = @region + DATA_ENDPOINT
190
- port = @ssl ? DATA_PORT_SECURE: DATA_PORT_UNSECURE
191
- end
192
- end
193
-
194
- if @udp_port
195
- @conn = UDPSocket.new
196
- @conn.connect(host, port)
197
- else
198
- socket = TCPSocket.new(host, port)
199
-
200
- if @ssl
201
- cert_store = OpenSSL::X509::Store.new
202
- cert_store.set_default_paths
203
-
204
- ssl_context = OpenSSL::SSL::SSLContext.new()
205
- ssl_context.cert_store = cert_store
206
-
207
- ssl_version_candidates = [:TLSv1_2, :TLSv1_1, :TLSv1]
208
- ssl_version_candidates = ssl_version_candidates.select { |version| OpenSSL::SSL::SSLContext::METHODS.include? version }
209
- if ssl_version_candidates.empty?
210
- raise "Could not find suitable TLS version"
211
- end
212
- # currently we only set the version when we have no choice
213
- ssl_context.ssl_version = ssl_version_candidates[0] if ssl_version_candidates.length == 1
214
- ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
215
- ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
216
- ssl_socket.hostname = host if ssl_socket.respond_to?(:hostname=)
217
- ssl_socket.sync_close = true
218
- Timeout::timeout(10) do
219
- ssl_socket.connect
220
- end
221
- @conn = ssl_socket
222
- else
223
- @conn = socket
224
- end
225
- end
226
-
227
- dbg "LE: Connection established"
228
- end
229
-
230
- def reopenConnection
231
- closeConnection
232
- root_delay = 0.1
233
- loop do
234
- begin
235
- openConnection
236
- break
237
- rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError
238
- dbg "LE: Unable to connect to Logentries due to timeout(#{ $! })"
239
- rescue
240
- dbg "LE: Got exception in reopenConnection - #{ $! }"
241
- raise
242
- end
243
- root_delay *= 2
244
- if root_delay >= 10
245
- root_delay = 10
246
- end
247
- wait_for = (root_delay + rand(root_delay)).to_i
248
- dbg "LE: Waiting for #{ wait_for }ms"
249
- sleep(wait_for)
250
- end
251
- end
252
-
253
- def closeConnection
254
- begin
255
- if @conn.respond_to?(:sysclose)
256
- @conn.sysclose
257
- elsif @conn.respond_to?(:close)
258
- @conn.close
259
- end
260
- rescue
261
- dbg "LE: couldn't close connection, close with exception - #{ $! }"
262
- ensure
263
- @conn = nil
264
- end
265
- end
266
-
267
- def run
268
- reopenConnection
269
-
270
- loop do
271
- data = @queue.pop
272
- break if data == SHUTDOWN_COMMAND
273
- loop do
274
- begin
275
- @conn.write(data)
276
- rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError
277
- dbg "LE: Connection timeout(#{ $! }), try to reopen connection"
278
- reopenConnection
279
- next
280
- rescue
281
- dbg("LE: Got exception in run loop - #{ $! }")
282
- raise
283
- end
284
-
285
- break
286
- end
287
- end
288
-
289
- dbg "LE: Closing Asynchronous socket writer"
290
-
291
- closeConnection
292
- end
293
-
294
- private
295
- def random_message_id
296
- @random_message_id_sample_space ||= ('0'..'9').to_a.concat(('A'..'Z').to_a)
297
- (0..5).map{ @random_message_id_sample_space.sample }.join
298
- end
299
-
300
- # at_exit handler.
301
- # Attempts to clear the queue and terminate the async worker cleanly before process ends.
302
- def shutdown!
303
- return unless @started
304
- dbg "LE: commencing shutdown, queue has #{queue.size} entries to clear"
305
- queue << SHUTDOWN_COMMAND
306
- SHUTDOWN_MAX_WAIT.div(SHUTDOWN_WAIT_STEP).times do
307
- break if queue.empty?
308
- sleep SHUTDOWN_WAIT_STEP
309
- end
310
- dbg "LE: shutdown complete, queue is #{queue.empty? ? '' : 'not '}empty with #{queue.size} entries"
311
- end
312
-
313
- end
314
- end
315
- end