Kong Gateway Google Cloud Postgres database authentication with GCP IAM and Workload Identity

Uses: Kong Gateway

You can use GCP Identity and Access Management (IAM) and Workload Identity authentication to connect to the Google Cloud Postgres database that you use for Kong Gateway. This page explains how to configure IAM and Workload Identity authentication to secure your database settings and connections.

With authentication enabled, you don’t need a password to connect to a database instance. Instead, you use a temporary authentication token. Because GCP manages the authentication externally, the database doesn’t store user credentials. If you’re using Google Cloud Postgres for Kong Gateway’s database, you can enable authentication on your running cluster. This eliminates the need to store user credentials on both the Kong Gateway (pg_password) and Google Cloud Postgres sides.

GCP authentication limitations

GCP authentication has some limitations. Go through each one before you use this feature in your production environment:

  • This feature cannot be used together with databases from other cloud providers, such as AWS RDS. These auth providers are mutually exclusive.
  • When pg_gcp_auth is enabled, the pg_password won’t be used. You can’t use both methods at the same time.
  • Any incorrect configuration on the GCP side will result in a failure in initializing the database connection, such as an improperly configured managed identity or a missing role inside GCP Postgres.

For additional recommendations and limitations, see the IAM authentication restrictions in the Google Cloud documentation.

Enabling GCP authentication

You can enable GCP authentication through an environment variable or the Kong Gateway configuration file. You can enable it for both read-write and read-only modes, or for read-only mode only.

Note: When GCP authentication is enabled, Kong Gateway ignores the corresponding password configurations. If authentication is enabled only for read-only mode, the read-write settings—such as pg_user and pg_password—remain unaffected and continue to function as usual.

Configuring your GCP resources

Before you enable GCP authentication, you must configure your Google Cloud Postgres database and the IAM role or Workload Identity that Kong Gateway uses.

Configuring GCP authentication in Kong Gateway

Before you enable GCP authentication, you must do the following in the kong.conf file:

  • Remove pg_password or pg_ro_password.
  • Check that pg_user or pg_ro_user matches the username you defined in the IAM policy and created in the Postgres RDS database.

FAQs

If Kong Gateway reports an error when connecting to Cloud SQL PostgreSQL, it indicates that the IAM (service account) PostgreSQL user needs public permissions.

You need to connect as a user with the ability to grant privileges. Usually, this is the Postgres built-in user. Run a SQL command like the following to grant privileges for the IAM user:

-- allow usage of public schema
GRANT USAGE ON SCHEMA public TO "service-account-name@project-name.iam";
-- allow creating tables in public schema
GRANT CREATE ON SCHEMA public TO "service-account-name@project-name.iam";
Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!