Skip to content

Commit 347e30d

Browse files
authored
Merge pull request #513 from ydb-platform/static
Added `credentials.NewStaticCredentials()` static credentials constructor + Changed `internal/credentials.NewStaticCredentials()` signature and behaviour for create grpc connection on each call to auth service
2 parents 56b89f7 + 163eb61 commit 347e30d

File tree

5 files changed

+153
-16
lines changed

5 files changed

+153
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Added `credentials.NewStaticCredentials()` static credentials constructor
2+
* Changed `internal/credentials.NewStaticCredentials()` signature and behaviour for create grpc connection on each call to auth service
13
* Downgrade `google.golang.org/grpc` to `v1.49.0`
24

35
## v3.42.2

connection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials"
1919
discoveryConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/discovery/config"
2020
"github.com/ydb-platform/ydb-go-sdk/v3/internal/dsn"
21-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/endpoint"
2221
internalRatelimiter "github.com/ydb-platform/ydb-go-sdk/v3/internal/ratelimiter"
2322
ratelimiterConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/ratelimiter/config"
2423
internalScheme "github.com/ydb-platform/ydb-go-sdk/v3/internal/scheme"
@@ -428,7 +427,8 @@ func connect(ctx context.Context, c *connection) error {
428427
c.config = c.config.With(config.WithCredentials(
429428
credentials.NewStaticCredentials(
430429
c.userInfo.User, c.userInfo.Password,
431-
c.pool.Get(endpoint.New(c.config.Endpoint())),
430+
c.config.Endpoint(),
431+
c.config.GrpcDialOptions()...,
432432
),
433433
))
434434
}

connection_e2e_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,31 @@ import (
77
"context"
88
"crypto/tls"
99
"fmt"
10+
"net/url"
1011
"os"
1112
"testing"
1213
"time"
1314

1415
"github.com/ydb-platform/ydb-go-genproto/Ydb_Discovery_V1"
1516
"github.com/ydb-platform/ydb-go-genproto/Ydb_Export_V1"
17+
"github.com/ydb-platform/ydb-go-genproto/Ydb_Monitoring_V1"
1618
"github.com/ydb-platform/ydb-go-genproto/Ydb_Scripting_V1"
1719
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
1820
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Discovery"
1921
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Export"
22+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Monitoring"
2023
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Operations"
2124
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Scripting"
2225
"google.golang.org/grpc"
26+
grpcCredentials "google.golang.org/grpc/credentials"
27+
"google.golang.org/grpc/credentials/insecure"
2328
"google.golang.org/grpc/metadata"
2429
"google.golang.org/protobuf/proto"
2530
"google.golang.org/protobuf/types/known/durationpb"
2631

2732
"github.com/ydb-platform/ydb-go-sdk/v3"
2833
"github.com/ydb-platform/ydb-go-sdk/v3/config"
34+
"github.com/ydb-platform/ydb-go-sdk/v3/credentials"
2935
"github.com/ydb-platform/ydb-go-sdk/v3/internal/meta"
3036
"github.com/ydb-platform/ydb-go-sdk/v3/log"
3137
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
@@ -314,3 +320,107 @@ func TestConnection(t *testing.T) {
314320
}
315321
})
316322
}
323+
324+
func TestStaticCredentials(t *testing.T) {
325+
t.Skip("wait for newest cr.yandex/yc/yandex-docker-local-ydb:latest was published")
326+
327+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
328+
defer cancel()
329+
330+
var dsn string
331+
if v, has := os.LookupEnv("YDB_CONNECTION_STRING"); !has {
332+
t.Fatal("env YDB_CONNECTION_STRING required")
333+
} else {
334+
dsn = v
335+
}
336+
337+
url, err := url.Parse(dsn)
338+
if err != nil {
339+
t.Fatal(err)
340+
}
341+
342+
staticCredentials := credentials.NewStaticCredentials("root", "", url.Host, func() grpc.DialOption {
343+
if url.Scheme == "grpcs" {
344+
transportCredentials, transportCredentialsErr := grpcCredentials.NewClientTLSFromFile(
345+
os.Getenv("YDB_SSL_ROOT_CERTIFICATES_FILE"), url.Hostname(),
346+
)
347+
if err != nil {
348+
t.Fatalf("cannot create transport credentials: %v", transportCredentialsErr)
349+
}
350+
return grpc.WithTransportCredentials(transportCredentials)
351+
}
352+
return grpc.WithTransportCredentials(insecure.NewCredentials())
353+
}())
354+
355+
token, err := staticCredentials.Token(ctx)
356+
if err != nil {
357+
t.Fatalf("get token failed: %v", err)
358+
} else {
359+
fmt.Printf("token: %s\n", token)
360+
}
361+
362+
db, err := ydb.Open(
363+
ctx,
364+
"", // corner case for check replacement of endpoint+database+secure
365+
ydb.WithConnectionString(os.Getenv("YDB_CONNECTION_STRING")),
366+
ydb.WithCredentials(staticCredentials),
367+
)
368+
if err != nil {
369+
t.Fatal(err)
370+
}
371+
defer func() {
372+
// cleanup connection
373+
if e := db.Close(ctx); e != nil {
374+
t.Fatalf("close failed: %+v", e)
375+
}
376+
}()
377+
_, err = db.Discovery().WhoAmI(ctx)
378+
if err != nil {
379+
t.Fatal(err)
380+
}
381+
}
382+
383+
func TestMonitoring(t *testing.T) {
384+
t.Skip("wait for newest cr.yandex/yc/yandex-docker-local-ydb:latest was published")
385+
386+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
387+
defer cancel()
388+
389+
db, err := ydb.Open(
390+
ctx,
391+
"", // corner case for check replacement of endpoint+database+secure
392+
ydb.WithConnectionString(os.Getenv("YDB_CONNECTION_STRING")),
393+
)
394+
if err != nil {
395+
t.Fatal(err)
396+
}
397+
defer func() {
398+
// cleanup connection
399+
if e := db.Close(ctx); e != nil {
400+
t.Fatalf("close failed: %+v", e)
401+
}
402+
}()
403+
t.Run("monitoring.SelfCheck", func(t *testing.T) {
404+
if err = retry.Retry(ctx, func(ctx context.Context) (err error) {
405+
client := Ydb_Monitoring_V1.NewMonitoringServiceClient(ydb.GRPCConn(db))
406+
response, err := client.SelfCheck(ctx, &Ydb_Monitoring.SelfCheckRequest{
407+
OperationParams: nil,
408+
ReturnVerboseStatus: false,
409+
MinimumStatus: 0,
410+
MaximumLevel: 0,
411+
})
412+
if err != nil {
413+
return err
414+
}
415+
var result Ydb_Monitoring.SelfCheckResult
416+
err = response.Operation.Result.UnmarshalTo(&result)
417+
if err != nil {
418+
return err
419+
}
420+
fmt.Printf("%+v\n", &result)
421+
return nil
422+
}, retry.WithIdempotent(true)); err != nil {
423+
t.Fatalf("Execute failed: %v", err)
424+
}
425+
})
426+
}

credentials/credentials.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package credentials
33
import (
44
"context"
55

6+
"google.golang.org/grpc"
7+
68
"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials"
79
)
810

@@ -48,3 +50,8 @@ func NewAnonymousCredentials(opts ...option) Credentials {
4850
}
4951
return credentials.NewAnonymousCredentials(h.sourceInfo)
5052
}
53+
54+
// NewStaticCredentials makes static credentials object
55+
func NewStaticCredentials(user, password string, authEndpoint string, opts ...grpc.DialOption) Credentials {
56+
return credentials.NewStaticCredentials(user, password, authEndpoint, opts...)
57+
}

internal/credentials/static.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ import (
1616
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1717
)
1818

19-
func NewStaticCredentials(user, password string, cc grpc.ClientConnInterface) Credentials {
19+
func NewStaticCredentials(user, password string, authEndpoint string, opts ...grpc.DialOption) Credentials {
2020
return &staticCredentials{
2121
user: user,
2222
password: password,
23-
client: Ydb_Auth_V1.NewAuthServiceClient(cc),
23+
endpoint: authEndpoint,
24+
opts: opts,
2425
}
2526
}
2627

@@ -29,32 +30,46 @@ func NewStaticCredentials(user, password string, cc grpc.ClientConnInterface) Cr
2930
type staticCredentials struct {
3031
user string
3132
password string
32-
client Ydb_Auth_V1.AuthServiceClient
33+
endpoint string
34+
opts []grpc.DialOption
3335
token string
3436
requestAt time.Time
3537
mu sync.Mutex
3638
}
3739

38-
func (lp *staticCredentials) Token(ctx context.Context) (token string, err error) {
39-
lp.mu.Lock()
40-
defer lp.mu.Unlock()
41-
if time.Until(lp.requestAt) > 0 {
42-
return lp.token, nil
40+
func (c *staticCredentials) Token(ctx context.Context) (token string, err error) {
41+
c.mu.Lock()
42+
defer c.mu.Unlock()
43+
if time.Until(c.requestAt) > 0 {
44+
return c.token, nil
4345
}
44-
response, err := lp.client.Login(ctx, &Ydb_Auth.LoginRequest{
46+
cc, err := grpc.DialContext(ctx, c.endpoint, c.opts...)
47+
if err != nil {
48+
return "", xerrors.WithStackTrace(
49+
fmt.Errorf("dial failed: %w", err),
50+
)
51+
}
52+
defer func() {
53+
_ = cc.Close()
54+
}()
55+
56+
client := Ydb_Auth_V1.NewAuthServiceClient(cc)
57+
58+
response, err := client.Login(ctx, &Ydb_Auth.LoginRequest{
4559
OperationParams: &Ydb_Operations.OperationParams{
4660
OperationMode: 0,
4761
OperationTimeout: nil,
4862
CancelAfter: nil,
4963
Labels: nil,
5064
ReportCostInfo: 0,
5165
},
52-
User: lp.user,
53-
Password: lp.password,
66+
User: c.user,
67+
Password: c.password,
5468
})
5569
if err != nil {
5670
return "", xerrors.WithStackTrace(err)
5771
}
72+
5873
switch {
5974
case !response.GetOperation().GetReady():
6075
return "", xerrors.WithStackTrace(
@@ -77,13 +92,16 @@ func (lp *staticCredentials) Token(ctx context.Context) (token string, err error
7792
if err = response.GetOperation().GetResult().UnmarshalTo(&result); err != nil {
7893
return "", xerrors.WithStackTrace(err)
7994
}
95+
8096
expiresAt, err := parseExpiresAt(result.GetToken())
8197
if err != nil {
8298
return "", xerrors.WithStackTrace(err)
8399
}
84-
lp.requestAt = time.Now().Add(time.Until(expiresAt) / 2)
85-
lp.token = result.GetToken()
86-
return lp.token, nil
100+
101+
c.requestAt = time.Now().Add(time.Until(expiresAt) / 2)
102+
c.token = result.GetToken()
103+
104+
return c.token, nil
87105
}
88106

89107
func parseExpiresAt(raw string) (expiresAt time.Time, err error) {

0 commit comments

Comments
 (0)