-
Notifications
You must be signed in to change notification settings - Fork 55
/
connector.go
309 lines (268 loc) · 6.85 KB
/
connector.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Copyright 2014 SAP SE
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package driver
import (
"context"
"crypto/tls"
"crypto/x509"
"database/sql/driver"
"fmt"
"io/ioutil"
"net/url"
"strconv"
"sync"
)
/*
SessionVariables maps session variables to their values.
All defined session variables will be set once after a database connection is opened.
*/
type SessionVariables map[string]string
/*
A Connector represents a hdb driver in a fixed configuration.
A Connector can be passed to sql.OpenDB (starting from go 1.10) allowing users to bypass a string based data source name.
*/
type Connector struct {
mu sync.RWMutex
host, username, password string
locale string
bufferSize, fetchSize, timeout int
tlsConfig *tls.Config
sessionVariables SessionVariables
}
func newConnector() *Connector {
return &Connector{
fetchSize: DefaultFetchSize,
timeout: DefaultTimeout,
}
}
// NewBasicAuthConnector creates a connector for basic authentication.
func NewBasicAuthConnector(host, username, password string) *Connector {
c := newConnector()
c.host = host
c.username = username
c.password = password
return c
}
// NewDSNConnector creates a connector from a data source name.
func NewDSNConnector(dsn string) (*Connector, error) {
c := newConnector()
url, err := url.Parse(dsn)
if err != nil {
return nil, err
}
c.host = url.Host
if url.User != nil {
c.username = url.User.Username()
c.password, _ = url.User.Password()
}
var certPool *x509.CertPool
for k, v := range url.Query() {
switch k {
default:
return nil, fmt.Errorf("URL parameter %s is not supported", k)
case DSNFetchSize:
if len(v) == 0 {
continue
}
fetchSize, err := strconv.Atoi(v[0])
if err != nil {
return nil, fmt.Errorf("failed to parse fetchSize: %s", v[0])
}
if fetchSize < minFetchSize {
c.fetchSize = minFetchSize
} else {
c.fetchSize = fetchSize
}
case DSNTimeout:
if len(v) == 0 {
continue
}
timeout, err := strconv.Atoi(v[0])
if err != nil {
return nil, fmt.Errorf("failed to parse timeout: %s", v[0])
}
if timeout < minTimeout {
c.timeout = minTimeout
} else {
c.timeout = timeout
}
case DSNLocale:
if len(v) == 0 {
continue
}
c.locale = v[0]
case DSNTLSServerName:
if len(v) == 0 {
continue
}
if c.tlsConfig == nil {
c.tlsConfig = &tls.Config{}
}
c.tlsConfig.ServerName = v[0]
case DSNTLSInsecureSkipVerify:
if len(v) == 0 {
continue
}
var err error
b := true
if v[0] != "" {
b, err = strconv.ParseBool(v[0])
if err != nil {
return nil, fmt.Errorf("failed to parse InsecureSkipVerify (bool): %s", v[0])
}
}
if c.tlsConfig == nil {
c.tlsConfig = &tls.Config{}
}
c.tlsConfig.InsecureSkipVerify = b
case DSNTLSRootCAFile:
for _, fn := range v {
rootPEM, err := ioutil.ReadFile(fn)
if err != nil {
return nil, err
}
if certPool == nil {
certPool = x509.NewCertPool()
}
if ok := certPool.AppendCertsFromPEM(rootPEM); !ok {
return nil, fmt.Errorf("failed to parse root certificate - filename: %s", fn)
}
}
if certPool != nil {
if c.tlsConfig == nil {
c.tlsConfig = &tls.Config{}
}
c.tlsConfig.RootCAs = certPool
}
}
}
return c, nil
}
// Host returns the host of the connector.
func (c *Connector) Host() string {
return c.host
}
// Username returns the username of the connector.
func (c *Connector) Username() string {
return c.username
}
// Password returns the password of the connector.
func (c *Connector) Password() string {
return c.password
}
// Locale returns the locale of the connector.
func (c *Connector) Locale() string {
c.mu.RLock()
defer c.mu.RUnlock()
return c.locale
}
/*
SetLocale sets the locale of the connector.
For more information please see DSNLocale.
*/
func (c *Connector) SetLocale(locale string) {
c.mu.Lock()
c.locale = locale
c.mu.Unlock()
}
// FetchSize returns the fetchSize of the connector.
func (c *Connector) FetchSize() int {
c.mu.RLock()
defer c.mu.RUnlock()
return c.fetchSize
}
/*
SetFetchSize sets the fetchSize of the connector.
For more information please see DSNFetchSize.
*/
func (c *Connector) SetFetchSize(fetchSize int) error {
c.mu.Lock()
defer c.mu.Unlock()
if fetchSize < minFetchSize {
fetchSize = minFetchSize
}
c.fetchSize = fetchSize
return nil
}
// Timeout returns the timeout of the connector.
func (c *Connector) Timeout() int {
c.mu.RLock()
defer c.mu.RUnlock()
return c.timeout
}
/*
SetTimeout sets the timeout of the connector.
For more information please see DSNTimeout.
*/
func (c *Connector) SetTimeout(timeout int) error {
c.mu.Lock()
defer c.mu.Unlock()
if timeout < minTimeout {
timeout = minTimeout
}
c.timeout = timeout
return nil
}
// TLSConfig returns the TLS configuration of the connector.
func (c *Connector) TLSConfig() *tls.Config {
c.mu.RLock()
defer c.mu.RUnlock()
return c.tlsConfig
}
// SetTLSConfig sets the TLS configuration of the connector.
func (c *Connector) SetTLSConfig(tlsConfig *tls.Config) error {
c.mu.Lock()
defer c.mu.Unlock()
c.tlsConfig = tlsConfig
return nil
}
// SessionVariables returns the session variables stored in connector.
func (c *Connector) SessionVariables() SessionVariables {
c.mu.RLock()
defer c.mu.RUnlock()
return c.sessionVariables
}
// SetSessionVariables sets the session varibles of the connector.
func (c *Connector) SetSessionVariables(sessionVariables SessionVariables) error {
c.mu.Lock()
defer c.mu.Unlock()
c.sessionVariables = sessionVariables
return nil
}
// BasicAuthDSN return the connector DSN for basic authentication.
func (c *Connector) BasicAuthDSN() string {
values := url.Values{}
if c.locale != "" {
values.Set(DSNLocale, c.locale)
}
if c.fetchSize != 0 {
values.Set(DSNFetchSize, fmt.Sprintf("%d", c.fetchSize))
}
if c.timeout != 0 {
values.Set(DSNTimeout, fmt.Sprintf("%d", c.timeout))
}
return (&url.URL{
Scheme: DriverName,
User: url.UserPassword(c.username, c.password),
Host: c.host,
RawQuery: values.Encode(),
}).String()
}
// Connect implements the database/sql/driver/Connector interface.
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
return newConn(ctx, c)
}
// Driver implements the database/sql/driver/Connector interface.
func (c *Connector) Driver() driver.Driver {
return drv
}