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-38320 - Fix invalid access token exception #238

Merged
merged 3 commits into from
Jul 18, 2024
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
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ jobs:
files.pythonhosted.org
install.python-poetry.org
pypi.org
*.ingest.us.sentry.io

- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/tests_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
files.pythonhosted.org
install.python-poetry.org
pypi.org
*.ingest.us.sentry.io

- name: Checkout repository
uses: actions/checkout@v4
Expand Down
4 changes: 4 additions & 0 deletions cycode/cli/commands/main_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from cycode.cli.consts import (
CLI_CONTEXT_SETTINGS,
)
from cycode.cli.sentry import add_breadcrumb, init_sentry
from cycode.cli.user_settings.configuration_manager import ConfigurationManager
from cycode.cli.utils.progress_bar import SCAN_PROGRESS_BAR_SECTIONS, get_progress_bar
from cycode.cyclient.config import set_logging_level
Expand Down Expand Up @@ -60,6 +61,9 @@
def main_cli(
context: click.Context, verbose: bool, no_progress_meter: bool, output: str, user_agent: Optional[str]
) -> None:
init_sentry()
add_breadcrumb('cycode')

context.ensure_object(dict)
configuration_manager = ConfigurationManager()

Expand Down
4 changes: 0 additions & 4 deletions cycode/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
import pip_system_certs.wrapt_requests # noqa: F401

from cycode.cli.commands.main_cli import main_cli
from cycode.cli.sentry import add_breadcrumb, init_sentry

if __name__ == '__main__':
# DO NOT REMOVE OR MOVE THIS LINE
# this is required to support multiprocessing in executables files packaged with PyInstaller
# see https://pyinstaller.org/en/latest/common-issues-and-pitfalls.html#multi-processing
freeze_support()

init_sentry()
add_breadcrumb('cycode')

main_cli()
2 changes: 1 addition & 1 deletion cycode/cli/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def init_sentry() -> None:
dsn=consts.SENTRY_DSN,
debug=consts.SENTRY_DEBUG,
release=_get_sentry_release(),
server_name='',
before_send=_before_sentry_event_send,
sample_rate=consts.SENTRY_SAMPLE_RATE,
send_default_pii=consts.SENTRY_SEND_DEFAULT_PII,
Expand All @@ -61,7 +62,6 @@ def init_sentry() -> None:
AtexitIntegration(lambda _, __: None) # disable output to stderr about pending events
],
)
sentry_sdk.set_user(None)


def setup_scope_from_access_token(access_token: Optional[str]) -> None:
Expand Down
17 changes: 11 additions & 6 deletions cycode/cli/utils/jwt_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from typing import Tuple
from typing import Optional, Tuple

import jwt

_JWT_PAYLOAD_POSSIBLE_USER_ID_FIELD_NAMES = ('userId', 'internalId', 'token-user-id')

def get_user_and_tenant_ids_from_access_token(access_token: str) -> Tuple[str, str]:

def get_user_and_tenant_ids_from_access_token(access_token: str) -> Tuple[Optional[str], Optional[str]]:
payload = jwt.decode(access_token, options={'verify_signature': False})
user_id = payload.get('userId')
tenant_id = payload.get('tenantId')

if not user_id or not tenant_id:
raise ValueError('Invalid access token')
user_id = None
for field in _JWT_PAYLOAD_POSSIBLE_USER_ID_FIELD_NAMES:
user_id = payload.get(field)
if user_id:
break

tenant_id = payload.get('tenantId')

return user_id, tenant_id
Loading