Teleport
Database Access with Cloud Spanner
- Edge version
- Version 17.x
- Version 16.x
- Version 15.x
- Older Versions
Teleport can provide secure access to Cloud Spanner via the Teleport Database Service. This allows for fine-grained access control through Teleport's RBAC.
In this guide, you will:
- Configure your Cloud Spanner database with a service account.
- Add the database to your Teleport cluster.
- Connect to the database via Teleport.
How it works
The Teleport Database Service uses IAM authentication to communicate with Spanner. 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 version 16.4.8 or above. If you want to get started with Teleport, sign up for a free trial or set up a demo environment.
-
The
tctl
admin tool andtsh
client tool.Visit Installation for instructions on downloading
tctl
andtsh
.
- Google Cloud account
- Google Cloud Spanner database.
- A host where you will run the Teleport Database Service, e.g. a Compute Engine instance.
- To check that you can connect to your Teleport cluster, sign in with
tsh login
, then verify that you can runtctl
commands using your current credentials. For example:If you can connect to the cluster and run thetsh login --proxy=teleport.example.com --user=[email protected]tctl statusCluster teleport.example.com
Version 16.4.8
CA pin sha256:abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678
tctl status
command, you can use your current credentials to run subsequenttctl
commands from your workstation. If you host your own Teleport cluster, you can also runtctl
commands on the computer that hosts the Teleport Auth Service for full permissions.
Step 1/8. Create a service account for the Teleport Database Service
A GCP service account will be used by the Teleport Database Service to create ephemeral access tokens for other GCP service accounts when it's acting on the behalf of authorized Teleport users.
Create a service account
Go to the Service Accounts page and create a service account:
Ignore the optional steps - click "Done". We will grant permissions to this service account later in this guide.
Step 2/8. Create a service account for a database user
When a user connects to Spanner via Teleport, they will specify a service account name as the database user and the Teleport Database service will impersonate that service account to authenticate to GCP.
You can create multiple service accounts to provide different access levels to Teleport users, but for this guide we will just create one.
Create a service account
Go to the IAM & Admin Service Accounts page and create a new service account named "spanner-user":
Ignore the optional steps - just click "Done". Rather than granting access at the project level, we will grant this service account permissions to just the databases it will be used to access.
Grant permissions
Navigate to the Spanner instance overview page and check the box of your Spanner instance, then click "Permissions".
In the permissions blade, click "Add Principal" then add the "spanner-user" service account as a principal and assign it the "Cloud Spanner Database User" role:
Click "Save".
Cloud Spanner Database User is a pre-defined role. You can use a different pre-defined role or create and customize your own role permissions with custom IAM roles.
Grant access to the service account
The Teleport Database Service must be able to impersonate this service account. Navigate to the "spanner-user" service account overview page and select the "permissions" tab:
Click "Grant Access" and add the "teleport-db-service" principal ID. Select the "Service Account Token Creator" role and save the change:
The "Service Account Token Creator" IAM role includes more permissions than the Database Service needs. To further restrict the service account, you can create a role that includes only the following permission:
# Used to generate IAM auth tokens when connecting to a database instance.
iam.serviceAccounts.getAccessToken
Step 3/8. Install Teleport
Install Teleport on your Linux server:
-
Assign edition to one of the following, depending on your Teleport edition:
Edition Value Teleport Enterprise Cloud cloud
Teleport Enterprise (Self-Hosted) enterprise
Teleport Community Edition oss
-
Get the version of Teleport to install. If you have automatic agent updates enabled in your cluster, query the latest Teleport version that is compatible with the updater:
TELEPORT_DOMAIN=example.teleport.comTELEPORT_VERSION="$(curl https://$TELEPORT_DOMAIN/v1/webapi/automaticupgrades/channel/default/version | sed 's/v//')"Otherwise, get the version of your Teleport cluster:
TELEPORT_DOMAIN=example.teleport.comTELEPORT_VERSION="$(curl https://$TELEPORT_DOMAIN/v1/webapi/ping | jq -r '.server_version')" -
Install Teleport on your Linux server:
curl https://cdn.teleport.dev/install-v16.4.8.sh | bash -s ${TELEPORT_VERSION} editionThe installation script detects the package manager on your Linux server and uses it to install Teleport binaries. To customize your installation, learn about the Teleport package repositories in the installation guide.
Step 4/8. Configure the Teleport Database Service
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=textabcd123-insecure-do-not-use-this
Provide the following information and then generate a configuration file for the Teleport Database Service:
- example.teleport.sh:443 The host and port of your Teleport Proxy Service or cloud-hosted Teleport Enterprise site
- project-id The GCP project ID. You can normally see it in the organization view at the top of the GCP dashboard.
- instance-id The name of your Cloud SQL instance.
sudo teleport db configure create \ -o file \ --name=spanner-example \ --protocol=spanner \ --labels=env=dev \ --token=/tmp/token \ --uri=spanner.googleapis.com:443 \ --proxy=example.teleport.sh:443 \ --gcp-project-id=project-id \ --gcp-instance-id=instance-id
Step 5/8. Configure GCP credentials
The Teleport Database Service must have credentials for the "teleport-db-service" GCP service account.
If the Teleport Database Service is hosted on a GCE instance, you can change the attached service account. For non-GCE deployments of Teleport, we recommend using workload identity.
Alternatively, go to that service account's Keys tab and create a new key:
Make sure to choose JSON format:
Save the file. Set the GOOGLE_APPLICATION_CREDENTIALS
environment variable to
point to the JSON credentials file you downloaded earlier. For example, if you
use systemd
to start teleport
, then you should edit the service's
EnvironmentFile
to include the env var:
echo 'GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json' | sudo tee -a /etc/default/teleport
A service account key can be a security risk - we only describe using a key in this guide for simplicity. We do not recommend using service account keys in production. See authentication in the Google Cloud documentation for more information about service account authentication methods.
Step 6/8. Start the Teleport 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 teleportsudo systemctl start teleport
On the host where you will run the Teleport Database Service, create a systemd service configuration for Teleport, enable the Teleport service, and start Teleport:
sudo teleport install systemd -o /etc/systemd/system/teleport.servicesudo systemctl enable teleportsudo 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 7/8. Create a Teleport user
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
Create a local Teleport user with the built-in access
and requester
roles:
tctl users add \ --roles=access,requester \ --db-users="*" \ --db-names="*" \ alice
Flag | Description |
---|---|
--roles | List of roles to assign to the user. The builtin access role allows them to connect to any database server registered with Teleport. |
--db-users | List of database usernames the user will be allowed to use when connecting to the databases. A wildcard allows any user. |
--db-names | List of logical databases (aka schemas) the user will be allowed to connect to within a database server. A wildcard allows any database. |
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.
Step 8/8. Connect
Once the Teleport Database Service has joined the cluster, log in to see the available databases:
tsh login --proxy=teleport.example.com --user=example-usertsh db lsName Description Allowed Users Labels Connect --------------- ----------------- ------------- ------- ------- spanner-example GCP Cloud Spanner [*] env=dev
tsh login --proxy=mytenant.teleport.sh --user=example-usertsh db lsName Description Allowed Users Labels Connect --------------- ----------------- ------------- ------- ------- spanner-example GCP Cloud Spanner [*] env=dev
You will only be able to see databases that your Teleport role has access to. See our RBAC guide for more details.
When connecting to the database, use the name of the service account that you created for a database user above, (minus the "@project-id.iam.gserviceaccount.com" suffix). Retrieve credentials for a database in the Cloud Spanner instance and connect to it:
tsh db connect --db-user=spanner-user --db-name=example-db spanner-example
To log out of the database and remove credentials:
Remove credentials for a particular database instance:
tsh db logout spanner-exampleOr remove credentials for all databases:
tsh db logout
Troubleshooting
Could not find default credentials
This error can come from either your client application or Teleport.
For a client application, ensure that you disable GCP credential loading. Your client should not attempt to load credentials because GCP credentials will be provided by the Teleport Database Service.
If you see the credentials error message in the Teleport Database Service logs (at DEBUG log level), then the Teleport Database Service does not have GCP credentials configured correctly.
If you are using a service account key, then ensure that the environment
variable
GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
is set and restart
your Teleport Database Service to ensure that the env var is available to
teleport
.
For example, if your Teleport Database Service runs as a systemd
service:
echo 'GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json' | sudo tee -a /etc/default/teleportsudo systemctl restart teleport
See authentication in the Google Cloud documentation for more information about service account authentication methods.
Next steps
- Learn how to restrict access to certain users and databases.
- View the High Availability (HA) guide.
- Take a look at the YAML configuration reference.
- See the full CLI reference.
- Learn how to connect with a GUI client.
- Learn more about authenticating as a service account in Google Cloud.