Skip to content

Commit

Permalink
Updated snowflake config for OAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
arnab-p committed Dec 16, 2024
1 parent 78e3a6a commit 48b0a02
Showing 1 changed file with 56 additions and 34 deletions.
90 changes: 56 additions & 34 deletions sqlconnect/internal/snowflake/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
User string `json:"user"`
Schema string `json:"schema"`
Role string `json:"role"`
Region string `json:"region"`

Password string `json:"password"`

Expand All @@ -40,45 +41,66 @@ type Config struct {
}

func (c Config) ConnectionString() (dsn string, err error) {
sc := gosnowflake.Config{
Authenticator: gosnowflake.AuthTypeSnowflake,
User: c.User,
Password: c.Password,
Account: c.Account,
Database: c.DBName,
Warehouse: c.Warehouse,
Schema: c.Schema,
Role: c.Role,
Application: c.Application,
LoginTimeout: c.LoginTimeout,
Params: make(map[string]*string),
}

if c.UseKeyPairAuth {
sc.Authenticator = gosnowflake.AuthTypeJwt
privateKey, err := c.ParsePrivateKey()
if c.UseOAuth {
sc := gosnowflake.Config{
Authenticator: gosnowflake.AuthTypeOAuth,
Account: c.Account,
Region: c.Region,
Token: c.OAuthToken,
Warehouse: c.Warehouse,
Schema: c.Schema,
Database: c.DBName,
Host: c.Host,
Protocol: "https",
Port: 443,
KeepSessionAlive: true,
}
dsn, err = gosnowflake.DSN(&sc)

Check warning on line 58 in sqlconnect/internal/snowflake/config.go

View check run for this annotation

Codecov / codecov/patch

sqlconnect/internal/snowflake/config.go#L45-L58

Added lines #L45 - L58 were not covered by tests
if err != nil {
return "", fmt.Errorf("parsing private key: %w", err)
err = fmt.Errorf("creating dsn: %v", err)
}

Check warning on line 61 in sqlconnect/internal/snowflake/config.go

View check run for this annotation

Codecov / codecov/patch

sqlconnect/internal/snowflake/config.go#L60-L61

Added lines #L60 - L61 were not covered by tests
} else {
sc := gosnowflake.Config{
Authenticator: gosnowflake.AuthTypeSnowflake,
User: c.User,
Password: c.Password,
Account: c.Account,
Database: c.DBName,
Warehouse: c.Warehouse,
Schema: c.Schema,
Role: c.Role,
Application: c.Application,
LoginTimeout: c.LoginTimeout,
Params: make(map[string]*string),
}
sc.PrivateKey = privateKey
} else if c.UseOAuth {
sc.Authenticator = gosnowflake.AuthTypeOAuth
sc.Host = c.Host
sc.Token = c.OAuthToken
}

if c.KeepSessionAlive {
valueTrue := "true"
sc.Params["client_session_keep_alive"] = &valueTrue
}
if c.UseKeyPairAuth {
sc.Authenticator = gosnowflake.AuthTypeJwt
privateKey, err := c.ParsePrivateKey()
if err != nil {
return "", fmt.Errorf("parsing private key: %w", err)
}

Check warning on line 82 in sqlconnect/internal/snowflake/config.go

View check run for this annotation

Codecov / codecov/patch

sqlconnect/internal/snowflake/config.go#L81-L82

Added lines #L81 - L82 were not covered by tests
sc.PrivateKey = privateKey
} else if c.UseOAuth {
sc.Authenticator = gosnowflake.AuthTypeOAuth
sc.Host = c.Host
sc.Token = c.OAuthToken
sc.User = c.User
}

Check warning on line 89 in sqlconnect/internal/snowflake/config.go

View check run for this annotation

Codecov / codecov/patch

sqlconnect/internal/snowflake/config.go#L85-L89

Added lines #L85 - L89 were not covered by tests

if c.QueryTag != "" {
sc.Params["query_tag"] = &c.QueryTag
}
if c.KeepSessionAlive {
valueTrue := "true"
sc.Params["client_session_keep_alive"] = &valueTrue
}

Check warning on line 94 in sqlconnect/internal/snowflake/config.go

View check run for this annotation

Codecov / codecov/patch

sqlconnect/internal/snowflake/config.go#L92-L94

Added lines #L92 - L94 were not covered by tests

dsn, err = gosnowflake.DSN(&sc)
if err != nil {
err = fmt.Errorf("creating dsn: %v", err)
if c.QueryTag != "" {
sc.Params["query_tag"] = &c.QueryTag
}

Check warning on line 98 in sqlconnect/internal/snowflake/config.go

View check run for this annotation

Codecov / codecov/patch

sqlconnect/internal/snowflake/config.go#L97-L98

Added lines #L97 - L98 were not covered by tests

dsn, err = gosnowflake.DSN(&sc)
if err != nil {
err = fmt.Errorf("creating dsn: %v", err)
}

Check warning on line 103 in sqlconnect/internal/snowflake/config.go

View check run for this annotation

Codecov / codecov/patch

sqlconnect/internal/snowflake/config.go#L102-L103

Added lines #L102 - L103 were not covered by tests
}
return
}
Expand Down

0 comments on commit 48b0a02

Please sign in to comment.