From 245af3729144cff3ccbc7a7e44b41d11a8ecd090 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Sun, 20 Feb 2022 15:33:19 +0100 Subject: [PATCH] feat(api): changed jira integration feat(api): changed github integration --- api/chalicelib/core/integration_base.py | 3 ++- api/chalicelib/core/integration_github.py | 15 ++++++++------- api/chalicelib/core/integration_jira_cloud.py | 13 ++++++++++--- api/schemas.py | 8 ++++++-- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/api/chalicelib/core/integration_base.py b/api/chalicelib/core/integration_base.py index 45e1891a1..f8edaad62 100644 --- a/api/chalicelib/core/integration_base.py +++ b/api/chalicelib/core/integration_base.py @@ -1,4 +1,5 @@ from abc import ABC, abstractmethod + from chalicelib.utils import pg_client, helper @@ -37,7 +38,7 @@ class BaseIntegration(ABC): pass @abstractmethod - def update(self, changes): + def update(self, changes, obfuscate=False): pass @abstractmethod diff --git a/api/chalicelib/core/integration_github.py b/api/chalicelib/core/integration_github.py index a13946e46..a05c946f4 100644 --- a/api/chalicelib/core/integration_github.py +++ b/api/chalicelib/core/integration_github.py @@ -1,6 +1,6 @@ -from chalicelib.utils import pg_client, helper -from chalicelib.core.integration_github_issue import GithubIntegrationIssue from chalicelib.core import integration_base +from chalicelib.core.integration_github_issue import GithubIntegrationIssue +from chalicelib.utils import pg_client, helper PROVIDER = "GITHUB" @@ -15,8 +15,6 @@ class GitHubIntegration(integration_base.BaseIntegration): def provider(self): return PROVIDER - - def get_obfuscated(self): integration = self.get() if integration is None: @@ -24,7 +22,7 @@ class GitHubIntegration(integration_base.BaseIntegration): token = "*" * (len(integration["token"]) - 4) + integration["token"][-4:] return {"token": token, "provider": self.provider.lower()} - def update(self, changes): + def update(self, changes, obfuscate=False): with pg_client.PostgresClient() as cur: sub_query = [f"{helper.key_to_snake_case(k)} = %({k})s" for k in changes.keys()] cur.execute( @@ -71,8 +69,11 @@ class GitHubIntegration(integration_base.BaseIntegration): if s is not None: return self.update( changes={ - "token": data["token"] - } + "token": data["token"] \ + if data.get("token") and len(data["token"]) > 0 and data["token"].find("***") == -1 \ + else s["token"] + }, + obfuscate=True ) else: return self.add(token=data["token"]) diff --git a/api/chalicelib/core/integration_jira_cloud.py b/api/chalicelib/core/integration_jira_cloud.py index 8caf22832..ea9c6c24e 100644 --- a/api/chalicelib/core/integration_jira_cloud.py +++ b/api/chalicelib/core/integration_jira_cloud.py @@ -5,6 +5,10 @@ from chalicelib.utils import pg_client, helper PROVIDER = "JIRA" +def obfuscate_string(string): + return "*" * (len(string) - 4) + string[-4:] + + class JIRAIntegration(integration_base.BaseIntegration): def __init__(self, tenant_id, user_id): self.__tenant_id = tenant_id @@ -36,11 +40,11 @@ class JIRAIntegration(integration_base.BaseIntegration): integration = self.get() if integration is None: return None - integration["token"] = "*" * (len(integration["token"]) - 4) + integration["token"][-4:] + integration["token"] = obfuscate_string(integration["token"]) integration["provider"] = self.provider.lower() return integration - def update(self, changes): + def update(self, changes, obfuscate=False): with pg_client.PostgresClient() as cur: sub_query = [f"{helper.key_to_snake_case(k)} = %({k})s" for k in changes.keys()] cur.execute( @@ -53,6 +57,8 @@ class JIRAIntegration(integration_base.BaseIntegration): **changes}) ) w = helper.dict_to_camel_case(cur.fetchone()) + if obfuscate: + w["token"] = obfuscate_string(w["token"]) return w # TODO: make this generic for all issue tracking integrations @@ -93,7 +99,8 @@ class JIRAIntegration(integration_base.BaseIntegration): if data.get("token") and len(data["token"]) > 0 and data["token"].find("***") == -1 \ else s["token"], "url": data["url"] - } + }, + obfuscate=True ) else: return self.add( diff --git a/api/schemas.py b/api/schemas.py index 56eb24cf7..4defa64df 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -1,7 +1,7 @@ from enum import Enum from typing import Optional, List, Union, Literal -from pydantic import BaseModel, Field, EmailStr, HttpUrl, root_validator +from pydantic import BaseModel, Field, EmailStr, HttpUrl, root_validator, validator from chalicelib.utils.TimeUTC import TimeUTC @@ -107,7 +107,11 @@ class JiraGithubSchema(BaseModel): provider: str = Field(...) username: str = Field(...) token: str = Field(...) - url: str = Field(...) + url: HttpUrl = Field(...) + + @validator('url') + def transform_url(cls, v: HttpUrl): + return HttpUrl.build(scheme=v.scheme, host=v.host) class CreateEditWebhookSchema(BaseModel):