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
8 changes: 7 additions & 1 deletion cli/auth/auth_manager.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from cli.utils.string_utils import generate_random_string, hash_string_to_sha256
from cli.user_settings.configuration_manager import ConfigurationManager


class AuthManager:

CODE_VERIFIER_LENGTH = 101

configuration_manager: ConfigurationManager

def __init__(self):
self.configuration_manager = ConfigurationManager()

def generate_pkce_code_pair(self) -> (str, str):
code_verifier = generate_random_string(self.CODE_VERIFIER_LENGTH)
code_challenge = hash_string_to_sha256(code_verifier)
return code_challenge, code_verifier
return code_challenge, code_verifier
4 changes: 3 additions & 1 deletion cli/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
]

DEFAULT_CYCODE_API_URL = "https://api.cycode.com"
DEFAULT_CYCODE_APP_URL = "https://app.cycode.com"

# env var names
CYCODE_API_URL_VAR_NAME = "CYCODE_API_URL"
CYCODE_API_URL_ENV_VAR_NAME = "CYCODE_API_URL"
CYCODE_APP_URL_ENV_VAR_NAME = "CYCODE_APP_URL"
TIMEOUT_ENV_VAR_NAME = "TIMEOUT"
LOGGING_LEVEL_ENV_VAR_NAME = "LOGGING_LEVEL"
# use only for dev envs locally
Expand Down
26 changes: 14 additions & 12 deletions cli/user_settings/config_file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,21 @@ class ConfigFileManager(BaseFileManager):
ENVIRONMENT_SECTION_NAME: str = 'environment'
EXCLUSIONS_SECTION_NAME: str = 'exclusions'

BASE_URL_FIELD_NAME: str = 'cycode_base_url'
API_URL_FIELD_NAME: str = 'cycode_api_url'
APP_URL_FIELD_NAME: str = 'cycode_app_url'
VERBOSE_FIELD_NAME: str = 'verbose'

def __init__(self, path):
self.path = path

def get_base_url(self) -> Optional[str]:
file_content = self.read_file()
def get_api_url(self) -> Optional[str]:
return self._get_value_from_environment_section(self.API_URL_FIELD_NAME)

environment_section = file_content.get(self.ENVIRONMENT_SECTION_NAME, {})
base_url = environment_section.get(self.BASE_URL_FIELD_NAME)
return base_url
def get_app_url(self) -> Optional[str]:
return self._get_value_from_environment_section(self.APP_URL_FIELD_NAME)

def get_verbose_flag(self) -> Optional[bool]:
file_content = self.read_file()

environment_section = file_content.get(self.ENVIRONMENT_SECTION_NAME, {})
verbose_flag = environment_section.get(self.VERBOSE_FIELD_NAME)
return verbose_flag
return self._get_value_from_environment_section(self.VERBOSE_FIELD_NAME)

def get_exclusions_by_scan_type(self, scan_type) -> Dict:
file_content = self.read_file()
Expand All @@ -40,7 +36,7 @@ def get_exclusions_by_scan_type(self, scan_type) -> Dict:
def update_base_url(self, base_url: str):
update_data = {
self.ENVIRONMENT_SECTION_NAME: {
self.BASE_URL_FIELD_NAME: base_url
self.API_URL_FIELD_NAME: base_url
}
}
self.write_content_to_file(update_data)
Expand Down Expand Up @@ -70,3 +66,9 @@ def get_filename(self) -> str:
def _get_exclusions_by_exclusion_type(self, scan_type, exclusion_type) -> List:
scan_type_exclusions = self.get_exclusions_by_scan_type(scan_type)
return scan_type_exclusions.get(exclusion_type, [])

def _get_value_from_environment_section(self, field_name: str):
file_content = self.read_file()
environment_section = file_content.get(self.ENVIRONMENT_SECTION_NAME, {})
value = environment_section.get(field_name)
return value
44 changes: 31 additions & 13 deletions cli/user_settings/configuration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from pathlib import Path
from typing import Optional, Dict
from cli.user_settings.config_file_manager import ConfigFileManager
from cli.consts import DEFAULT_CYCODE_API_URL, CYCODE_API_URL_VAR_NAME, VERBOSE_ENV_VAR_NAME
from cli.consts import DEFAULT_CYCODE_API_URL, DEFAULT_CYCODE_APP_URL, CYCODE_API_URL_ENV_VAR_NAME, \
CYCODE_APP_URL_ENV_VAR_NAME, VERBOSE_ENV_VAR_NAME


class ConfigurationManager:
Expand All @@ -13,30 +14,47 @@ def __init__(self):
self.global_config_file_manager = ConfigFileManager(Path.home())
self.local_config_file_manager = ConfigFileManager(os.getcwd())

def get_base_url(self) -> str:
base_url = self.get_base_url_from_environment_variables()
if base_url is not None:
return base_url
def get_cycode_api_url(self) -> str:
api_url = self.get_api_url_from_environment_variables()
if api_url is not None:
return api_url

base_url = self.local_config_file_manager.get_base_url()
if base_url is not None:
return base_url
api_url = self.local_config_file_manager.get_api_url()
if api_url is not None:
return api_url

base_url = self.global_config_file_manager.get_base_url()
if base_url is not None:
return base_url
api_url = self.global_config_file_manager.get_api_url()
if api_url is not None:
return api_url

return DEFAULT_CYCODE_API_URL

def get_cycode_app_url(self) -> str:
app_url = self.get_app_url_from_environment_variables()
if app_url is not None:
return app_url

app_url = self.local_config_file_manager.get_app_url()
if app_url is not None:
return app_url

app_url = self.global_config_file_manager.get_app_url()
if app_url is not None:
return app_url

return DEFAULT_CYCODE_APP_URL

def get_verbose_flag(self) -> bool:
verbose_flag_env_var = self.get_verbose_flag_from_environment_variables()
verbose_flag_local_config = self.local_config_file_manager.get_verbose_flag()
verbose_flag_global_config = self.global_config_file_manager.get_verbose_flag()
return verbose_flag_env_var or verbose_flag_local_config or verbose_flag_global_config

def get_base_url_from_environment_variables(self) -> Optional[str]:
return self._get_value_from_environment_variables(CYCODE_API_URL_VAR_NAME)
def get_api_url_from_environment_variables(self) -> Optional[str]:
return self._get_value_from_environment_variables(CYCODE_API_URL_ENV_VAR_NAME)

def get_app_url_from_environment_variables(self) -> Optional[str]:
return self._get_value_from_environment_variables(CYCODE_APP_URL_ENV_VAR_NAME)

def get_verbose_flag_from_environment_variables(self) -> bool:
value = self._get_value_from_environment_variables(VERBOSE_ENV_VAR_NAME, '')
Expand Down
2 changes: 1 addition & 1 deletion cyclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, client_id: str, client_secret: str):
self.client_id = client_id
self.timeout = config.timeout

self.base_url = config.base_url
self.base_url = config.cycode_api_url

self._api_token = None
self._expires_in = None
Expand Down
8 changes: 4 additions & 4 deletions cyclient/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ def _get_val_as_int(key):

configuration_manager = ConfigurationManager()

base_url = configuration_manager.get_base_url()
cycode_api_url = configuration_manager.get_cycode_api_url()
try:
urlparse(base_url)
urlparse(cycode_api_url)
except ValueError as e:
logger.warning(f'Invalid cycode api url: {base_url}, using default value', e)
base_url = DEFAULT_CYCODE_API_URL
logger.warning(f'Invalid cycode api url: {cycode_api_url}, using default value', e)
cycode_api_url = DEFAULT_CYCODE_API_URL

timeout = _get_val_as_int(TIMEOUT_ENV_VAR_NAME)
dev_mode = _get_val_as_bool(DEV_MODE_ENV_VAR_NAME)
Expand Down
14 changes: 7 additions & 7 deletions tests/user_settings/test_configuration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_get_base_url_from_environment_variable(mocker):
GLOBAL_CONFIG_BASE_URL_VALUE)

# Act
result = configuration_manager.get_base_url()
result = configuration_manager.get_cycode_api_url()

# Assert
assert result == ENV_VARS_BASE_URL_VALUE
Expand All @@ -31,7 +31,7 @@ def test_get_base_url_from_local_config(mocker):
GLOBAL_CONFIG_BASE_URL_VALUE)

# Act
result = configuration_manager.get_base_url()
result = configuration_manager.get_cycode_api_url()

# Assert
assert result == LOCAL_CONFIG_FILE_BASE_URL_VALUE
Expand All @@ -42,7 +42,7 @@ def test_get_base_url_from_global_config(mocker):
configuration_manager = _configure_mocks(mocker, None, None, GLOBAL_CONFIG_BASE_URL_VALUE)

# Act
result = configuration_manager.get_base_url()
result = configuration_manager.get_cycode_api_url()

# Assert
assert result == GLOBAL_CONFIG_BASE_URL_VALUE
Expand All @@ -53,7 +53,7 @@ def test_get_base_url_not_configured(mocker):
configuration_manager = _configure_mocks(mocker, None, None, None)

# Act
result = configuration_manager.get_base_url()
result = configuration_manager.get_cycode_api_url()

# Assert
assert result == DEFAULT_CYCODE_API_URL
Expand All @@ -63,12 +63,12 @@ def _configure_mocks(mocker,
expected_env_var_base_url,
expected_local_config_file_base_url,
expected_global_config_file_base_url):
mocker.patch.object(ConfigurationManager, 'get_base_url_from_environment_variables',
mocker.patch.object(ConfigurationManager, 'get_api_url_from_environment_variables',
return_value=expected_env_var_base_url)
configuration_manager = ConfigurationManager()
configuration_manager.local_config_file_manager = Mock()
configuration_manager.local_config_file_manager.get_base_url.return_value = expected_local_config_file_base_url
configuration_manager.local_config_file_manager.get_api_url.return_value = expected_local_config_file_base_url
configuration_manager.global_config_file_manager = Mock()
configuration_manager.global_config_file_manager.get_base_url.return_value = expected_global_config_file_base_url
configuration_manager.global_config_file_manager.get_api_url.return_value = expected_global_config_file_base_url

return configuration_manager