From 0ac6e333c222d15c2fbbaa7a15c3125ada3493a3 Mon Sep 17 00:00:00 2001 From: rongzhang Date: Tue, 29 Oct 2024 16:13:29 +0000 Subject: [PATCH] address comments --- .../config/querybook_default_config.yaml | 4 ++ querybook/server/datasources/github.py | 4 +- querybook/server/env.py | 4 ++ .../__init__.py | 0 .../github.py} | 60 +++++++------------ 5 files changed, 31 insertions(+), 41 deletions(-) rename querybook/server/lib/{github_integration => github}/__init__.py (100%) rename querybook/server/lib/{github_integration/github_integration.py => github/github.py} (65%) diff --git a/querybook/config/querybook_default_config.yaml b/querybook/config/querybook_default_config.yaml index 42bd611c2..268a12424 100644 --- a/querybook/config/querybook_default_config.yaml +++ b/querybook/config/querybook_default_config.yaml @@ -45,6 +45,10 @@ OAUTH_AUTHORIZATION_URL: ~ OAUTH_TOKEN_URL: ~ OAUTH_USER_PROFILE: ~ +# --------------- GitHub Integration --------------- +GITHUB_CLIENT_ID: ~ +GITHUB_CLIENT_SECRET: ~ + # LDAP LDAP_CONN: ~ LDAP_USER_DN: uid={},dc=example,dc=com diff --git a/querybook/server/datasources/github.py b/querybook/server/datasources/github.py index 82420604c..08601cdf5 100644 --- a/querybook/server/datasources/github.py +++ b/querybook/server/datasources/github.py @@ -1,16 +1,14 @@ from app.datasource import register -from lib.github_integration.github_integration import get_github_manager +from lib.github.github import github_manager from typing import Dict @register("/github/auth/", methods=["GET"]) def connect_github() -> Dict[str, str]: - github_manager = get_github_manager() return github_manager.initiate_github_integration() @register("/github/is_authenticated/", methods=["GET"]) def is_github_authenticated() -> str: - github_manager = get_github_manager() is_authenticated = github_manager.get_github_token() is not None return {"is_authenticated": is_authenticated} diff --git a/querybook/server/env.py b/querybook/server/env.py index 2128717a3..2eedb83d9 100644 --- a/querybook/server/env.py +++ b/querybook/server/env.py @@ -89,6 +89,10 @@ class QuerybookSettings(object): OAUTH_USER_PROFILE = get_env_config("OAUTH_USER_PROFILE") AZURE_TENANT_ID = get_env_config("AZURE_TENANT_ID") + # GitHub App settings for feature integration + GITHUB_CLIENT_ID = os.getenv("GITHUB_CLIENT_ID") + GITHUB_CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET") + LDAP_CONN = get_env_config("LDAP_CONN") LDAP_USE_TLS = str(get_env_config("LDAP_USE_TLS")).lower() == "true" LDAP_USE_BIND_USER = str(get_env_config("LDAP_USE_BIND_USER")).lower() == "true" diff --git a/querybook/server/lib/github_integration/__init__.py b/querybook/server/lib/github/__init__.py similarity index 100% rename from querybook/server/lib/github_integration/__init__.py rename to querybook/server/lib/github/__init__.py diff --git a/querybook/server/lib/github_integration/github_integration.py b/querybook/server/lib/github/github.py similarity index 65% rename from querybook/server/lib/github_integration/github_integration.py rename to querybook/server/lib/github/github.py index 4cced5956..b383cb1c7 100644 --- a/querybook/server/lib/github_integration/github_integration.py +++ b/querybook/server/lib/github/github.py @@ -8,13 +8,20 @@ LOG = get_logger(__file__) - GITHUB_OAUTH_CALLBACK = "/github/oauth2callback" +GITHUB_ACCESS_TOKEN = "github_access_token" -class GitHubIntegrationManager(GitHubLoginManager): - def __init__(self, additional_scopes: Optional[list] = None): +class GitHubManager(GitHubLoginManager): + def __init__( + self, + additional_scopes: Optional[list] = None, + client_id: Optional[str] = None, + client_secret: Optional[str] = None, + ): self.additional_scopes = additional_scopes or [] + self._client_id = client_id + self._client_secret = client_secret super().__init__() @property @@ -24,14 +31,18 @@ def oauth_config(self) -> Dict[str, Any]: config[ "callback_url" ] = f"{QuerybookSettings.PUBLIC_URL}{GITHUB_OAUTH_CALLBACK}" + if self._client_id: + config["client_id"] = self._client_id + if self._client_secret: + config["client_secret"] = self._client_secret return config def save_github_token(self, token: str) -> None: - flask_session["github_access_token"] = token + flask_session[GITHUB_ACCESS_TOKEN] = token LOG.debug("Saved GitHub token to session") def get_github_token(self) -> Optional[str]: - return flask_session.get("github_access_token") + return flask_session.get(GITHUB_ACCESS_TOKEN) def initiate_github_integration(self) -> Dict[str, str]: github = self.oauth_session @@ -70,41 +81,14 @@ def error_response(self, error_message: str) -> str: """ -def get_github_manager() -> GitHubIntegrationManager: - return GitHubIntegrationManager(additional_scopes=["repo"]) +# Create a module-level instance with dynamic client_id and client_secret +github_manager = GitHubManager( + additional_scopes=["repo"], + client_id=QuerybookSettings.GITHUB_CLIENT_ID, + client_secret=QuerybookSettings.GITHUB_CLIENT_SECRET, +) @flask_app.route(GITHUB_OAUTH_CALLBACK) def github_callback() -> str: - github_manager = get_github_manager() return github_manager.github_integration_callback() - - -# Test GitHub OAuth Flow -def main(): - github_manager = GitHubIntegrationManager() - oauth_config = github_manager.oauth_config - client_id = oauth_config["client_id"] - client_secret = oauth_config["client_secret"] - - from requests_oauthlib import OAuth2Session - - github = OAuth2Session(client_id) - authorization_url, state = github.authorization_url( - oauth_config["authorization_url"] - ) - print("Please go here and authorize,", authorization_url) - - redirect_response = input("Paste the full redirect URL here:") - github.fetch_token( - oauth_config["token_url"], - client_secret=client_secret, - authorization_response=redirect_response, - ) - - user_profile = github.get(oauth_config["profile_url"]).json() - print(user_profile) - - -if __name__ == "__main__": - main()