environ

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2024 License: Apache-2.0 Imports: 10 Imported by: 21

README

ydb-go-sdk-auth-environ

helpers to connect to YDB using environ

Installation

go get -u github.com/ydb-platform/ydb-go-sdk-auth-environ

Usage

import (
	env "github.com/ydb-platform/ydb-go-sdk-auth-environ"
)
...
    db, err := ydb.New(
        ctx,
        connectParams,
        env.WithEnvironCredentials(ctx), 
    )
    

Auth environment variables

Name Type Default yandex-cloud Description
YDB_ANONYMOUS_CREDENTIALS 0 or 1 0 - flag for use anonymous credentials
YDB_ACCESS_TOKEN_CREDENTIALS string +/- use access token for authenticate with YDB. For authenticate with YDB inside yandex-cloud use short-life IAM-token. Other YDB installations can use access token depending on authenticate method
YDB_STATIC_CREDENTIALS_USER
YDB_STATIC_CREDENTIALS_PASSWORD
YDB_STATIC_CREDENTIALS_ENDPOINT
string
string
string
- static credentials from user, password and auth service endpoint
YDB_METADATA_CREDENTIALS 0 or 1 + flag for use metadata credentials
YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS string + path to service account key file credentials
YDB_SERVICE_ACCOUNT_KEY_CREDENTIALS string + service account key credentials

Documentation

Overview

Example (DsnParameterForUseEnvironCredentials)
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()
// force set up environment variable for example
os.Setenv("YDB_ACCESS_TOKEN_CREDENTIALS", "")
// For use go_environ_credentials parameter in connection string - need write blank import such as
// import _ "github.com/ydb-platform/ydb-go-sdk-auth-environ"
db, err := ydb.Open(ctx, "grpc://localhost:2136/local?use_env_credentials")
if err != nil {
	panic(err)
}
defer db.Close(ctx) // cleanup resources

var helloWorld string
err = db.Query().Do(ctx, func(ctx context.Context, s query.Session) error {
	_, result, err := s.Execute(ctx, "SELECT 'HELLO WORLD'u")
	if err != nil {
		return err
	}
	defer result.Close(ctx)

	for {
		rs, err := result.NextResultSet(ctx)
		if err != nil {
			if errors.Is(err, io.EOF) {
				return nil
			}

			return err
		}

		for {
			row, err := rs.NextRow(ctx)
			if err != nil {
				if errors.Is(err, io.EOF) {
					return nil
				}
			}

			if err := row.Scan(&helloWorld); err != nil {
				return err
			}
		}

		return nil
	}

	return nil
}, query.WithIdempotent())
if err != nil {
	panic(err)
}

fmt.Println(helloWorld)
Output:

HELLO WORLD
Example (WithEnvironCredentials)
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()
// force set up environment variable for example
os.Setenv("YDB_ANONYMOUS_CREDENTIALS", "1")
db, err := ydb.Open(ctx, "grpc://localhost:2136/local",
	environ.WithEnvironCredentials(),
)
if err != nil {
	panic(err)
}
defer db.Close(ctx) // cleanup resources

var helloWorld string
err = db.Query().Do(ctx, func(ctx context.Context, s query.Session) error {
	_, result, err := s.Execute(ctx, "SELECT 'HELLO WORLD'u")
	if err != nil {
		return err
	}
	defer result.Close(ctx)

	for {
		rs, err := result.NextResultSet(ctx)
		if err != nil {
			if errors.Is(err, io.EOF) {
				return nil
			}

			return err
		}

		for {
			row, err := rs.NextRow(ctx)
			if err != nil {
				if errors.Is(err, io.EOF) {
					return nil
				}
			}

			if err := row.Scan(&helloWorld); err != nil {
				return err
			}
		}

		return nil
	}

	return nil
}, query.WithIdempotent())
if err != nil {
	panic(err)
}

fmt.Println(helloWorld)
Output:

HELLO WORLD

Index

Examples

Constants

View Source
const (
	VersionMajor = "0"
	VersionMinor = "5"
	VersionPatch = "0"
)
View Source
const Version = VersionMajor + "." + VersionMinor + "." + VersionPatch

Variables

This section is empty.

Functions

func WithEnvironCredentials

func WithEnvironCredentials() ydb.Option

WithEnvironCredentials check environment variables and creates credentials by it

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL