-
Notifications
You must be signed in to change notification settings - Fork 527
SNOW-2454885: Add auth tests for aio #2664
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
Open
sfc-gh-turbaszek
wants to merge
3
commits into
main
Choose a base branch
from
turbaszek-add-auth-aio-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,174
−11
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| import logging.config | ||
| import os | ||
| import subprocess | ||
| import threading | ||
| import webbrowser | ||
| from enum import Enum | ||
| from typing import Union | ||
|
|
||
| import requests | ||
|
|
||
| import snowflake.connector.aio | ||
|
|
||
| try: | ||
| from src.snowflake.connector.vendored.requests.auth import HTTPBasicAuth | ||
| except ImportError: | ||
| pass | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| logger.setLevel(logging.INFO) | ||
|
|
||
|
|
||
| class Scenario(Enum): | ||
| SUCCESS = "success" | ||
| FAIL = "fail" | ||
| TIMEOUT = "timeout" | ||
| EXTERNAL_OAUTH_OKTA_SUCCESS = "externalOauthOktaSuccess" | ||
| INTERNAL_OAUTH_SNOWFLAKE_SUCCESS = "internalOauthSnowflakeSuccess" | ||
|
|
||
|
|
||
| def get_access_token_oauth(cfg): | ||
| auth_url = cfg["auth_url"] | ||
|
|
||
| data = { | ||
| "username": cfg["okta_user"], | ||
| "password": cfg["okta_pass"], | ||
| "grant_type": "password", | ||
| "scope": f"session:role:{cfg['role']}", | ||
| } | ||
|
|
||
| headers = {"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"} | ||
|
|
||
| auth_credentials = HTTPBasicAuth(cfg["oauth_client_id"], cfg["oauth_client_secret"]) | ||
| try: | ||
| response = requests.post( | ||
| url=auth_url, data=data, headers=headers, auth=auth_credentials | ||
| ) | ||
| response.raise_for_status() | ||
| return response.json()["access_token"] | ||
|
|
||
| except requests.exceptions.HTTPError as http_err: | ||
| logger.error(f"HTTP error occurred: {http_err}") | ||
| raise | ||
|
|
||
|
|
||
| def clean_browser_processes(): | ||
| if os.getenv("AUTHENTICATION_TESTS_ENV") == "docker": | ||
| try: | ||
| clean_browser_processes_path = "/externalbrowser/cleanBrowserProcesses.js" | ||
| process = subprocess.run(["node", clean_browser_processes_path], timeout=15) | ||
| logger.debug(f"OUTPUT: {process.stdout}, ERRORS: {process.stderr}") | ||
| except Exception as e: | ||
| raise RuntimeError(e) | ||
|
|
||
|
|
||
| class AuthorizationTestHelper: | ||
| def __init__(self, configuration: dict): | ||
| self.auth_test_env = os.getenv("AUTHENTICATION_TESTS_ENV") | ||
| self.configuration = configuration | ||
| self.error_msg = "" | ||
|
|
||
| def update_config(self, configuration): | ||
| self.configuration = configuration | ||
|
|
||
| async def connect_and_provide_credentials( | ||
| self, scenario: Scenario, login: str, password: str | ||
| ): | ||
| import asyncio | ||
|
|
||
| try: | ||
| # Use asyncio task for connection instead of thread | ||
| connect_task = asyncio.create_task(self.connect_and_execute_simple_query()) | ||
|
|
||
| if self.auth_test_env == "docker": | ||
| # Browser credentials still needs to run in thread since it's sync | ||
| browser = threading.Thread( | ||
| target=self._provide_credentials, args=(scenario, login, password) | ||
| ) | ||
| browser.start() | ||
| # Wait for browser thread to complete | ||
| await asyncio.get_event_loop().run_in_executor(None, browser.join) | ||
|
|
||
| # Wait for connection task to complete | ||
| await connect_task | ||
|
|
||
| except Exception as e: | ||
| self.error_msg = e | ||
| logger.error(e) | ||
|
|
||
| def get_error_msg(self) -> str: | ||
| return str(self.error_msg) | ||
|
|
||
| async def connect_and_execute_simple_query(self): | ||
| try: | ||
| logger.info("Trying to connect to Snowflake") | ||
| async with snowflake.connector.aio.SnowflakeConnection( | ||
| **self.configuration | ||
| ) as con: | ||
| result = await con.cursor().execute("select 1;") | ||
| logger.debug(await result.fetchall()) | ||
| logger.info("Successfully connected to Snowflake") | ||
| return True | ||
| except Exception as e: | ||
| self.error_msg = e | ||
| logger.error(e) | ||
| return False | ||
|
|
||
| async def connect_and_execute_set_session_state(self, key: str, value: str): | ||
| try: | ||
| logger.info("Trying to connect to Snowflake") | ||
| async with snowflake.connector.aio.SnowflakeConnection( | ||
| **self.configuration | ||
| ) as con: | ||
| result = await con.cursor().execute(f"SET {key} = '{value}'") | ||
| logger.debug(await result.fetchall()) | ||
| logger.info("Successfully SET session variable") | ||
| return True | ||
| except Exception as e: | ||
| self.error_msg = e | ||
| logger.error(e) | ||
| return False | ||
|
|
||
| async def connect_and_execute_check_session_state(self, key: str): | ||
| try: | ||
| logger.info("Trying to connect to Snowflake") | ||
| async with snowflake.connector.aio.SnowflakeConnection( | ||
| **self.configuration | ||
| ) as con: | ||
| result = await con.cursor().execute(f"SELECT 1, ${key}") | ||
| value = (await result.fetchone())[1] | ||
| logger.debug(value) | ||
| logger.info("Successfully READ session variable") | ||
| return value | ||
| except Exception as e: | ||
| self.error_msg = e | ||
| logger.error(e) | ||
| return False | ||
|
|
||
| def _provide_credentials(self, scenario: Scenario, login: str, password: str): | ||
| try: | ||
| webbrowser.register("xdg-open", None, webbrowser.GenericBrowser("xdg-open")) | ||
| provide_browser_credentials_path = ( | ||
| "/externalbrowser/provideBrowserCredentials.js" | ||
| ) | ||
| process = subprocess.run( | ||
| [ | ||
| "node", | ||
| provide_browser_credentials_path, | ||
| scenario.value, | ||
| login, | ||
| password, | ||
| ], | ||
| timeout=15, | ||
| ) | ||
| logger.debug(f"OUTPUT: {process.stdout}, ERRORS: {process.stderr}") | ||
| except Exception as e: | ||
| self.error_msg = e | ||
| raise RuntimeError(e) | ||
|
|
||
| def get_totp(self, seed: str = "") -> []: | ||
| if self.auth_test_env == "docker": | ||
| try: | ||
| provide_totp_generator_path = "/externalbrowser/totpGenerator.js" | ||
| process = subprocess.run( | ||
| ["node", provide_totp_generator_path, seed], | ||
| timeout=40, | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
| logger.debug(f"OUTPUT: {process.stdout}, ERRORS: {process.stderr}") | ||
| return process.stdout.strip().split() | ||
| except Exception as e: | ||
| self.error_msg = e | ||
| raise RuntimeError(e) | ||
| else: | ||
| logger.info("TOTP generation is not supported in this environment") | ||
| return "" | ||
|
|
||
| async def connect_using_okta_connection_and_execute_custom_command( | ||
| self, command: str, return_token: bool = False | ||
| ) -> Union[bool, str]: | ||
| try: | ||
| logger.info("Setup PAT") | ||
| async with snowflake.connector.aio.SnowflakeConnection( | ||
| **self.configuration | ||
| ) as con: | ||
| result = await con.cursor().execute(command) | ||
| token = (await result.fetchall())[0][1] | ||
| except Exception as e: | ||
| self.error_msg = e | ||
| logger.error(e) | ||
| return False | ||
| if return_token: | ||
| return token | ||
| return False | ||
|
|
||
| async def connect_and_execute_simple_query_with_mfa_token(self, totp_codes): | ||
| # Try each TOTP code until one works | ||
| for i, totp_code in enumerate(totp_codes): | ||
| logging.info(f"Trying TOTP code {i + 1}/{len(totp_codes)}") | ||
|
|
||
| self.configuration["passcode"] = totp_code | ||
| self.error_msg = "" | ||
|
|
||
| connection_success = await self.connect_and_execute_simple_query() | ||
|
|
||
| if connection_success: | ||
| logging.info(f"Successfully connected with TOTP code {i + 1}") | ||
| return True | ||
| else: | ||
| last_error = str(self.error_msg) | ||
| logging.warning(f"TOTP code {i + 1} failed: {last_error}") | ||
| if "TOTP Invalid" in last_error: | ||
| logging.info("TOTP/MFA error detected.") | ||
| continue | ||
| else: | ||
| logging.error(f"Non-TOTP error detected: {last_error}") | ||
| break | ||
| return False |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.