Skip to content

Commit 3da00e8

Browse files
authored
Merge pull request #346 from ydb-platform/static-credentials
* Supports static credentials as part of connection string (dsn - dat…
2 parents b533c23 + 1ebeca1 commit 3da00e8

File tree

24 files changed

+372
-182
lines changed

24 files changed

+372
-182
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
name: golangci-lint
1111
strategy:
1212
matrix:
13-
go-version: [1.14.x, 1.18.x, 1.19.x]
13+
go-version: [1.16.x, 1.18.x, 1.19.x]
1414
os: [ubuntu-latest]
1515
env:
1616
OS: ${{ matrix.os }}

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
unit:
1010
strategy:
1111
matrix:
12-
go-version: [1.14.x, 1.18.x, 1.19.x]
12+
go-version: [1.16.x, 1.18.x, 1.19.x]
1313
os: [ubuntu-latest, windows-latest, macOS-latest]
1414
env:
1515
OS: ${{ matrix.os }}
@@ -33,7 +33,7 @@ jobs:
3333
e2e:
3434
strategy:
3535
matrix:
36-
go-version: [1.14.x, 1.18.x, 1.19.x]
36+
go-version: [1.16.x, 1.18.x, 1.19.x]
3737
os: [ubuntu-latest]
3838
services:
3939
ydb:

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
* Removed support of `YDB_LOG_NO_COLOR` environment variable
77
* Changed default behaviour of internal logger to without coloring
88
* Fixed coloring (to true) with environment variable `YDB_LOG_SEVERITY_LEVEL`
9+
* Added `ydb.WithStaticCredentials(user, password)` option for make static credentials
10+
* Supports static credentials as part of connection string (dsn - data source name)
11+
* Changed minimal supported version of go from 1.14 to 1.16 (required for jwt library)
12+
913

1014
## v3.33.0
1115
* Added `retry.DoTx` helper for retrying `database/sql` transactions

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ More examples of usage placed in [examples](https://github.com/ydb-platform/ydb-
9393

9494
## Credentials <a name="credentials"></a>
9595

96-
Driver contains two options for making simple `credentials.Credentials`:
96+
Driver implements several ways for making credentials for `YDB`:
9797
- `ydb.WithAnonymousCredentials()` (enabled by default unless otherwise specified)
9898
- `ydb.WithAccessTokenCredentials("token")`
99+
- `ydb.WithStaticCredentials("user", "password")`,
100+
- as part of connection string, like as `grpcs://user:password@endpoint/database`
99101

100102
Another variants of `credentials.Credentials` object provides with external packages:
101103

config/config.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@ func (c Config) Database() string {
9090
return c.database
9191
}
9292

93-
// Credentials is a ydb client credentials.
94-
// In most cases Credentials are required.
95-
func (c Config) Credentials() credentials.Credentials {
96-
return c.credentials
97-
}
98-
9993
// Trace contains driver tracing options.
10094
func (c Config) Trace() trace.Driver {
10195
return c.trace
@@ -268,12 +262,8 @@ func New(opts ...Option) Config {
268262
for _, o := range opts {
269263
o(&c)
270264
}
271-
c.grpcOptions = append(
272-
c.grpcOptions,
273-
grpcCredentials(
274-
c.secure,
275-
c.tlsConfig,
276-
),
265+
c.grpcOptions = append(c.grpcOptions,
266+
grpcCredentials(c.secure, c.tlsConfig),
277267
)
278268
c.meta = meta.New(
279269
c.database,
@@ -284,6 +274,20 @@ func New(opts ...Option) Config {
284274
return c
285275
}
286276

277+
// With makes copy of current Config with specified options
278+
func (c Config) With(opts ...Option) Config {
279+
for _, o := range opts {
280+
o(&c)
281+
}
282+
c.meta = meta.New(
283+
c.database,
284+
c.credentials,
285+
c.trace,
286+
c.metaOptions...,
287+
)
288+
return c
289+
}
290+
287291
func certPool() *x509.CertPool {
288292
certPool, err := x509.SystemCertPool()
289293
if err == nil {

connection.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import (
1515
"github.com/ydb-platform/ydb-go-sdk/v3/internal/conn"
1616
internalCoordination "github.com/ydb-platform/ydb-go-sdk/v3/internal/coordination"
1717
coordinationConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/coordination/config"
18+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials"
1819
discoveryConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/discovery/config"
20+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/dsn"
21+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/endpoint"
1922
internalRatelimiter "github.com/ydb-platform/ydb-go-sdk/v3/internal/ratelimiter"
2023
ratelimiterConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/ratelimiter/config"
2124
internalScheme "github.com/ydb-platform/ydb-go-sdk/v3/internal/scheme"
@@ -83,6 +86,8 @@ type Connection interface {
8386

8487
//nolint:maligned
8588
type connection struct {
89+
userInfo *dsn.UserInfo
90+
8691
opts []Option
8792

8893
config config.Config
@@ -408,16 +413,20 @@ func open(ctx context.Context, opts ...Option) (_ Connection, err error) {
408413
}()
409414

410415
if c.pool == nil {
411-
c.pool = conn.NewPool(
412-
ctx,
413-
c.config,
414-
)
416+
c.pool = conn.NewPool(ctx, c.config)
415417
}
416418

417-
c.balancer, err = balancer.New(
418-
ctx,
419-
c.config,
420-
c.pool,
419+
if c.userInfo != nil {
420+
c.config = c.config.With(config.WithCredentials(
421+
credentials.NewStaticCredentials(
422+
c.userInfo.User, c.userInfo.Password,
423+
c.pool.Get(endpoint.New(c.config.Endpoint())),
424+
),
425+
))
426+
}
427+
428+
c.balancer, err = balancer.New(ctx,
429+
c.config, c.pool,
421430
append(
422431
// prepend common params from root config
423432
[]discoveryConfig.Option{

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/ydb-platform/ydb-go-sdk/v3
33
go 1.18
44

55
require (
6+
github.com/golang-jwt/jwt/v4 v4.4.1
67
github.com/golang/mock v1.6.0
78
github.com/jonboulle/clockwork v0.2.2
89
github.com/stretchr/testify v1.7.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
2020
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
2121
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
2222
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
23+
github.com/golang-jwt/jwt/v4 v4.4.1 h1:pC5DB52sCeK48Wlb9oPcdhnjkz1TKt1D/P7WKJ0kUcQ=
24+
github.com/golang-jwt/jwt/v4 v4.4.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
2325
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
2426
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
2527
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package credentials
2+
3+
import (
4+
"context"
5+
)
6+
7+
// accessTokenCredentials implements Credentials interface with static
8+
// authorization parameters.
9+
type accessTokenCredentials struct {
10+
token string
11+
sourceInfo string
12+
}
13+
14+
func NewAccessTokenCredentials(token string, sourceInfo string) Credentials {
15+
return &accessTokenCredentials{
16+
token: token,
17+
sourceInfo: sourceInfo,
18+
}
19+
}
20+
21+
// Token implements Credentials.
22+
func (a accessTokenCredentials) Token(_ context.Context) (string, error) {
23+
return a.token, nil
24+
}

internal/credentials/anonymous.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package credentials
2+
3+
import (
4+
"context"
5+
)
6+
7+
// anonymousCredentials implements Credentials interface with anonymousCredentials access
8+
type anonymousCredentials struct {
9+
sourceInfo string
10+
}
11+
12+
func NewAnonymousCredentials(sourceInfo string) Credentials {
13+
return &anonymousCredentials{
14+
sourceInfo: sourceInfo,
15+
}
16+
}
17+
18+
// Token implements Credentials.
19+
func (a anonymousCredentials) Token(_ context.Context) (string, error) {
20+
return "", nil
21+
}

0 commit comments

Comments
 (0)