-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconnection.go
More file actions
212 lines (184 loc) · 5.15 KB
/
Copy pathconnection.go
File metadata and controls
212 lines (184 loc) · 5.15 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package trino
import (
"context"
"crypto/tls"
"crypto/x509"
"database/sql/driver"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"net/url"
"strconv"
"time"
"gopkg.in/jcmturner/gokrb5.v6/client"
"gopkg.in/jcmturner/gokrb5.v6/config"
"gopkg.in/jcmturner/gokrb5.v6/keytab"
)
// Conn is a Trino connection. implements driver.Conn & driver.ConnPrepareContext
type Conn struct {
baseURL string
auth *url.Userinfo
httpClient http.Client
httpHeaders http.Header
kerberosClient client.Client
kerberosEnabled bool
}
var (
_ driver.Conn = &Conn{}
_ driver.ConnPrepareContext = &Conn{}
)
func newConn(dsn string) (*Conn, error) {
serverURL, err := url.Parse(dsn)
if err != nil {
return nil, fmt.Errorf("trino: malformed dsn: %v", err)
}
query := serverURL.Query()
kerberosEnabled, _ := strconv.ParseBool(query.Get(KerberosEnabledConfig))
var kerberosClient client.Client
if kerberosEnabled {
kt, err := keytab.Load(query.Get(_kerberosKeytabPathConfig))
if err != nil {
return nil, fmt.Errorf("trino: Error loading Keytab: %v", err)
}
kerberosClient = client.NewClientWithKeytab(query.Get(_kerberosPrincipalConfig), query.Get(_kerberosRealmConfig), kt)
conf, err := config.Load(query.Get(_kerberosConfigPathConfig))
if err != nil {
return nil, fmt.Errorf("trino: Error loading krb config: %v", err)
}
kerberosClient.WithConfig(conf)
loginErr := kerberosClient.Login()
if loginErr != nil {
return nil, fmt.Errorf("trino: Error login to KDC: %v", loginErr)
}
}
var httpClient = http.DefaultClient
if clientKey := query.Get("custom_client"); clientKey != "" {
httpClient = getCustomClient(clientKey)
if httpClient == nil {
return nil, fmt.Errorf("trino: custom client not registered: %q", clientKey)
}
} else if certPath := query.Get(SSLCertPathConfig); certPath != "" && serverURL.Scheme == "https" {
cert, err := ioutil.ReadFile(certPath)
if err != nil {
return nil, fmt.Errorf("trino: Error loading SSL Cert File: %v", err)
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(cert)
httpClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certPool,
},
},
}
}
c := &Conn{
baseURL: serverURL.Scheme + "://" + serverURL.Host,
httpClient: *httpClient,
httpHeaders: make(http.Header),
kerberosClient: kerberosClient,
kerberosEnabled: kerberosEnabled,
}
var user string
if serverURL.User != nil {
user = serverURL.User.Username()
pass, _ := serverURL.User.Password()
if pass != "" && serverURL.Scheme == "https" {
c.auth = serverURL.User
}
}
for k, v := range map[string]string{
vhs[v]["user"]: user,
vhs[v]["source"]: query.Get("source"),
vhs[v]["catalog"]: query.Get("catalog"),
vhs[v]["schema"]: query.Get("schema"),
vhs[v]["session"]: query.Get("session_properties"),
} {
if v != "" {
c.httpHeaders.Add(k, v)
}
}
return c, nil
}
// Begin implements the driver.Conn interface.
func (c *Conn) Begin() (driver.Tx, error) {
return nil, ErrOperationNotSupported
}
// Prepare implements the driver.Conn interface.
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
return nil, driver.ErrSkip
}
// PrepareContext implements the driver.ConnPrepareContext interface.
func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
return &driverStmt{conn: c, query: query}, nil
}
// Close implements the driver.Conn interface.
func (c *Conn) Close() error {
return nil
}
// ResetSession implements driver.SessionResetter
func (c *Conn) ResetSession(ctx context.Context) error {
return nil
}
func (c *Conn) newRequest(method, url string, body io.Reader, hs http.Header) (*http.Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, fmt.Errorf("trino: %v", err)
}
if c.kerberosEnabled {
err = c.kerberosClient.SetSPNEGOHeader(req, "presto/"+req.URL.Hostname())
if err != nil {
return nil, fmt.Errorf("error setting client SPNEGO header: %v", err)
}
}
for k, v := range c.httpHeaders {
req.Header[k] = v
}
for k, v := range hs {
req.Header[k] = v
}
if c.auth != nil {
pass, _ := c.auth.Password()
req.SetBasicAuth(c.auth.Username(), pass)
}
return req, nil
}
func (c *Conn) roundTrip(ctx context.Context, req *http.Request) (*http.Response, error) {
delay := 100 * time.Millisecond
const maxDelayBetweenRequests = float64(15 * time.Second)
timer := time.NewTimer(0)
defer timer.Stop()
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-timer.C:
timeout := DefaultQueryTimeout
if deadline, ok := ctx.Deadline(); ok {
timeout = deadline.Sub(time.Now())
}
client := c.httpClient
client.Timeout = timeout
resp, err := client.Do(req)
if err != nil {
return nil, &ErrQueryFailed{Reason: err}
}
switch resp.StatusCode {
case http.StatusOK:
return resp, nil
case http.StatusServiceUnavailable:
resp.Body.Close()
timer.Reset(delay)
delay = time.Duration(math.Min(
float64(delay)*math.Phi,
maxDelayBetweenRequests,
))
continue
default:
return nil, newErrQueryFailedFromResponse(resp)
}
}
}
}