Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
securenative (0.1.27)
securenative (0.1.28)

GEM
remote: https://rubygems.org/
Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,27 @@ def webhook_endpoint(request)
# Checks if request is verified
is_verified = securenative.verify_request_payload(request)
end
```
```

## 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)
```
11 changes: 5 additions & 6 deletions lib/securenative/event_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,18 @@ 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'
item.retry_sending = false
elsif res.code != '200'
@queue.append(item)
raise SecureNativeHttpError, res.status_code
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
Expand All @@ -104,7 +103,7 @@ def run
end
end
end
sleep @interval / 1000
sleep @interval / 1000 if @queue.empty?
end
end

Expand Down
24 changes: 12 additions & 12 deletions out/test/securenative-ruby/spec_securenative_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"}'

Expand Down
2 changes: 1 addition & 1 deletion securenative.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_api_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {})
Expand Down
6 changes: 3 additions & 3 deletions spec/spec_event_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {})
Expand All @@ -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: {})
Expand All @@ -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: {})
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_securenative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_securenative_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {})

Expand Down