github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/tests/integration/connection_secure_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"context"
     8  	"crypto/tls"
     9  	"fmt"
    10  	"os"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/ydb-platform/ydb-go-genproto/Ydb_Discovery_V1"
    15  	"github.com/ydb-platform/ydb-go-genproto/Ydb_Export_V1"
    16  	"github.com/ydb-platform/ydb-go-genproto/Ydb_Scripting_V1"
    17  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
    18  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Discovery"
    19  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Export"
    20  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Operations"
    21  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Scripting"
    22  	"google.golang.org/protobuf/proto"
    23  	"google.golang.org/protobuf/types/known/durationpb"
    24  
    25  	"github.com/ydb-platform/ydb-go-sdk/v3"
    26  	"github.com/ydb-platform/ydb-go-sdk/v3/config"
    27  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
    28  	"github.com/ydb-platform/ydb-go-sdk/v3/retry"
    29  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
    30  )
    31  
    32  //nolint:gocyclo
    33  func TestConnectionSecure(t *testing.T) {
    34  	dsn, has := os.LookupEnv("YDB_CONNECTION_STRING_SECURE")
    35  	if !has {
    36  		t.Skipf("require YDB_CONNECTION_STRING_SECURE env")
    37  	}
    38  
    39  	const sumColumn = "sum"
    40  	ctx := xtest.Context(t)
    41  
    42  	db, err := ydb.Open(ctx,
    43  		"", // corner case for check replacement of endpoint+database+secure
    44  		ydb.WithConnectionString(dsn),
    45  		ydb.WithAccessTokenCredentials(
    46  			os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS"),
    47  		),
    48  		ydb.With(
    49  			config.WithOperationTimeout(time.Second*2),
    50  			config.WithOperationCancelAfter(time.Second*2),
    51  		),
    52  		ydb.WithConnectionTTL(time.Millisecond*10000),
    53  		ydb.WithMinTLSVersion(tls.VersionTLS10),
    54  		ydb.WithLogger(
    55  			newLogger(t),
    56  			trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
    57  		),
    58  	)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	defer func() {
    63  		// cleanup connection
    64  		if e := db.Close(ctx); e != nil {
    65  			t.Fatalf("close failed: %+v", e)
    66  		}
    67  	}()
    68  	t.Run("discovery.WhoAmI", func(t *testing.T) {
    69  		if err = retry.Retry(ctx, func(ctx context.Context) (err error) {
    70  			discoveryClient := Ydb_Discovery_V1.NewDiscoveryServiceClient(ydb.GRPCConn(db))
    71  			response, err := discoveryClient.WhoAmI(
    72  				ctx,
    73  				&Ydb_Discovery.WhoAmIRequest{IncludeGroups: true},
    74  			)
    75  			if err != nil {
    76  				return err
    77  			}
    78  			var result Ydb_Discovery.WhoAmIResult
    79  			err = proto.Unmarshal(response.GetOperation().GetResult().GetValue(), &result)
    80  			if err != nil {
    81  				return
    82  			}
    83  			return nil
    84  		}, retry.WithIdempotent(true)); err != nil {
    85  			t.Fatalf("Execute failed: %v", err)
    86  		}
    87  	})
    88  	t.Run("scripting.ExecuteYql", func(t *testing.T) {
    89  		if err = retry.Retry(ctx, func(ctx context.Context) (err error) {
    90  			scriptingClient := Ydb_Scripting_V1.NewScriptingServiceClient(ydb.GRPCConn(db))
    91  			response, err := scriptingClient.ExecuteYql(
    92  				ctx,
    93  				&Ydb_Scripting.ExecuteYqlRequest{Script: "SELECT 1+100 AS sum"},
    94  			)
    95  			if err != nil {
    96  				return err
    97  			}
    98  			var result Ydb_Scripting.ExecuteYqlResult
    99  			err = proto.Unmarshal(response.GetOperation().GetResult().GetValue(), &result)
   100  			if err != nil {
   101  				return
   102  			}
   103  			if len(result.GetResultSets()) != 1 {
   104  				return fmt.Errorf(
   105  					"unexpected result sets count: %d",
   106  					len(result.GetResultSets()),
   107  				)
   108  			}
   109  			if len(result.GetResultSets()[0].GetColumns()) != 1 {
   110  				return fmt.Errorf(
   111  					"unexpected colums count: %d",
   112  					len(result.GetResultSets()[0].GetColumns()),
   113  				)
   114  			}
   115  			if result.GetResultSets()[0].GetColumns()[0].GetName() != sumColumn {
   116  				return fmt.Errorf(
   117  					"unexpected colum name: %s",
   118  					result.GetResultSets()[0].GetColumns()[0].GetName(),
   119  				)
   120  			}
   121  			if len(result.GetResultSets()[0].GetRows()) != 1 {
   122  				return fmt.Errorf(
   123  					"unexpected rows count: %d",
   124  					len(result.GetResultSets()[0].GetRows()),
   125  				)
   126  			}
   127  			if result.GetResultSets()[0].GetRows()[0].GetItems()[0].GetInt32Value() != 101 {
   128  				return fmt.Errorf(
   129  					"unexpected result of select: %d",
   130  					result.GetResultSets()[0].GetRows()[0].GetInt64Value(),
   131  				)
   132  			}
   133  			return nil
   134  		}, retry.WithIdempotent(true)); err != nil {
   135  			t.Fatalf("Execute failed: %v", err)
   136  		}
   137  	})
   138  	t.Run("scripting.StreamExecuteYql", func(t *testing.T) {
   139  		if err = retry.Retry(ctx, func(ctx context.Context) (err error) {
   140  			scriptingClient := Ydb_Scripting_V1.NewScriptingServiceClient(ydb.GRPCConn(db))
   141  			client, err := scriptingClient.StreamExecuteYql(
   142  				ctx,
   143  				&Ydb_Scripting.ExecuteYqlRequest{Script: "SELECT 1+100 AS sum"},
   144  			)
   145  			if err != nil {
   146  				return err
   147  			}
   148  			response, err := client.Recv()
   149  			if err != nil {
   150  				return err
   151  			}
   152  			if len(response.GetResult().GetResultSet().GetColumns()) != 1 {
   153  				return fmt.Errorf(
   154  					"unexpected colums count: %d",
   155  					len(response.GetResult().GetResultSet().GetColumns()),
   156  				)
   157  			}
   158  			if response.GetResult().GetResultSet().GetColumns()[0].GetName() != sumColumn {
   159  				return fmt.Errorf(
   160  					"unexpected colum name: %s",
   161  					response.GetResult().GetResultSet().GetColumns()[0].GetName(),
   162  				)
   163  			}
   164  			if len(response.GetResult().GetResultSet().GetRows()) != 1 {
   165  				return fmt.Errorf(
   166  					"unexpected rows count: %d",
   167  					len(response.GetResult().GetResultSet().GetRows()),
   168  				)
   169  			}
   170  			if response.GetResult().GetResultSet().GetRows()[0].GetItems()[0].GetInt32Value() != 101 {
   171  				return fmt.Errorf(
   172  					"unexpected result of select: %d",
   173  					response.GetResult().GetResultSet().GetRows()[0].GetInt64Value(),
   174  				)
   175  			}
   176  			return nil
   177  		}, retry.WithIdempotent(true)); err != nil {
   178  			t.Fatalf("Stream execute failed: %v", err)
   179  		}
   180  	})
   181  	t.Run("with.scripting.StreamExecuteYql", func(t *testing.T) {
   182  		var childDB *ydb.Driver
   183  		childDB, err = db.With(
   184  			ctx,
   185  			ydb.WithDialTimeout(time.Second*5),
   186  		)
   187  		if err != nil {
   188  			t.Fatalf("failed to open sub-connection: %v", err)
   189  		}
   190  		defer func() {
   191  			_ = childDB.Close(ctx)
   192  		}()
   193  		if err = retry.Retry(ctx, func(ctx context.Context) (err error) {
   194  			scriptingClient := Ydb_Scripting_V1.NewScriptingServiceClient(ydb.GRPCConn(childDB))
   195  			client, err := scriptingClient.StreamExecuteYql(
   196  				ctx,
   197  				&Ydb_Scripting.ExecuteYqlRequest{Script: "SELECT 1+100 AS sum"},
   198  			)
   199  			if err != nil {
   200  				return err
   201  			}
   202  			response, err := client.Recv()
   203  			if err != nil {
   204  				return err
   205  			}
   206  			if len(response.GetResult().GetResultSet().GetColumns()) != 1 {
   207  				return fmt.Errorf(
   208  					"unexpected colums count: %d",
   209  					len(response.GetResult().GetResultSet().GetColumns()),
   210  				)
   211  			}
   212  			if response.GetResult().GetResultSet().GetColumns()[0].GetName() != sumColumn {
   213  				return fmt.Errorf(
   214  					"unexpected colum name: %s",
   215  					response.GetResult().GetResultSet().GetColumns()[0].GetName(),
   216  				)
   217  			}
   218  			if len(response.GetResult().GetResultSet().GetRows()) != 1 {
   219  				return fmt.Errorf(
   220  					"unexpected rows count: %d",
   221  					len(response.GetResult().GetResultSet().GetRows()),
   222  				)
   223  			}
   224  			if response.GetResult().GetResultSet().GetRows()[0].GetItems()[0].GetInt32Value() != 101 {
   225  				return fmt.Errorf(
   226  					"unexpected result of select: %d",
   227  					response.GetResult().GetResultSet().GetRows()[0].GetInt64Value(),
   228  				)
   229  			}
   230  			return nil
   231  		}, retry.WithIdempotent(true)); err != nil {
   232  			t.Fatalf("Stream execute failed: %v", err)
   233  		}
   234  	})
   235  	t.Run("export.ExportToS3", func(t *testing.T) {
   236  		if err = retry.Retry(ctx, func(ctx context.Context) (err error) {
   237  			exportClient := Ydb_Export_V1.NewExportServiceClient(ydb.GRPCConn(db))
   238  			response, err := exportClient.ExportToS3(
   239  				ctx,
   240  				&Ydb_Export.ExportToS3Request{
   241  					OperationParams: &Ydb_Operations.OperationParams{
   242  						OperationTimeout: durationpb.New(time.Second),
   243  						CancelAfter:      durationpb.New(time.Second),
   244  					},
   245  					Settings: &Ydb_Export.ExportToS3Settings{},
   246  				},
   247  			)
   248  			if err != nil {
   249  				return err
   250  			}
   251  			if response.GetOperation().GetStatus() != Ydb.StatusIds_BAD_REQUEST {
   252  				return fmt.Errorf(
   253  					"operation must be BAD_REQUEST: %s",
   254  					response.GetOperation().GetStatus().String(),
   255  				)
   256  			}
   257  			return nil
   258  		}, retry.WithIdempotent(true)); err != nil {
   259  			t.Fatalf("check export failed: %v", err)
   260  		}
   261  	})
   262  }