Skip to content

Commit ad2bf48

Browse files
authored
Merge pull request #22 from securenative/dev
Fix event manager loop
2 parents c3979e4 + 2d4cb35 commit ad2bf48

9 files changed

Lines changed: 49 additions & 27 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
securenative (0.1.27)
4+
securenative (0.1.28)
55

66
GEM
77
remote: https://rubygems.org/

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,27 @@ def webhook_endpoint(request)
172172
# Checks if request is verified
173173
is_verified = securenative.verify_request_payload(request)
174174
end
175-
```
175+
```
176+
177+
## Extract proxy headers from Cloudflare
178+
179+
You can specify custom header keys to allow extraction of client ip from different providers.
180+
This example demonstrates the usage of proxy headers for ip extraction from Cloudflare.
181+
182+
### Option 1: Using config file
183+
```yaml
184+
SECURENATIVE_API_KEY: dsbe27fh3437r2yd326fg3fdg36f43
185+
SECURENATIVE_PROXY_HEADERS: ["CF-Connecting-IP"]
186+
```
187+
188+
Initialize sdk as showed above.
189+
190+
### Options 2: Using ConfigurationBuilder
191+
192+
```ruby
193+
require 'securenative/sdk'
194+
195+
options = SecureNative::SecureNativeOptions.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR', proxy_headers: ['CF-Connecting-IP'])
196+
197+
SecureNative::SecureNative.init_with_options(options)
198+
```

lib/securenative/event_manager.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,18 @@ def send_sync(event, resource_path, retry_sending)
7878
def run
7979
loop do
8080
@semaphore.synchronize do
81-
next unless !@queue.empty? && @send_enabled
82-
83-
@queue.each do |item|
81+
if (item = !@queue.empty? && @send_enabled)
8482
begin
83+
item = @queue.shift
8584
res = @http_client.post(item.url, item.body)
8685
if res.code == '401'
8786
item.retry_sending = false
8887
elsif res.code != '200'
88+
@queue.append(item)
8989
raise SecureNativeHttpError, res.status_code
9090
end
9191
SecureNativeLogger.debug("Event successfully sent; #{item.body}")
92-
return res
93-
rescue StandardError => e
92+
rescue Exception => e
9493
SecureNativeLogger.error("Failed to send event; #{e}")
9594
if item.retry_sending
9695
@attempt = 0 if @coefficients.length == @attempt + 1
@@ -104,7 +103,7 @@ def run
104103
end
105104
end
106105
end
107-
sleep @interval / 1000
106+
sleep @interval / 1000 if @queue.empty?
108107
end
109108
end
110109

out/test/securenative-ruby/spec_securenative_http_client.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
it 'makes a simple post call' do
1010
options = ConfigurationBuilder.new(api_key: 'YOUR_API_KEY', api_url: 'https://api.securenative-stg.com/collector/api/v1')
1111

12-
stub_request(:post, "https://api.securenative-stg.com/collector/api/v1/track").
13-
with(
14-
body: "{\"event\": \"SOME_EVENT_NAME\"}",
15-
headers: {
16-
'Accept'=>'*/*',
17-
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
18-
'Authorization'=>'YOUR_API_KEY',
19-
'Content-Type'=>'application/json',
20-
'Sn-Version'=>'0.1.22',
21-
'User-Agent'=>'SecureNative-ruby'
22-
}).
23-
to_return(status: 200, body: "", headers: {})
12+
stub_request(:post, 'https://api.securenative-stg.com/collector/api/v1/track')
13+
.with(
14+
body: '{"event": "SOME_EVENT_NAME"}',
15+
headers: {
16+
'Accept' => '*/*',
17+
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
18+
'Authorization' => 'YOUR_API_KEY',
19+
'Content-Type' => 'application/json',
20+
'Sn-Version' => '0.1.28',
21+
'User-Agent' => 'SecureNative-ruby'
22+
}
23+
).to_return(status: 200, body: '', headers: {})
2424
client = SecureNativeHttpClient.new(options)
2525
payload = '{"event": "SOME_EVENT_NAME"}'
2626

securenative.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require_relative 'lib/securenative/utils/version_utils'
66

77
Gem::Specification.new do |spec|
88
spec.name = 'securenative'
9-
spec.version = '0.1.27'
9+
spec.version = '0.1.28'
1010
spec.authors = ['SecureNative']
1111
spec.email = ['support@securenative.com']
1212

spec/spec_api_manager.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
6464
'Authorization' => 'YOUR_API_KEY',
6565
'Content-Type' => 'application/json',
66-
'Sn-Version' => '0.1.27',
66+
'Sn-Version' => '0.1.28',
6767
'User-Agent' => 'SecureNative-ruby'
6868
}
6969
).to_return(status: 200, body: '', headers: {})

spec/spec_event_manager.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def initialize
3232
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
3333
'Authorization' => 'YOUR_API_KEY',
3434
'Content-Type' => 'application/json',
35-
'Sn-Version' => '0.1.27',
35+
'Sn-Version' => '0.1.28',
3636
'User-Agent' => 'SecureNative-ruby'
3737
})
3838
.to_return(status: 200, body: '', headers: {})
@@ -56,7 +56,7 @@ def initialize
5656
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
5757
'Authorization' => 'YOUR_API_KEY',
5858
'Content-Type' => 'application/json',
59-
'Sn-Version' => '0.1.27',
59+
'Sn-Version' => '0.1.28',
6060
'User-Agent' => 'SecureNative-ruby'
6161
})
6262
.to_return(status: 401, body: '', headers: {})
@@ -77,7 +77,7 @@ def initialize
7777
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
7878
'Authorization' => 'YOUR_API_KEY',
7979
'Content-Type' => 'application/json',
80-
'Sn-Version' => '0.1.27',
80+
'Sn-Version' => '0.1.28',
8181
'User-Agent' => 'SecureNative-ruby'
8282
})
8383
.to_return(status: 500, body: '', headers: {})

spec/spec_securenative.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
it 'inits sdk with api key and defaults' do
2424
SecureNative::SecureNative._flush
25-
api_key = 'API_KEY'
25+
api_key = "API_KEY"
2626
securenative = SecureNative::SecureNative.init_with_api_key(api_key)
2727
options = securenative.options
2828

spec/spec_securenative_http_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
1717
'Authorization' => 'YOUR_API_KEY',
1818
'Content-Type' => 'application/json',
19-
'Sn-Version' => '0.1.27',
19+
'Sn-Version' => '0.1.28',
2020
'User-Agent' => 'SecureNative-ruby'
2121
}).to_return(status: 200, body: '', headers: {})
2222

0 commit comments

Comments
 (0)