Skip to content

Access token from gcloud auth application-default login expires after inactivity and isn't auto-refreshed by Cloud SQL Proxy #2295

Description

@rvishruth

Bug Description

The Cloud SQL Proxy fails to automatically refresh the OAuth 2.0 access token obtained via gcloud auth application-default login after a period of inactivity. This leads to authentication errors when trying query the database, with the following error:

googleapi: Error 401: Request had invalid authentication credentials.

This issue seems to occur only after prolonged periods (6-12 hours) of inactivity.

Expected behavior: The Cloud SQL Proxy should automatically refresh the access token as needed. (See: https://cloud.google.com/sql/docs/mysql/sql-proxy#benefits_of_the)

Example code (or command)

N/A

Stacktrace

date 10:44:56 [<instance-connection-name>] Accepted connection from 127.0.0.1:52346
date 10:44:56 [<instance-connection-name>] failed to connect to instance: failed to get instance: Refresh error: failed to get instance metadata (connection name = "<instance-connection-name>"): googleapi: Error 401: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
Details:
[
  {
    "@type": "type.googleapis.com/google.rpc.ErrorInfo",
    "metadata": {
      "method": "google.cloud.sql.v1beta4.SqlConnectService.GetConnectSettings",
      "service": "sqladmin.googleapis.com"
    },
    "reason": "ACCESS_TOKEN_TYPE_UNSUPPORTED"
  }
]

More details:
Reason: authError, Message: Invalid Credentials
date 10:44:56 [<instance-connection-name>] Accepted connection from 127.0.0.1:52347
date 10:44:56 [<instance-connection-name>] failed to connect to instance: failed to get instance: Refresh error: failed to get instance metadata (connection name = "<instance-connection-name>"): googleapi: Error 401: Request had invalid authentication credentials.

Steps to reproduce?

  1. Authenticate using gcloud auth application-default login.
  2. Start the Cloud SQL Proxy using ./cloud-sql-proxy.x64.exe --config-file config.toml
  3. Leave the proxy inactive for several hours.
  4. Attempt to reconnect and observe that the proxy encounters a 401 error due to expired credentials.

Environment

  1. OS type and version: Windows 11 Home
  2. Cloud SQL Proxy version (./cloud-sql-proxy --version): cloud-sql-proxy version 2.11.4+windows.amd64
  3. Proxy invocation command (for example, ./cloud-sql-proxy --port 5432 INSTANCE_CONNECTION_NAME): ./cloud-sql-proxy.x64.exe --config-file config.toml

config.toml

# use instance-connection-name-0, instance-connection-name-1, etc.
# for multiple instances
instance-connection-name = "redacted"
auto-iam-authn = true
debug = true
debug-logs = true
port = "5432"

Additional Details

N/A

Metadata

Metadata

Labels

priority: p3Desirable enhancement or fix. May not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions