Skip to content

Commit

Permalink
fix: err when reading profile (#1853)
Browse files Browse the repository at this point in the history
* fix: err when reading profile

* profile/default config
  • Loading branch information
sfc-gh-swinkler authored Jun 3, 2023
1 parent 6e5d643 commit 29c4633
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
22 changes: 17 additions & 5 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,26 @@ func DSN(
config.Token = oauthAccessToken
} else if password != "" {
config.Password = password
} else if account == "" && user == "" {
} else if account == "" || user == "" {
// If account and user are empty then we need to fall back on using profile config
log.Printf("[DEBUG] No account or user provided, falling back to profile %s\n", profile)
profileConfig, err := sdk.ProfileConfig(profile)
if err != nil {
return "", errors.New("no authentication method provided")
if profile == "default" {
defaultConfig := sdk.DefaultConfig()
if defaultConfig.Account == "" || defaultConfig.User == "" {
return "", errors.New("Account and User must be set in provider config, ~/.snowflake/config, or as an environment variable.")
}
config = sdk.MergeConfig(config, defaultConfig)
} else {
profileConfig, err := sdk.ProfileConfig(profile)
if err != nil {
return "", errors.New("could not retrieve profile config: " + err.Error())
}
if profileConfig == nil {
return "", errors.New("profile with name: " + profile + " not found in config file")
}
// merge any credentials found in profile with config
config = sdk.MergeConfig(config, profileConfig)
}
config = sdk.MergeConfig(config, profileConfig)
}
config.Application = "terraform-provider-snowflake"
return gosnowflake.DSN(config)
Expand Down
3 changes: 1 addition & 2 deletions pkg/sdk/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"github.com/stretchr/testify/require"
)

func TestClient_newClient(t *testing.T) {
func TestClient_NewClient(t *testing.T) {
t.Run("with default config", func(t *testing.T) {
config := DefaultConfig()
_, err := NewClient(config)
require.NoError(t, err)
})

t.Run("uses env vars if values are missing", func(t *testing.T) {
cleanupEnvVars := setupEnvVars(t, "TEST_ACCOUNT", "TEST_USER", "abcd1234", "ACCOUNTADMIN", "")
t.Cleanup(cleanupEnvVars)
Expand Down
5 changes: 5 additions & 0 deletions pkg/sdk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func ProfileConfig(profile string) (*gosnowflake.Config, error) {
config = cfg
}

if config == nil {
log.Printf("[DEBUG] no config found for profile: \"%s\"", profile)
return nil, nil
}

// us-west-2 is Snowflake's default region, but if you actually specify that it won't trigger the default code
// https://github.com/snowflakedb/gosnowflake/blob/52137ce8c32eaf93b0bd22fc5c7297beff339812/dsn.go#L61
if config.Region == "us-west-2" {
Expand Down
27 changes: 19 additions & 8 deletions pkg/sdk/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,25 @@ func TestProfileConfig(t *testing.T) {
role='SECURITYADMIN'
`
configPath := testFile(t, "config", []byte(c))
cleanupEnvVars := setupEnvVars(t, "", "", "", "", configPath)
t.Cleanup(cleanupEnvVars)
config, err := ProfileConfig("securityadmin")
require.NoError(t, err)
assert.Equal(t, "TEST_ACCOUNT", config.Account)
assert.Equal(t, "TEST_USER", config.User)
assert.Equal(t, "abcd1234", config.Password)
assert.Equal(t, "SECURITYADMIN", config.Role)

t.Run("with found profile", func(t *testing.T) {
cleanupEnvVars := setupEnvVars(t, "", "", "", "", configPath)
t.Cleanup(cleanupEnvVars)
config, err := ProfileConfig("securityadmin")
require.NoError(t, err)
assert.Equal(t, "TEST_ACCOUNT", config.Account)
assert.Equal(t, "TEST_USER", config.User)
assert.Equal(t, "abcd1234", config.Password)
assert.Equal(t, "SECURITYADMIN", config.Role)
})

t.Run("with not found profile", func(t *testing.T) {
cleanupEnvVars := setupEnvVars(t, "", "", "", "", configPath)
t.Cleanup(cleanupEnvVars)
config, err := ProfileConfig("orgadmin")
require.NoError(t, err)
require.Nil(t, config)
})
}

func TestEnvConfig(t *testing.T) {
Expand Down

0 comments on commit 29c4633

Please sign in to comment.