Анонимная аутентификация
Ниже приведены примеры кода анонимной аутентификации в разных YDB SDK.
Go (native)
Go (database/sql)
Java
Node.js
Python
Python (asyncio)
C# (.NET)
PHP
Анонимная аутентификация является аутентификацией по умолчанию.
Явным образом анонимную аутентификацию можно включить так:
package main
import (
"context"
"github.com/ydb-platform/ydb-go-sdk/v3"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, err := ydb.Open(ctx,
os.Getenv("YDB_CONNECTION_STRING"),
ydb.WithAnonymousCredentials(),
)
if err != nil {
panic(err)
}
defer db.Close(ctx)
...
}
Анонимная аутентификация является аутентификацией по умолчанию.
Явным образом анонимную аутентификацию можно включить так:
package main
import (
"context"
"database/sql"
"os"
"github.com/ydb-platform/ydb-go-sdk/v3"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nativeDriver, err := ydb.Open(ctx,
os.Getenv("YDB_CONNECTION_STRING"),
ydb.WithAnonymousCredentials(),
)
if err != nil {
panic(err)
}
defer nativeDriver.Close(ctx)
connector, err := ydb.Connector(nativeDriver)
if err != nil {
panic(err)
}
db := sql.OpenDB(connector)
defer db.Close()
...
}
public void work(String connectionString) {
AuthProvider authProvider = NopAuthProvider.INSTANCE;
GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider)
.build());
TableClient tableClient = TableClient.newClient(transport).build();
doWork(tableClient);
tableClient.close();
transport.close();
}
import { Driver, AnonymousAuthService } from 'ydb-sdk';
export async function connect(endpoint: string, database: string) {
const authService = new AnonymousAuthService();
const driver = new Driver({endpoint, database, authService});
const timeout = 10000;
if (!await driver.ready(timeout)) {
console.log(`Driver has not become ready in ${timeout}ms!`);
process.exit(1);
}
console.log('Driver connected')
return driver
}
import os
import ydb
with ydb.Driver(
connection_string=os.environ["YDB_CONNECTION_STRING"],
credentials=ydb.credentials.AnonymousCredentials(),
) as driver:
driver.wait(timeout=5)
...
import os
import ydb
import asyncio
async def ydb_init():
async with ydb.aio.Driver(
endpoint=os.environ["YDB_ENDPOINT"],
database=os.environ["YDB_DATABASE"],
credentials=ydb.credentials.AnonymousCredentials(),
) as driver:
await driver.wait()
...
asyncio.run(ydb_init())
using Ydb.Sdk;
using Ydb.Sdk.Auth;
const string endpoint = "grpc://localhost:2136";
const string database = "/local";
var config = new DriverConfig(
endpoint: endpoint,
database: database,
credentials: new AnonymousProvider()
);
await using var driver = await Driver.CreateInitialized(config);
<?php
use YdbPlatform\Ydb\Ydb;
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
$config = [
// Database path
'database' => '/local',
// Database endpoint
'endpoint' => 'localhost:2136',
// Auto discovery (dedicated server only)
'discovery' => false,
// IAM config
'iam_config' => [
'insecure' => true,
// 'root_cert_file' => './CA.pem', // Root CA file (uncomment for dedicated server)
],
'credentials' => new AnonymousAuthentication()
];
$ydb = new Ydb($config);