Cloud Spanner로 데이터베이스 액세스
Teleport can provide secure access to Cloud Spanner via the Teleport Database Service. In this guide, you will: The Teleport Database Service uses IAM authentication to communicate with Spanner.
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 서비스 계정으로.
- Add the database to your Teleport cluster.
- Connect to the database via Teleport.
작동 방식#
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.


사전 조건#
-
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
tctlandtshclients.Installing `tctl` and `tsh` clients
-
Determine the version of your Teleport cluster. The
tctlandtshclients must be at most one major version behind your Teleport cluster version. Send a GET request to the Proxy Service at/v1/webapi/findand use a JSON query tool to obtain your cluster version. Replace with the web address of your Teleport Proxy Service:$ TELEPORT_DOMAIN= $ TELEPORT_VERSION="$(curl -s https://$TELEPORT_DOMAIN/v1/webapi/find | jq -r '.server_version')" -
Follow the instructions for your platform to install
tctlandtshclients:
-
- Google Cloud 계정
- Google Cloud Spanner 데이터베이스.
- Teleport Database Service를 실행할 호스트(예: Compute Engine 인스턴스).
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 to the domain name of the Teleport Proxy Service in your cluster and to your Teleport username:
$ tsh login --proxy= --user=
$ tctl status
# Cluster (=teleport.url=)
# Version (=teleport.version=)
# CA pin (=presets.ca_pin=)
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.
1/8단계. Teleport Database Service용 서비스 계정 생성#
(!docs/pages/includes/database-access/cloudsql-create-service-account-for-db-service.mdx !)
선택적 단계는 무시하고 "완료"를 클릭합니다. 이 가이드의 후반부에서 이 서비스 계정에 권한을 부여할 것입니다.
2/8단계. 데이터베이스 사용자용 서비스 계정 생성#
사용자가 Teleport를 통해 Spanner에 연결하면 서비스 계정 이름을 데이터베이스 사용자로 지정하고, Teleport Database Service는 해당 서비스 계정을 가장하여 GCP에 인증합니다.
Teleport 사용자에게 서로 다른 액세스 수준을 제공하기 위해 여러 서비스 계정을 생성할 수 있지만, 이 가이드에서는 하나만 생성합니다.
서비스 계정 생성#
IAM & Admin 서비스 계정 페이지로 이동하여 "spanner-user"라는 이름의 새 서비스 계정을 만듭니다:

선택적 단계는 무시하고 "완료"를 클릭합니다. 프로젝트 수준에서 액세스를 부여하는 대신, 이 서비스 계정이 액세스할 데이터베이스에만 권한을 부여합니다.
권한 부여#
Spanner 인스턴스 개요 페이지로 이동하여 Spanner 인스턴스의 체크박스를 선택한 후 "권한"을 클릭합니다.

권한 블레이드에서 "주 구성원 추가"를 클릭한 다음 "spanner-user" 서비스 계정을 주 구성원으로 추가하고 "Cloud Spanner Database User" 역할을 할당합니다:

"저장"을 클릭합니다.
Cloud Spanner Database User는 사전 정의된 역할입니다. 다른 사전 정의된 역할을 사용하거나 커스텀 IAM 역할을 사용하여 자체 역할 권한을 생성하고 맞춤화할 수 있습니다.
서비스 계정에 액세스 권한 부여#
Teleport Database Service는 이 서비스 계정을 가장할 수 있어야 합니다. "spanner-user" 서비스 계정 개요 페이지로 이동하여 "권한" 탭을 선택합니다:

"액세스 권한 부여"를 클릭하고 "teleport-db-service" 주 구성원 ID를 추가합니다. "Service Account Token Creator" 역할을 선택하고 변경 사항을 저장합니다:

"Service Account Token Creator" IAM 역할에는 Database Service에 필요한 것보다 더 많은 권한이 포함되어 있습니다. 서비스 계정을 더욱 제한하려면 다음 권한만 포함하는 역할을 생성할 수 있습니다:
# 데이터베이스 인스턴스에 연결할 때 IAM 인증 토큰을 생성하는 데 사용됩니다.
iam.serviceAccounts.getAccessToken
3/8단계. Teleport 설치#
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.
-
Assign to your Teleport cluster hostname and port, but not the scheme (https://).
-
Run your cluster's install script:
$ curl "https:///scripts/install.sh" | sudo bash
4/8단계. 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=text
(=presets.tokens.first=)
다음 정보를 제공한 후 Teleport Database Service의 구성 파일을 생성합니다:
- Teleport Proxy Service 또는 클라우드 호스팅 Teleport Enterprise 사이트의 호스트 및 포트
- GCP 프로젝트 ID. 일반적으로 GCP 대시보드 상단의 조직 보기에서 확인할 수 있습니다.
- Cloud Spanner 인스턴스의 이름.
$ sudo teleport db configure create \
-o file \
--name=spanner-example \
--protocol=spanner \
--labels=env=dev \
--token=/tmp/token \
--uri=spanner.googleapis.com:443 \
--proxy= \
--gcp-project-id= \
--gcp-instance-id=
5/8단계. GCP 자격 증명 구성#
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.
Using service account keys (insecure)
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
6/8단계. 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.
You can check the status of the Teleport Database Service with systemctl status teleport
and view its logs with journalctl -fu teleport.
7/8단계. Teleport 사용자 생성#
| 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. |
For more detailed information about database access controls and how to restrict access see RBAC documentation.
8/8단계. 연결#
Teleport Database Service가 클러스터에 참여하면 로그인하여 사용 가능한 데이터베이스를 확인합니다:
$ tsh login --proxy=teleport.example.com --user=
$ tsh db ls
Name Description Allowed Users Labels Connect
--------------- ----------------- ------------- ------- -------
spanner-example GCP Cloud Spanner [*] env=dev
$ tsh login --proxy=mytenant.teleport.sh --user=
$ tsh db ls
Name Description Allowed Users Labels Connect
--------------- ----------------- ------------- ------- -------
spanner-example GCP Cloud Spanner [*] env=dev
Teleport 역할이 액세스 권한을 가진 데이터베이스만 볼 수 있습니다. 자세한 내용은 RBAC 가이드를 참조하세요.
데이터베이스에 연결할 때 위에서 데이터베이스 사용자로 생성한 서비스 계정의 이름("@project-id.iam.gserviceaccount.com" 접미사 제외)을 사용하세요. Cloud Spanner 인스턴스의 데이터베이스에 대한 자격 증명을 가져와 연결합니다:
$ tsh db connect --db-user=spanner-user --db-name=example-db spanner-example
이 섹션의 내용은 원문 문서를 참조하세요. (proxy-db-tunnel.mdx)
데이터베이스에서 로그아웃하고 자격 증명을 제거하려면:
# 특정 데이터베이스 인스턴스의 자격 증명 제거:
$ tsh db logout spanner-example
# 또는 모든 데이터베이스의 자격 증명 제거:
$ tsh db logout
문제 해결#
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/teleport
$ sudo systemctl restart teleport
See authentication in the Google Cloud documentation for more information about service account authentication methods.
다음 단계#
-
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.
- GUI 클라이언트로 연결하는 방법을 알아보세요.
- Google Cloud에서 서비스 계정으로 인증하기에 대해 자세히 알아보세요.
