This document shows you how to establish secure connections to Spanner Omni by configuring the Go client library. You establish these connections by setting client options when you create a database administrative client or a database client.
The Go client library supports plain text, TLS, and mTLS connections. For all
connection types, include option.WithoutAuthentication() to prevent
transmitting Google Cloud credentials to a
Spanner Omni endpoint.
For more information, see Getting started with Spanner in Go in the Spanner documentation.
Before you begin
To connect the Go client library to Spanner Omni, you need to use the
IsExperimentalHost: true configuration. This option specifies that the library
connects to a local or on-premises deployment of Spanner Omni
rather than the managed version of Spanner.
To use this configuration option, your environment must meet the following requirements:
The Go client library must be version v1.91.0 or later.
The Go release must be version 1.25 or later.
Plain-text communication
To establish plain-text communication, run the following code:
import ("google.golang.org/grpc/credentials/insecure")
adminClient, err := database.NewDatabaseAdminClient(ctx,
option.WithEndpoint(OMNI_ENDPOINT),
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
)
clientConfig := ClientConfig{
IsExperimentalHost: true,
}
databaseClient, err := spanner.NewClientWithConfig(ctx, db,
clientConfig,
option.WithEndpoint(OMNI_ENDPOINT),
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
)
TLS connection
To establish a TLS connection, run the following code:
func createClients(ca_certificate, database, omniEndpoint string){
// TLS CA cert configuration
caCert, err := os.ReadFile(ca_certificate)
capool := x509.NewCertPool()
capool.AppendCertsFromPEM(caCert)
creds := credentials.NewTLS(&tls.Config{RootCAs: capool})
adminClient, err := database.NewDatabaseAdminClient(ctx,
option.WithEndpoint(omniEndpoint),
option.WithGRPCDialOption(grpc.WithTransportCredentials(creds)),
option.WithoutAuthentication(),
)
clientConfig := ClientConfig{
IsExperimentalHost: true,
}
databaseClient, err := spanner.NewClientWithConfig(ctx, db,
clientConfig,
option.WithEndpoint(omniEndpoint),
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(creds)),
)
}
mTLS connection
To establish an mTLS connection, run the following code:
func createClients(ca_certificate, client_certificate, client_key, database, omniEndpoint string){
// mTLS cred configuration
caCert, err := os.ReadFile(ca_certificate)
capool := x509.NewCertPool()
capool.AppendCertsFromPEM(caCert)
cert := tls.LoadX509KeyPair(client_certificate, client_key)
creds := credentials.NewTLS(&tls.Config{Certificates: []tls.Certificate{cert}, RootCAs: capool})
adminClient, err := database.NewDatabaseAdminClient(ctx,
option.WithEndpoint(omniEndpoint),
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(creds))
)
clientConfig := ClientConfig{
IsExperimentalHost: true,
}
databaseClient, err := spanner.NewClientWithConfig(ctx, db,
clientConfig,
option.WithEndpoint(omniEndpoint),
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(creds)),
)
}