Skip to content

Commit ce60bc4

Browse files
authored
Merge pull request #224 from ydb-platform/tls-config
define tls.Config explicitly instead redefine default TLS config options
2 parents 32b38fe + 61188db commit ce60bc4

File tree

6 files changed

+126
-68
lines changed

6 files changed

+126
-68
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
* Added `WithTLSConfig` option for redefine TLS config
2+
* Added `sugar.LoadCertificatesFromFile` and `sugar.LoadCertificatesFromPem` helpers
3+
14
## v3.22.0
25
* Supported `json.Unmarshaler` type for scanning row to values
36
* Reimplement `sugar.DSN` with `net/url`

config/config.go

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,46 @@ import (
2424
type Config struct {
2525
config.Common
2626

27-
trace trace.Driver
28-
dialTimeout time.Duration
29-
connectionTTL time.Duration
30-
balancer balancer.Balancer
31-
secure bool
32-
endpoint string
33-
database string
34-
requestsType string
35-
userAgent string
27+
trace trace.Driver
28+
dialTimeout time.Duration
29+
connectionTTL time.Duration
30+
balancer balancer.Balancer
31+
secure bool
32+
endpoint string
33+
database string
34+
requestsType string
35+
userAgent string
36+
grpcOptions []grpc.DialOption
37+
credentials credentials.Credentials
38+
tlsConfig *tls.Config
39+
meta meta.Meta
40+
3641
excludeGRPCCodesForPessimization []grpcCodes.Code
37-
grpcOptions []grpc.DialOption
38-
credentials credentials.Credentials
39-
tlsConfig *tls.Config
40-
meta meta.Meta
4142
}
4243

4344
// ExcludeGRPCCodesForPessimization defines grpc codes for exclude its from pessimization trigger
4445
func (c Config) ExcludeGRPCCodesForPessimization() []grpcCodes.Code {
4546
return c.excludeGRPCCodesForPessimization
4647
}
4748

48-
// GrpcDialOptions is an custom client grpc dial options which will appends to
49-
// default grpc dial options
49+
// GrpcDialOptions reports about used grpc dialing options
5050
func (c Config) GrpcDialOptions() []grpc.DialOption {
5151
return c.grpcOptions
5252
}
5353

54-
// Meta is an internal option which contains meta information about database connection
54+
// Meta reports meta information about database connection
5555
func (c Config) Meta() meta.Meta {
5656
return c.meta
5757
}
5858

59-
// ConnectionTTL is a time to live of a connection
60-
// If ConnectionTTL is zero then TTL is not used.
59+
// ConnectionTTL defines interval for parking grpc connections.
60+
//
61+
// If ConnectionTTL is zero - connections are not park.
6162
func (c Config) ConnectionTTL() time.Duration {
6263
return c.connectionTTL
6364
}
6465

65-
// Secure is an flag for secure connection
66+
// Secure is a flag for secure connection
6667
func (c Config) Secure() bool {
6768
return c.secure
6869
}
@@ -72,6 +73,7 @@ func (c Config) Endpoint() string {
7273
return c.endpoint
7374
}
7475

76+
// TLSConfig reports about TLS configuration
7577
func (c Config) TLSConfig() *tls.Config {
7678
return c.tlsConfig
7779
}
@@ -89,7 +91,7 @@ func (c Config) Database() string {
8991
return c.database
9092
}
9193

92-
// Credentials is an ydb client credentials.
94+
// Credentials is a ydb client credentials.
9395
// In most cases Credentials are required.
9496
func (c Config) Credentials() credentials.Credentials {
9597
return c.credentials
@@ -116,7 +118,7 @@ type Option func(c *Config)
116118

117119
// WithInternalDNSResolver
118120
//
119-
// Deprecated: already used internal dns-resolver
121+
// Deprecated: always used internal dns-resolver
120122
func WithInternalDNSResolver() Option {
121123
return func(c *Config) {}
122124
}
@@ -127,6 +129,9 @@ func WithEndpoint(endpoint string) Option {
127129
}
128130
}
129131

132+
// WithSecure changes secure connection flag.
133+
//
134+
// Warning: if secure is false - TLS config options has no effect.
130135
func WithSecure(secure bool) Option {
131136
return func(c *Config) {
132137
c.secure = secure
@@ -139,12 +144,22 @@ func WithDatabase(database string) Option {
139144
}
140145
}
141146

147+
// WithCertificate appends certificate to TLS config root certificates
142148
func WithCertificate(certificate *x509.Certificate) Option {
143149
return func(c *Config) {
144150
c.tlsConfig.RootCAs.AddCert(certificate)
145151
}
146152
}
147153

154+
// WithTLSConfig replaces older TLS config
155+
//
156+
// Warning: all early changes of TLS config will be lost
157+
func WithTLSConfig(tlsConfig *tls.Config) Option {
158+
return func(c *Config) {
159+
c.tlsConfig = tlsConfig
160+
}
161+
}
162+
148163
func WithTrace(t trace.Driver, opts ...trace.DriverComposeOption) Option {
149164
return func(c *Config) {
150165
c.trace = c.trace.Compose(t, opts...)
@@ -218,12 +233,14 @@ func WithRequestsType(requestsType string) Option {
218233
}
219234
}
220235

236+
// WithMinTLSVersion applies minimum TLS version that is acceptable.
221237
func WithMinTLSVersion(minVersion uint16) Option {
222238
return func(c *Config) {
223239
c.tlsConfig.MinVersion = minVersion
224240
}
225241
}
226242

243+
// WithTLSSInsecureSkipVerify applies InsecureSkipVerify flag to TLS config
227244
func WithTLSSInsecureSkipVerify() Option {
228245
return func(c *Config) {
229246
c.tlsConfig.InsecureSkipVerify = true
@@ -267,32 +284,28 @@ func New(opts ...Option) Config {
267284
return c
268285
}
269286

270-
func certPool() (certPool *x509.CertPool) {
271-
defer func() {
272-
// on darwin system panic raced on checking system security
273-
if e := recover(); e != nil {
274-
certPool = x509.NewCertPool()
275-
}
276-
}()
277-
var err error
278-
certPool, err = x509.SystemCertPool()
279-
if err != nil {
280-
certPool = x509.NewCertPool()
287+
func certPool() *x509.CertPool {
288+
certPool, err := x509.SystemCertPool()
289+
if err == nil {
290+
return certPool
291+
}
292+
return x509.NewCertPool()
293+
}
294+
295+
func defaultTLSConfig() *tls.Config {
296+
return &tls.Config{
297+
MinVersion: tls.VersionTLS12,
298+
RootCAs: certPool(),
281299
}
282-
return
283300
}
284301

285302
func defaultConfig() (c Config) {
286303
return Config{
287304
credentials: credentials.NewAnonymousCredentials(
288305
credentials.WithSourceInfo("default"),
289306
),
290-
balancer: balancers.Default(),
291-
secure: true,
292-
tlsConfig: &tls.Config{
293-
MinVersion: tls.VersionTLS12,
294-
RootCAs: certPool(),
295-
},
307+
balancer: balancers.Default(),
308+
tlsConfig: defaultTLSConfig(),
296309
grpcOptions: []grpc.DialOption{
297310
grpc.WithContextDialer(
298311
func(ctx context.Context, address string) (net.Conn, error) {

internal/dsn/dsn.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@ var (
1616
}, nil
1717
},
1818
}
19-
schemasSecure = map[string]bool{
20-
"": true,
21-
"grpcs": true,
22-
"grpc": false,
23-
}
24-
errSchemeNotValid = xerrors.Wrap(fmt.Errorf("schema not valid"))
25-
errParserExists = xerrors.Wrap(fmt.Errorf("already exists parser. newest parser replaced old. param"))
19+
insecureSchema = "grpc"
20+
errParserExists = xerrors.Wrap(fmt.Errorf("already exists parser. newest parser replaced old. param"))
2621
)
2722

2823
type Parser func(value string) ([]config.Option, error)
@@ -41,15 +36,10 @@ func Parse(dsn string) (options []config.Option, err error) {
4136
if err != nil {
4237
return nil, xerrors.WithStackTrace(err)
4338
}
44-
if _, has := schemasSecure[uri.Scheme]; !has {
45-
return nil, xerrors.WithStackTrace(
46-
fmt.Errorf("%w: %v", errSchemeNotValid, uri.Scheme),
47-
)
48-
}
4939
options = append(
5040
options,
5141
config.WithEndpoint(uri.Host),
52-
config.WithSecure(schemasSecure[uri.Scheme]),
42+
config.WithSecure(uri.Scheme != insecureSchema),
5343
)
5444
for param, values := range uri.Query() {
5545
if p, has := parsers[param]; has {

internal/dsn/dsn_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/ydb-platform/ydb-go-sdk/v3/config"
88
"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials"
9-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
109
"github.com/ydb-platform/ydb-go-sdk/v3/testutil"
1110
)
1211

@@ -27,7 +26,6 @@ func TestParseConnectionString(t *testing.T) {
2726
endpoint string
2827
database string
2928
token string
30-
error error
3129
}{
3230
{
3331
"grpc://ydb-ru.yandex.net:2135/?" +
@@ -36,7 +34,6 @@ func TestParseConnectionString(t *testing.T) {
3634
"ydb-ru.yandex.net:2135",
3735
"/ru/home/gvit/mydb",
3836
"123",
39-
nil,
4037
},
4138
{
4239
"grpcs://ydb.serverless.yandexcloud.net:2135/?" +
@@ -45,7 +42,6 @@ func TestParseConnectionString(t *testing.T) {
4542
"ydb.serverless.yandexcloud.net:2135",
4643
"/ru-central1/b1g8skpblkos03malf3s/etn02qso4v3isjb00te1",
4744
"123",
48-
nil,
4945
},
5046
{
5147
"grpcs://lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.net:2135/?" +
@@ -54,20 +50,18 @@ func TestParseConnectionString(t *testing.T) {
5450
"lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.net:2135",
5551
"/ru-central1/b1g8skpblkos03malf3s/etn03r9df42nb631unbv",
5652
"123",
57-
nil,
5853
},
5954
{
6055
"abcd://ydb-ru.yandex.net:2135/?database=/ru/home/gvit/mydb",
6156
true,
57+
"ydb-ru.yandex.net:2135",
58+
"/ru/home/gvit/mydb",
6259
"",
63-
"",
64-
"",
65-
errSchemeNotValid,
6660
},
6761
} {
6862
t.Run(test.connectionString, func(t *testing.T) {
6963
options, err := Parse(test.connectionString)
70-
if !xerrors.Is(err, test.error) {
64+
if err != nil {
7165
t.Fatalf("Received unexpected error:\n%+v", err)
7266
}
7367
config := config.New(options...)

options.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ydb
22

33
import (
44
"context"
5+
"crypto/tls"
56
"crypto/x509"
67
"encoding/pem"
78
"fmt"
@@ -109,17 +110,17 @@ func WithDatabase(database string) Option {
109110

110111
// WithSecure defines secure option
111112
//
112-
// Warning: use WithConnectionString or dsn package instead
113+
// Warning: if secure is false - TLS config options has no effect.
113114
func WithSecure(secure bool) Option {
114115
return func(ctx context.Context, c *connection) error {
115116
c.options = append(c.options, config.WithSecure(secure))
116117
return nil
117118
}
118119
}
119120

120-
// WithInsecure defines secure option
121+
// WithInsecure defines secure option.
121122
//
122-
// Warning: use WithConnectionString or dsn package instead
123+
// Warning: WithInsecure lost current TLS config.
123124
func WithInsecure() Option {
124125
return func(ctx context.Context, c *connection) error {
125126
c.options = append(c.options, config.WithSecure(false))
@@ -135,6 +136,7 @@ func WithMinTLSVersion(minVersion uint16) Option {
135136
}
136137
}
137138

139+
// WithTLSSInsecureSkipVerify applies InsecureSkipVerify flag to TLS config
138140
func WithTLSSInsecureSkipVerify() Option {
139141
return func(ctx context.Context, c *connection) error {
140142
c.options = append(c.options, config.WithTLSSInsecureSkipVerify())
@@ -143,6 +145,7 @@ func WithTLSSInsecureSkipVerify() Option {
143145
}
144146

145147
// WithLogger add enables logging for selected tracing events.
148+
//
146149
// See trace package documentation for details.
147150
func WithLogger(details trace.Details, opts ...LoggerOption) Option {
148151
loggerOpts := make([]logger.Option, 0, len(opts))
@@ -205,7 +208,7 @@ func WithDialTimeout(timeout time.Duration) Option {
205208
}
206209

207210
// With collects additional configuration options.
208-
// This option does not replace collected option, instead it will appen provided options.
211+
// This option does not replace collected option, instead it will append provided options.
209212
func With(options ...config.Option) Option {
210213
return func(ctx context.Context, c *connection) error {
211214
c.options = append(c.options, options...)
@@ -241,15 +244,15 @@ func WithTraceDriver(trace trace.Driver, opts ...trace.DriverComposeOption) Opti
241244
}
242245
}
243246

244-
// WithCertificate provides custom CA certificate.
247+
// WithCertificate appends certificate to TLS config root certificates
245248
func WithCertificate(cert *x509.Certificate) Option {
246249
return func(ctx context.Context, c *connection) error {
247250
c.options = append(c.options, config.WithCertificate(cert))
248251
return nil
249252
}
250253
}
251254

252-
// WithCertificate provides filepath to load custom CA certificates.
255+
// WithCertificatesFromFile appends certificates by filepath to TLS config root certificates
253256
func WithCertificatesFromFile(caFile string) Option {
254257
return func(ctx context.Context, c *connection) error {
255258
if len(caFile) > 0 && caFile[0] == '~' {
@@ -270,7 +273,18 @@ func WithCertificatesFromFile(caFile string) Option {
270273
}
271274
}
272275

273-
// WithCertificate provides PEM encoded custom CA certificates.
276+
// WithTLSConfig replaces older TLS config
277+
//
278+
// Warning: all early TLS config changes (such as WithCertificate, WithCertificatesFromFile, WithCertificatesFromPem,
279+
// WithMinTLSVersion, WithTLSSInsecureSkipVerify) will be lost
280+
func WithTLSConfig(tlsConfig *tls.Config) Option {
281+
return func(ctx context.Context, c *connection) error {
282+
c.options = append(c.options, config.WithTLSConfig(tlsConfig))
283+
return nil
284+
}
285+
}
286+
287+
// WithCertificatesFromPem appends certificates by filepath to TLS config root certificates
274288
func WithCertificatesFromPem(bytes []byte) Option {
275289
return func(ctx context.Context, c *connection) error {
276290
if ok, err := func(bytes []byte) (ok bool, err error) {

0 commit comments

Comments
 (0)