Authentication using a service account file

Note

The article is being updated.

Below are examples of the code for authentication using a service account file 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.WithServiceAccountKeyFileCredentials(
  15. os.Getenv("YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"),
  16. ),
  17. yc.WithInternalCA(), // append Yandex Cloud certificates
  18. )
  19. if err != nil {
  20. panic(err)
  21. }
  22. defer func() {
  23. _ = db.Close(ctx)
  24. }()
  25. }

Service account file - 图1

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

Service account file - 图2