From cf175f11bcf0e67347821b316da1747f45cc1037 Mon Sep 17 00:00:00 2001 From: John Yun <140559986+sfc-gh-ext-simba-jy@users.noreply.github.com> Date: Thu, 26 Sep 2024 17:25:46 -0700 Subject: [PATCH] fix --- connection_configuration.go | 29 ++++++++++------------------- connection_configuration_test.go | 26 +++++++++++++------------- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/connection_configuration.go b/connection_configuration.go index 86474adda..fbcc4a0ed 100644 --- a/connection_configuration.go +++ b/connection_configuration.go @@ -15,20 +15,20 @@ import ( ) const ( - connectionName = "SNOWFLAKE_DEFAULT_CONNECTION_NAME" - home = "SNOWFLAKE_HOME" + snowflake_connectionName = "SNOWFLAKE_DEFAULT_CONNECTION_NAME" + snowflake_home = "SNOWFLAKE_HOME" ) // LoadConnectionConfig returns connection configs loaded from the toml file. -// By default, SNOWFLAKE_HOME(toml file path) is os.home/snowflake +// By default, SNOWFLAKE_HOME(toml file path) is os.snowflake_home/.snowflake // and SNOWFLAKE_DEFAULT_CONNECTION_NAME(DSN) is 'default' func LoadConnectionConfig() (*Config, error) { cfg := &Config{ Params: make(map[string]*string), Authenticator: AuthTypeSnowflake, // Default to snowflake } - dsn := getConnectionDSN(os.Getenv(connectionName)) - snowflakeConfigDir, err := getTomlFilePath(os.Getenv(home)) + dsn := getConnectionDSN(os.Getenv(snowflake_connectionName)) + snowflakeConfigDir, err := getTomlFilePath(os.Getenv(snowflake_home)) if err != nil { return nil, err } @@ -213,12 +213,7 @@ func parseInt(i interface{}) (int, error) { } return num, nil } - num, err := strconv.Atoi(v) - - if err != nil { - return num, err - } - return num, nil + return strconv.Atoi(v) } func parseBool(i interface{}) (bool, error) { @@ -230,11 +225,7 @@ func parseBool(i interface{}) (bool, error) { } return vv, nil } - vv, err := strconv.ParseBool(v) - if err != nil { - return false, errors.New("failed to parse the value to boolean") - } - return vv, nil + return strconv.ParseBool(v) } func parseConfigBool(i interface{}) (ConfigBool, error) { @@ -266,7 +257,7 @@ func readToken(tokenPath string) (string, error) { tokenPath = "./snowflake/session/token" } if !path.IsAbs(tokenPath) { - snowflakeConfigDir, err := getTomlFilePath(os.Getenv(home)) + snowflakeConfigDir, err := getTomlFilePath(os.Getenv(snowflake_home)) if err != nil { return "", err } @@ -297,7 +288,7 @@ func getTomlFilePath(filePath string) (string, error) { if err != nil { return "", err } - filePath = path.Join(homeDir, "snowflake") + filePath = path.Join(homeDir, ".snowflake") } absDir, err := path.Abs(filePath) if err != nil { @@ -322,7 +313,7 @@ func validateFilePermission(filePath string) error { return err } if permission := fileInfo.Mode().Perm(); permission != os.FileMode(0600) { - return errors.New("your access to the file was denied") + return errors.New("file permissions different than read/write for user") } return nil } diff --git a/connection_configuration_test.go b/connection_configuration_test.go index f68f42b08..1dc130ca3 100644 --- a/connection_configuration_test.go +++ b/connection_configuration_test.go @@ -19,7 +19,7 @@ func TestLoadConnectionConfig_Default(t *testing.T) { err := os.Chmod("./test_data/connections.toml", 0600) assertNilF(t, err, "The error occurred because you cannot change the file permission") - os.Setenv("SNOWFLAKE_HOME", "./test_data") + os.Setenv(snowflake_home, "./test_data") cfg, err := LoadConnectionConfig() assertNilF(t, err, "The error should not occur") @@ -37,8 +37,8 @@ func TestLoadConnectionConfig_OAuth(t *testing.T) { err := os.Chmod("./test_data/connections.toml", 0600) assertNilF(t, err, "The error occurred because you cannot change the file permission") - os.Setenv("SNOWFLAKE_HOME", "./test_data") - os.Setenv("SNOWFLAKE_DEFAULT_CONNECTION_NAME", "aws-oauth") + os.Setenv(snowflake_home, "./test_data") + os.Setenv(snowflake_connectionName, "aws-oauth") cfg, err := LoadConnectionConfig() assertNilF(t, err, "The error should not occur") @@ -61,16 +61,16 @@ func TestReadTokenValueWithTokenFilePath(t *testing.T) { err = os.Chmod("./test_data/snowflake/session/token", 0600) assertNilF(t, err, "The error occurred because you cannot change the file permission") - os.Setenv("SNOWFLAKE_HOME", "./test_data") - os.Setenv("SNOWFLAKE_DEFAULT_CONNECTION_NAME", "no-token-path") + os.Setenv(snowflake_home, "./test_data") + os.Setenv(snowflake_connectionName, "no-token-path") cfg, err := LoadConnectionConfig() assertNilF(t, err, "The error should not occur") assertEqualF(t, cfg.Authenticator, AuthTypeOAuth) assertEqualF(t, cfg.Token, "mock_token123456") - os.Setenv("SNOWFLAKE_HOME", "./test_data") - os.Setenv("SNOWFLAKE_DEFAULT_CONNECTION_NAME", "read-token") + os.Setenv(snowflake_home, "./test_data") + os.Setenv(snowflake_connectionName, "read-token") cfg, err = LoadConnectionConfig() assertNilF(t, err, "The error should not occur") @@ -82,8 +82,8 @@ func TestLoadConnectionConfigWitNonExistingDSN(t *testing.T) { err := os.Chmod("./test_data/connections.toml", 0600) assertNilF(t, err, "The error occurred because you cannot change the file permission") - os.Setenv("SNOWFLAKE_HOME", "./test_data") - os.Setenv("SNOWFLAKE_DEFAULT_CONNECTION_NAME", "unavailableDSN") + os.Setenv(snowflake_home, "./test_data") + os.Setenv(snowflake_connectionName, "unavailableDSN") _, err = LoadConnectionConfig() assertNotNilF(t, err, "The error should occur") @@ -97,8 +97,8 @@ func TestLoadConnectionConfigWithTokenFileNotExist(t *testing.T) { err := os.Chmod("./test_data/connections.toml", 0600) assertNilF(t, err, "The error occurred because you cannot change the file permission") - os.Setenv("SNOWFLAKE_HOME", "./test_data") - os.Setenv("SNOWFLAKE_DEFAULT_CONNECTION_NAME", "aws-oauth-file") + os.Setenv(snowflake_home, "./test_data") + os.Setenv(snowflake_connectionName, "aws-oauth-file") _, err = LoadConnectionConfig() assertNotNilF(t, err, "The error should occur") @@ -248,8 +248,8 @@ func TestGetTomlFilePath(t *testing.T) { dir, err := getTomlFilePath("") assertNilF(t, err, "should not have failed") homeDir, err := os.UserHomeDir() - assertNilF(t, err, "The connection cannot find the user home directory") - assertEqualF(t, dir, path.Join(homeDir, "snowflake")) + assertNilF(t, err, "The connection cannot find the user snowflake_home directory") + assertEqualF(t, dir, path.Join(homeDir, ".snowflake")) location := "../user//somelocation///b" dir, err = getTomlFilePath(location)