Skip to content
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

Snow 1733048 Add internal app data to cli #1971

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Add ability to list release channels through `snow app release-channel list` command
* Add ability to add and remove accounts from release channels through `snow app release-channel add-accounts` and snow app release-channel remove-accounts` commands.
* Add ability to add/remove versions to/from release channels through `snow app release-channel add-version` and `snow app release-channel remove-version` commands.

* Add support for restricting Snowflake user authentication policy to Snowflake CLI-only.
sfc-gh-turbaszek marked this conversation as resolved.
Show resolved Hide resolved
## Fixes and improvements
* Fixed crashes with older x86_64 Intel CPUs.
* Fixed inability to add patches to lowercase quoted versions
Expand Down
1 change: 1 addition & 0 deletions src/snowflake/cli/_app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
from typing import Literal

PARAM_APPLICATION_NAME: Literal["snowcli"] = "snowcli"
INTERNAL_APPLICATION_NAME: Literal["SNOWFLAKE_CLI"] = "SNOWFLAKE_CLI"
sfc-gh-pczajka marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 12 additions & 0 deletions src/snowflake/cli/_app/snow_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

import snowflake.connector
from click.exceptions import ClickException
from snowflake.cli.__about__ import VERSION
from snowflake.cli._app.constants import (
INTERNAL_APPLICATION_NAME,
PARAM_APPLICATION_NAME,
)
from snowflake.cli._app.secret import SecretType
Expand All @@ -35,6 +37,7 @@
InvalidConnectionConfiguration,
SnowflakeConnectionError,
)
from snowflake.cli.api.feature_flags import FeatureFlag
from snowflake.cli.api.secure_path import SecurePath
from snowflake.connector import SnowflakeConnection
from snowflake.connector.errors import DatabaseError, ForbiddenError
Expand Down Expand Up @@ -150,6 +153,8 @@ def connect_to_snowflake(

_update_connection_application_name(connection_parameters)

_update_internal_application_info(connection_parameters)

try:
# Whatever output is generated when creating connection,
# we don't want it in our output. This is particularly important
Expand Down Expand Up @@ -238,6 +243,13 @@ def _update_connection_application_name(connection_parameters: Dict):
connection_parameters.update(connection_application_params)


def _update_internal_application_info(connection_parameters: Dict):
"""Update internal application data if ENABLE_SEPARATE_AUTHENTICATION_POLICY_ID is enabled."""
if FeatureFlag.ENABLE_SEPARATE_AUTHENTICATION_POLICY_ID.is_enabled():
connection_parameters["internal_application_name"] = INTERNAL_APPLICATION_NAME
connection_parameters["internal_application_version"] = VERSION


def _load_pem_from_file(private_key_file: str) -> SecretType:
with SecurePath(private_key_file).open(
"rb", read_file_limit_mb=DEFAULT_SIZE_LIMIT_MB
Expand Down
3 changes: 3 additions & 0 deletions src/snowflake/cli/api/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ class FeatureFlag(FeatureFlagMixin):
"ENABLE_STREAMLIT_VERSIONED_STAGE", False
)
ENABLE_SPCS_LOG_STREAMING = BooleanFlag("ENABLE_SPCS_LOG_STREAMING", False)
ENABLE_SEPARATE_AUTHENTICATION_POLICY_ID = BooleanFlag(
"ENABLE_SEPARATE_AUTHENTICATION_POLICY_ID", False
)
29 changes: 29 additions & 0 deletions tests/test_snow_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,32 @@ def test_returns_nice_error_in_case_of_missing_master_token(runner):
"When using a session token, you must provide the corresponding master token"
in result.output
)


@mock.patch("snowflake.connector.connect")
@pytest.mark.parametrize("feature_flag", [None, True, False])
def test_internal_application_data_is_sent_if_feature_flag_is_set(
mock_connect, runner, feature_flag
):
expected_kwargs = {
"application": "SNOWCLI.SQL",
"database": "db_for_test",
"schema": "test_public",
"role": "test_role",
"warehouse": "xs",
"password": "dummy_password",
"application_name": "snowcli",
}
env = {}
if feature_flag is not None:
env["SNOWFLAKE_CLI_FEATURES_ENABLE_SEPARATE_AUTHENTICATION_POLICY_ID"] = str(
feature_flag
)
if feature_flag:
# internal app data should be disabled by default
expected_kwargs["internal_application_name"] = "SNOWFLAKE_CLI"
expected_kwargs["internal_application_version"] = "0.0.0-test_patched"
with mock.patch.dict(os.environ, env):
result = runner.invoke(["sql", "-q", "select 1"])
assert result.exit_code == 0
mock_connect.assert_called_once_with(**expected_kwargs)
Loading