securenative 0.1.5 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +49 -0
- data/.github/workflows/publish.yml +60 -0
- data/.github/workflows/test.yml +48 -0
- data/.gitignore +3 -1
- data/.rakeTasks +7 -0
- data/.rspec +3 -0
- data/Gemfile +6 -1
- data/Gemfile.lock +254 -17
- data/README.md +140 -67
- data/Rakefile +5 -1
- data/lib/api_manager.rb +39 -0
- data/lib/config/configuration_builder.rb +26 -0
- data/lib/config/configuration_manager.rb +55 -0
- data/lib/config/securenative_options.rb +22 -0
- data/lib/context/hanami_context.rb +42 -0
- data/lib/context/rails_context.rb +44 -0
- data/lib/context/securenative_context.rb +67 -0
- data/lib/context/sinatra_context.rb +42 -0
- data/lib/enums/api_route.rb +6 -0
- data/lib/enums/event_types.rb +23 -0
- data/lib/enums/failover_strategy.rb +6 -0
- data/lib/enums/risk_level.rb +7 -0
- data/lib/errors/securenative_config_error.rb +4 -0
- data/lib/errors/securenative_http_error.rb +4 -0
- data/lib/errors/securenative_invalid_options_error.rb +4 -0
- data/lib/errors/securenative_invalid_uri_error.rb +4 -0
- data/lib/errors/securenative_parse_error.rb +4 -0
- data/lib/errors/securenative_sdk_Illegal_state_error.rb +4 -0
- data/lib/errors/securenative_sdk_error.rb +4 -0
- data/lib/event_manager.rb +157 -0
- data/lib/http/secure_native_http_response.rb +12 -0
- data/lib/http/securenative_http_client.rb +50 -0
- data/lib/models/client_token.rb +12 -0
- data/lib/models/device.rb +10 -0
- data/lib/models/event_options.rb +37 -0
- data/lib/models/request_context.rb +18 -0
- data/lib/models/request_options.rb +12 -0
- data/lib/models/sdk_event.rb +40 -0
- data/lib/models/user_traits.rb +13 -0
- data/lib/models/verify_result.rb +16 -0
- data/lib/securenative.rb +69 -25
- data/lib/utils/date_utils.rb +9 -0
- data/lib/utils/encryption_utils.rb +49 -0
- data/lib/utils/ip_utils.rb +23 -0
- data/lib/utils/request_utils.rb +54 -0
- data/lib/utils/secure_native_logger.rb +44 -0
- data/lib/utils/signature_utils.rb +16 -0
- data/lib/utils/utils.rb +9 -0
- data/lib/utils/version_utils.rb +11 -0
- data/out/production/securenative-ruby/api_manager.rb +31 -0
- data/out/production/securenative-ruby/config/configuration_builder.rb +30 -0
- data/out/production/securenative-ruby/config/configuration_manager.rb +55 -0
- data/out/production/securenative-ruby/config/securenative_options.rb +20 -0
- data/out/production/securenative-ruby/context/securenative_context.rb +40 -0
- data/out/production/securenative-ruby/enums/api_route.rb +6 -0
- data/out/production/securenative-ruby/enums/event_types.rb +23 -0
- data/out/production/securenative-ruby/enums/failover_strategy.rb +6 -0
- data/out/production/securenative-ruby/enums/risk_level.rb +7 -0
- data/out/production/securenative-ruby/errors/securenative_config_error.rb +4 -0
- data/out/production/securenative-ruby/errors/securenative_http_error.rb +4 -0
- data/out/production/securenative-ruby/errors/securenative_invalid_options_error.rb +4 -0
- data/out/production/securenative-ruby/errors/securenative_invalid_uri_error.rb +4 -0
- data/out/production/securenative-ruby/errors/securenative_parse_error.rb +4 -0
- data/out/production/securenative-ruby/errors/securenative_sdk_Illegal_state_error.rb +4 -0
- data/out/production/securenative-ruby/errors/securenative_sdk_error.rb +4 -0
- data/out/production/securenative-ruby/event_manager.rb +156 -0
- data/out/production/securenative-ruby/event_options.rb +32 -0
- data/out/production/securenative-ruby/http/http_response.rb +12 -0
- data/out/production/securenative-ruby/http/securenative_http_client.rb +32 -0
- data/out/production/securenative-ruby/models/client_token.rb +12 -0
- data/out/production/securenative-ruby/models/device.rb +10 -0
- data/out/production/securenative-ruby/models/event_options.rb +15 -0
- data/out/production/securenative-ruby/models/request_context.rb +18 -0
- data/out/production/securenative-ruby/models/request_options.rb +12 -0
- data/out/production/securenative-ruby/models/sdk_event.rb +33 -0
- data/out/production/securenative-ruby/models/user_traits.rb +13 -0
- data/out/production/securenative-ruby/models/verify_result.rb +12 -0
- data/out/production/securenative-ruby/securenative.rb +91 -0
- data/out/production/securenative-ruby/utils/date_utils.rb +9 -0
- data/out/production/securenative-ruby/utils/encryption_utils.rb +35 -0
- data/out/production/securenative-ruby/utils/ip_utils.rb +23 -0
- data/out/production/securenative-ruby/utils/request_utils.rb +23 -0
- data/out/production/securenative-ruby/utils/secure_native_logger.rb +44 -0
- data/out/production/securenative-ruby/utils/signature_utils.rb +16 -0
- data/out/production/securenative-ruby/utils/utils.rb +9 -0
- data/out/production/securenative-ruby/utils/version_utils.rb +12 -0
- data/out/test/securenative-ruby/spec_api_manager.rb +81 -0
- data/out/test/securenative-ruby/spec_context_builder.rb +69 -0
- data/out/test/securenative-ruby/spec_date_utils.rb +13 -0
- data/out/test/securenative-ruby/spec_encryption_utils.rb +26 -0
- data/out/test/securenative-ruby/spec_event_manager.rb +59 -0
- data/out/test/securenative-ruby/spec_helper.rb +20 -0
- data/out/test/securenative-ruby/spec_ip_utils.rb +41 -0
- data/out/test/securenative-ruby/spec_securenative.rb +65 -0
- data/out/test/securenative-ruby/spec_securenative_http_client.rb +23 -0
- data/out/test/securenative-ruby/spec_signature_utils.rb +18 -0
- data/securenative.gemspec +4 -4
- metadata +96 -15
- data/lib/securenative/config.rb +0 -9
- data/lib/securenative/event_manager.rb +0 -88
- data/lib/securenative/event_options.rb +0 -86
- data/lib/securenative/event_type.rb +0 -21
- data/lib/securenative/http_client.rb +0 -20
- data/lib/securenative/secure_native_sdk.rb +0 -62
- data/lib/securenative/securenative_options.rb +0 -17
- data/lib/securenative/sn_exception.rb +0 -5
- data/lib/securenative/utils.rb +0 -41
data/README.md
CHANGED
@@ -1,7 +1,32 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
<p align="center">
|
2
|
+
<a href="https://www.securenative.com"><img src="https://user-images.githubusercontent.com/45174009/77826512-f023ed80-7120-11ea-80e0-58aacde0a84e.png" alt="SecureNative Logo"/></a>
|
3
|
+
</p>
|
4
|
+
|
5
|
+
<p align="center">
|
6
|
+
<b>A Cloud-Native Security Monitoring and Protection for Modern Applications</b>
|
7
|
+
</p>
|
8
|
+
<p align="center">
|
9
|
+
<a href="https://github.com/securenative/securenative-ruby">
|
10
|
+
<img alt="Github Actions" src="https://github.com/securenative/securenative-ruby/workflows/CI/badge.svg">
|
11
|
+
</a>
|
12
|
+
<a href="https://codecov.io/gh/securenative/securenative-ruby">
|
13
|
+
<img src="https://codecov.io/gh/securenative/securenative-ruby/branch/master/graph/badge.svg" />
|
14
|
+
</a>
|
15
|
+
<a href="https://badge.fury.io/rb/securenative"><img src="https://badge.fury.io/rb/securenative.svg" alt="Gem Version" height="18"></a>
|
16
|
+
</p>
|
17
|
+
<p align="center">
|
18
|
+
<a href="https://docs.securenative.com">Documentation</a> |
|
19
|
+
<a href="https://docs.securenative.com/quick-start">Quick Start</a> |
|
20
|
+
<a href="https://blog.securenative.com">Blog</a> |
|
21
|
+
<a href="">Chat with us on Slack!</a>
|
22
|
+
</p>
|
23
|
+
<hr/>
|
24
|
+
|
25
|
+
|
26
|
+
[SecureNative](https://www.securenative.com/) performs user monitoring by analyzing user interactions with your application and various factors such as network, devices, locations and access patterns to stop and prevent account takeover attacks.
|
27
|
+
|
28
|
+
|
29
|
+
## Install the SDK
|
5
30
|
|
6
31
|
Add this line to your application's Gemfile:
|
7
32
|
|
@@ -9,94 +34,142 @@ Add this line to your application's Gemfile:
|
|
9
34
|
gem 'securenative'
|
10
35
|
```
|
11
36
|
|
12
|
-
|
37
|
+
Then execute:
|
13
38
|
|
14
|
-
$ bundle
|
39
|
+
$ bundle install
|
15
40
|
|
16
41
|
Or install it yourself as:
|
17
42
|
|
18
43
|
$ gem install securenative
|
19
44
|
|
20
45
|
## Initialize the SDK
|
21
|
-
|
46
|
+
|
47
|
+
To get your *API KEY*, login to your SecureNative account and go to project settings page:
|
48
|
+
|
49
|
+
### Option 1: Initialize via Config file
|
50
|
+
SecureNative can automatically load your config from *securenative.yml* file or from the file that is specified in your *SECURENATIVE_CONFIG_FILE* env variable:
|
51
|
+
|
22
52
|
```ruby
|
23
53
|
require 'securenative'
|
24
54
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
55
|
+
|
56
|
+
secureative = SecureNative.init
|
57
|
+
```
|
58
|
+
### Option 2: Initialize via API Key
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
require 'securenative'
|
62
|
+
|
63
|
+
|
64
|
+
securenative = SecureNative.init_with_api_key('YOUR_API_KEY')
|
65
|
+
```
|
66
|
+
|
67
|
+
### Option 3: Initialize via ConfigurationBuilder
|
68
|
+
```ruby
|
69
|
+
require 'securenative'
|
70
|
+
|
71
|
+
|
72
|
+
options = ConfigurationBuilder.new(api_key: 'API_KEY', max_events: 10, log_level: 'ERROR')
|
73
|
+
SecureNative.init_with_options(options)
|
74
|
+
```
|
75
|
+
|
76
|
+
## Getting SecureNative instance
|
77
|
+
Once initialized, sdk will create a singleton instance which you can get:
|
78
|
+
```ruby
|
79
|
+
require 'securenative'
|
80
|
+
|
81
|
+
|
82
|
+
secureNative = SecureNative.instance
|
34
83
|
```
|
35
84
|
|
36
85
|
## Tracking events
|
37
|
-
|
86
|
+
|
87
|
+
Once the SDK has been initialized, tracking requests sent through the SDK
|
88
|
+
instance. Make sure you build event with the EventBuilder:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
require 'securenative'
|
92
|
+
require 'models/event_options'
|
93
|
+
require 'enums/event_types'
|
94
|
+
require 'models/user_traits'
|
95
|
+
|
96
|
+
|
97
|
+
def track
|
98
|
+
securenative = SecureNative.instance
|
99
|
+
context = SecureNativeContext.new(client_token: '2a980d872b939c7e4f4378aa111a5eeffb22808b58b5372f658d34904ebd5b05fff0daab91921243ac08b72442a5b3992e402dc21df16aa7cc0e19f8bffa9d6cc59996d480d70aa22b857189403675d37fd144ebaf9dc697fed149b907678f2b1f964d73b332dc8ea7df63fcfc3c11f7bbb51ba2672652ca7d5d43f36a62e15db8b13dfd794a5eccfc5968ca514dd7cce59f2df2b9d8184d076eba808c81b311', ip: '127.0.0.1',
|
100
|
+
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' })
|
101
|
+
|
102
|
+
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
|
103
|
+
user_traits: UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
|
104
|
+
properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })
|
105
|
+
|
106
|
+
securenative.track(event_options)
|
107
|
+
|
108
|
+
@message = 'tracked'
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
You can also create request context from requests:
|
113
|
+
|
38
114
|
```ruby
|
39
115
|
require 'securenative'
|
40
|
-
require '
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
116
|
+
require 'models/event_options'
|
117
|
+
require 'enums/event_types'
|
118
|
+
require 'models/user_traits'
|
119
|
+
|
120
|
+
|
121
|
+
def track
|
122
|
+
securenative = SecureNative.instance
|
123
|
+
context = SecureNativeContext.from_http_request(request)
|
124
|
+
|
125
|
+
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
|
126
|
+
user_traits: UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
|
127
|
+
properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })
|
128
|
+
|
129
|
+
securenative.track(event_options)
|
130
|
+
|
131
|
+
@message = 'tracked'
|
55
132
|
end
|
56
133
|
```
|
57
134
|
|
58
|
-
##
|
59
|
-
|
135
|
+
## Verify events
|
136
|
+
|
137
|
+
**Example**
|
138
|
+
|
60
139
|
```ruby
|
61
140
|
require 'securenative'
|
62
|
-
require '
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
141
|
+
require 'models/event_options'
|
142
|
+
require 'enums/event_types'
|
143
|
+
require 'models/user_traits'
|
144
|
+
|
145
|
+
|
146
|
+
def verify(request)
|
147
|
+
securenative = SecureNative.instance
|
148
|
+
context = SecureNativeContext.from_http_request(request)
|
149
|
+
|
150
|
+
event_options = EventOptions.new(event: EventTypes::LOG_IN, user_id: '1234', context: context,
|
151
|
+
user_traits: UserTraits.new(name: 'Your Name', email: 'name@gmail.com', phone: '+1234567890'),
|
152
|
+
properties: { custom_param1: 'CUSTOM_PARAM_VALUE', custom_param2: true, custom_param3: 3 })
|
153
|
+
|
154
|
+
verify_result = securenative.verify(event_options)
|
155
|
+
verify_result.risk_level # Low, Medium, High
|
156
|
+
verify_result.score # Risk score: 0 -1 (0 - Very Low, 1 - Very High)
|
157
|
+
verify_result.triggers # ["TOR", "New IP", "New City"]
|
79
158
|
end
|
80
159
|
```
|
81
160
|
|
82
|
-
##
|
83
|
-
|
161
|
+
## Webhook signature verification
|
162
|
+
|
163
|
+
Apply our filter to verify the request is from us, for example:
|
164
|
+
|
84
165
|
```ruby
|
85
166
|
require 'securenative'
|
86
167
|
|
87
|
-
begin
|
88
|
-
SecureNative.init('YOUR_API_KEY') # Should be called before any other call to securenative
|
89
|
-
rescue SecureNativeSDKException => e
|
90
|
-
# Do some error handling
|
91
|
-
end
|
92
168
|
|
93
|
-
def
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
else
|
99
|
-
# This request wasn't sent from Secure Native servers, you can dismiss/investigate it
|
100
|
-
end
|
169
|
+
def webhook_endpoint(request)
|
170
|
+
securenative = SecureNative.instance
|
171
|
+
|
172
|
+
# Checks if request is verified
|
173
|
+
is_verified = securenative.verify_request_payload(request)
|
101
174
|
end
|
102
|
-
```
|
175
|
+
```
|
data/Rakefile
CHANGED
data/lib/api_manager.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'models/sdk_event'
|
4
|
+
require 'enums/failover_strategy'
|
5
|
+
require 'enums/risk_level'
|
6
|
+
require 'enums/api_route'
|
7
|
+
require 'models/verify_result'
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
class ApiManager
|
11
|
+
def initialize(event_manager, securenative_options)
|
12
|
+
@event_manager = event_manager
|
13
|
+
@options = securenative_options
|
14
|
+
end
|
15
|
+
|
16
|
+
def track(event_options)
|
17
|
+
SecureNativeLogger.debug('Track event call')
|
18
|
+
event = SDKEvent.new(event_options, @options)
|
19
|
+
@event_manager.send_async(event, ApiRoute::TRACK)
|
20
|
+
end
|
21
|
+
|
22
|
+
def verify(event_options)
|
23
|
+
SecureNativeLogger.debug('Verify event call')
|
24
|
+
event = SDKEvent.new(event_options, @options)
|
25
|
+
|
26
|
+
begin
|
27
|
+
res = @event_manager.send_sync(event, ApiRoute::VERIFY, false)
|
28
|
+
ver_result = JSON.parse(res.body)
|
29
|
+
return VerifyResult.new(risk_level: ver_result['riskLevel'], score: ver_result['score'], triggers: ver_result['triggers'])
|
30
|
+
rescue StandardError => e
|
31
|
+
SecureNativeLogger.debug("Failed to call verify; #{e}")
|
32
|
+
end
|
33
|
+
if @options.fail_over_strategy == FailOverStrategy::FAIL_OPEN
|
34
|
+
return VerifyResult.new(risk_level: RiskLevel::LOW, score: 0, triggers: nil)
|
35
|
+
end
|
36
|
+
|
37
|
+
VerifyResult.new(risk_level: RiskLevel::HIGH, score: 1, triggers: nil)
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'enums/failover_strategy'
|
4
|
+
|
5
|
+
class ConfigurationBuilder
|
6
|
+
attr_reader :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
|
7
|
+
attr_writer :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
|
8
|
+
|
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: FailOverStrategy::FAIL_OPEN)
|
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
|
+
end
|
22
|
+
|
23
|
+
def self.default_securenative_options
|
24
|
+
SecureNativeOptions.new
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
require 'config/configuration_builder'
|
5
|
+
|
6
|
+
class ConfigurationManager
|
7
|
+
DEFAULT_CONFIG_FILE = 'securenative.yml'
|
8
|
+
CUSTOM_CONFIG_FILE_ENV_NAME = 'SECURENATIVE_CONFIG_FILE'
|
9
|
+
@config = nil
|
10
|
+
|
11
|
+
def self.read_resource_file(resource_path)
|
12
|
+
properties = {}
|
13
|
+
begin
|
14
|
+
@config = YAML.load_file(resource_path)
|
15
|
+
properties = @config unless @config.nil?
|
16
|
+
rescue StandardError => e
|
17
|
+
SecureNativeLogger.error("Could not parse config file #{resource_path}; #{e}")
|
18
|
+
end
|
19
|
+
properties
|
20
|
+
end
|
21
|
+
|
22
|
+
def self._get_resource_path(env_name)
|
23
|
+
Env.fetch(env_name, ENV[DEFAULT_CONFIG_FILE])
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.config_builder
|
27
|
+
ConfigurationBuilder.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def self._get_env_or_default(properties, key, default)
|
31
|
+
return ENV[key] if ENV[key]
|
32
|
+
return properties[key] if properties[key]
|
33
|
+
|
34
|
+
default
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.load_config
|
38
|
+
options = ConfigurationBuilder.default_securenative_options
|
39
|
+
|
40
|
+
resource_path = DEFAULT_CONFIG_FILE
|
41
|
+
resource_path = ENV[CUSTOM_CONFIG_FILE_ENV_NAME] unless ENV[CUSTOM_CONFIG_FILE_ENV_NAME].nil?
|
42
|
+
|
43
|
+
properties = read_resource_file(resource_path)
|
44
|
+
|
45
|
+
ConfigurationBuilder.new(api_key: _get_env_or_default(properties, 'SECURENATIVE_API_KEY', options.api_key),
|
46
|
+
api_url: _get_env_or_default(properties, 'SECURENATIVE_API_URL', options.api_url),
|
47
|
+
interval: _get_env_or_default(properties, 'SECURENATIVE_INTERVAL', options.interval),
|
48
|
+
max_events: _get_env_or_default(properties, 'SECURENATIVE_MAX_EVENTS', options.max_events),
|
49
|
+
timeout: _get_env_or_default(properties, 'SECURENATIVE_TIMEOUT', options.timeout),
|
50
|
+
auto_send: _get_env_or_default(properties, 'SECURENATIVE_AUTO_SEND', options.auto_send),
|
51
|
+
disable: _get_env_or_default(properties, 'SECURENATIVE_DISABLE', options.disable),
|
52
|
+
log_level: _get_env_or_default(properties, 'SECURENATIVE_LOG_LEVEL', options.log_level),
|
53
|
+
fail_over_strategy: _get_env_or_default(properties, 'SECURENATIVE_FAILOVER_STRATEGY', options.fail_over_strategy))
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'enums/failover_strategy'
|
4
|
+
|
5
|
+
class SecureNativeOptions
|
6
|
+
attr_reader :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
|
7
|
+
attr_writer :api_key, :api_url, :interval, :max_events, :timeout, :auto_send, :disable, :log_level, :fail_over_strategy
|
8
|
+
|
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: FailOverStrategy::FAIL_OPEN)
|
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
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class HanamiContext
|
4
|
+
SECURENATIVE_COOKIE = '_sn'
|
5
|
+
|
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
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.get_url(request)
|
19
|
+
begin
|
20
|
+
request.env['REQUEST_PATH']
|
21
|
+
rescue StandardError
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.get_method(request)
|
27
|
+
begin
|
28
|
+
request.request_method
|
29
|
+
rescue StandardError
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
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
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class RailsContext
|
4
|
+
SECURENATIVE_COOKIE = '_sn'
|
5
|
+
|
6
|
+
def self.get_client_token(request)
|
7
|
+
begin
|
8
|
+
request.cookies[SECURENATIVE_COOKIE]
|
9
|
+
rescue StandardError
|
10
|
+
nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.get_url(request)
|
15
|
+
begin
|
16
|
+
# Rails >= 3.x
|
17
|
+
request.fullpath
|
18
|
+
rescue StandardError
|
19
|
+
begin
|
20
|
+
# Rails < 3.x & Sinatra
|
21
|
+
request.url if url.nil?
|
22
|
+
rescue StandardError
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.get_method(request)
|
29
|
+
begin
|
30
|
+
request.method
|
31
|
+
rescue StandardError
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
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
|
44
|
+
end
|