From 197997e3d4e7754bad6d5b4ca76f74242a45daef Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Mon, 7 Nov 2022 13:10:23 +0100 Subject: [PATCH] feat(chalice): fixed SSO --- api/chalicelib/core/log_tool_elasticsearch.py | 1 - api/chalicelib/core/metadata.py | 2 +- api/chalicelib/core/sessions_assignments.py | 5 +++-- api/db_changes.sql | 3 --- ee/api/chalicelib/core/authorizers.py | 5 +++-- ee/api/chalicelib/core/metrics_exp.py | 2 +- ee/api/chalicelib/core/sessions_exp.py | 2 +- ee/api/chalicelib/utils/assist_helper.py | 4 ++-- ee/api/chalicelib/utils/helper_ee.py | 6 ++++++ ee/api/or_dependencies.py | 1 - 10 files changed, 17 insertions(+), 14 deletions(-) delete mode 100644 api/db_changes.sql create mode 100644 ee/api/chalicelib/utils/helper_ee.py diff --git a/api/chalicelib/core/log_tool_elasticsearch.py b/api/chalicelib/core/log_tool_elasticsearch.py index ba20636c4..7b00074c6 100644 --- a/api/chalicelib/core/log_tool_elasticsearch.py +++ b/api/chalicelib/core/log_tool_elasticsearch.py @@ -1,7 +1,6 @@ # from elasticsearch import Elasticsearch, RequestsHttpConnection from elasticsearch import Elasticsearch from chalicelib.core import log_tools -import base64 import logging logging.getLogger('elasticsearch').level = logging.ERROR diff --git a/api/chalicelib/core/metadata.py b/api/chalicelib/core/metadata.py index c245e44bf..0f73d0962 100644 --- a/api/chalicelib/core/metadata.py +++ b/api/chalicelib/core/metadata.py @@ -1,7 +1,7 @@ import re from chalicelib.core import projects -from chalicelib.utils import pg_client, dev +from chalicelib.utils import pg_client MAX_INDEXES = 10 diff --git a/api/chalicelib/core/sessions_assignments.py b/api/chalicelib/core/sessions_assignments.py index 4491f62d0..567200b07 100644 --- a/api/chalicelib/core/sessions_assignments.py +++ b/api/chalicelib/core/sessions_assignments.py @@ -23,7 +23,8 @@ def __get_saved_data(project_id, session_id, issue_id, tool): return helper.dict_to_camel_case(cur.fetchone()) -def create_new_assignment(tenant_id, project_id, session_id, creator_id, assignee, description, title, issue_type, integration_project_id): +def create_new_assignment(tenant_id, project_id, session_id, creator_id, assignee, description, title, issue_type, + integration_project_id): error, integration = integrations_manager.get_integration(tenant_id=tenant_id, user_id=creator_id) if error is not None: return error @@ -40,7 +41,7 @@ def create_new_assignment(tenant_id, project_id, session_id, creator_id, assigne integration_project_id=integration_project_id) except integration_base_issue.RequestException as e: return integration_base_issue.proxy_issues_handler(e) - if issue is not None and "id" not in issue: + if issue is None or "id" not in issue: return {"errors": ["something went wrong while creating the issue"]} with pg_client.PostgresClient() as cur: query = cur.mogrify("""\ diff --git a/api/db_changes.sql b/api/db_changes.sql deleted file mode 100644 index 155f10278..000000000 --- a/api/db_changes.sql +++ /dev/null @@ -1,3 +0,0 @@ -BEGIN; -CREATE INDEX pages_ttfb_idx ON events.pages (ttfb) WHERE ttfb > 0; -COMMIT; \ No newline at end of file diff --git a/ee/api/chalicelib/core/authorizers.py b/ee/api/chalicelib/core/authorizers.py index 3f6767d40..363745f4b 100644 --- a/ee/api/chalicelib/core/authorizers.py +++ b/ee/api/chalicelib/core/authorizers.py @@ -38,12 +38,13 @@ def jwt_context(context): } -def generate_jwt(id, tenant_id, iat, aud): +def generate_jwt(id, tenant_id, iat, aud, exp=None): token = jwt.encode( payload={ "userId": id, "tenantId": tenant_id, - "exp": iat // 1000 + config("JWT_EXPIRATION", cast=int) + TimeUTC.get_utc_offset() // 1000, + "exp": exp + TimeUTC.get_utc_offset() // 1000 if exp is not None \ + else iat // 1000 + config("JWT_EXPIRATION", cast=int) + TimeUTC.get_utc_offset() // 1000, "iss": config("JWT_ISSUER"), "iat": iat // 1000, "aud": aud diff --git a/ee/api/chalicelib/core/metrics_exp.py b/ee/api/chalicelib/core/metrics_exp.py index 04e180e93..9a8af012b 100644 --- a/ee/api/chalicelib/core/metrics_exp.py +++ b/ee/api/chalicelib/core/metrics_exp.py @@ -1,7 +1,7 @@ import math import schemas -from chalicelib.utils import pg_client, exp_ch_helper +from chalicelib.utils import exp_ch_helper from chalicelib.utils import args_transformer from chalicelib.utils import helper from chalicelib.utils.TimeUTC import TimeUTC diff --git a/ee/api/chalicelib/core/sessions_exp.py b/ee/api/chalicelib/core/sessions_exp.py index 20c0db9dd..2ad9cdc86 100644 --- a/ee/api/chalicelib/core/sessions_exp.py +++ b/ee/api/chalicelib/core/sessions_exp.py @@ -101,7 +101,7 @@ def get_by_id2_pg(project_id, session_id, context: schemas_ee.CurrentContext, fu data['crashes'] = events_ios.get_crashes_by_session_id(session_id=session_id) data['userEvents'] = events_ios.get_customs_by_sessionId(project_id=project_id, session_id=session_id) - data['mobsUrl'] = sessions_mobs.get_ios(sessionId=session_id) + data['mobsUrl'] = sessions_mobs.get_ios(session_id=session_id) else: data['events'] = events.get_by_sessionId2_pg(project_id=project_id, session_id=session_id, group_clickrage=True) diff --git a/ee/api/chalicelib/utils/assist_helper.py b/ee/api/chalicelib/utils/assist_helper.py index 913435bd4..d182226c0 100644 --- a/ee/api/chalicelib/utils/assist_helper.py +++ b/ee/api/chalicelib/utils/assist_helper.py @@ -6,7 +6,7 @@ from time import time from decouple import config from chalicelib.core import assist -from chalicelib.utils import helper +from chalicelib.utils import helper_ee def __get_secret(): @@ -18,7 +18,7 @@ def get_temporary_credentials(): secret = __get_secret() if secret is None: return {"errors": ["secret not defined"]} - user = helper.generate_salt() + user = helper_ee.generate_salt() ttl = config("assist_ttl", cast=int, default=48) * 3600 timestamp = int(time()) + ttl username = str(timestamp) + ':' + user diff --git a/ee/api/chalicelib/utils/helper_ee.py b/ee/api/chalicelib/utils/helper_ee.py new file mode 100644 index 000000000..919f25219 --- /dev/null +++ b/ee/api/chalicelib/utils/helper_ee.py @@ -0,0 +1,6 @@ +import random +import string + + +def generate_salt(): + return "".join(random.choices(string.hexdigits, k=36)) diff --git a/ee/api/or_dependencies.py b/ee/api/or_dependencies.py index ea5d45ef2..421f0853f 100644 --- a/ee/api/or_dependencies.py +++ b/ee/api/or_dependencies.py @@ -12,7 +12,6 @@ from starlette.responses import Response, JSONResponse import schemas_ee from chalicelib.core import traces -from chalicelib.core import permissions async def OR_context(request: Request) -> schemas_ee.CurrentContext: