Authentication using the metadata service

Note

The article is being updated.

Below are examples of the code for authentication using environment variables in different YDB SDKs.

Go

Java

  1. package main
  2. import (
  3. "context"
  4. "os"
  5. "github.com/ydb-platform/ydb-go-sdk/v3"
  6. yc "github.com/ydb-platform/ydb-go-yc"
  7. )
  8. func main() {
  9. ctx, cancel := context.WithCancel(context.Background())
  10. defer cancel()
  11. db, err := ydb.Open(
  12. ctx,
  13. os.Getenv("YDB_CONNECTION_STRING"),
  14. yc.WithMetadataCredentials(ctx),
  15. yc.WithInternalCA(), // append Yandex Cloud certificates
  16. )
  17. if err != nil {
  18. panic(err)
  19. }
  20. defer func() {
  21. _ = db.Close(ctx)
  22. }()
  23. }

Metadata service - 图1

  1. public void work(String connectionString) {
  2. AuthProvider authProvider = CloudAuthProvider.newAuthProvider(
  3. ComputeEngineCredentialProvider.builder()
  4. .build()
  5. );
  6. GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
  7. .withAuthProvider(authProvider)
  8. .build();
  9. TableClient tableClient = TableClient
  10. .newClient(GrpcTableRpc.ownTransport(transport))
  11. .build());
  12. doWork(tableClient);
  13. tableClient.close();
  14. }

Metadata service - 图2