From 8bdfce9cb143e6b20803f2317dcf392050cdcd81 Mon Sep 17 00:00:00 2001 From: Inbal Tako Date: Thu, 24 Sep 2020 21:07:19 +0300 Subject: [PATCH 1/2] Fix event manager loop --- Gemfile.lock | 2 +- README.md | 25 ++++++++++++++++++- lib/securenative/event_manager.rb | 16 ++++++------ .../spec_securenative_http_client.rb | 24 +++++++++--------- securenative.gemspec | 2 +- spec/spec_api_manager.rb | 2 +- spec/spec_event_manager.rb | 6 ++--- spec/spec_securenative_http_client.rb | 2 +- 8 files changed, 50 insertions(+), 29 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f010140..3f3465d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - securenative (0.1.27) + securenative (0.1.28) GEM remote: https://rubygems.org/ diff --git a/README.md b/README.md index 24db1a8..5d11b7d 100644 --- a/README.md +++ b/README.md @@ -172,4 +172,27 @@ def webhook_endpoint(request) # Checks if request is verified is_verified = securenative.verify_request_payload(request) end - ``` \ No newline at end of file + ``` + +## Extract proxy headers from Cloudflare + +You can specify custom header keys to allow extraction of client ip from different providers. +This example demonstrates the usage of proxy headers for ip extraction from Cloudflare. + +### Option 1: Using config file +```yaml +SECURENATIVE_API_KEY: dsbe27fh3437r2yd326fg3fdg36f43 +SECURENATIVE_PROXY_HEADERS: ["CF-Connecting-IP"] +``` + +Initialize sdk as showed above. + +### Options 2: Using ConfigurationBuilder + +```ruby +require 'securenative/sdk' + +options = SecureNative::SecureNativeOptions.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR', proxy_headers: ['CF-Connecting-IP']) + +SecureNative::SecureNative.init_with_options(options) +``` \ No newline at end of file diff --git a/lib/securenative/event_manager.rb b/lib/securenative/event_manager.rb index 90d952a..ab96bfb 100644 --- a/lib/securenative/event_manager.rb +++ b/lib/securenative/event_manager.rb @@ -78,19 +78,17 @@ def send_sync(event, resource_path, retry_sending) def run loop do @semaphore.synchronize do - next unless !@queue.empty? && @send_enabled - - @queue.each do |item| + if (item = !@queue.empty? && @send_enabled) begin + item = @queue.shift res = @http_client.post(item.url, item.body) - if res.code == '401' + if res.code == "401" item.retry_sending = false - elsif res.code != '200' - raise SecureNativeHttpError, res.status_code + elsif res.code != "200" + @queue.append(item) end SecureNativeLogger.debug("Event successfully sent; #{item.body}") - return res - rescue StandardError => e + rescue Exception => e SecureNativeLogger.error("Failed to send event; #{e}") if item.retry_sending @attempt = 0 if @coefficients.length == @attempt + 1 @@ -104,7 +102,7 @@ def run end end end - sleep @interval / 1000 + sleep @interval / 1000 if @queue.empty? end end diff --git a/out/test/securenative-ruby/spec_securenative_http_client.rb b/out/test/securenative-ruby/spec_securenative_http_client.rb index adc60a7..ec32ed9 100644 --- a/out/test/securenative-ruby/spec_securenative_http_client.rb +++ b/out/test/securenative-ruby/spec_securenative_http_client.rb @@ -9,18 +9,18 @@ it 'makes a simple post call' do options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1') - stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/track"). - with( - body: "{\"event\": \"SOME_EVENT_NAME\"}", - headers: { - 'Accept'=>'*/*', - 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'Authorization'=>'YOUR_API_KEY', - 'Content-Type'=>'application/json', - 'Sn-Version'=>'0.1.22', - 'User-Agent'=>'SecureNative-ruby' - }). - to_return(status: 200, body: "", headers: {}) + stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track') + .with( + body: '{"event": "SOME_EVENT_NAME"}', + headers: { + 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Authorization' => 'YOUR_API_KEY', + 'Content-Type' => 'application/json', + 'Sn-Version' => '0.1.28', + 'User-Agent' => 'SecureNative-ruby' + } + ).to_return(status: 200, body: '', headers: {}) client = SecureNativeHttpClient.new(options) payload = '{"event": "SOME_EVENT_NAME"}' diff --git a/securenative.gemspec b/securenative.gemspec index 9501b83..2e61253 100644 --- a/securenative.gemspec +++ b/securenative.gemspec @@ -6,7 +6,7 @@ require_relative 'lib/securenative/utils/version_utils' Gem::Specification.new do |spec| spec.name = 'securenative' - spec.version = '0.1.27' + spec.version = '0.1.28' spec.authors = ['SecureNative'] spec.email = ['support@securenative.com'] diff --git a/spec/spec_api_manager.rb b/spec/spec_api_manager.rb index 6d5e04e..2035895 100644 --- a/spec/spec_api_manager.rb +++ b/spec/spec_api_manager.rb @@ -63,7 +63,7 @@ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'YOUR_API_KEY', 'Content-Type' => 'application/json', - 'Sn-Version' => '0.1.27', + 'Sn-Version' => '0.1.28', 'User-Agent' => 'SecureNative-ruby' } ).to_return(status: 200, body: '', headers: {}) diff --git a/spec/spec_event_manager.rb b/spec/spec_event_manager.rb index 5aa623e..d3158e8 100644 --- a/spec/spec_event_manager.rb +++ b/spec/spec_event_manager.rb @@ -32,7 +32,7 @@ def initialize 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'YOUR_API_KEY', 'Content-Type' => 'application/json', - 'Sn-Version' => '0.1.27', + 'Sn-Version' => '0.1.28', 'User-Agent' => 'SecureNative-ruby' }) .to_return(status: 200, body: '', headers: {}) @@ -56,7 +56,7 @@ def initialize 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'YOUR_API_KEY', 'Content-Type' => 'application/json', - 'Sn-Version' => '0.1.27', + 'Sn-Version' => '0.1.28', 'User-Agent' => 'SecureNative-ruby' }) .to_return(status: 401, body: '', headers: {}) @@ -77,7 +77,7 @@ def initialize 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'YOUR_API_KEY', 'Content-Type' => 'application/json', - 'Sn-Version' => '0.1.27', + 'Sn-Version' => '0.1.28', 'User-Agent' => 'SecureNative-ruby' }) .to_return(status: 500, body: '', headers: {}) diff --git a/spec/spec_securenative_http_client.rb b/spec/spec_securenative_http_client.rb index 4193ad1..32db261 100644 --- a/spec/spec_securenative_http_client.rb +++ b/spec/spec_securenative_http_client.rb @@ -16,7 +16,7 @@ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'YOUR_API_KEY', 'Content-Type' => 'application/json', - 'Sn-Version' => '0.1.27', + 'Sn-Version' => '0.1.28', 'User-Agent' => 'SecureNative-ruby' }).to_return(status: 200, body: '', headers: {}) From 2d4cb350b42d6fb0fe4c2d7972d3a012fac84454 Mon Sep 17 00:00:00 2001 From: Inbal Tako Date: Thu, 24 Sep 2020 21:18:51 +0300 Subject: [PATCH 2/2] Fix event manager loop --- lib/securenative/event_manager.rb | 5 +++-- spec/spec_securenative.rb | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/securenative/event_manager.rb b/lib/securenative/event_manager.rb index ab96bfb..bf3ae50 100644 --- a/lib/securenative/event_manager.rb +++ b/lib/securenative/event_manager.rb @@ -82,10 +82,11 @@ def run begin item = @queue.shift res = @http_client.post(item.url, item.body) - if res.code == "401" + if res.code == '401' item.retry_sending = false - elsif res.code != "200" + elsif res.code != '200' @queue.append(item) + raise SecureNativeHttpError, res.status_code end SecureNativeLogger.debug("Event successfully sent; #{item.body}") rescue Exception => e diff --git a/spec/spec_securenative.rb b/spec/spec_securenative.rb index f440599..7959c9b 100644 --- a/spec/spec_securenative.rb +++ b/spec/spec_securenative.rb @@ -22,7 +22,7 @@ it 'inits sdk with api key and defaults' do SecureNative::SecureNative._flush - api_key = 'API_KEY' + api_key = "API_KEY" securenative = SecureNative::SecureNative.init_with_api_key(api_key) options = securenative.options