Skip to content

Commit 6b61dc2

Browse files
author
Inbal Tako
committed
Read config from a file
1 parent 1b48050 commit 6b61dc2

29 files changed

Lines changed: 174 additions & 21 deletions

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
requests~=2.22.0
1+
requests~=2.23.0
22
pycrypto~=2.6.1

securenative/config/__init__.py

Whitespace-only changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from securenative.config.securenative_options import SecurenativeOptions
2+
from securenative.enums.failover_strategy import FailOverStrategy
3+
4+
5+
class ConfigurationBuilder(object):
6+
7+
def __init__(self):
8+
self.api_key = ""
9+
self.api_url = "https://api.securenative.com/collector/api/v1"
10+
self.interval = 1000
11+
self.max_events = 1000
12+
self.timeout = 1500
13+
self.auto_send = True
14+
self.disable = False
15+
self.log_level = "CRITICAL"
16+
self.fail_over_strategy = FailOverStrategy.FAIL_OPEN
17+
18+
@staticmethod
19+
def default_config_builder():
20+
return ConfigurationBuilder()
21+
22+
def with_api_key(self, api_key):
23+
self.api_key = api_key
24+
return self
25+
26+
def with_api_url(self, api_url):
27+
self.api_url = api_url
28+
return self
29+
30+
def with_interval(self, interval):
31+
self.interval = interval
32+
return self
33+
34+
def with_max_events(self, max_events):
35+
self.max_events = max_events
36+
return self
37+
38+
def with_timeout(self, timeout):
39+
self.timeout = timeout
40+
return self
41+
42+
def with_auto_send(self, auto_send):
43+
self.auto_send = auto_send
44+
return self
45+
46+
def with_disable(self, disable):
47+
self.disable = disable
48+
return self
49+
50+
def with_log_level(self, log_level):
51+
self.log_level = log_level
52+
return self
53+
54+
def with_fail_over_strategy(self, fail_over_strategy):
55+
self.fail_over_strategy = fail_over_strategy
56+
return self
57+
58+
@staticmethod
59+
def get_default_securenative_options():
60+
return SecurenativeOptions("", "https://api.securenative.com/collector/api/v1", 1000,
61+
1000, 1500, True, False, "CRITICAL", FailOverStrategy.FAIL_OPEN)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import os
2+
from configparser import ConfigParser, NoSectionError
3+
4+
from securenative.config.configuration_builder import ConfigurationBuilder
5+
6+
7+
class ConfigurationManager(object):
8+
DEFAULT_CONFIG_FILE = "securenative.ini"
9+
CUSTOM_CONFIG_FILE_ENV_NAME = "SECURENATIVE_COMFIG_FILE"
10+
config = ConfigParser.ConfigParser()
11+
12+
@staticmethod
13+
def read_resource_file(cls, resource_path):
14+
cls.config.read(resource_path)
15+
sections = cls.config.sections()
16+
17+
properties = {}
18+
for section in sections:
19+
options = cls.config.options(section)
20+
for option in options:
21+
try:
22+
properties[option] = cls.config.get(section, option)
23+
except NoSectionError:
24+
properties[option] = None
25+
26+
return properties
27+
28+
@staticmethod
29+
def _get_resource_path(cls, env_name):
30+
env_value = os.environ.get(env_name)
31+
32+
if env_value:
33+
return env_value
34+
35+
return os.environ.get(cls.DEFAULT_CONFIG_FILE)
36+
37+
@staticmethod
38+
def config_builder():
39+
return ConfigurationBuilder.default_config_builder()
40+
41+
@staticmethod
42+
def load_config(cls):
43+
builder = ConfigurationBuilder()
44+
options = builder.get_default_securenative_options()
45+
resource_path = cls._get_env_or_default(cls.DEFAULT_CONFIG_FILE, cls.CUSTOM_CONFIG_FILE_ENV_NAME)
46+
properties = cls.read_resource_file(resource_path)
47+
48+
builder.\
49+
with_api_key(cls._get_env_or_default(properties, "SECURENATIVE_API_KEY", options.api)).\
50+
with_api_url(cls._get_env_or_default(properties, "SECURENATIVE_API_URL", options.api_url)).\
51+
with_interval(cls._get_env_or_default(properties, "SECURENATIVE_INTERVAL", options.interval)).\
52+
with_max_events(cls._get_env_or_default(properties, "SECURENATIVE_MAX_EVENTS", options.max_events)).\
53+
with_timeout(cls._get_env_or_default(properties, "SECURENATIVE_TIMEOUT", options.timeout)).\
54+
with_auto_send(cls._get_env_or_default(properties, "SECURENATIVE_AUTO_SEND", options.auto_send)).\
55+
with_disable(cls._get_env_or_default(properties, "SECURENATIVE_DISABLE", options.disable)).\
56+
with_log_level(cls._get_env_or_default(properties, "SECURENATIVE_LOG_LEVEL", options.log_level)).\
57+
with_fail_over_strategy(cls._get_env_or_default(properties, "SECURENATIVE_FAILOVER_STRATEGY", options))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class SecurenativeOptions(object):
2+
3+
def __init__(self, api_key, api_url, interval, max_events, timeout, auto_send,
4+
disable, log_level, fail_over_strategy):
5+
self.api_key = api_key
6+
self.api_url = api_url
7+
self.interval = interval
8+
self.max_events = max_events
9+
self.timeout = timeout
10+
self.auto_send = auto_send
11+
self.disable = disable
12+
self.log_level = log_level
13+
self.fail_over_strategy = fail_over_strategy

securenative/context/__init__.py

Whitespace-only changes.

securenative/enums/__init__.py

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from enum import Enum
2+
3+
4+
class FailOverStrategy(Enum):
5+
FAIL_OPEN = "fail-open"
6+
FAIL_CLOSED = "fail-closed"

securenative/event_options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import uuid
22
import time
33

4-
from securenative.utils import _parse_cookie
4+
from securenative.utils.utils import _parse_cookie
55

66

77
class User:
@@ -30,7 +30,7 @@ def __init__(self, event_type, user=User(), ip=u'127.0.0.1', remote_ip=u'127.0.0
3030
self.cid, self.fp = _parse_cookie(sn_cookie_value)
3131

3232
self.vid = str(uuid.uuid4())
33-
self.ts = int(time.time())*1000
33+
self.ts = int(time.time()) * 1000
3434

3535
def as_dict(self):
3636
return {

securenative/events/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)