From 7d2763776cef9df467bb3113696994f61758bdab Mon Sep 17 00:00:00 2001 From: saileshwar-skyflow <156889717+saileshwar-skyflow@users.noreply.github.com> Date: Mon, 25 Nov 2024 14:29:01 +0530 Subject: [PATCH] SK-1731: Updated error, info logs and Fixes (#139) * SK-1731: Updated error, info logs and Fixes --- requirements.txt | 3 +- skyflow/utils/_skyflow_messages.py | 273 +++++++++++----------- skyflow/utils/_utils.py | 10 +- skyflow/utils/validations/_validations.py | 2 +- skyflow/vault/client/client.py | 8 +- skyflow/vault/controller/_vault.py | 2 - tests/constants/test_constants.py | 4 +- tests/utils/test__utils.py | 22 +- tests/vault/client/test__client.py | 2 +- 9 files changed, 156 insertions(+), 170 deletions(-) diff --git a/requirements.txt b/requirements.txt index 7c009b1..46a8594 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,5 @@ DateTime~=5.5 PyJWT~=2.9.0 requests~=2.32.3 coverage -cryptography \ No newline at end of file +cryptography +python-dotenv~=1.0.1 \ No newline at end of file diff --git a/skyflow/utils/_skyflow_messages.py b/skyflow/utils/_skyflow_messages.py index 2d3dd26..8a8e8dd 100644 --- a/skyflow/utils/_skyflow_messages.py +++ b/skyflow/utils/_skyflow_messages.py @@ -3,6 +3,8 @@ from ._version import SDK_VERSION error_prefix = f"Skyflow Python SDK {SDK_VERSION}" +INFO = "INFO" +ERROR = "ERROR" class SkyflowMessages: class ErrorCodes(Enum): @@ -141,150 +143,137 @@ class Error(Enum): INVALID_JSON_FORMAT_IN_CREDENTIALS_ENV = f"{error_prefix} Validation error. Invalid JSON format in SKYFLOW_CREDENTIALS environment variable." class Info(Enum): - CLIENT_INITIALIZED = "INFO: [Skyflow] Initialized skyflow client." - VALIDATING_VAULT_CONFIG = "INFO: [Skyflow] Validating vault config." - VALIDATING_CONNECTION_CONFIG = "INFO: [Skyflow] Validating connection config." - UNABLE_TO_GENERATE_SDK_METRIC = f"INFO: [Skyflow] Unable to generate {{}} metric." - VAULT_CONTROLLER_INITIALIZED = f"INFO: [Skyflow] Initialized vault controller with vault ID {{}}." - CONNECTION_CONTROLLER_INITIALIZED = f"INFO: [Skyflow] Initialized connection controller with connection ID {{}}." - VAULT_CONFIG_EXISTS = f"INFO: [Skyflow] Vault config with vault ID {{}} already exists." - VAULT_CONFIG_DOES_NOT_EXIST = f"INFO: [Skyflow] Vault config with vault ID {{}} doesn't exist." - CONNECTION_CONFIG_EXISTS = f"INFO: [Skyflow] Connection config with connection ID {{}} already exists." - CONNECTION_CONFIG_DOES_NOT_EXIST = f"INFO: [Skyflow] Connection config with connection ID {{}} doesn't exist." - LOGGER_SETUP_DONE = f"INFO: [Skyflow] Set up logger." - CURRENT_LOG_LEVEL = f"INFO: [Skyflow] Current log level is {{}}." - - BEARER_TOKEN_EXPIRED = "INFO: [Skyflow] Bearer token is expired." - GET_BEARER_TOKEN_TRIGGERED = "INFO: [Skyflow] generate_bearer_token method triggered." - GET_BEARER_TOKEN_SUCCESS = "INFO: [Skyflow] Bearer token generated." - GET_SIGNED_DATA_TOKENS_TRIGGERED = "INFO: [Skyflow] generate_signed_data_tokens method triggered." - GET_SIGNED_DATA_TOKEN_SUCCESS = "INFO: [Skyflow] Signed data tokens generated." - GENERATE_BEARER_TOKEN_FROM_CREDENTIALS_STRING_TRIGGERED = "INFO: [Skyflow] generate bearer_token_from_credential_string method triggered ." - REUSE_BEARER_TOKEN = "INFO: [Skyflow] Reusing bearer token." - - - VALIDATE_INSERT_REQUEST = "INFO: [Skyflow] Validating insert request." - INSERT_TRIGGERED = "INFO: [Skyflow] Insert method triggered." - INSERT_SUCCESS = "INFO: [Skyflow] Data inserted." - INSERT_REQUEST_RESOLVED = "INFO: [Skyflow] Insert request resolved." - - VALIDATE_UPDATE_REQUEST = "INFO: [Skyflow] Validating update request." - UPDATE_REQUEST_RESOLVED = "INFO: [Skyflow] Update request resolved." - UPDATE_SUCCESS = "INFO: [Skyflow] Data updated." - UPDATE_TRIGGERED = "INFO: [Skyflow] Update method triggered." - - DELETE_TRIGGERED = "INFO: [Skyflow] Delete method triggered." - VALIDATING_DELETE_REQUEST = "INFO: [Skyflow] Validating delete request." - DELETE_REQUEST_RESOLVED = "INFO: [Skyflow] Delete request resolved." - DELETE_SUCCESS = "INFO: [Skyflow] Data deleted." - - GET_TRIGGERED = "INFO: [Skyflow] Get method triggered." - VALIDATE_GET_REQUEST = "INFO: [Skyflow] Validating get request." - GET_REQUEST_RESOLVED = "INFO: [Skyflow] Get request resolved." - GET_SUCCESS = "INFO: [Skyflow] Data revealed." - - QUERY_TRIGGERED = "INFO: [Skyflow] Query method triggered." - VALIDATING_QUERY_REQUEST = "INFO: [Skyflow] Validating query request." - QUERY_REQUEST_RESOLVED = "INFO: [Skyflow] Query request resolved." - QUERY_SUCCESS = "INFO: [Skyflow] Query executed." - - DETOKENIZE_TRIGGERED = "INFO: [Skyflow] Detokenize method triggered." - VALIDATE_DETOKENIZE_REQUEST = "INFO: [Skyflow] Validating detokenize request." - DETOKENIZE_REQUEST_RESOLVED = "INFO: [Skyflow] Detokenize request resolved." - DETOKENIZE_SUCCESS = "INFO: [Skyflow] Data detokenized." - - TOKENIZE_TRIGGERED = "INFO: [Skyflow] Tokenize method triggered." - VALIDATING_TOKENIZE_REQUEST = "INFO: [Skyflow] Validating tokenize request." - TOKENIZE_REQUEST_RESOLVED = "INFO: [Skyflow] Tokenize request resolved." - TOKENIZE_SUCCESS = "INFO: [Skyflow] Data tokenized." - - INVOKE_CONNECTION_TRIGGERED = "INFO: [Skyflow] Invoke connection method triggered." - VALIDATING_INVOKE_CONNECTION_REQUEST = "INFO: [Skyflow] Validating invoke connection request." - INVOKE_CONNECTION_REQUEST_RESOLVED = "INFO: [Skyflow] Invoke connection request resolved." - INVOKE_CONNECTION_SUCCESS = "info: [Skyflow] Invoke Connection Success." - - - VALIDATE_DETOKENIZE_INPUT = "Validating detokenize input" - VALIDATE_GET_RECORDS = "Validating get records" - VALIDATE_CONNECTION_CONFIG = "Validating connection config" - BEARER_TOKEN_RECEIVED = "tokenProvider returned token successfully." - GENERATE_BEARER_TOKEN_TRIGGERED = "Generate bearer token triggered" - GENERATE_BEARER_TOKEN_SUCCESS = "Generate bearer token returned successfully" - IS_TOKEN_VALID_TRIGGERED = "isTokenValid() triggered" - IS_EXPIRED_TRIGGERED = "is_expired() triggered" - EMPTY_ACCESS_TOKEN = "Give access token is empty" - INVALID_TOKEN = "Given token is invalid" - UPDATE_DATA_SUCCESS = "Data has been updated successfully" + CLIENT_INITIALIZED = f"{INFO}: [{error_prefix}] Initialized skyflow client." + VALIDATING_VAULT_CONFIG = f"{INFO}: [{error_prefix}] Validating vault config." + VALIDATING_CONNECTION_CONFIG = f"{INFO}: [{error_prefix}] Validating connection config." + UNABLE_TO_GENERATE_SDK_METRIC = f"{INFO}: [{error_prefix}] Unable to generate {{}} metric." + VAULT_CONTROLLER_INITIALIZED = f"{INFO}: [{error_prefix}] Initialized vault controller with vault ID {{}}." + CONNECTION_CONTROLLER_INITIALIZED = f"{INFO}: [{error_prefix}] Initialized connection controller with connection ID {{}}." + VAULT_CONFIG_EXISTS = f"{INFO}: [{error_prefix}] Vault config with vault ID {{}} already exists." + VAULT_CONFIG_DOES_NOT_EXIST = f"{INFO}: [{error_prefix}] Vault config with vault ID {{}} doesn't exist." + CONNECTION_CONFIG_EXISTS = f"{INFO}: [{error_prefix}] Connection config with connection ID {{}} already exists." + CONNECTION_CONFIG_DOES_NOT_EXIST = f"{INFO}: [{error_prefix}] Connection config with connection ID {{}} doesn't exist." + LOGGER_SETUP_DONE = f"{INFO}: [{error_prefix}] Set up logger." + CURRENT_LOG_LEVEL = f"{INFO}: [{error_prefix}] Current log level is {{}}." + + BEARER_TOKEN_EXPIRED = f"{INFO}: [{error_prefix}] Bearer token is expired." + GET_BEARER_TOKEN_TRIGGERED = f"{INFO}: [{error_prefix}] generate_bearer_token method triggered." + GET_BEARER_TOKEN_SUCCESS = f"{INFO}: [{error_prefix}] Bearer token generated." + GET_SIGNED_DATA_TOKENS_TRIGGERED = f"{INFO}: [{error_prefix}] generate_signed_data_tokens method triggered." + GET_SIGNED_DATA_TOKEN_SUCCESS = f"{INFO}: [{error_prefix}] Signed data tokens generated." + GENERATE_BEARER_TOKEN_FROM_CREDENTIALS_STRING_TRIGGERED = f"{INFO}: [{error_prefix}] generate bearer_token_from_credential_string method triggered ." + REUSE_BEARER_TOKEN = f"{INFO}: [{error_prefix}] Reusing bearer token." + + + VALIDATE_INSERT_REQUEST = f"{INFO}: [{error_prefix}] Validating insert request." + INSERT_TRIGGERED = f"{INFO}: [{error_prefix}] Insert method triggered." + INSERT_SUCCESS = f"{INFO}: [{error_prefix}] Data inserted." + INSERT_REQUEST_RESOLVED = f"{INFO}: [{error_prefix}] Insert request resolved." + + VALIDATE_UPDATE_REQUEST = f"{INFO}: [{error_prefix}] Validating update request." + UPDATE_REQUEST_RESOLVED = f"{INFO}: [{error_prefix}] Update request resolved." + UPDATE_SUCCESS = f"{INFO}: [{error_prefix}] Data updated." + UPDATE_TRIGGERED = f"{INFO}: [{error_prefix}] Update method triggered." + + DELETE_TRIGGERED = f"{INFO}: [{error_prefix}] Delete method triggered." + VALIDATING_DELETE_REQUEST = f"{INFO}: [{error_prefix}] Validating delete request." + DELETE_REQUEST_RESOLVED = f"{INFO}: [{error_prefix}] Delete request resolved." + DELETE_SUCCESS = f"{INFO}: [{error_prefix}] Data deleted." + + GET_TRIGGERED = f"{INFO}: [{error_prefix}] Get method triggered." + VALIDATE_GET_REQUEST = f"{INFO}: [{error_prefix}] Validating get request." + GET_REQUEST_RESOLVED = f"{INFO}: [{error_prefix}] Get request resolved." + GET_SUCCESS = f"{INFO}: [{error_prefix}] Data revealed." + + QUERY_TRIGGERED = f"{INFO}: [{error_prefix}] Query method triggered." + VALIDATING_QUERY_REQUEST = f"{INFO}: [{error_prefix}] Validating query request." + QUERY_REQUEST_RESOLVED = f"{INFO}: [{error_prefix}] Query request resolved." + QUERY_SUCCESS = f"{INFO}: [{error_prefix}] Query executed." + + DETOKENIZE_TRIGGERED = f"{INFO}: [{error_prefix}] Detokenize method triggered." + VALIDATE_DETOKENIZE_REQUEST = f"{INFO}: [{error_prefix}] Validating detokenize request." + DETOKENIZE_REQUEST_RESOLVED = f"{INFO}: [{error_prefix}] Detokenize request resolved." + DETOKENIZE_SUCCESS = f"{INFO}: [{error_prefix}] Data detokenized." + + TOKENIZE_TRIGGERED = f"{INFO}: [{error_prefix}] Tokenize method triggered." + VALIDATING_TOKENIZE_REQUEST = f"{INFO}: [{error_prefix}] Validating tokenize request." + TOKENIZE_REQUEST_RESOLVED = f"{INFO}: [{error_prefix}] Tokenize request resolved." + TOKENIZE_SUCCESS = f"{INFO}: [{error_prefix}] Data tokenized." + + INVOKE_CONNECTION_TRIGGERED = f"{INFO}: [{error_prefix}] Invoke connection method triggered." + VALIDATING_INVOKE_CONNECTION_REQUEST = f"{INFO}: [{error_prefix}] Validating invoke connection request." + INVOKE_CONNECTION_REQUEST_RESOLVED = f"{INFO}: [{error_prefix}] Invoke connection request resolved." + INVOKE_CONNECTION_SUCCESS = f"{INFO}: [{error_prefix}] Invoke Connection Success." class ErrorLogs(Enum): - VAULTID_IS_REQUIRED = "ERROR: [Skyflow] Invalid vault config. Vault ID is required." - EMPTY_VAULTID = "ERROR: [Skyflow] Invalid vault config. Vault ID can not be empty." - CLUSTER_ID_IS_REQUIRED = "ERROR: [Skyflow] Invalid vault config. Cluster ID is required." - EMPTY_CLUSTER_ID = "ERROR: [Skyflow] Invalid vault config. Cluster ID can not be empty." - ENV_IS_REQUIRED = "ERROR: [Skyflow] Invalid vault config. Env is required." - CONNECTION_ID_IS_REQUIRED = "ERROR: [Skyflow] Invalid connection config. Connection ID is required." - EMPTY_CONNECTION_ID = "ERROR: [Skyflow] Invalid connection config. Connection ID can not be empty." - CONNECTION_URL_IS_REQUIRED = "ERROR: [Skyflow] Invalid connection config. Connection URL is required." - EMPTY_CONNECTION_URL = "ERROR: [Skyflow] Invalid connection config. Connection URL can not be empty." - INVALID_CONNECTION_URL = "ERROR: [Skyflow] Invalid connection config. Connection URL is not a valid URL." - EMPTY_CREDENTIALS_PATH = "ERROR: [Skyflow] Invalid credentials. Credentials path can not be empty." - EMPTY_CREDENTIALS_STRING = "ERROR: [Skyflow] Invalid credentials. Credentials string can not be empty." - EMPTY_TOKEN_VALUE = "ERROR: [Skyflow] Invalid credentials. Token can not be empty." - EMPTY_API_KEY_VALUE = "ERROR: [Skyflow] Invalid credentials. Api key can not be empty." - INVALID_API_KEY = "ERROR: [Skyflow] Invalid credentials. Api key is invalid." - - INVALID_BEARER_TOKEN = "ERROR: [Skyflow] Bearer token is invalid or expired." - INVALID_CREDENTIALS_FILE = "ERROR: [Skyflow] Credentials file is either null or an invalid file." - INVALID_CREDENTIALS_STRING_FORMAT = "ERROR: [Skyflow] Credentials string in not in a valid JSON string format." - PRIVATE_KEY_IS_REQUIRED = "ERROR: [Skyflow] Private key is required." - CLIENT_ID_IS_REQUIRED = "ERROR: [Skyflow] Client ID is required." - KEY_ID_IS_REQUIRED = "ERROR: [Skyflow] Key ID is required." - TOKEN_URI_IS_REQUIRED = "ERROR: [Skyflow] Token URI is required." - INVALID_TOKEN_URI = "ERROR: [Skyflow] Invalid value for token URI in credentials." - - - TABLE_IS_REQUIRED = f"ERROR: [Skyflow] Invalid {{}} request. Table is required." - EMPTY_TABLE_NAME = f"ERROR: [Skyflow] Invalid {{}} request. Table name can not be empty." - VALUES_IS_REQUIRED = f"ERROR: [Skyflow] Invalid {{}} request. Values are required." - EMPTY_VALUES = f"ERROR: [Skyflow] Invalid {{}} request. Values can not be empty." - EMPTY_OR_NULL_VALUE_IN_VALUES = f"ERROR: [Skyflow] Invalid {{}} request. Value can not be null or empty in values for key {{}}." - EMPTY_OR_NULL_KEY_IN_VALUES = f"ERROR: [Skyflow] Invalid {{}} request. Key can not be null or empty in values." - EMPTY_UPSERT = f"ERROR: [Skyflow] Invalid {{}} request. Upsert can not be empty." - HOMOGENOUS_NOT_SUPPORTED_WITH_UPSERT = f"ERROR: [Skyflow] Invalid {{}} request. Homogenous is not supported when upsert is passed." - EMPTY_TOKENS = f"ERROR: [Skyflow] Invalid {{}} request. Tokens can not be empty." - EMPTY_OR_NULL_VALUE_IN_TOKENS = f"ERROR: [Skyflow] Invalid {{}} request. Value can not be null or empty in tokens for key {{}}." - EMPTY_OR_NULL_KEY_IN_TOKENS = f"ERROR: [Skyflow] Invalid {{}} request. Key can not be null or empty in tokens." - MISMATCH_OF_FIELDS_AND_TOKENS = f"ERROR: [Skyflow] Invalid {{}} request. Keys for values and tokens are not matching." - - EMPTY_IDS = f"ERROR: [Skyflow] Invalid {{}} request. Ids can not be empty." - EMPTY_OR_NULL_ID_IN_IDS = f"ERROR: [Skyflow] Invalid {{}} request. Id can not be null or empty in ids at index {{}}." - TOKENIZATION_NOT_SUPPORTED_WITH_REDACTION= f"ERROR: [Skyflow] Invalid {{}} request. Tokenization is not supported when redaction is applied." - TOKENIZATION_SUPPORTED_ONLY_WITH_IDS=f"ERROR: [Skyflow] Invalid {{}} request. Tokenization is not supported when column name and values are passed." - TOKENS_NOT_ALLOWED_WITH_BYOT_DISABLE = f"ERROR: [Skyflow] Invalid {{}} request. Tokens are not allowed when token_strict is DISABLE." - INSUFFICIENT_TOKENS_PASSED_FOR_BYOT_ENABLE_STRICT =f"ERROR: [Skyflow] Invalid {{}} request. For tokenStrict as ENABLE_STRICT, tokens should be passed for all fields." - TOKENS_REQUIRED = f"ERROR: [Skyflow] Invalid {{}} request. Tokens are required." - EMPTY_FIELDS = f"ERROR: [Skyflow] Invalid {{}} request. Fields can not be empty." - EMPTY_OFFSET = f"ERROR: [Skyflow] Invalid {{}} request. Offset ca not be empty." - NEITHER_IDS_NOR_COLUMN_NAME_PASSED = f"ERROR: [Skyflow] Invalid {{}} request. Neither ids nor column name and values are passed." - BOTH_IDS_AND_COLUMN_NAME_PASSED = f"ERROR: [Skyflow] Invalid {{}} request. Both ids and column name and values are passed." - COLUMN_NAME_IS_REQUIRED = f"ERROR: [Skyflow] Invalid {{}} request. Column name is required when column values are passed." - COLUMN_VALUES_IS_REQUIRED_GET = f"ERROR: [Skyflow] Invalid {{}} request. Column values are required when column name is passed." - SKYFLOW_ID_IS_REQUIRED = f"ERROR: [Skyflow] Invalid {{}} request. Skyflow Id is required." - EMPTY_SKYFLOW_ID = f"ERROR: [Skyflow] Invalid {{}} request. Skyflow Id can not be empty." - - COLUMN_VALUES_IS_REQUIRED_TOKENIZE = f"ERROR: [Skyflow] Invalid {{}} request. ColumnValues are required." - EMPTY_COLUMN_GROUP_IN_COLUMN_VALUES = f"ERROR: [Skyflow] Invalid {{}} request. Column group can not be null or empty in column values at index %s2." - - EMPTY_QUERY= f"ERROR: [Skyflow] Invalid {{}} request. Query can not be empty." - QUERY_IS_REQUIRED = f"ERROR: [Skyflow] Invalid {{}} request. Query is required." - - INSERT_RECORDS_REJECTED = f"ERROR: [Skyflow] Insert call resulted in failure." - DETOKENIZE_REQUEST_REJECTED = "ERROR: [Skyflow] Detokenize request resulted in failure." - DELETE_REQUEST_REJECTED = "ERROR: [Skyflow] Delete request resulted in failure." - TOKENIZE_REQUEST_REJECTED = "ERROR: [Skyflow] Tokenize request resulted in failure." - UPDATE_REQUEST_REJECTED = "ERROR: [Skyflow] Update request resulted in failure." - QUERY_REQUEST_REJECTED = "ERROR: [Skyflow] Query request resulted in failure." - GET_REQUEST_REJECTED = f"ERROR: [Skyflow] Get request resulted in failure." + VAULTID_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Invalid vault config. Vault ID is required." + EMPTY_VAULTID = f"{ERROR}: [{error_prefix}] Invalid vault config. Vault ID can not be empty." + CLUSTER_ID_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Invalid vault config. Cluster ID is required." + EMPTY_CLUSTER_ID = f"{ERROR}: [{error_prefix}] Invalid vault config. Cluster ID can not be empty." + ENV_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Invalid vault config. Env is required." + CONNECTION_ID_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Invalid connection config. Connection ID is required." + EMPTY_CONNECTION_ID = f"{ERROR}: [{error_prefix}] Invalid connection config. Connection ID can not be empty." + CONNECTION_URL_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Invalid connection config. Connection URL is required." + EMPTY_CONNECTION_URL = f"{ERROR}: [{error_prefix}] Invalid connection config. Connection URL can not be empty." + INVALID_CONNECTION_URL = f"{ERROR}: [{error_prefix}] Invalid connection config. Connection URL is not a valid URL." + EMPTY_CREDENTIALS_PATH = f"{ERROR}: [{error_prefix}] Invalid credentials. Credentials path can not be empty." + EMPTY_CREDENTIALS_STRING = f"{ERROR}: [{error_prefix}] Invalid credentials. Credentials string can not be empty." + EMPTY_TOKEN_VALUE = f"{ERROR}: [{error_prefix}] Invalid credentials. Token can not be empty." + EMPTY_API_KEY_VALUE = f"{ERROR}: [{error_prefix}] Invalid credentials. Api key can not be empty." + INVALID_API_KEY = f"{ERROR}: [{error_prefix}] Invalid credentials. Api key is invalid." + + INVALID_BEARER_TOKEN = f"{ERROR}: [{error_prefix}] Bearer token is invalid or expired." + INVALID_CREDENTIALS_FILE = f"{ERROR}: [{error_prefix}] Credentials file is either null or an invalid file." + INVALID_CREDENTIALS_STRING_FORMAT = f"{ERROR}: [{error_prefix}] Credentials string in not in a valid JSON string format." + PRIVATE_KEY_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Private key is required." + CLIENT_ID_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Client ID is required." + KEY_ID_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Key ID is required." + TOKEN_URI_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Token URI is required." + INVALID_TOKEN_URI = f"{ERROR}: [{error_prefix}] Invalid value for token URI in credentials." + + + TABLE_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Table is required." + EMPTY_TABLE_NAME =f"{ERROR}: [{error_prefix}] Invalid {{}} request. Table name can not be empty." + VALUES_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Values are required." + EMPTY_VALUES = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Values can not be empty." + EMPTY_OR_NULL_VALUE_IN_VALUES = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Value can not be null or empty in values for key {{}}." + EMPTY_OR_NULL_KEY_IN_VALUES = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Key can not be null or empty in values." + EMPTY_UPSERT = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Upsert can not be empty." + HOMOGENOUS_NOT_SUPPORTED_WITH_UPSERT = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Homogenous is not supported when upsert is passed." + EMPTY_TOKENS = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Tokens can not be empty." + EMPTY_OR_NULL_VALUE_IN_TOKENS = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Value can not be null or empty in tokens for key {{}}." + EMPTY_OR_NULL_KEY_IN_TOKENS = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Key can not be null or empty in tokens." + MISMATCH_OF_FIELDS_AND_TOKENS = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Keys for values and tokens are not matching." + + EMPTY_IDS = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Ids can not be empty." + EMPTY_OR_NULL_ID_IN_IDS = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Id can not be null or empty in ids at index {{}}." + TOKENIZATION_NOT_SUPPORTED_WITH_REDACTION= f"{ERROR}: [{error_prefix}] Invalid {{}} request. Tokenization is not supported when redaction is applied." + TOKENIZATION_SUPPORTED_ONLY_WITH_IDS=f"{ERROR}: [{error_prefix}] Invalid {{}} request. Tokenization is not supported when column name and values are passed." + TOKENS_NOT_ALLOWED_WITH_BYOT_DISABLE = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Tokens are not allowed when token_strict is DISABLE." + INSUFFICIENT_TOKENS_PASSED_FOR_BYOT_ENABLE_STRICT =f"{ERROR}: [{error_prefix}] Invalid {{}} request. For tokenStrict as ENABLE_STRICT, tokens should be passed for all fields." + TOKENS_REQUIRED = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Tokens are required." + EMPTY_FIELDS = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Fields can not be empty." + EMPTY_OFFSET = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Offset ca not be empty." + NEITHER_IDS_NOR_COLUMN_NAME_PASSED = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Neither ids nor column name and values are passed." + BOTH_IDS_AND_COLUMN_NAME_PASSED = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Both ids and column name and values are passed." + COLUMN_NAME_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Column name is required when column values are passed." + COLUMN_VALUES_IS_REQUIRED_GET = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Column values are required when column name is passed." + SKYFLOW_ID_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Skyflow Id is required." + EMPTY_SKYFLOW_ID = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Skyflow Id can not be empty." + + COLUMN_VALUES_IS_REQUIRED_TOKENIZE = f"{ERROR}: [{error_prefix}] Invalid {{}} request. ColumnValues are required." + EMPTY_COLUMN_GROUP_IN_COLUMN_VALUES = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Column group can not be null or empty in column values at index %s2." + + EMPTY_QUERY= f"{ERROR}: [{error_prefix}] Invalid {{}} request. Query can not be empty." + QUERY_IS_REQUIRED = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Query is required." + + INSERT_RECORDS_REJECTED = f"{ERROR}: [{error_prefix}] Insert call resulted in failure." + DETOKENIZE_REQUEST_REJECTED = f"{ERROR}: [{error_prefix}] Detokenize request resulted in failure." + DELETE_REQUEST_REJECTED = f"{ERROR}: [{error_prefix}] Delete request resulted in failure." + TOKENIZE_REQUEST_REJECTED = f"{ERROR}: [{error_prefix}] Tokenize request resulted in failure." + UPDATE_REQUEST_REJECTED = f"{ERROR}: [{error_prefix}] Update request resulted in failure." + QUERY_REQUEST_REJECTED = f"{ERROR}: [{error_prefix}] Query request resulted in failure." + GET_REQUEST_REJECTED = f"{ERROR}: [{error_prefix}] Get request resulted in failure." class Interface(Enum): INSERT = "INSERT" diff --git a/skyflow/utils/_utils.py b/skyflow/utils/_utils.py index fda915a..cea5729 100644 --- a/skyflow/utils/_utils.py +++ b/skyflow/utils/_utils.py @@ -1,6 +1,7 @@ import os import json import urllib.parse +from dotenv import load_dotenv from requests.sessions import PreparedRequest from requests.models import HTTPError import requests @@ -22,6 +23,9 @@ invalid_input_error_code = SkyflowMessages.ErrorCodes.INVALID_INPUT.value def get_credentials(config_level_creds = None, common_skyflow_creds = None, logger = None): + dotenv_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), ".env") + if dotenv_path: + load_dotenv(dotenv_path) env_skyflow_credentials = os.getenv("SKYFLOW_CREDENTIALS") if config_level_creds: return config_level_creds @@ -30,8 +34,10 @@ def get_credentials(config_level_creds = None, common_skyflow_creds = None, logg if env_skyflow_credentials: env_skyflow_credentials.strip() try: - env_creds = json.loads(env_skyflow_credentials.replace('\n', '\\n')) - return env_creds + env_creds = env_skyflow_credentials.replace('\n', '\\n') + return { + 'credentials_string': env_creds + } except json.JSONDecodeError: raise SkyflowError(SkyflowMessages.Error.INVALID_JSON_FORMAT_IN_CREDENTIALS_ENV.value, invalid_input_error_code) else: diff --git a/skyflow/utils/validations/_validations.py b/skyflow/utils/validations/_validations.py index ccea4b1..4fa688e 100644 --- a/skyflow/utils/validations/_validations.py +++ b/skyflow/utils/validations/_validations.py @@ -202,7 +202,7 @@ def validate_update_vault_config(logger, config): return True def validate_connection_config(logger, config): - log_info(SkyflowMessages.Info.VALIDATE_CONNECTION_CONFIG.value, logger) + log_info(SkyflowMessages.Info.VALIDATING_CONNECTION_CONFIG.value, logger) validate_keys(logger, config, valid_connection_config_keys) validate_required_field( diff --git a/skyflow/vault/client/client.py b/skyflow/vault/client/client.py index ff1ad7f..34a9374 100644 --- a/skyflow/vault/client/client.py +++ b/skyflow/vault/client/client.py @@ -1,3 +1,4 @@ +import json from skyflow.generated.rest import Configuration, RecordsApi, ApiClient, TokensApi, QueryApi from skyflow.service_account import generate_bearer_token, generate_bearer_token_from_creds, is_expired from skyflow.utils import get_vault_url, get_credentials, SkyflowMessages @@ -23,7 +24,7 @@ def set_logger(self, log_level, logger): self.__logger = logger def initialize_client_configuration(self): - credentials = get_credentials(self.__config.get("credentials"), self.__common_skyflow_credentials, logger = self.__logger) + credentials = get_credentials(self.__config.get("credentials"), self.__common_skyflow_credentials, logger = self.__logger) token = self.get_bearer_token(credentials) vault_url = get_vault_url(self.__config.get("cluster_id"), self.__config.get("env"), @@ -58,8 +59,6 @@ def get_bearer_token(self, credentials): "ctx": self.__config.get("ctx") } - log_info(SkyflowMessages.Info.GENERATE_BEARER_TOKEN_TRIGGERED, self.__logger) - if self.__bearer_token is None or self.__is_config_updated: if 'path' in credentials: path = credentials.get("path") @@ -77,12 +76,13 @@ def get_bearer_token(self, credentials): self.__logger ) self.__is_config_updated = False + else: + log_info(SkyflowMessages.Info.REUSE_BEARER_TOKEN.value, self.__logger) if is_expired(self.__bearer_token): self.__is_config_updated = True raise SyntaxError(SkyflowMessages.Error.EXPIRED_TOKEN.value, SkyflowMessages.ErrorCodes.INVALID_INPUT.value) - log_info(SkyflowMessages.Info.REUSE_BEARER_TOKEN.value, self.__logger) return self.__bearer_token def update_config(self, config): diff --git a/skyflow/vault/controller/_vault.py b/skyflow/vault/controller/_vault.py index 06a5508..4e1a616 100644 --- a/skyflow/vault/controller/_vault.py +++ b/skyflow/vault/controller/_vault.py @@ -1,5 +1,3 @@ -from venv import logger - from skyflow.generated.rest import V1FieldRecords, RecordServiceInsertRecordBody, V1DetokenizeRecordRequest, \ V1DetokenizePayload, V1TokenizeRecordRequest, V1TokenizePayload, QueryServiceExecuteQueryBody, \ RecordServiceBulkDeleteRecordBody, RecordServiceUpdateRecordBody, RecordServiceBatchOperationBody, V1BatchRecord, \ diff --git a/tests/constants/test_constants.py b/tests/constants/test_constants.py index fff3382..00155d3 100644 --- a/tests/constants/test_constants.py +++ b/tests/constants/test_constants.py @@ -64,7 +64,7 @@ EMPTY_URL = "" SCOPES_LIST = ["admin", "user", "viewer"] FORMATTED_SCOPES = "role:admin role:user role:viewer" -INVALID_JSON_FORMAT = '{"invalid": json}' +INVALID_JSON_FORMAT = '[{"invalid": "json"}]' TEST_ERROR_MESSAGE = "Test error message." @@ -90,6 +90,8 @@ CREDENTIALS_WITH_PATH = {"path": "/path/to/creds.json"} CREDENTIALS_WITH_STRING = {"credentials_string": "dummy_credentials_string"} +VALID_ENV_CREDENTIALS = {"clientID":"CLIENT_ID","clientName":"test_V2","tokenURI":"TOKEN_URI","keyID":"KEY_ID","privateKey":"PRIVATE_KEY","keyValidAfterTime":"2024-10-21T18:06:26.000Z","keyValidBeforeTime":"2025-10-21T18:06:26.000Z","keyAlgorithm":"KEY_ALG_RSA_2048"} + # connection controller constants diff --git a/tests/utils/test__utils.py b/tests/utils/test__utils.py index de5643e..ef4bf26 100644 --- a/tests/utils/test__utils.py +++ b/tests/utils/test__utils.py @@ -15,21 +15,17 @@ from skyflow.vault.connection import InvokeConnectionResponse from skyflow.vault.data import InsertResponse, DeleteResponse, GetResponse, QueryResponse from skyflow.vault.tokens import DetokenizeResponse, TokenizeResponse -from tests.constants.test_constants import VALID_CREDENTIALS_STRING, INVALID_JSON_FORMAT, TEST_ERROR_MESSAGE +from tests.constants.test_constants import VALID_CREDENTIALS_STRING, INVALID_JSON_FORMAT, TEST_ERROR_MESSAGE, \ + VALID_ENV_CREDENTIALS class TestUtils(unittest.TestCase): - # def test_get_credentials_empty_credentials(self): - # with self.assertRaises(SkyflowError) as context: - # get_credentials() - # self.assertIn(context.exception.message, SkyflowMessages.Error.INVALID_CREDENTIALS.value) - @patch.dict(os.environ, {"SKYFLOW_CREDENTIALS": VALID_CREDENTIALS_STRING}) + @patch.dict(os.environ, {"SKYFLOW_CREDENTIALS": json.dumps(VALID_ENV_CREDENTIALS)}) def test_get_credentials_env_variable(self): - creds = get_credentials() - VALID_CREDENTIALS_STRING.strip() - print(type(creds)) - self.assertEqual(creds, json.loads(VALID_CREDENTIALS_STRING.replace('\n', '\\n'))) + credentials = get_credentials() + credentials_string = credentials.get('credentials_string') + self.assertEqual(credentials_string, json.dumps(VALID_ENV_CREDENTIALS).replace('\n', '\\n')) def test_get_credentials_with_config_level_creds(self): test_creds = {"authToken": "test_token"} @@ -41,12 +37,6 @@ def test_get_credentials_with_common_creds(self): creds = get_credentials(common_skyflow_creds=test_creds) self.assertEqual(creds, test_creds) - @patch.dict(os.environ, {"SKYFLOW_CREDENTIALS": INVALID_JSON_FORMAT}) - def test_get_credentials_invalid_json_format(self): - with self.assertRaises(SkyflowError) as context: - get_credentials() - self.assertIn(context.exception.message, SkyflowMessages.Error.INVALID_JSON_FORMAT_IN_CREDENTIALS_ENV.value) - def test_get_vault_url_valid(self): valid_cluster_id = "testCluster" valid_env = Env.DEV diff --git a/tests/vault/client/test__client.py b/tests/vault/client/test__client.py index 91f05d3..e26988f 100644 --- a/tests/vault/client/test__client.py +++ b/tests/vault/client/test__client.py @@ -26,7 +26,7 @@ def test_set_logger(self): @patch("skyflow.vault.client.client.VaultClient.initialize_api_client") def test_initialize_client_configuration(self, mock_init_api_client, mock_config, mock_get_vault_url, mock_get_credentials): - mock_get_credentials.return_value = CREDENTIALS_WITH_API_KEY + mock_get_credentials.return_value = (CREDENTIALS_WITH_API_KEY) mock_get_vault_url.return_value = "https://test-vault-url.com" self.vault_client.initialize_client_configuration()