Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CM-38374 - Disable Sentry for on-premise installations; fix CLI config loading #239

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions cycode/cli/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@

COMMIT_RANGE_BASED_COMMAND_SCAN_TYPES = [PRE_RECEIVE_COMMAND_SCAN_TYPE, COMMIT_HISTORY_COMMAND_SCAN_TYPE]

DEFAULT_CYCODE_API_URL = 'https://api.cycode.com'
DEFAULT_CYCODE_APP_URL = 'https://app.cycode.com'
DEFAULT_CYCODE_DOMAIN = 'cycode.com'
DEFAULT_CYCODE_API_URL = f'https://api.{DEFAULT_CYCODE_DOMAIN}'
DEFAULT_CYCODE_APP_URL = f'https://app.{DEFAULT_CYCODE_DOMAIN}'

# env var names
CYCODE_API_URL_ENV_VAR_NAME = 'CYCODE_API_URL'
Expand Down
15 changes: 14 additions & 1 deletion cycode/cli/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

import sentry_sdk
from sentry_sdk.integrations.atexit import AtexitIntegration
from sentry_sdk.integrations.dedupe import DedupeIntegration
from sentry_sdk.integrations.excepthook import ExcepthookIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.scrubber import DEFAULT_DENYLIST, EventScrubber

from cycode import __version__
from cycode.cli import consts
from cycode.cli.utils.jwt_utils import get_user_and_tenant_ids_from_access_token
from cycode.cyclient import logger
from cycode.cyclient.config import on_premise_installation

# when Sentry is blocked on the machine, we want to keep clean output without retries warnings
logging.getLogger('urllib3.connectionpool').setLevel(logging.ERROR)
Expand All @@ -36,9 +40,14 @@ def _get_sentry_local_release() -> str:


_SENTRY_LOCAL_RELEASE = _get_sentry_local_release()
_SENTRY_DISABLED = on_premise_installation


def _before_sentry_event_send(event: dict, _: dict) -> Optional[dict]:
if _SENTRY_DISABLED:
# drop all events when Sentry is disabled
return None

if event.get('release') == _SENTRY_LOCAL_RELEASE:
logger.debug('Dropping Sentry event due to local development setup')
return None
Expand All @@ -58,8 +67,12 @@ def init_sentry() -> None:
include_local_variables=consts.SENTRY_INCLUDE_LOCAL_VARIABLES,
max_request_body_size=consts.SENTRY_MAX_REQUEST_BODY_SIZE,
event_scrubber=EventScrubber(denylist=_DENY_LIST, recursive=True),
default_integrations=False,
integrations=[
AtexitIntegration(lambda _, __: None) # disable output to stderr about pending events
AtexitIntegration(lambda _, __: None), # disable output to stderr about pending events
ExcepthookIntegration(),
DedupeIntegration(),
LoggingIntegration(),
],
)

Expand Down
26 changes: 19 additions & 7 deletions cycode/cli/utils/yaml_utils.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
from typing import Any, Dict, Hashable
from typing import Any, Dict, Hashable, TextIO

import yaml


def _yaml_safe_load(file: TextIO) -> Dict[Hashable, Any]:
# loader.get_single_data could return None
loaded_file = yaml.safe_load(file)
if loaded_file is None:
return {}

return loaded_file


def read_file(filename: str) -> Dict[Hashable, Any]:
with open(filename, 'r', encoding='UTF-8') as file:
return yaml.safe_load(file)
return _yaml_safe_load(file)


def write_file(filename: str, content: Dict[Hashable, Any]) -> None:
with open(filename, 'w', encoding='UTF-8') as file:
yaml.safe_dump(content, file)
cycode-security[bot] marked this conversation as resolved.
Show resolved Hide resolved


def update_file(filename: str, content: Dict[Hashable, Any]) -> None:
try:
with open(filename, 'r', encoding='UTF-8') as file:
file_content = yaml.safe_load(file)
file_content = read_file(filename)
except FileNotFoundError:
file_content = {}

with open(filename, 'w', encoding='UTF-8') as file:
file_content = _deep_update(file_content, content)
yaml.safe_dump(file_content, file)
write_file(filename, _deep_update(file_content, content))


def _deep_update(source: Dict[Hashable, Any], overrides: Dict[Hashable, Any]) -> Dict[Hashable, Any]:
Expand All @@ -26,4 +37,5 @@ def _deep_update(source: Dict[Hashable, Any], overrides: Dict[Hashable, Any]) ->
source[key] = _deep_update(source.get(key, {}), value)
else:
source[key] = overrides[key]

return source
7 changes: 7 additions & 0 deletions cycode/cyclient/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ def is_valid_url(url: str) -> bool:
)
cycode_api_url = consts.DEFAULT_CYCODE_API_URL


def _is_on_premise_installation(cycode_domain: str) -> bool:
return not cycode_api_url.endswith(cycode_domain)


on_premise_installation = _is_on_premise_installation(consts.DEFAULT_CYCODE_DOMAIN)

timeout = get_val_as_int(consts.CYCODE_CLI_REQUEST_TIMEOUT_ENV_VAR_NAME)
if not timeout:
timeout = get_val_as_int(consts.TIMEOUT_ENV_VAR_NAME)
Expand Down
24 changes: 24 additions & 0 deletions tests/cyclient/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import TYPE_CHECKING

from cycode.cli.consts import DEFAULT_CYCODE_DOMAIN
from cycode.cyclient.config import _is_on_premise_installation

if TYPE_CHECKING:
from _pytest.monkeypatch import MonkeyPatch


def test_is_on_premise_installation(monkeypatch: 'MonkeyPatch') -> None:
monkeypatch.setattr('cycode.cyclient.config.cycode_api_url', 'api.cycode.com')
assert not _is_on_premise_installation(DEFAULT_CYCODE_DOMAIN)
monkeypatch.setattr('cycode.cyclient.config.cycode_api_url', 'api.eu.cycode.com')
assert not _is_on_premise_installation(DEFAULT_CYCODE_DOMAIN)

monkeypatch.setattr('cycode.cyclient.config.cycode_api_url', 'cycode.google.com')
assert _is_on_premise_installation(DEFAULT_CYCODE_DOMAIN)
monkeypatch.setattr('cycode.cyclient.config.cycode_api_url', 'cycode.blabla.google.com')
elsapet marked this conversation as resolved.
Show resolved Hide resolved
assert _is_on_premise_installation(DEFAULT_CYCODE_DOMAIN)

monkeypatch.setattr('cycode.cyclient.config.cycode_api_url', 'api.cycode.com')
assert _is_on_premise_installation('blabla')
monkeypatch.setattr('cycode.cyclient.config.cycode_api_url', 'cycode.blabla.google.com')
assert _is_on_premise_installation('blabla')
elsapet marked this conversation as resolved.
Show resolved Hide resolved
Loading