Skip to content
Merged

Dev #14

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
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ source "https://rubygems.org"
gemspec
gem "rspec"
gem "rake"
gem "parseconfig"
gem "simplecov", :require => false, :group => :test
gem "codecov", :require => false, :group => :test
gem "webmock", :require => false, :group => :test
Expand Down
26 changes: 12 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ GEM
crack (0.4.3)
safe_yaml (~> 1.0.0)
crass (1.0.6)
diff-lcs (1.3)
diff-lcs (1.4.4)
docile (1.3.2)
dry-configurable (0.11.6)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -174,7 +174,6 @@ GEM
nio4r (2.5.2)
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
parseconfig (1.0.8)
public_suffix (4.0.5)
rack (2.2.3)
rack-protection (2.0.8.1)
Expand Down Expand Up @@ -208,19 +207,19 @@ GEM
rake (>= 0.8.7)
thor (>= 0.20.3, < 2.0)
rake (13.0.1)
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-core (3.8.2)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.4)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.2)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.1)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.2)
rspec-support (~> 3.9.0)
rspec-support (3.9.3)
ruby2_keywords (0.0.2)
safe_yaml (1.0.5)
simplecov (0.19.0)
Expand Down Expand Up @@ -263,7 +262,6 @@ DEPENDENCIES
bundler (~> 2.0)
codecov
hanami
parseconfig
rails
rake
rspec
Expand Down
69 changes: 37 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Or install it yourself as:
To get your *API KEY*, login to your SecureNative account and go to project settings page:

### Option 1: Initialize via Config file
SecureNative can automatically load your config from *securenative.cfg* file or from the file that is specified in your *SECURENATIVE_CONFIG_FILE* env variable:
SecureNative can automatically load your config from *securenative.yml* file or from the file that is specified in your *SECURENATIVE_CONFIG_FILE* env variable:

```ruby
require 'securenative'
Expand All @@ -69,7 +69,8 @@ securenative = SecureNative.init_with_api_key('YOUR_API_KEY')
require 'securenative'


securenative = SecureNative.init_with_options(SecureNative.config_builder(api_key = 'API_KEY', max_event = 10, log_level = 'ERROR'))
options = ConfigurationBuilder.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR')
SecureNative.init_with_options(options)
```

## Getting SecureNative instance
Expand All @@ -88,42 +89,46 @@ instance. Make sure you build event with the EventBuilder:

```ruby
require 'securenative'
require 'securenative/enums/event_types'
require 'securenative/event_options_builder'
require 'securenative/models/user_traits'
require 'securenative/context/context_builder'
require 'models/event_options'
require 'enums/event_types'
require 'models/user_traits'


securenative = SecureNative.instance

context = securenative.context_builder(ip = '127.0.0.1', client_token = 'SECURED_CLIENT_TOKEN',
headers = { 'user-agent' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405' })

event_options = EventOptions(event_type = EventTypes::LOG_IN,
user_id = '1234', user_traits = UserTraits('Your Name', 'name@gmail.com', '+1234567890'),
context = context, properties = {prop1 => 'CUSTOM_PARAM_VALUE', prop2 => true, prop3 => 3}).build

securenative.track(event_options)
def track
securenative = SecureNative.instance
context = SecureNativeContext.new(client_token: '2a980d872b939c7e4f4378aa111a5eeffb22808b58b5372f658d34904ebd5b05fff0daab91921243ac08b72442a5b3992e402dc21df16aa7cc0e19f8bffa9d6cc59996d480d70aa22b857189403675d37fd144ebaf9dc697fed149b907678f2b1f964d73b332dc8ea7df63fcfc3c11f7bbb51ba2672652ca7d5d43f36a62e15db8b13dfd794a5eccfc5968ca514dd7cce59f2df2b9d8184d076eba808c81b311', ip: '127.0.0.1',
headers: { 'user-agent' => 'Mozilla: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.3 Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/43.4' })

event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
user_traits: UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })

securenative.track(event_options)

@message = 'tracked'
end
```

You can also create request context from requests:

```ruby
require 'securenative'
require 'securenative/enums/event_types'
require 'securenative/event_options_builder'
require 'securenative/models/user_traits'
require 'models/event_options'
require 'enums/event_types'
require 'models/user_traits'


def track(request)
def track
securenative = SecureNative.instance
context = SecureNative.context_builder.from_http_request(request).build

event_options = EventOptions(event_type = EventTypes::LOG_IN,
user_id = '1234', user_traits = UserTraits('Your Name', 'name@gmail.com', '+1234567890'),
context = context, properties = {prop1 => 'CUSTOM_PARAM_VALUE', prop2 => true, prop3 => 3}).build
context = SecureNativeContext.from_http_request(request)
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
user_traits: UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })

securenative.track(event_options)

@message = 'tracked'
end
```

Expand All @@ -133,18 +138,18 @@ end

```ruby
require 'securenative'
require 'securenative/enums/event_types'
require 'securenative/event_options_builder'
require 'securenative/models/user_traits'
require 'models/event_options'
require 'enums/event_types'
require 'models/user_traits'


def verify(request)
securenative = SecureNative.instance
context = SecureNative.context_builder.from_http_request(request).build
context = SecureNativeContext.from_http_request(request)

event_options = EventOptions(event_type = EventTypes::LOG_IN,
user_id = '1234', user_traits = UserTraits('Your Name', 'name@gmail.com', '+1234567890'),
context = context, properties = {prop1 => 'CUSTOM_PARAM_VALUE', prop2 => true, prop3 => 3}).build
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
user_traits: UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })

verify_result = securenative.verify(event_options)
verify_result.risk_level # Low, Medium, High
Expand Down
5 changes: 3 additions & 2 deletions lib/api_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def verify(event_options)
event = SDKEvent.new(event_options, @options)

begin
res = @event_manager.send_sync(event, ApiRoute::VERIFY, false).to_json
return VerifyResult.new(risk_level: res['riskLevel'], score: res['score'], triggers: res['triggers'])
res = @event_manager.send_sync(event, ApiRoute::VERIFY, false)
ver_result = JSON.parse(res.body)
return VerifyResult.new(risk_level: ver_result['riskLevel'], score: ver_result['score'], triggers: ver_result['triggers'])
rescue StandardError => e
SecureNativeLogger.debug("Failed to call verify; #{e}")
end
Expand Down
4 changes: 0 additions & 4 deletions lib/config/configuration_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ def initialize(api_key: nil, api_url: 'https://api.securenative.com/collector/ap
@fail_over_strategy = fail_over_strategy
end

def self.default_config_builder
ConfigurationBuilder.new
end

def self.default_securenative_options
SecureNativeOptions.new
end
Expand Down
42 changes: 21 additions & 21 deletions lib/config/configuration_manager.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# frozen_string_literal: true

require 'parseconfig'
require 'yaml'
require 'config/configuration_builder'

class ConfigurationManager
DEFAULT_CONFIG_FILE = 'securenative.cfg'
DEFAULT_CONFIG_FILE = 'securenative.yml'
CUSTOM_CONFIG_FILE_ENV_NAME = 'SECURENATIVE_CONFIG_FILE'
@config = nil

def self.read_resource_file(resource_path)
@config = ParseConfig.new(resource_path)

properties = {}
@config.get_groups.each do |group|
group.each do |key, value|
properties[key.upcase] = value
end
begin
@config = YAML.load_file(resource_path)
properties = @config unless @config.nil?
rescue StandardError => e
SecureNativeLogger.error("Could not parse config file #{resource_path}; #{e}")
end
properties
end
Expand All @@ -24,32 +24,32 @@ def self._get_resource_path(env_name)
end

def self.config_builder
ConfigurationBuilder.default_config_builder
ConfigurationBuilder.new
end

def self._get_env_or_default(properties, key, default)
return Env[key] if Env[key]
return ENV[key] if ENV[key]
return properties[key] if properties[key]

default
end

def self.load_config
options = ConfigurationBuilder().default_securenative_options
options = ConfigurationBuilder.default_securenative_options

resource_path = DEFAULT_CONFIG_FILE
resource_path = Env[CUSTOM_CONFIG_FILE_ENV_NAME] if Env[CUSTOM_CONFIG_FILE_ENV_NAME]
resource_path = ENV[CUSTOM_CONFIG_FILE_ENV_NAME] unless ENV[CUSTOM_CONFIG_FILE_ENV_NAME].nil?

properties = read_resource_file(resource_path)

ConfigurationBuilder(_get_env_or_default(properties, 'SECURENATIVE_API_KEY', options.api_key),
_get_env_or_default(properties, 'SECURENATIVE_API_URL', options.api_url),
_get_env_or_default(properties, 'SECURENATIVE_INTERVAL', options.interval),
_get_env_or_default(properties, 'SECURENATIVE_MAX_EVENTS', options.max_events),
_get_env_or_default(properties, 'SECURENATIVE_TIMEOUT', options.timeout),
_get_env_or_default(properties, 'SECURENATIVE_AUTO_SEND', options.auto_send),
_get_env_or_default(properties, 'SECURENATIVE_DISABLE', options.disable),
_get_env_or_default(properties, 'SECURENATIVE_LOG_LEVEL', options.log_level),
_get_env_or_default(properties, 'SECURENATIVE_FAILOVER_STRATEGY', options.fail_over_strategy))
ConfigurationBuilder.new(api_key: _get_env_or_default(properties, 'SECURENATIVE_API_KEY', options.api_key),
api_url: _get_env_or_default(properties, 'SECURENATIVE_API_URL', options.api_url),
interval: _get_env_or_default(properties, 'SECURENATIVE_INTERVAL', options.interval),
max_events: _get_env_or_default(properties, 'SECURENATIVE_MAX_EVENTS', options.max_events),
timeout: _get_env_or_default(properties, 'SECURENATIVE_TIMEOUT', options.timeout),
auto_send: _get_env_or_default(properties, 'SECURENATIVE_AUTO_SEND', options.auto_send),
disable: _get_env_or_default(properties, 'SECURENATIVE_DISABLE', options.disable),
log_level: _get_env_or_default(properties, 'SECURENATIVE_LOG_LEVEL', options.log_level),
fail_over_strategy: _get_env_or_default(properties, 'SECURENATIVE_FAILOVER_STRATEGY', options.fail_over_strategy))
end
end
11 changes: 9 additions & 2 deletions lib/context/hanami_context.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# frozen_string_literal: true

class HanamiContext
SECURENATIVE_COOKIE = '_sn'

def self.get_client_token(request)
begin
request.env[SECURENATIVE_COOKIE]
rescue StandardError
nil
begin
request.cookies[SECURENATIVE_COOKIE]
rescue StandardError
nil
end
end
end

Expand All @@ -27,7 +33,8 @@ def self.get_method(request)

def self.get_headers(request)
begin
request.headers.to_hash
# Note: At the moment we're filtering out everything but user-agent since ruby's payload is way too big
{ 'user-agent' => request.env['HTTP_USER_AGENT'] }
rescue StandardError
nil
end
Expand Down
5 changes: 4 additions & 1 deletion lib/context/rails_context.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class RailsContext
SECURENATIVE_COOKIE = '_sn'

def self.get_client_token(request)
begin
request.cookies[SECURENATIVE_COOKIE]
Expand Down Expand Up @@ -33,7 +35,8 @@ def self.get_method(request)

def self.get_headers(request)
begin
request.headers.to_hash
# Note: At the moment we're filtering out everything but user-agent since ruby's payload is way too big
{ 'user-agent' => request.env['HTTP_USER_AGENT'] }
rescue StandardError
nil
end
Expand Down
15 changes: 11 additions & 4 deletions lib/context/sinatra_context.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
# frozen_string_literal: true

class SinatraContext
SECURENATIVE_COOKIE = '_sn'

def self.get_client_token(request)
begin
request.env[SECURENATIVE_COOKIE]
rescue StandardError
nil
begin
request.cookies[SECURENATIVE_COOKIE]
rescue StandardError
nil
end
end
end

def self.get_url(request)
begin
request.url
request.env['REQUEST_URI']
rescue StandardError
nil
end
end

def self.get_method(request)
begin
request.method
request.env['REQUEST_METHOD']
rescue StandardError
nil
end
end

def self.get_headers(request)
begin
request.headers.to_hash
# Note: At the moment we're filtering out everything but user-agent since ruby's payload is way too big
{ 'user-agent' => request.env['HTTP_USER_AGENT'] }
rescue StandardError
nil
end
Expand Down
2 changes: 1 addition & 1 deletion lib/event_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def send_sync(event, resource_path, retry_sending)
SecureNativeLogger.debug("Attempting to send event #{event}")
res = @http_client.post(resource_path, EventManager.serialize(event).to_json)

if res.nil? || res.code != 200
if res.nil? || res.code != '200'
SecureNativeLogger.info("SecureNative failed to call endpoint #{resource_path} with event #{event}. adding back to queue")
item = QueueItem.new(resource_path, EventManager.serialize(event).to_json, retry_sending)
@queue.append(item)
Expand Down
Loading