-
Notifications
You must be signed in to change notification settings - Fork 54
/
dsn.go
232 lines (202 loc) · 6.18 KB
/
dsn.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
package driver
import (
"errors"
"fmt"
"net/url"
"strconv"
"time"
)
// DSN parameters.
const (
DSNDatabaseName = "databaseName" // Tenant database name.
DSNDefaultSchema = "defaultSchema" // Database default schema.
DSNTimeout = "timeout" // Driver side connection timeout in seconds.
DSNPingInterval = "pingInterval" // Connection ping interval in seconds.
)
/*
DSN TLS parameters.
For more information please see https://golang.org/pkg/crypto/tls/#Config.
For more flexibility in TLS configuration please see driver.Connector.
*/
const (
DSNTLSRootCAFile = "TLSRootCAFile" // Path,- filename to root certificate(s).
DSNTLSServerName = "TLSServerName" // ServerName to verify the hostname.
DSNTLSInsecureSkipVerify = "TLSInsecureSkipVerify" // Controls whether a client verifies the server's certificate chain and host name.
)
// TLSPrms is holding the TLS parameters of a DSN structure.
type TLSPrms struct {
ServerName string
InsecureSkipVerify bool
RootCAFiles []string
}
const urlSchema = "hdb" // mirrored from driver/DriverName
/*
A DSN represents a parsed DSN string. A DSN string is an URL string with the following format
"hdb://<username>:<password>@<host address>:<port number>"
and optional query parameters (see DSN query parameters and DSN query default values).
Examples:
"hdb://myUser:myPassword@localhost:30015?databaseName=myTenantDatabaseName"
"hdb://myUser:myPassword@localhost:30015?timeout=60"
Examples TLS connection:
"hdb://myUser:myPassword@localhost:39013?TLSRootCAFile=trust.pem"
"hdb://myUser:myPassword@localhost:39013?TLSRootCAFile=trust.pem&TLSServerName=hostname"
"hdb://myUser:myPassword@localhost:39013?TLSInsecureSkipVerify"
*/
type DSN struct {
host string
username, password string
databaseName string
defaultSchema string
timeout time.Duration
pingInterval time.Duration
tls *TLSPrms
}
// ParseError is the error returned in case DSN is invalid.
type ParseError struct {
s string
err error
}
func (e ParseError) Error() string {
if err := errors.Unwrap(e.err); err != nil {
return err.Error()
}
return e.s
}
// Unwrap returns the nested error.
func (e ParseError) Unwrap() error { return e.err }
// Cause returns the cause of the error.
func (e ParseError) Cause() error { return e.err }
func parameterNotSupportedError(k string) error {
return &ParseError{s: fmt.Sprintf("parameter %s is not supported", k)}
}
func invalidNumberOfParametersError(k string, act, exp int) error { //nolint:unparam
return &ParseError{s: fmt.Sprintf("invalid number of parameters for %s %d - expected %d", k, act, exp)}
}
func invalidNumberOfParametersRangeError(k string, act, min, max int) error {
return &ParseError{s: fmt.Sprintf("invalid number of parameters for %s %d - expected %d - %d", k, act, min, max)}
}
func invalidNumberOfParametersMinError(k string, act, min int) error {
return &ParseError{s: fmt.Sprintf("invalid number of parameters for %s %d - expected at least %d", k, act, min)}
}
func parseError(k, v string) error {
return &ParseError{s: fmt.Sprintf("failed to parse %s: %s", k, v)}
}
// ParseDSN parses a DSN string into a DSN structure.
func ParseDSN(s string) (*DSN, error) {
if s == "" {
return nil, &ParseError{s: "invalid parameter - DSN is empty"}
}
u, err := url.Parse(s)
if err != nil {
return nil, &ParseError{err: err}
}
dsn := &DSN{host: u.Host}
if u.User != nil {
dsn.username = u.User.Username()
password, _ := u.User.Password()
dsn.password = password
}
for k, v := range u.Query() {
switch k {
default:
return nil, parameterNotSupportedError(k)
case DSNDatabaseName:
if len(v) != 1 {
return nil, invalidNumberOfParametersError(k, len(v), 1)
}
dsn.databaseName = v[0]
case DSNDefaultSchema:
if len(v) != 1 {
return nil, invalidNumberOfParametersError(k, len(v), 1)
}
dsn.defaultSchema = v[0]
case DSNTimeout:
if len(v) != 1 {
return nil, invalidNumberOfParametersError(k, len(v), 1)
}
t, err := strconv.Atoi(v[0])
if err != nil {
return nil, parseError(k, v[0])
}
dsn.timeout = time.Duration(t) * time.Second
case DSNPingInterval:
if len(v) != 1 {
return nil, invalidNumberOfParametersError(k, len(v), 1)
}
t, err := strconv.Atoi(v[0])
if err != nil {
return nil, parseError(k, v[0])
}
dsn.pingInterval = time.Duration(t) * time.Second
case DSNTLSServerName:
if len(v) != 1 {
return nil, invalidNumberOfParametersError(k, len(v), 1)
}
if dsn.tls == nil {
dsn.tls = &TLSPrms{}
}
dsn.tls.ServerName = v[0]
case DSNTLSInsecureSkipVerify:
if len(v) > 1 {
return nil, invalidNumberOfParametersRangeError(k, len(v), 0, 1)
}
b := true
if len(v) > 0 && v[0] != "" {
b, err = strconv.ParseBool(v[0])
if err != nil {
return nil, parseError(k, v[0])
}
}
if dsn.tls == nil {
dsn.tls = &TLSPrms{}
}
dsn.tls.InsecureSkipVerify = b
case DSNTLSRootCAFile:
if len(v) == 0 {
return nil, invalidNumberOfParametersMinError(k, len(v), 1)
}
if dsn.tls == nil {
dsn.tls = &TLSPrms{}
}
dsn.tls.RootCAFiles = v
}
}
return dsn, nil
}
// String reassembles the DSN into a valid DSN string.
func (dsn *DSN) String() string {
values := url.Values{}
if dsn.databaseName != "" {
values.Set(DSNDatabaseName, dsn.databaseName)
}
if dsn.defaultSchema != "" {
values.Set(DSNDefaultSchema, dsn.defaultSchema)
}
if dsn.timeout != 0 {
values.Set(DSNTimeout, fmt.Sprintf("%d", dsn.timeout/time.Second))
}
if dsn.pingInterval != 0 {
values.Set(DSNPingInterval, fmt.Sprintf("%d", dsn.pingInterval/time.Second))
}
if dsn.tls != nil {
if dsn.tls.ServerName != "" {
values.Set(DSNTLSServerName, dsn.tls.ServerName)
}
values.Set(DSNTLSInsecureSkipVerify, strconv.FormatBool(dsn.tls.InsecureSkipVerify))
for _, fn := range dsn.tls.RootCAFiles {
values.Add(DSNTLSRootCAFile, fn)
}
}
u := &url.URL{
Scheme: urlSchema,
Host: dsn.host,
RawQuery: values.Encode(),
}
switch {
case dsn.username != "" && dsn.password != "":
u.User = url.UserPassword(dsn.username, dsn.password)
case dsn.username != "":
u.User = url.User(dsn.username)
}
return u.String()
}