Skip to content

Commit 10900f0

Browse files
author
Inbal Tako
committed
PR fixes
1 parent edf99f5 commit 10900f0

36 files changed

Lines changed: 488 additions & 421 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ securenative = SecureNativeSDK.init_with_api_key('YOUR_API_KEY')
6969
require 'securenative'
7070

7171

72-
options = ConfigurationBuilder.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR')
72+
options = SecureNative::Config::ConfigurationBuilder.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR')
7373
SecureNativeSDK.init_with_options(options)
7474
```
7575

lib/securenative.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
require 'securenative/event_manager'
3636
require 'securenative/api_manager'
3737
require 'securenative/sdk'
38+
require 'securenative/version'
3839

3940
require 'yaml'
4041
require 'net/http'

lib/securenative/api_manager.rb

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
# frozen_string_literal: true
22

3-
class ApiManager
4-
def initialize(event_manager, securenative_options)
5-
@event_manager = event_manager
6-
@options = securenative_options
7-
end
3+
module SecureNative
4+
class ApiManager
5+
def initialize(event_manager, securenative_options)
6+
@event_manager = event_manager
7+
@options = securenative_options
8+
end
89

9-
def track(event_options)
10-
SecureNative::Log.debug('Track event call')
11-
event = SecureNative::SDKEvent.new(event_options, @options)
12-
@event_manager.send_async(event, ApiRoute::TRACK)
13-
end
10+
def track(event_options)
11+
SecureNative::Log.debug('Track event call')
12+
event = SecureNative::SDKEvent.new(event_options, @options)
13+
@event_manager.send_async(event, SecureNative::Enums::ApiRoute::TRACK)
14+
end
1415

15-
def verify(event_options)
16-
SecureNative::Log.debug('Verify event call')
17-
event = SecureNative::SDKEvent.new(event_options, @options)
16+
def verify(event_options)
17+
SecureNative::Log.debug('Verify event call')
18+
event = SecureNative::SDKEvent.new(event_options, @options)
1819

19-
begin
20-
res = @event_manager.send_sync(event, ApiRoute::VERIFY, false)
21-
ver_result = JSON.parse(res.body)
22-
return VerifyResult.new(risk_level: ver_result['riskLevel'], score: ver_result['score'], triggers: ver_result['triggers'])
23-
rescue StandardError => e
24-
SecureNative::Log.debug("Failed to call verify; #{e}")
25-
end
26-
if @options.fail_over_strategy == SecureNative::FailOverStrategy::FAIL_OPEN
27-
return SecureNative::VerifyResult.new(risk_level: RiskLevel::LOW, score: 0, triggers: nil)
28-
end
20+
begin
21+
res = @event_manager.send_sync(event, SecureNative::Enums::ApiRoute::VERIFY)
22+
ver_result = JSON.parse(res.body)
23+
return VerifyResult.new(risk_level: ver_result['riskLevel'], score: ver_result['score'], triggers: ver_result['triggers'])
24+
rescue StandardError => e
25+
SecureNative::Log.debug("Failed to call verify; #{e}")
26+
end
27+
if @options.fail_over_strategy == SecureNative::FailOverStrategy::FAIL_OPEN
28+
return SecureNative::VerifyResult.new(risk_level: SecureNative::Enums::RiskLevel::LOW, score: 0, triggers: nil)
29+
end
2930

30-
VerifyResult.new(risk_level: RiskLevel::HIGH, score: 1, triggers: nil)
31+
VerifyResult.new(risk_level: SecureNative::Enums::RiskLevel::HIGH, score: 1, triggers: nil)
32+
end
3133
end
3234
end
Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
# frozen_string_literal: true
22

3-
class ConfigurationBuilder
4-
attr_reader :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy, :proxy_headers
5-
attr_writer :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy, :proxy_headers
3+
module SecureNative
4+
module Config
5+
class ConfigurationBuilder
6+
attr_reader :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy, :proxy_headers
7+
attr_writer :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy, :proxy_headers
68

7-
def initialize(api_key: nil, api_url: 'https://api.securenative.com/collector/api/v1', interval: 1000,
8-
max_events: 1000, timeout: 1500, auto_send: true, disable: false, log_level: 'FATAL',
9-
fail_over_strategy: SecureNative::FailOverStrategy::FAIL_OPEN, proxy_headers: nil)
10-
@api_key = api_key
11-
@api_url = api_url
12-
@interval = interval
13-
@max_events = max_events
14-
@timeout = timeout
15-
@auto_send = auto_send
16-
@disable = disable
17-
@log_level = log_level
18-
@fail_over_strategy = fail_over_strategy
19-
@proxy_headers = proxy_headers
20-
end
9+
def initialize(api_key: nil, api_url: 'https://api.securenative.com/collector/api/v1', interval: 1000,
10+
max_events: 1000, timeout: 1500, auto_send: true, disable: false, log_level: 'FATAL',
11+
fail_over_strategy: SecureNative::FailOverStrategy::FAIL_OPEN, proxy_headers: nil)
12+
@api_key = api_key
13+
@api_url = api_url
14+
@interval = interval
15+
@max_events = max_events
16+
@timeout = timeout
17+
@auto_send = auto_send
18+
@disable = disable
19+
@log_level = log_level
20+
@fail_over_strategy = fail_over_strategy
21+
@proxy_headers = proxy_headers
22+
end
2123

22-
def self.default_securenative_options
23-
Options.new
24+
def self.default_securenative_options
25+
Options.new
26+
end
27+
end
2428
end
2529
end
Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,57 @@
11
# frozen_string_literal: true
22

3-
class ConfigurationManager
4-
DEFAULT_CONFIG_FILE = 'securenative.yml'
5-
CUSTOM_CONFIG_FILE_ENV_NAME = 'SECURENATIVE_CONFIG_FILE'
6-
@config = nil
7-
8-
def self.read_resource_file(resource_path)
9-
properties = {}
10-
begin
11-
@config = YAML.load_file(resource_path)
12-
properties = @config unless @config.nil?
13-
rescue StandardError => e
14-
SecureNative::Log.error("Could not parse securenative.config file #{resource_path}; #{e}")
3+
module SecureNative
4+
module Config
5+
class ConfigurationManager
6+
DEFAULT_CONFIG_FILE = 'securenative.yml'
7+
CUSTOM_CONFIG_FILE_ENV_NAME = 'SECURENATIVE_CONFIG_FILE'
8+
@config = nil
9+
10+
def self.read_resource_file(resource_path)
11+
properties = {}
12+
begin
13+
@config = YAML.load_file(resource_path)
14+
properties = @config unless @config.nil?
15+
rescue StandardError => e
16+
SecureNative::Log.error("Could not parse securenative.config file #{resource_path}; #{e}")
17+
end
18+
properties
19+
end
20+
21+
def self._get_resource_path(env_name)
22+
Env.fetch(env_name, ENV[DEFAULT_CONFIG_FILE])
23+
end
24+
25+
def self.config_builder
26+
SecureNative::Config::ConfigurationBuilder.new
27+
end
28+
29+
def self._get_env_or_default(properties, key, default)
30+
return ENV[key] if ENV[key]
31+
return properties[key] if properties[key]
32+
33+
default
34+
end
35+
36+
def self.load_config
37+
options = SecureNative::Config::ConfigurationBuilder.default_securenative_options
38+
39+
resource_path = DEFAULT_CONFIG_FILE
40+
resource_path = ENV[CUSTOM_CONFIG_FILE_ENV_NAME] unless ENV[CUSTOM_CONFIG_FILE_ENV_NAME].nil?
41+
42+
properties = read_resource_file(resource_path)
43+
44+
SecureNative::Config::ConfigurationBuilder.new(api_key: _get_env_or_default(properties, 'SECURENATIVE_API_KEY', options.api_key),
45+
api_url: _get_env_or_default(properties, 'SECURENATIVE_API_URL', options.api_url),
46+
interval: _get_env_or_default(properties, 'SECURENATIVE_INTERVAL', options.interval),
47+
max_events: _get_env_or_default(properties, 'SECURENATIVE_MAX_EVENTS', options.max_events),
48+
timeout: _get_env_or_default(properties, 'SECURENATIVE_TIMEOUT', options.timeout),
49+
auto_send: _get_env_or_default(properties, 'SECURENATIVE_AUTO_SEND', options.auto_send),
50+
disable: _get_env_or_default(properties, 'SECURENATIVE_DISABLE', options.disable),
51+
log_level: _get_env_or_default(properties, 'SECURENATIVE_LOG_LEVEL', options.log_level),
52+
fail_over_strategy: _get_env_or_default(properties, 'SECURENATIVE_FAILOVER_STRATEGY', options.fail_over_strategy),
53+
proxy_headers: _get_env_or_default(properties, 'SECURENATIVE_PROXY_HEADERS', options.proxy_headers))
54+
end
1555
end
16-
properties
17-
end
18-
19-
def self._get_resource_path(env_name)
20-
Env.fetch(env_name, ENV[DEFAULT_CONFIG_FILE])
21-
end
22-
23-
def self.config_builder
24-
ConfigurationBuilder.new
25-
end
26-
27-
def self._get_env_or_default(properties, key, default)
28-
return ENV[key] if ENV[key]
29-
return properties[key] if properties[key]
30-
31-
default
32-
end
33-
34-
def self.load_config
35-
options = ConfigurationBuilder.default_securenative_options
36-
37-
resource_path = DEFAULT_CONFIG_FILE
38-
resource_path = ENV[CUSTOM_CONFIG_FILE_ENV_NAME] unless ENV[CUSTOM_CONFIG_FILE_ENV_NAME].nil?
39-
40-
properties = read_resource_file(resource_path)
41-
42-
ConfigurationBuilder.new(api_key: _get_env_or_default(properties, 'SECURENATIVE_API_KEY', options.api_key),
43-
api_url: _get_env_or_default(properties, 'SECURENATIVE_API_URL', options.api_url),
44-
interval: _get_env_or_default(properties, 'SECURENATIVE_INTERVAL', options.interval),
45-
max_events: _get_env_or_default(properties, 'SECURENATIVE_MAX_EVENTS', options.max_events),
46-
timeout: _get_env_or_default(properties, 'SECURENATIVE_TIMEOUT', options.timeout),
47-
auto_send: _get_env_or_default(properties, 'SECURENATIVE_AUTO_SEND', options.auto_send),
48-
disable: _get_env_or_default(properties, 'SECURENATIVE_DISABLE', options.disable),
49-
log_level: _get_env_or_default(properties, 'SECURENATIVE_LOG_LEVEL', options.log_level),
50-
fail_over_strategy: _get_env_or_default(properties, 'SECURENATIVE_FAILOVER_STRATEGY', options.fail_over_strategy),
51-
proxy_headers: _get_env_or_default(properties, 'SECURENATIVE_PROXY_HEADERS', options.proxy_headers))
5256
end
5357
end

lib/securenative/context.rb

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,29 @@ def self.default_context_builder
2222
end
2323

2424
def self.from_http_request(request)
25-
client_token = RailsContext.get_client_token(request)
26-
client_token = SinatraContext.get_client_token(request) if client_token.nil?
27-
client_token = HanamiContext.get_client_token(request) if client_token.nil?
25+
client_token = SecureNative::FrameWorkContext::RailsContext.get_client_token(request)
26+
client_token = SecureNative::FrameWorkContext::SinatraContext.get_client_token(request) if client_token.nil?
27+
client_token = SecureNative::FrameWorkContext::HanamiContext.get_client_token(request) if client_token.nil?
2828

2929
begin
30-
headers = RailsContext.get_headers(request)
31-
headers = SinatraContext.get_headers(request) if headers.nil?
32-
headers = HanamiContext.get_headers(request) if headers.nil?
30+
headers = SecureNative::FrameWorkContext::RailsContext.get_headers(request)
31+
headers = SecureNative::FrameWorkContext::SinatraContext.get_headers(request) if headers.nil?
32+
headers = SecureNative::FrameWorkContext::HanamiContext.get_headers(request) if headers.nil?
3333

3434
# Standard Ruby request
3535
headers = request.header.to_hash if headers.nil?
3636
rescue StandardError
3737
headers = []
3838
end
3939

40-
url = RailsContext.get_url(request)
41-
url = SinatraContext.get_url(request) if url.nil?
42-
url = HanamiContext.get_url(request) if url.nil?
40+
url = SecureNative::FrameWorkContext::RailsContext.get_url(request)
41+
url = SecureNative::FrameWorkContext::SinatraContext.get_url(request) if url.nil?
42+
url = SecureNative::FrameWorkContext::HanamiContext.get_url(request) if url.nil?
4343
url = '' if url.nil?
4444

45-
method = RailsContext.get_method(request)
46-
method = SinatraContext.get_method(request) if method.nil?
47-
method = HanamiContext.get_method(request) if method.nil?
45+
method = SecureNative::FrameWorkContext::RailsContext.get_method(request)
46+
method = SecureNative::FrameWorkContext::SinatraContext.get_method(request) if method.nil?
47+
method = SecureNative::FrameWorkContext::HanamiContext.get_method(request) if method.nil?
4848
method = '' if method.nil?
4949

5050
begin
@@ -53,10 +53,12 @@ def self.from_http_request(request)
5353
body = ''
5454
end
5555

56-
client_token = RequestUtils.get_secure_header_from_request(headers) if Utils.null_or_empty?(client_token)
56+
if SecureNative::Utils::Utils.null_or_empty?(client_token)
57+
client_token = SecureNative::Utils::RequestUtils.get_secure_header_from_request(headers)
58+
end
5759

58-
Context.new(client_token: client_token, ip: RequestUtils.get_client_ip_from_request(request),
59-
remote_ip: RequestUtils.get_remote_ip_from_request(request),
60+
Context.new(client_token: client_token, ip: SecureNative::Utils::RequestUtils.get_client_ip_from_request(request),
61+
remote_ip: SecureNative::Utils::RequestUtils.get_remote_ip_from_request(request),
6062
headers: headers, url: url, http_method: method || '', body: body)
6163
end
6264
end
Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
11
# frozen_string_literal: true
22

3-
class HanamiContext
4-
SECURENATIVE_COOKIE = '_sn'
3+
module SecureNative
4+
module FrameWorkContext
5+
class HanamiContext
6+
SECURENATIVE_COOKIE = '_sn'
57

6-
def self.get_client_token(request)
7-
begin
8-
request.env[SECURENATIVE_COOKIE]
9-
rescue StandardError
10-
begin
11-
request.cookies[SECURENATIVE_COOKIE]
12-
rescue StandardError
13-
nil
8+
def self.get_client_token(request)
9+
begin
10+
request.env[SECURENATIVE_COOKIE]
11+
rescue StandardError
12+
begin
13+
request.cookies[SECURENATIVE_COOKIE]
14+
rescue StandardError
15+
nil
16+
end
17+
end
1418
end
15-
end
16-
end
1719

18-
def self.get_url(request)
19-
begin
20-
request.env['REQUEST_PATH']
21-
rescue StandardError
22-
nil
23-
end
24-
end
20+
def self.get_url(request)
21+
begin
22+
request.env['REQUEST_PATH']
23+
rescue StandardError
24+
nil
25+
end
26+
end
2527

26-
def self.get_method(request)
27-
begin
28-
request.request_method
29-
rescue StandardError
30-
nil
31-
end
32-
end
28+
def self.get_method(request)
29+
begin
30+
request.request_method
31+
rescue StandardError
32+
nil
33+
end
34+
end
3335

34-
def self.get_headers(request)
35-
begin
36-
# Note: At the moment we're filtering out everything but user-agent since ruby's payload is way too big
37-
{'user-agent' => request.env['HTTP_USER_AGENT']}
38-
rescue StandardError
39-
nil
36+
def self.get_headers(request)
37+
begin
38+
# Note: At the moment we're filtering out everything but user-agent since ruby's payload is way too big
39+
{'user-agent' => request.env['HTTP_USER_AGENT']}
40+
rescue StandardError
41+
nil
42+
end
43+
end
4044
end
4145
end
42-
end
46+
end

0 commit comments

Comments
 (0)