Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When connecting to Google Cloud Spanner Emulator with jdbc, credentials that should not be needed are required

When I try to connect using the Google Cloud Spanner Open Source JDBC Driver, I get the following error message:

The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more

URI:jdbc:cloudspanner:localhost:9010/projects/my-project/instances/my-instance/databases/my-database

I don't think the emulator needs any credentials to work locally. Is there a way around this error? Thank you.

like image 575
Ryosuke Sugisawa Avatar asked Oct 18 '25 22:10

Ryosuke Sugisawa


2 Answers

You are correct that the emulator does not require any credentials. The reason that you are getting this error, is that the JDBC driver does not know that you are trying to connect to the emulator. Instead, it thinks you are trying to connect to some server at localhost:9010, which could be the official emulator or any other emulator or mock server.

[EDIT 2024-06-09: Added the recommended autoConfigEmulator=true flag]

There are three ways to connect to the emulator:

  1. Recommended: Add autoConfigEmulator=true to the JDBC connection URL. This instructs the JDBC driver to use the default settings for using the Spanner emulator, such as no credentials and plain text, and additionally also creates any instance and database that you reference in your connection string. This removes the requirement to first create the instance and database on the emulator before connecting to them.
  2. Setting the SPANNER_EMULATOR_HOST environment variable to the address of the emulator. In this case that would be localhost:9010. When this environment variable is set, the JDBC driver knows that you are trying to connect to the emulator and will ensure that no credentials are required or used. You can omit the localhost:9010 part from the JDBC connection URL when you set the SPANNER_EMULATOR_HOST environment variable.
  3. Using a specific host name in the JDBC connection URL. This is the method that you are trying to use in your example. In order to instruct the JDBC driver not to use any credentials, you need to include usePlainText=true in the connection URL. This will tell the JDBC driver to create a plain text connection (i.e. no SSL) and not to use any credentials.

The following is the recommended way to connect to the emulator using JDBC:

// The autoConfigEmulator=true property instructs the JDBC driver to
// configure the connection for the emulator, and to create any
// instance or database in the connection string that does
// not yet exist.
try (Connection connection = DriverManager.getConnection(
      "jdbc:cloudspanner://localhost:9010/projects/test-project/instances/test-instance/databases/test-db;autoConfigEmulator=true")) {
  try (ResultSet rs = connection.createStatement().executeQuery("SELECT 1")) {
    while (rs.next()) {
      System.out.printf("%d%n", rs.getLong(1));
     }
  }
}

The following is a valid example of how to connect to the emulator by specifying a host name in the URL:

try (Connection connection = DriverManager.getConnection(
      "jdbc:cloudspanner://localhost:9010/projects/test-project/instances/test-instance/databases/test-db;usePlainText=true")) {
  try (ResultSet rs = connection.createStatement().executeQuery("SELECT 1")) {
    while (rs.next()) {
      System.out.printf("%d%n", rs.getLong(1));
     }
  }
}

like image 90
Knut Olav Løite Avatar answered Oct 22 '25 06:10

Knut Olav Løite


It seems the statement on the google documentation is bit confusing.

On page: https://cloud.google.com/spanner/docs/emulator

Once SPANNER_EMULATOR_HOST is set, you can test the emulator by following the Getting Started guides below. You can ignore the instructions related to project creation, authentication, and credentials since these are not needed to use the emulator.

On the starter guide: https://cloud.google.com/spanner/docs/getting-started/java

Before you begin

Complete the steps described in Set up, which cover creating and setting a default Google Cloud project, enabling billing, enabling the Cloud Spanner API, and setting up OAuth 2.0 to get authentication credentials to use the Cloud Spanner API.

In particular, make sure that you run gcloud auth application-default login to set up your local development environment with authentication credentials.

So I recommend you follow the instructions in the setup guide and create a service account key for your local use and emulator use. Instructions can be found at: https://cloud.google.com/docs/authentication/production

This seems to be working with all emulator tests and Startup guides provided by google.

like image 20
Saurabh Avatar answered Oct 22 '25 05:10

Saurabh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!