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

feat(snowflake): support oauth authentication #236

Merged
merged 9 commits into from
Jan 15, 2025
97 changes: 67 additions & 30 deletions sqlconnect/internal/snowflake/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
User string `json:"user"`
Schema string `json:"schema"`
Role string `json:"role"`
Region string `json:"region"`

Password string `json:"password"`

Expand All @@ -34,44 +35,80 @@
KeepSessionAlive bool `json:"keepSessionAlive"`
UseLegacyMappings bool `json:"useLegacyMappings"`
QueryTag string `json:"queryTag"`
Host string `json:"host"`
UseOAuth bool `json:"use_oauth"`
OAuthToken string `json:"oauth_token"`
}

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 {
atzoum marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println("sqlconnect: Account: " + c.Account)
fmt.Println("sqlconnect: Region: " + c.Region)
fmt.Println("sqlconnect: Token: " + c.OAuthToken)
fmt.Println("sqlconnect: Warehouse: " + c.Warehouse)
fmt.Println("sqlconnect: Schema: " + c.Schema)
fmt.Println("sqlconnect: Host: " + c.Host)
fmt.Println("sqlconnect: DBName: " + c.DBName)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented logs.


sc := gosnowflake.Config{
atzoum marked this conversation as resolved.
Show resolved Hide resolved
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,
atzoum marked this conversation as resolved.
Show resolved Hide resolved
KeepSessionAlive: true,
arnab-p marked this conversation as resolved.
Show resolved Hide resolved
}
dsn, err = gosnowflake.DSN(&sc)

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L45 - L66 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 69 in sqlconnect/internal/snowflake/config.go

View check run for this annotation

Codecov / codecov/patch

sqlconnect/internal/snowflake/config.go#L68-L69

Added lines #L68 - L69 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
}

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 90 in sqlconnect/internal/snowflake/config.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L89 - L90 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
atzoum marked this conversation as resolved.
Show resolved Hide resolved
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L93 - L97 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 102 in sqlconnect/internal/snowflake/config.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L100 - L102 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 106 in sqlconnect/internal/snowflake/config.go

View check run for this annotation

Codecov / codecov/patch

sqlconnect/internal/snowflake/config.go#L105-L106

Added lines #L105 - L106 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

sqlconnect/internal/snowflake/config.go#L110-L111

Added lines #L110 - L111 were not covered by tests
}
return
}
Expand Down
Loading