Skip to content

Commit ee66d3c

Browse files
committed
Added String method to credentials types
1 parent 7d96e3e commit ee66d3c

File tree

11 files changed

+164
-135
lines changed

11 files changed

+164
-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: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,38 @@ 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"
59
)
610

7-
// accessTokenCredentials implements Credentials interface with static
11+
// AccessToken implements Credentials interface with static
812
// authorization parameters.
9-
type accessTokenCredentials struct {
13+
type AccessToken struct {
1014
token string
1115
sourceInfo string
1216
}
1317

14-
func NewAccessTokenCredentials(token, sourceInfo string) Credentials {
15-
return &accessTokenCredentials{
18+
func NewAccessTokenCredentials(token string, opts ...Option) *AccessToken {
19+
options := optionsHolder{
20+
sourceInfo: stack.Record(1),
21+
}
22+
for _, opt := range opts {
23+
opt(&options)
24+
}
25+
return &AccessToken{
1626
token: token,
17-
sourceInfo: sourceInfo,
27+
sourceInfo: options.sourceInfo,
1828
}
1929
}
2030

2131
// Token implements Credentials.
22-
func (a accessTokenCredentials) Token(_ context.Context) (string, error) {
23-
return a.token, nil
32+
func (c AccessToken) Token(_ context.Context) (string, error) {
33+
return c.token, nil
34+
}
35+
36+
// Token implements Credentials.
37+
func (c AccessToken) String() string {
38+
return fmt.Sprintf("AccessToken(token:%q,from:%q)", secret.Token(c.token), c.sourceInfo)
2439
}

internal/credentials/anonymous.go

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

33
import (
44
"context"
5+
"fmt"
6+
7+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
58
)
69

7-
// anonymousCredentials implements Credentials interface with anonymousCredentials access
8-
type anonymousCredentials struct {
10+
// Anonymous implements Credentials interface with Anonymous access
11+
type Anonymous struct {
912
sourceInfo string
1013
}
1114

12-
func NewAnonymousCredentials(sourceInfo string) Credentials {
13-
return &anonymousCredentials{
14-
sourceInfo: sourceInfo,
15+
func NewAnonymousCredentials(opts ...Option) *Anonymous {
16+
options := optionsHolder{
17+
sourceInfo: stack.Record(1),
18+
}
19+
for _, opt := range opts {
20+
opt(&options)
21+
}
22+
return &Anonymous{
23+
sourceInfo: options.sourceInfo,
1524
}
1625
}
1726

1827
// Token implements Credentials.
19-
func (a anonymousCredentials) Token(_ context.Context) (string, error) {
28+
func (c Anonymous) Token(_ context.Context) (string, error) {
2029
return "", nil
2130
}
31+
32+
// Token implements Credentials.
33+
func (c Anonymous) String() string {
34+
return fmt.Sprintf("Anonymous(from:%q)", c.sourceInfo)
35+
}

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: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,46 @@ 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+
// Static implements Credentials interface with static
2943
// 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
44+
type Static struct {
45+
user string
46+
password string
47+
endpoint string
48+
opts []grpc.DialOption
49+
token string
50+
requestAt time.Time
51+
mu sync.Mutex
52+
sourceInfo string
3853
}
3954

40-
func (c *staticCredentials) Token(ctx context.Context) (token string, err error) {
55+
func (c *Static) Token(ctx context.Context) (token string, err error) {
4156
c.mu.Lock()
4257
defer c.mu.Unlock()
4358
if time.Until(c.requestAt) > 0 {
@@ -110,3 +125,13 @@ func parseExpiresAt(raw string) (expiresAt time.Time, err error) {
110125
}
111126
return claims.ExpiresAt.Time, nil
112127
}
128+
129+
func (c *Static) String() string {
130+
return fmt.Sprintf(
131+
"Static(user:%q,password:%q,token:%q,from:%q)",
132+
c.user,
133+
secret.Password(c.password),
134+
secret.Token(c.token),
135+
c.sourceInfo,
136+
)
137+
}

0 commit comments

Comments
 (0)