Skip to content

Commit c3812d1

Browse files
committed
Fix Service Discovery goroutines leaks, refactoring Token -> ycsdk Credentials middleware
1 parent f87cf02 commit c3812d1

5 files changed

Lines changed: 112 additions & 223 deletions

File tree

internal/discovery/cloud/yandex/iam.go

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

internal/discovery/cloud/yandex/iam_test.go

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package yandex
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/cherts/pgscv/discovery/log"
7+
"github.com/go-playground/validator/v10"
8+
"os"
9+
"path/filepath"
10+
"time"
11+
)
12+
13+
type authorizedKey struct {
14+
ID string `json:"id"`
15+
ServiceAccountID string `json:"service_account_id" validate:"required"`
16+
CreatedAt time.Time `json:"created_at" validate:"required"`
17+
KeyAlgorithm string `json:"key_algorithm" validate:"required"`
18+
PublicKey string `json:"public_key" validate:"required"`
19+
PrivateKey string `json:"private_key" validate:"required"`
20+
}
21+
22+
func (k *authorizedKey) validate() error {
23+
v := validator.New()
24+
25+
err := v.Struct(k)
26+
if err != nil {
27+
return fmt.Errorf("validate | %w", err)
28+
}
29+
30+
return nil
31+
}
32+
33+
func loadAuthorizedKey(filePath string) (*authorizedKey, error) {
34+
35+
log.Debugf("[SD] Loading authorized key from path '%s'", filePath)
36+
data, err := os.ReadFile(filepath.Clean(filePath))
37+
if err != nil {
38+
log.Errorf("[SD] Failed to load authorized key, error: %s", err.Error())
39+
return nil, err
40+
}
41+
42+
var key authorizedKey
43+
44+
err = json.Unmarshal(data, &key)
45+
if err != nil {
46+
log.Errorf("[SD] Failed to parse authorized key, JSON parse error: %s", err.Error())
47+
return nil, err
48+
}
49+
50+
err = key.validate()
51+
if err != nil {
52+
log.Errorf("[SD] Failed to validate authorized key, error: %s", err.Error())
53+
}
54+
55+
return &key, nil
56+
}

internal/discovery/cloud/yandex/postgresql.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package yandex
22

33
import (
44
"context"
5-
"github.com/cherts/pgscv/internal/discovery/filter"
6-
75
"github.com/cherts/pgscv/discovery/log"
6+
"github.com/cherts/pgscv/internal/discovery/filter"
87
"github.com/yandex-cloud/go-genproto/yandex/cloud/mdb/postgresql/v1"
98
)
109

@@ -39,17 +38,21 @@ type Cluster struct {
3938
// GetPostgreSQLClusters get a filtered list of clusters and their databases from Yandex cloud API
4039
func (sdk *SDK) GetPostgreSQLClusters(ctx context.Context, folderID string, filter []filter.Filter) ([]Cluster, error) {
4140
log.Debug("[Yandex.Cloud SD] Init SDK...")
41+
4242
yandexSdk, err := sdk.Build(ctx)
4343
if err != nil {
4444
log.Errorf("[Yandex.Cloud SD] Failed to init SDK, error: %v", err)
4545
return nil, err
4646
}
4747

48+
ctxOp, cancel := context.WithCancel(ctx)
49+
defer cancel()
50+
4851
var clusters []Cluster
4952
var req postgresql.ListClustersRequest
5053
req.FolderId = folderID
5154
for {
52-
resp, err := yandexSdk.MDB().PostgreSQL().Cluster().List(ctx, &req)
55+
resp, err := yandexSdk.MDB().PostgreSQL().Cluster().List(ctxOp, &req)
5356
if err != nil {
5457
log.Errorf("[Yandex.Cloud SD] Failed to get cluster list, error: %v", err)
5558
return nil, err
@@ -74,7 +77,7 @@ func (sdk *SDK) GetPostgreSQLClusters(ctx context.Context, folderID string, filt
7477
}
7578
var hosts []Host
7679
var databases []Database
77-
hostsIterator := yandexSdk.MDB().PostgreSQL().Cluster().ClusterHostsIterator(ctx,
80+
hostsIterator := yandexSdk.MDB().PostgreSQL().Cluster().ClusterHostsIterator(ctxOp,
7881
&postgresql.ListClusterHostsRequest{ClusterId: cluster.Id})
7982
for hostsIterator.Next() {
8083
host := hostsIterator.Value()
@@ -92,7 +95,7 @@ func (sdk *SDK) GetPostgreSQLClusters(ctx context.Context, folderID string, filt
9295
var dbReq postgresql.ListDatabasesRequest
9396
dbReq.ClusterId = cluster.Id
9497
for {
95-
dbResp, err := yandexSdk.MDB().PostgreSQL().Database().List(ctx,
98+
dbResp, err := yandexSdk.MDB().PostgreSQL().Database().List(ctxOp,
9699
&dbReq)
97100
if err != nil {
98101
return nil, err

0 commit comments

Comments
 (0)