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

3. import (
4. "context"
5. "os"

7. "github.com/ydb-platform/ydb-go-sdk/v3"
8. yc "github.com/ydb-platform/ydb-go-yc"
9. )

11. func main() {
12. ctx, cancel := context.WithCancel(context.Background())
13. defer cancel()
14. db, err := ydb.Open(
15. ctx,
16. os.Getenv("YDB_CONNECTION_STRING"),
17. yc.WithServiceAccountKeyFileCredentials(
18. os.Getenv("YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"),
19. ),
20. yc.WithInternalCA(), // append Yandex Cloud certificates
21. )
22. if err != nil {
23. panic(err)
24. }
25. defer func() {
26. _ = db.Close(ctx)
27. }()
28. }

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. );

8. GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
9. .withAuthProvider(authProvider)
10. .build();

12. TableClient tableClient = TableClient
13. .newClient(GrpcTableRpc.ownTransport(transport))
14. .build());

16. doWork(tableClient);

18. tableClient.close();
19. }

Service account file - 图2