Skip to main content

Database Access with AlloyDB

Report an Issue

Teleport can provide secure access to AlloyDB via the Teleport Database Service. This allows for fine-grained access control through Teleport's RBAC.

In this guide, you will:

  1. Configure your AlloyDB database with a service account.
  2. Add the database to your Teleport cluster.
  3. Connect to the database via Teleport.

How it works

The Teleport Database Service uses IAM authentication to communicate with AlloyDB. When a user connects to the database via Teleport, the Teleport Database Service obtains Google Cloud credentials and authenticates to Google Cloud as an IAM principal with permissions to access the database.

Prerequisites

  • A running Teleport cluster. If you want to get started with Teleport, sign up for a free trial or set up a demo environment.

  • The tctl and tsh clients.

    Installing tctl and tsh clients
    1. Determine the version of your Teleport cluster. The tctl and tsh clients must be at most one major version behind your Teleport cluster version. Send a GET request to the Proxy Service at /v1/webapi/find and use a JSON query tool to obtain your cluster version. Replace teleport.example.com:443 with the web address of your Teleport Proxy Service:

      TELEPORT_DOMAIN=teleport.example.com:443
      TELEPORT_VERSION="$(curl -s https://$TELEPORT_DOMAIN/v1/webapi/find | jq -r '.server_version')"
    2. Follow the instructions for your platform to install tctl and tsh clients:

      Download the signed macOS .pkg installer for Teleport, which includes the tctl and tsh clients:

      curl -O https://cdn.teleport.dev/teleport-${TELEPORT_VERSION?}.pkg

      In Finder double-click the pkg file to begin installation.

      danger

      Using Homebrew to install Teleport is not supported. The Teleport package in Homebrew is not maintained by Teleport and we can't guarantee its reliability or security.

  • Google Cloud account with an AlloyDB cluster and instance deployed, configured for IAM database authentication.
  • psql installed and in your system PATH.
  • A host (e.g., a Compute Engine instance) to run the Teleport Database Service.
  • To check that you can connect to your Teleport cluster, sign in with tsh login, then verify that you can run tctl commands using your current credentials. For example, run the following command, assigning teleport.example.com to the domain name of the Teleport Proxy Service in your cluster and [email protected] to your Teleport username:
    tsh login --proxy=teleport.example.com --user=[email protected]
    tctl status

    Cluster teleport.example.com

    Version 19.0.0-dev

    CA pin sha256:abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678

    If you can connect to the cluster and run the tctl status command, you can use your current credentials to run subsequent tctl commands from your workstation. If you host your own Teleport cluster, you can also run tctl commands on the computer that hosts the Teleport Auth Service for full permissions.

Step 1/4. Configure IAM and create a database user

In this step, you will create two required service accounts.

  • teleport-db-service: used by the Teleport Database Service to access AlloyDB metadata and generate tokens.
  • alloydb-user: used by end-users to authenticate to the database.

Create a service account for the Teleport Database Service

Go to Service Accounts and create a service account named teleport-db-service. Assign the predefined roles/alloydb.client role.

Create the database user account

note

If you already have a GCP service account for database access with the required roles, you can use it instead.

Go to Service Accounts and create a service account named alloydb-user. Assign these roles:

Then, on the alloydb-user overview page, go to the "Principals with Access" tab, click "Grant Access", and add teleport-db-service with the Service Account Token Creator role.

Add the IAM database user to AlloyDB

note

Skip this if your AlloyDB instance already has an IAM user for this service account.

Ensure IAM authentication is enabled on your instance (the alloydb.iam_authentication flag must be set) before adding the User.

Instance Restart Required

Enabling the static alloydb.iam_authentication flag triggers a mandatory restart of the AlloyDB instance. This will cause a brief period of downtime (typically 60 - 120 seconds) as the instance performs a maintenance update. We recommend performing this during a scheduled maintenance window.

  1. Go to the AlloyDB Clusters page.
  2. Click Edit on your primary-instance, scroll to Advanced configuration options, and under Flags ensure alloydb.iam_authentication is present and set to on.
  3. Go to the Users page of your AlloyDB instance.
  4. Click Add User Account.
  5. Choose Cloud IAM authentication.
  6. In the Principal field, enter alloydb-user@project-id.iam.

Step 2/4. Create a Teleport Database Service host

The Teleport Database Service must run on a host that can reach the AlloyDB instance and authenticate with GCP.

note

If you already have a host running the Teleport Database Service with the teleport-db-service credentials, skip to Step 3.

Create a GCE instance and attach the teleport-db-service service account in the "Identity and API access" section.

Attaching the service account to an existing GCE instance
  1. Navigate to VM instances and open your instance.
  2. Stop the instance.
  3. Edit the instance, find Service account under Identity and API access, and select teleport-db-service.
  4. Save and restart.

If the Database Service is running outside of GCE, use workload identity federation to provide credentials.

Using service account keys (not recommended for production)

Create a JSON key for the teleport-db-service account. If you use systemd to start Teleport, add the environment variable to the service's EnvironmentFile:

echo 'GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json' | \sudo tee -a /etc/default/teleport
warning

Service account keys are a security risk. Use workload identity or attached service accounts in production. See Google Cloud authentication docs for details.

Step 3/4. Configure Teleport

In this step, you will configure the Teleport Database Service to connect to AlloyDB and handle authentication and access on behalf of users.

Install the Teleport Database Service

To install a Teleport Agent on your Linux server:

The recommended installation method is the cluster install script. It will select the correct version, edition, and installation mode for your cluster.

  1. Assign teleport.example.com:443 to your Teleport cluster hostname and port, but not the scheme (https://).

  2. Run your cluster's install script:

    curl "https://teleport.example.com:443/scripts/install.sh" | sudo bash

Create a join token

The Database Service requires a valid join token to join your Teleport cluster. Run the following tctl command and save the token output in /tmp/token on the server that will run the Database Service:

tctl tokens add --type=db --format=text
abcd123-insecure-do-not-use-this

Configure and start the Database Service

In the command below, replace teleport.example.com:443 with the host and port of your Teleport Proxy Service or Enterprise Cloud site, and replace connection-uri with your AlloyDB connection URI.

The connection URI has the format projects/PROJECT-ID/locations/REGION/clusters/CLUSTER/instances/INSTANCE. You can copy it from the AlloyDB instance details page in the Google Cloud console.

Run the command as follows. Make sure to include the mandatory alloydb:// prefix in the specified URI.

sudo teleport db configure create \ -o file \ --name=alloydb \ --protocol=postgres \ --labels=env=dev \ --token=/tmp/token \ --proxy=teleport.example.com:443 \ --uri=alloydb://connection-uri

By default, Teleport uses the private AlloyDB endpoint. To use a public or Private Service Connect (PSC) endpoint instead, set endpoint_type in the config:

db_service:
  resources:
    - name: alloydb
      protocol: postgres
      uri: alloydb://projects/PROJECT-ID/locations/REGION/clusters/CLUSTER/instances/INSTANCE
      gcp:
        alloydb:
          endpoint_type: public  # private | public | psc

Create alloydb.yaml:

kind: db
version: v3
metadata:
  name: alloydb-dynamic
  labels:
    env: dev
spec:
  protocol: "postgres"
  uri: "alloydb://connection-uri"
  gcp:
    alloydb:
      endpoint_type: private

Apply it:

tctl create -f alloydb.yaml

Start the Database Service:

Configure the Teleport Database Service to start automatically when the host boots up by creating a systemd service for it. The instructions depend on how you installed the Teleport Database Service.

On the host where you will run the Teleport Database Service, enable and start Teleport:

sudo systemctl enable teleport
sudo systemctl start teleport

You can check the status of the Teleport Database Service with systemctl status teleport and view its logs with journalctl -fu teleport.

Step 4/4. Connect to your database

You will only be able to see databases that your Teleport role has access to. See our RBAC guide for more details.

tip

To modify an existing user to provide access to the Database Service, see Database Access Controls

Create a local Teleport user with the built-in access role:

tctl users add \ --roles=access \ --db-users="*" \ --db-names="*" \ alice
FlagDescription
--rolesList of roles to assign to the user. The builtin access role allows them to connect to any database server registered with Teleport.
--db-usersList of database usernames the user will be allowed to use when connecting to the databases. A wildcard allows any user.
--db-namesList of logical databases (aka schemas) the user will be allowed to connect to within a database server. A wildcard allows any database.
warning

Database names are only enforced for PostgreSQL, MongoDB, and Cloud Spanner databases.

For more detailed information about database access controls and how to restrict access see RBAC documentation.

Log in and list databases:

tsh login --proxy=teleport.example.com --user=alice
tsh db ls
Name Description Labels ------- ----------- ------- alloydb GCP AlloyDB env=dev

The database user name is shown on the Users page of your AlloyDB instance. Connect using the service account name (minus .gserviceaccount.com):

tsh db connect --db-user=alloydb-user@project-id.iam --db-name=postgres alloydb
tip

From version 17.1, you can also connect via the Web UI.

To log out:

tsh db logout alloydb

Or for all databases:

tsh db logout

Optional: least-privilege access

When possible, enforce least-privilege by defining custom IAM roles that grant only the required permissions.

Custom role for the Teleport Database Service

The Teleport Database Service, running as the teleport-db-service service account, needs permissions to access the AlloyDB instance.

Create a custom role with the following permissions:

# Used to generate client certificate
alloydb.clusters.generateClientCertificate
# Used to fetch connection information
alloydb.instances.connect

For impersonating the alloydb-user service account, the built-in "Service Account Token Creator" IAM role is broader than necessary. To restrict permissions for that service account, create a custom role that includes only:

iam.serviceAccounts.getAccessToken

Custom role for the database user

The alloydb-user service account used for database access requires permissions to connect to the instance and authenticate as a database user. Create a custom role with:

alloydb.instances.connect
alloydb.users.login
serviceusage.services.use

Troubleshooting

Checkpoint: Unable to connect to the database

Confirm that you can access the database without errors.

Next steps