Skip to content

Commit 86783b9

Browse files
authored
Merge pull request #780 from ydb-platform/credentials
Added String method to credentials types
2 parents 7d96e3e + c2118f0 commit 86783b9

File tree

11 files changed

+179
-135
lines changed

11 files changed

+179
-135
lines changed

connection.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,7 @@ func connect(ctx context.Context, c *Driver) error {
465465
c.config = c.config.With(config.WithCredentials(
466466
credentials.NewStaticCredentials(
467467
c.userInfo.User, c.userInfo.Password,
468-
c.config.Endpoint(),
469-
c.config.GrpcDialOptions()...,
468+
c.config,
470469
),
471470
))
472471
}

credentials/credentials.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"google.golang.org/grpc"
77

88
"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials"
9+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
910
)
1011

1112
// Credentials is an interface of YDB credentials required for connect with YDB
@@ -29,33 +30,52 @@ func WithSourceInfo(sourceInfo string) option {
2930

3031
// NewAccessTokenCredentials makes access token credentials object
3132
// Passed options redefines default values of credentials object internal fields
32-
func NewAccessTokenCredentials(accessToken string, opts ...option) Credentials {
33+
func NewAccessTokenCredentials(accessToken string, opts ...option) *credentials.AccessToken {
3334
h := &optionsHolder{
34-
sourceInfo: "credentials.NewAccessTokenCredentials(token)",
35+
sourceInfo: stack.Record(1),
3536
}
3637
for _, o := range opts {
3738
if o != nil {
3839
o(h)
3940
}
4041
}
41-
return credentials.NewAccessTokenCredentials(accessToken, h.sourceInfo)
42+
return credentials.NewAccessTokenCredentials(accessToken, credentials.WithSourceInfo(h.sourceInfo))
4243
}
4344

4445
// NewAnonymousCredentials makes anonymous credentials object
4546
// Passed options redefines default values of credentials object internal fields
46-
func NewAnonymousCredentials(opts ...option) Credentials {
47+
func NewAnonymousCredentials(opts ...option) *credentials.Anonymous {
4748
h := &optionsHolder{
48-
sourceInfo: "credentials.NewAnonymousCredentials()",
49+
sourceInfo: stack.Record(1),
4950
}
5051
for _, o := range opts {
5152
if o != nil {
5253
o(h)
5354
}
5455
}
55-
return credentials.NewAnonymousCredentials(h.sourceInfo)
56+
return credentials.NewAnonymousCredentials(credentials.WithSourceInfo(h.sourceInfo))
57+
}
58+
59+
type staticCredentialsConfig struct {
60+
authEndpoint string
61+
opts []grpc.DialOption
62+
}
63+
64+
func (s staticCredentialsConfig) Endpoint() string {
65+
return s.authEndpoint
66+
}
67+
68+
func (s staticCredentialsConfig) GrpcDialOptions() []grpc.DialOption {
69+
return s.opts
5670
}
5771

5872
// NewStaticCredentials makes static credentials object
59-
func NewStaticCredentials(user, password, authEndpoint string, opts ...grpc.DialOption) Credentials {
60-
return credentials.NewStaticCredentials(user, password, authEndpoint, opts...)
73+
func NewStaticCredentials(user, password, authEndpoint string, opts ...grpc.DialOption) *credentials.Static {
74+
return credentials.NewStaticCredentials(user, password,
75+
staticCredentialsConfig{
76+
authEndpoint: authEndpoint,
77+
opts: opts,
78+
},
79+
credentials.WithSourceInfo(stack.Record(1)),
80+
)
6181
}

internal/credentials/access_token.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,43 @@ package credentials
22

33
import (
44
"context"
5+
"fmt"
6+
7+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/secret"
8+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
9+
)
10+
11+
var (
12+
_ Credentials = (*AccessToken)(nil)
13+
_ fmt.Stringer = (*AccessToken)(nil)
514
)
615

7-
// accessTokenCredentials implements Credentials interface with static
16+
// AccessToken implements Credentials interface with static
817
// authorization parameters.
9-
type accessTokenCredentials struct {
18+
type AccessToken struct {
1019
token string
1120
sourceInfo string
1221
}
1322

14-
func NewAccessTokenCredentials(token, sourceInfo string) Credentials {
15-
return &accessTokenCredentials{
23+
func NewAccessTokenCredentials(token string, opts ...Option) *AccessToken {
24+
options := optionsHolder{
25+
sourceInfo: stack.Record(1),
26+
}
27+
for _, opt := range opts {
28+
opt(&options)
29+
}
30+
return &AccessToken{
1631
token: token,
17-
sourceInfo: sourceInfo,
32+
sourceInfo: options.sourceInfo,
1833
}
1934
}
2035

2136
// Token implements Credentials.
22-
func (a accessTokenCredentials) Token(_ context.Context) (string, error) {
23-
return a.token, nil
37+
func (c AccessToken) Token(_ context.Context) (string, error) {
38+
return c.token, nil
39+
}
40+
41+
// Token implements Credentials.
42+
func (c AccessToken) String() string {
43+
return fmt.Sprintf("AccessToken(token:%q,from:%q)", secret.Token(c.token), c.sourceInfo)
2444
}

internal/credentials/anonymous.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,39 @@ package credentials
22

33
import (
44
"context"
5+
"fmt"
6+
7+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
8+
)
9+
10+
var (
11+
_ Credentials = (*Anonymous)(nil)
12+
_ fmt.Stringer = (*Anonymous)(nil)
513
)
614

7-
// anonymousCredentials implements Credentials interface with anonymousCredentials access
8-
type anonymousCredentials struct {
15+
// Anonymous implements Credentials interface with Anonymous access
16+
type Anonymous struct {
917
sourceInfo string
1018
}
1119

12-
func NewAnonymousCredentials(sourceInfo string) Credentials {
13-
return &anonymousCredentials{
14-
sourceInfo: sourceInfo,
20+
func NewAnonymousCredentials(opts ...Option) *Anonymous {
21+
options := optionsHolder{
22+
sourceInfo: stack.Record(1),
23+
}
24+
for _, opt := range opts {
25+
opt(&options)
26+
}
27+
return &Anonymous{
28+
sourceInfo: options.sourceInfo,
1529
}
1630
}
1731

1832
// Token implements Credentials.
19-
func (a anonymousCredentials) Token(_ context.Context) (string, error) {
33+
func (c Anonymous) Token(_ context.Context) (string, error) {
2034
return "", nil
2135
}
36+
37+
// Token implements Credentials.
38+
func (c Anonymous) String() string {
39+
return fmt.Sprintf("Anonymous(from:%q)", c.sourceInfo)
40+
}

internal/credentials/credentials.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,8 @@ package credentials
22

33
import (
44
"context"
5-
"fmt"
6-
7-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
85
)
96

10-
// errNoCredentials may be returned by Credentials implementations to
11-
// make driver act as if there are no Credentials at all. That is, driver will
12-
// not send any token meta information during request.
13-
var errNoCredentials = xerrors.Wrap(fmt.Errorf("ydb: credentials: no credentials"))
14-
157
// Credentials is an interface of YDB credentials required for connect with YDB
168
type Credentials interface {
179
// Token must return actual token or error
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package credentials
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestCredentialsString(t *testing.T) {
11+
for _, test := range []struct {
12+
c Credentials
13+
s string
14+
}{
15+
{
16+
NewAnonymousCredentials(),
17+
"Anonymous(from:\"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestCredentialsString(credentials_test.go:16)\")", //nolint:lll
18+
},
19+
{
20+
NewAnonymousCredentials(WithSourceInfo("test")),
21+
"Anonymous(from:\"test\")",
22+
},
23+
{
24+
NewAccessTokenCredentials("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
25+
"AccessToken(token:\"1234****WXYZ(CRC-32c: 81993EA5)\",from:\"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestCredentialsString(credentials_test.go:24)\")", //nolint:lll
26+
},
27+
{
28+
NewAccessTokenCredentials("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", WithSourceInfo("test")),
29+
"AccessToken(token:\"1234****WXYZ(CRC-32c: 81993EA5)\",from:\"test\")",
30+
},
31+
} {
32+
t.Run(test.s, func(t *testing.T) {
33+
if stringer, ok := test.c.(fmt.Stringer); ok {
34+
require.Equal(t, test.s, stringer.String())
35+
}
36+
})
37+
}
38+
}

internal/credentials/multi.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

internal/credentials/options.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package credentials
2+
3+
type optionsHolder struct {
4+
sourceInfo string
5+
}
6+
7+
type Option func(opts *optionsHolder)
8+
9+
func WithSourceInfo(sourceInfo string) Option {
10+
return func(opts *optionsHolder) {
11+
opts.sourceInfo = sourceInfo
12+
}
13+
}

internal/credentials/static.go

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,51 @@ import (
1313
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Operations"
1414
"google.golang.org/grpc"
1515

16+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/secret"
17+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1618
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1719
)
1820

19-
func NewStaticCredentials(user, password, authEndpoint string, opts ...grpc.DialOption) Credentials {
20-
return &staticCredentials{
21-
user: user,
22-
password: password,
23-
endpoint: authEndpoint,
24-
opts: opts,
21+
type staticCredentialsConfig interface {
22+
Endpoint() string
23+
GrpcDialOptions() []grpc.DialOption
24+
}
25+
26+
func NewStaticCredentials(user, password string, config staticCredentialsConfig, opts ...Option) *Static {
27+
options := optionsHolder{
28+
sourceInfo: stack.Record(1),
29+
}
30+
for _, opt := range opts {
31+
opt(&options)
32+
}
33+
return &Static{
34+
user: user,
35+
password: password,
36+
endpoint: config.Endpoint(),
37+
sourceInfo: options.sourceInfo,
38+
opts: config.GrpcDialOptions(),
2539
}
2640
}
2741

28-
// staticCredentials implements Credentials interface with static
42+
var (
43+
_ Credentials = (*Static)(nil)
44+
_ fmt.Stringer = (*Static)(nil)
45+
)
46+
47+
// Static implements Credentials interface with static
2948
// authorization parameters.
30-
type staticCredentials struct {
31-
user string
32-
password string
33-
endpoint string
34-
opts []grpc.DialOption
35-
token string
36-
requestAt time.Time
37-
mu sync.Mutex
49+
type Static struct {
50+
user string
51+
password string
52+
endpoint string
53+
opts []grpc.DialOption
54+
token string
55+
requestAt time.Time
56+
mu sync.Mutex
57+
sourceInfo string
3858
}
3959

40-
func (c *staticCredentials) Token(ctx context.Context) (token string, err error) {
60+
func (c *Static) Token(ctx context.Context) (token string, err error) {
4161
c.mu.Lock()
4262
defer c.mu.Unlock()
4363
if time.Until(c.requestAt) > 0 {
@@ -110,3 +130,13 @@ func parseExpiresAt(raw string) (expiresAt time.Time, err error) {
110130
}
111131
return claims.ExpiresAt.Time, nil
112132
}
133+
134+
func (c *Static) String() string {
135+
return fmt.Sprintf(
136+
"Static(user:%q,password:%q,token:%q,from:%q)",
137+
c.user,
138+
secret.Password(c.password),
139+
secret.Token(c.token),
140+
c.sourceInfo,
141+
)
142+
}

0 commit comments

Comments
 (0)