forked from snowflakedb/gosnowflake
/
driver.go
105 lines (100 loc) · 2.62 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2017 Snowflake Computing Inc. All right reserved.
package gosnowflake
import (
"database/sql"
"database/sql/driver"
"net/http"
"time"
)
// SnowflakeDriver is a context of Go Driver
type SnowflakeDriver struct{}
// Open creates a new connection.
func (d SnowflakeDriver) Open(dsn string) (driver.Conn, error) {
glog.V(2).Info("Open")
var err error
sc := &snowflakeConn{
SequeceCounter: 0,
}
sc.cfg, err = ParseDSN(dsn)
if err != nil {
sc.cleanup()
return nil, err
}
st := SnowflakeTransport
if sc.cfg.InsecureMode {
// no revocation check with OCSP. Think twice when you want to enable this option.
st = snowflakeInsecureTransport
}
proxyURL, err := proxyURL(proxyHost, proxyPort, proxyUser, proxyPassword)
if err != nil {
return nil, err
}
if proxyURL != nil {
st.Proxy = http.ProxyURL(proxyURL)
glog.V(2).Infof("proxy: %v", proxyURL)
}
// authenticate
sc.rest = &snowflakeRestful{
Host: sc.cfg.Host,
Port: sc.cfg.Port,
Protocol: sc.cfg.Protocol,
Client: &http.Client{
Timeout: 60 * time.Second, // each request timeout
Transport: st,
},
Authenticator: sc.cfg.Authenticator,
LoginTimeout: sc.cfg.LoginTimeout,
RequestTimeout: sc.cfg.RequestTimeout,
FuncPost: postRestful,
FuncGet: getRestful,
FuncPostQuery: postRestfulQuery,
FuncPostQueryHelper: postRestfulQueryHelper,
FuncRenewSession: renewRestfulSession,
FuncPostAuth: postAuth,
FuncCloseSession: closeSession,
FuncCancelQuery: cancelQuery,
FuncPostAuthSAML: postAuthSAML,
FuncPostAuthOKTA: postAuthOKTA,
FuncGetSSO: getSSO,
}
var authData *authResponseMain
var samlResponse []byte
if sc.cfg.Authenticator != "snowflake" {
samlResponse, err = authenticateBySAML(sc.rest, sc.cfg.Authenticator, sc.cfg.Application, sc.cfg.Account, sc.cfg.User, sc.cfg.Password)
if err != nil {
sc.cleanup()
return nil, err
}
}
authData, err = authenticate(
sc.rest,
sc.cfg.User,
sc.cfg.Password,
sc.cfg.Account,
sc.cfg.Database,
sc.cfg.Schema,
sc.cfg.Warehouse,
sc.cfg.Role,
sc.cfg.Passcode,
sc.cfg.PasscodeInPassword,
sc.cfg.Application,
sc.cfg.Params,
samlResponse,
"",
"",
)
if err != nil {
sc.cleanup()
return nil, err
}
glog.V(2).Infof("Auth Data: %v", authData)
sc.cfg.Database = authData.SessionInfo.DatabaseName
sc.cfg.Schema = authData.SessionInfo.SchemaName
sc.cfg.Role = authData.SessionInfo.RoleName
sc.cfg.Warehouse = authData.SessionInfo.WarehouseName
sc.populateSessionParameters(authData.Parameters)
return sc, nil
}
func init() {
sql.Register("snowflake", &SnowflakeDriver{})
}