Spanner Omni と Spanner は、Go クライアント ライブラリを同様に使用します。このドキュメントでは、Go クライアント ライブラリを構成して Spanner Omni への安全な接続を確立する方法について説明します。これらの接続を確立するには、データベース管理クライアントまたはデータベース クライアントを作成するときに spanner.ClientConfig を構成します。
Go クライアント ライブラリは、書式なしテキスト、TLS、認証情報付き TLS、mTLS 接続をサポートしています。
詳細については、Spanner ドキュメントの Go で Spanner を使ってみるをご覧ください。
始める前に
Spanner Omni で Go クライアント ライブラリを使用するには、Go クライアント ライブラリのバージョン v1.94.0 以降と Go リリース バージョン 1.25 以降を使用します。
Spanner Go モジュールを go.mod ファイルに追加するには、次のコマンドを実行します。
go get cloud.google.com/go/spanner@v1.94.0
ClientConfig オブジェクトを構成する
Go クライアント ライブラリを使用して Client または DatabaseAdminClient を作成するには、Type: spanner.OMNI を指定して ClientConfig オブジェクトを構成し、option.WithEndpoint() を使用してエンドポイントを指定します。
次の例は、サポートされている各セキュリティ構成に対して ClientConfig オブジェクトを構成する方法を示しています。
書式なしテキスト
プレーン テキスト接続を確立するには、spanner.ClientConfig で UsePlainText を true に設定します。
clientConfig := spanner.ClientConfig{
Type: spanner.OMNI,
UsePlainText: true,
}
adminClient, err := database.NewDatabaseAdminClientWithConfig(ctx, clientConfig,
option.WithEndpoint("ENDPOINT"),
)
if err != nil {
// Handle error.
}
defer adminClient.Close()
databaseClient, err := spanner.NewClientWithConfig(ctx, "DATABASE_NAME", clientConfig,
option.WithEndpoint("ENDPOINT"),
)
if err != nil {
// Handle error.
}
defer databaseClient.Close()
TLS
TLS 接続を確立するには、CaCertificateFile を使用して CA 証明書のパスを指定します。
clientConfig := spanner.ClientConfig{
Type: spanner.OMNI,
CaCertificateFile: "PATH_TO_CA_CERT",
}
adminClient, err := database.NewDatabaseAdminClientWithConfig(ctx, clientConfig,
option.WithEndpoint("ENDPOINT"),
)
if err != nil {
// Handle error.
}
defer adminClient.Close()
databaseClient, err := spanner.NewClientWithConfig(ctx, "DATABASE_NAME", clientConfig,
option.WithEndpoint("ENDPOINT"),
)
if err != nil {
// Handle error.
}
defer databaseClient.Close()
認証情報を使用した TLS
ユーザー名とパスワード認証で TLS 接続を確立するには、CaCertificateFile、Username、Password を指定します。
clientConfig := spanner.ClientConfig{
Type: spanner.OMNI,
CaCertificateFile: "PATH_TO_CA_CERT",
Username: "USERNAME",
Password: []byte("PASSWORD"),
}
adminClient, err := database.NewDatabaseAdminClientWithConfig(ctx, clientConfig,
option.WithEndpoint("ENDPOINT"),
)
if err != nil {
// Handle error.
}
defer adminClient.Close()
databaseClient, err := spanner.NewClientWithConfig(ctx, "DATABASE_NAME", clientConfig,
option.WithEndpoint("ENDPOINT"),
)
if err != nil {
// Handle error.
}
defer databaseClient.Close()
mTLS
相互 TLS(mTLS)接続を確立するには、CaCertificateFile、ClientCertificateFile、ClientKeyFile を指定します。
clientConfig := spanner.ClientConfig{
Type: spanner.OMNI,
CaCertificateFile: "PATH_TO_CA_CERT",
ClientCertificateFile: "PATH_TO_CLIENT_CERT",
ClientKeyFile: "PATH_TO_CLIENT_KEY",
}
adminClient, err := database.NewDatabaseAdminClientWithConfig(ctx, clientConfig,
option.WithEndpoint("ENDPOINT"),
)
if err != nil {
// Handle error.
}
defer adminClient.Close()
databaseClient, err := spanner.NewClientWithConfig(ctx, "DATABASE_NAME", clientConfig,
option.WithEndpoint("ENDPOINT"),
)
if err != nil {
// Handle error.
}
defer databaseClient.Close()
次のように置き換えます。
PATH_TO_CA_CERT: CA 証明書ファイルのパス。PATH_TO_CLIENT_CERT: クライアント証明書ファイルのパス。PATH_TO_CLIENT_KEY: クライアント キーファイルのパス。