MCPcopy Create free account
hub / github.com/bytebase/bytebase / buildSnowflakeDSN

Function buildSnowflakeDSN

backend/plugin/db/snowflake/snowflake.go:74–120  ·  view source on GitHub ↗

buildSnowflakeDSN returns the Snowflake Golang DSN and a redacted version of the DSN.

(config db.ConnectionConfig)

Source from the content-addressed store, hash-verified

72
73// buildSnowflakeDSN returns the Snowflake Golang DSN and a redacted version of the DSN.
74func buildSnowflakeDSN(config db.ConnectionConfig) (string, string, error) {
75 snowConfig := &snow.Config{
76 User: config.DataSource.Username,
77 }
78 if config.ConnectionContext.DatabaseName != "" {
79 snowConfig.Database = fmt.Sprintf(`"%s"`, config.ConnectionContext.DatabaseName)
80 }
81 if config.DataSource.GetAuthenticationPrivateKey() != "" {
82 // Use key-pair authentication (JWT) - password is not required
83 rsaPrivKey, err := decodeRSAPrivateKey(config.DataSource.GetAuthenticationPrivateKey(), config.DataSource.GetAuthenticationPrivateKeyPassphrase())
84 if err != nil {
85 return "", "", errors.Wrapf(err, "failed to decode rsa private key")
86 }
87 snowConfig.PrivateKey = rsaPrivKey
88 snowConfig.Authenticator = snow.AuthTypeJwt
89 } else {
90 // Use password authentication
91 snowConfig.Password = config.Password
92 }
93
94 // Host can also be account e.g. xma12345, or xma12345@host_ip where host_ip is the proxy server IP.
95 if strings.Contains(config.DataSource.Host, "@") {
96 parts := strings.Split(config.DataSource.Host, "@")
97 if len(parts) != 2 {
98 return "", "", errors.Errorf("expected one @ in the host at most, got %q", config.DataSource.Host)
99 }
100 snowConfig.Account = parts[0]
101 snowConfig.Host = parts[1]
102 } else {
103 snowConfig.Account = config.DataSource.Host
104 }
105 dsn, err := snow.DSN(snowConfig)
106 if err != nil {
107 return "", "", errors.Wrapf(err, "failed to build Snowflake DSN")
108 }
109 snowConfig.Password = "xxxxxx"
110 if snowConfig.PrivateKey != nil {
111 snowConfig.PrivateKey = nil
112 }
113 redactedDSN, err := snow.DSN(snowConfig)
114 if err != nil {
115 // nolint
116 slog.Warn("failed to build redacted Snowflake DSN", log.BBError(err))
117 return dsn, "", nil
118 }
119 return dsn, redactedDSN, nil
120}
121
122// Close closes the driver.
123func (d *Driver) Close(context.Context) error {

Callers 2

TestBuildSnowflakeDSNFunction · 0.85
OpenMethod · 0.85

Calls 5

BBErrorFunction · 0.92
decodeRSAPrivateKeyFunction · 0.85
ErrorfMethod · 0.80

Tested by 1

TestBuildSnowflakeDSNFunction · 0.68