Skip to content

Commit 9f588fc

Browse files
authored
CM-38374 - Disable Sentry for on-premise installations; fix CLI config loading (#239)
1 parent c7b77f0 commit 9f588fc

File tree

6 files changed

+71
-18
lines changed

6 files changed

+71
-18
lines changed

cycode/cli/consts.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@
107107

108108
COMMIT_RANGE_BASED_COMMAND_SCAN_TYPES = [PRE_RECEIVE_COMMAND_SCAN_TYPE, COMMIT_HISTORY_COMMAND_SCAN_TYPE]
109109

110-
DEFAULT_CYCODE_API_URL = 'https://api.cycode.com'
111-
DEFAULT_CYCODE_APP_URL = 'https://app.cycode.com'
110+
DEFAULT_CYCODE_DOMAIN = 'cycode.com'
111+
DEFAULT_CYCODE_API_URL = f'https://api.{DEFAULT_CYCODE_DOMAIN}'
112+
DEFAULT_CYCODE_APP_URL = f'https://app.{DEFAULT_CYCODE_DOMAIN}'
112113

113114
# env var names
114115
CYCODE_API_URL_ENV_VAR_NAME = 'CYCODE_API_URL'

cycode/cli/sentry.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
import sentry_sdk
66
from sentry_sdk.integrations.atexit import AtexitIntegration
7+
from sentry_sdk.integrations.dedupe import DedupeIntegration
8+
from sentry_sdk.integrations.excepthook import ExcepthookIntegration
9+
from sentry_sdk.integrations.logging import LoggingIntegration
710
from sentry_sdk.scrubber import DEFAULT_DENYLIST, EventScrubber
811

912
from cycode import __version__
1013
from cycode.cli import consts
1114
from cycode.cli.utils.jwt_utils import get_user_and_tenant_ids_from_access_token
1215
from cycode.cyclient import logger
16+
from cycode.cyclient.config import on_premise_installation
1317

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

3741

3842
_SENTRY_LOCAL_RELEASE = _get_sentry_local_release()
43+
_SENTRY_DISABLED = on_premise_installation
3944

4045

4146
def _before_sentry_event_send(event: dict, _: dict) -> Optional[dict]:
47+
if _SENTRY_DISABLED:
48+
# drop all events when Sentry is disabled
49+
return None
50+
4251
if event.get('release') == _SENTRY_LOCAL_RELEASE:
4352
logger.debug('Dropping Sentry event due to local development setup')
4453
return None
@@ -58,8 +67,12 @@ def init_sentry() -> None:
5867
include_local_variables=consts.SENTRY_INCLUDE_LOCAL_VARIABLES,
5968
max_request_body_size=consts.SENTRY_MAX_REQUEST_BODY_SIZE,
6069
event_scrubber=EventScrubber(denylist=_DENY_LIST, recursive=True),
70+
default_integrations=False,
6171
integrations=[
62-
AtexitIntegration(lambda _, __: None) # disable output to stderr about pending events
72+
AtexitIntegration(lambda _, __: None), # disable output to stderr about pending events
73+
ExcepthookIntegration(),
74+
DedupeIntegration(),
75+
LoggingIntegration(),
6376
],
6477
)
6578

cycode/cli/user_settings/base_file_manager.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ def get_filename(self) -> str:
1111
...
1212

1313
def read_file(self) -> Dict[Hashable, Any]:
14-
try:
15-
return read_file(self.get_filename())
16-
except FileNotFoundError:
17-
return {}
14+
return read_file(self.get_filename())
1815

1916
def write_content_to_file(self, content: Dict[Hashable, Any]) -> None:
2017
filename = self.get_filename()

cycode/cli/utils/yaml_utils.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
from typing import Any, Dict, Hashable
1+
import os
2+
from typing import Any, Dict, Hashable, TextIO
23

34
import yaml
45

56

7+
def _yaml_safe_load(file: TextIO) -> Dict[Hashable, Any]:
8+
# loader.get_single_data could return None
9+
loaded_file = yaml.safe_load(file)
10+
if loaded_file is None:
11+
return {}
12+
13+
return loaded_file
14+
15+
616
def read_file(filename: str) -> Dict[Hashable, Any]:
7-
with open(filename, 'r', encoding='UTF-8') as file:
8-
return yaml.safe_load(file)
17+
if not os.path.exists(filename):
18+
return {}
919

20+
with open(filename, 'r', encoding='UTF-8') as file:
21+
return _yaml_safe_load(file)
1022

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

24+
def write_file(filename: str, content: Dict[Hashable, Any]) -> None:
1825
with open(filename, 'w', encoding='UTF-8') as file:
19-
file_content = _deep_update(file_content, content)
20-
yaml.safe_dump(file_content, file)
26+
yaml.safe_dump(content, file)
27+
28+
29+
def update_file(filename: str, content: Dict[Hashable, Any]) -> None:
30+
write_file(filename, _deep_update(read_file(filename), content))
2131

2232

2333
def _deep_update(source: Dict[Hashable, Any], overrides: Dict[Hashable, Any]) -> Dict[Hashable, Any]:
@@ -26,4 +36,5 @@ def _deep_update(source: Dict[Hashable, Any], overrides: Dict[Hashable, Any]) ->
2636
source[key] = _deep_update(source.get(key, {}), value)
2737
else:
2838
source[key] = overrides[key]
39+
2940
return source

cycode/cyclient/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ def is_valid_url(url: str) -> bool:
106106
)
107107
cycode_api_url = consts.DEFAULT_CYCODE_API_URL
108108

109+
110+
def _is_on_premise_installation(cycode_domain: str) -> bool:
111+
return not cycode_api_url.endswith(cycode_domain)
112+
113+
114+
on_premise_installation = _is_on_premise_installation(consts.DEFAULT_CYCODE_DOMAIN)
115+
109116
timeout = get_val_as_int(consts.CYCODE_CLI_REQUEST_TIMEOUT_ENV_VAR_NAME)
110117
if not timeout:
111118
timeout = get_val_as_int(consts.TIMEOUT_ENV_VAR_NAME)

tests/cyclient/test_config.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import TYPE_CHECKING
2+
3+
from cycode.cli.consts import DEFAULT_CYCODE_DOMAIN
4+
from cycode.cyclient.config import _is_on_premise_installation
5+
6+
if TYPE_CHECKING:
7+
from _pytest.monkeypatch import MonkeyPatch
8+
9+
10+
def test_is_on_premise_installation(monkeypatch: 'MonkeyPatch') -> None:
11+
monkeypatch.setattr('cycode.cyclient.config.cycode_api_url', 'api.cycode.com')
12+
assert not _is_on_premise_installation(DEFAULT_CYCODE_DOMAIN)
13+
monkeypatch.setattr('cycode.cyclient.config.cycode_api_url', 'api.eu.cycode.com')
14+
assert not _is_on_premise_installation(DEFAULT_CYCODE_DOMAIN)
15+
16+
monkeypatch.setattr('cycode.cyclient.config.cycode_api_url', 'cycode.google.com')
17+
assert _is_on_premise_installation(DEFAULT_CYCODE_DOMAIN)
18+
monkeypatch.setattr('cycode.cyclient.config.cycode_api_url', 'cycode.blabla.google.com')
19+
assert _is_on_premise_installation(DEFAULT_CYCODE_DOMAIN)
20+
21+
monkeypatch.setattr('cycode.cyclient.config.cycode_api_url', 'api.cycode.com')
22+
assert _is_on_premise_installation('blabla')
23+
monkeypatch.setattr('cycode.cyclient.config.cycode_api_url', 'cycode.blabla.google.com')
24+
assert _is_on_premise_installation('blabla')

0 commit comments

Comments
 (0)