Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-jy committed Sep 27, 2024
1 parent 13cbdbb commit cf175f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
29 changes: 10 additions & 19 deletions connection_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ import (
)

const (
connectionName = "SNOWFLAKE_DEFAULT_CONNECTION_NAME"
home = "SNOWFLAKE_HOME"
snowflake_connectionName = "SNOWFLAKE_DEFAULT_CONNECTION_NAME"

Check failure on line 18 in connection_configuration.go

View workflow job for this annotation

GitHub Actions / Check linter

don't use underscores in Go names; const snowflake_connectionName should be snowflakeConnectionName
snowflake_home = "SNOWFLAKE_HOME"

Check failure on line 19 in connection_configuration.go

View workflow job for this annotation

GitHub Actions / Check linter

don't use underscores in Go names; const snowflake_home should be snowflakeHome
)

// 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
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
26 changes: 13 additions & 13 deletions connection_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cf175f1

Please sign in to comment.