데이터베이스 접근과 머신 및 워크로드 아이덴티티
Teleport는 데이터베이스에 대한 접근을 보호하고 제어합니다. 이 가이드에서는 Teleport에서 설정된 데이터베이스에 접근하는 데 사용할 수 있는 자격 증명을 생성하도록 tbot을 설정합니다. A running Teleport cluster.
Teleport는 데이터베이스에 대한 접근을 보호하고 제어합니다. 머신 및 워크로드 아이덴티티를 사용하여 머신에 이러한 데이터베이스에 대한 안전하고 단기적인 접근을 허용할 수 있습니다.
이 가이드에서는 Teleport에서 설정된 데이터베이스에 접근하는 데 사용할 수 있는 자격 증명을 생성하도록 tbot을 설정합니다.
사전 조건#
-
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:
-
- 아직 데이터베이스를 Teleport Database Service 뒤에 두지 않았다면 데이터베이스 접근 시작 가이드를 따르세요. Teleport Database Service는 PostgreSQL, MongoDB, Redis 등 다양한 데이터베이스를 지원합니다. 전체 목록은 데이터베이스 접근 가이드를 참조하세요.
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.
- 데이터베이스에 접근할 머신에
tsh바이너리가 설치되어 있어야 합니다.tbot설치 방법에 따라 이미 설치되어 있을 수 있습니다. 설치되어 있지 않은 경우 설치를 참조하세요. - 데이터베이스에 접근할 머신에
tbot이 이미 설치되고 설정되어 있어야 합니다. 자세한 내용은 배포 가이드를 참조하세요.
1단계/4단계. RBAC 설정#
먼저 봇이 생성한 자격 증명이 데이터베이스 서버와 데이터베이스에 접근할 수 있도록 Teleport를 설정해야 합니다. 이는 필요한 권한을 부여하는 역할을 만들고 해당 역할을 봇에 할당하는 방식으로 수행됩니다.
다음 내용으로 role.yaml 파일을 만듭니다:
kind: role
version: v6
metadata:
name: example-role
spec:
allow:
db_labels:
'*': '*'
db_names: [example-db]
db_users: [alice]
rules:
- resources: [db_server, db]
verbs: [read, list]
교체:
example-role을 사용 사례와 관련된 설명적인 이름으로.example-db를 봇이 접근할 데이터베이스 이름으로.alice를 봇이 데이터베이스 연결 시 사용할 사용자 이름으로.
tctl create -f ./role.yaml을 사용하여 역할을 만듭니다.
이제 tctl bots update를 사용하여 봇에 역할을 추가합니다. example을 배포 가이드에서 만든 봇의 이름으로, example-role을 방금 만든 역할의 이름으로 교체하세요:
$ tctl bots update example --add-roles example-role
이 규칙은 봇이 두 가지를 할 수 있게 합니다:
- (
'*': '*'레이블 선택기로 인해) 모든 데이터베이스 서버에서alice사용자로example-db데이터베이스에 접근. - Teleport에서 데이터베이스 리소스에 대한 정보 검색.
'*': '*' 레이블 선택기는 Teleport에서 설정된 모든 데이터베이스 서버에 대한 접근을 허용합니다. 프로덕션에서는 더 구체적인 레이블 선택기를 사용하여 봇의 접근을 제한하는 것을 고려하세요. 데이터베이스 관련 역할 옵션의 전체 레퍼런스는 데이터베이스 접근 RBAC 가이드를 참조하세요.
2단계/4단계. 데이터베이스 tbot 출력 서비스 설정#
이제 데이터베이스 접근에 필요한 자격 증명을 생성할 출력 서비스로 tbot을 설정해야 합니다. 이를 위해 database 출력 서비스 유형이 사용됩니다.
자격 증명을 생성할 데이터베이스는 database 서비스의 일부로 설정됩니다. 세 가지 필드를 사용하여 제어됩니다:
service는 자격 증명이 접근 권한을 부여할 Teleport 설정에서의 Database Service 이름을 지정합니다.database는 자격 증명이 접근 권한을 부여할 Database Service의 데이터베이스를 지정합니다.username은 자격 증명이 접근 권한을 부여할 데이터베이스의 사용자를 지정합니다. 이 필드는 모든 종류의 데이터베이스에 필요하지 않습니다.
또한 데이터베이스 출력 서비스의 format 필드는 생성된 자격 증명의 형식을 제어합니다. 이를 통해 특정 형식을 기대하는 클라이언트와의 호환성을 제공합니다. 이 필드가 지정되지 않으면 대부분의 클라이언트와 호환되는 기본 옵션이 사용됩니다. 지원되는 format 옵션의 전체 목록은 아래와 같습니다:
| 클라이언트 | format |
설명 |
|---|---|---|
| 기본 | 미지정 | tlscert에 인증서, key에 개인 키, teleport-database-ca.crt에 CA를 제공합니다. 대부분의 클라이언트와 호환됩니다. |
| MongoDB | mongo |
mongo.crt 및 mongo.cas를 제공합니다. |
| CockroachDB | cockroach |
cockroach/node.key, cockroach/node.crt, cockroach/ca.crt를 제공합니다. |
| 일반 TLS | tls |
특정 파일 확장자를 필요로 하는 일반 클라이언트를 위해 tls.key, tls.crt, tls.cas를 제공합니다. |
출력 서비스는 대상으로 설정해야 합니다. 이 예시에서는 directory 대상을 사용합니다. 이는 아티팩트를 디스크의 지정된 디렉터리에 기록합니다. tbot이 실행되는 Linux 사용자가 이 디렉터리에 쓸 수 있고 애플리케이션에 접근하는 Linux 사용자가 읽을 수 있도록 하세요.
tbot 설정을 수정하여 database 서비스를 추가합니다:
services:
- type: database
destination:
type: directory
path: /opt/machine-id
# 연결할 데이터베이스의 세부 정보를 지정합니다.
service: example-server
database: example-db
username: alice
# 출력 자격 증명에 사용할 형식을 지정합니다. 대부분의 데이터베이스의 경우
# 이 설정 필드를 생략할 수 있습니다.
# format: mongo
tbot을 백그라운드 서비스로 실행 중인 경우 재시작하세요. 원샷 모드로 실행 중이라면 데이터베이스에 접근하기 전에 실행해야 합니다.
3단계/4단계. 로컬 데이터베이스 접근 프록시 설정#
tbot이 데이터베이스 접근 자격 증명을 생성하면 데이터베이스 클라이언트에서 TLS 연결을 통해 Teleport Proxy Service로 데이터베이스 연결을 전달하는 로컬 프록시를 설정해야 합니다. TLS 연결을 통해 Teleport Proxy Service가 프로토콜과 의도된 수신자를 식별할 수 있으므로 이 작업이 필요합니다.
로컬 프록시는 클라이언트가 데이터베이스에 연결해야 하는 동안 또는 연결이 아직 열려 있는 동안 실행되어야 합니다. 이 로컬 프록시를 백그라운드에서 실행하는 한 가지 방법은 systemd 서비스를 사용하는 것입니다. 이 단계의 나머지에서 이를 보여주지만 다른 서비스 관리자를 사용하거나 클라이언트가 실행 중인 동안 로컬 프록시를 실행하는 다른 방법을 사용할 수 있습니다.
로컬 프록시는 로컬 루프백 인터페이스의 지정된 포트를 엽니다. 클라이언트는 그런 다음 localhost의 이 포트에 연결하도록 설정해야 합니다. 포트가 로컬 루프백 인터페이스에서 열리므로 로컬 프록시는 데이터베이스에 연결하려는 클라이언트와 동일한 호스트에서 실행해야 합니다.
기본적으로 데이터베이스 클라이언트는 로컬 포트에 연결할 때도 자격 증명을 사용하도록 설정해야 합니다. 이렇게 하면 호스트의 다른 사용자가 로컬 포트를 통해 데이터베이스에 접근하지 못하고, 데이터베이스 클라이언트와 서버 간의 연결이 localhost를 통해서도 암호화되지 않은 상태가 되지 않습니다.
이 목적을 위한 systemd 서비스를 만들려면 /etc/systemd/system/tbot-db-proxy.service에 유닛 파일을 만듭니다:
[Unit]
Description=Teleport 머신 및 워크로드 아이덴티티 Proxy Service
After=network.target
# 이전 가이드를 따라 tbot 자체를 systemd 서비스로 설정했다면
# 두 서비스 간의 의존성을 만들기 위해 다음 줄의 주석을 제거하세요.
# Requires=tbot.service
[Service]
Type=simple
# teleport 사용자/그룹이 존재하고 대상 디렉터리에 대한 읽기 접근이 있는지 확인하세요.
User=teleport
Group=teleport
Restart=always
RestartSec=5
# `12345`를 로컬 루프백 인터페이스에서 비어 있는 원하는 포트로 조정합니다.
# `example-server`를 teleport의 Database Service 이름으로 조정합니다.
ExecStart=/usr/local/bin/tbot -c /etc/tbot.yaml proxy --proxy=proxy.example.com:3080 --destination-dir=/opt/machine-id db --port=12345 example-server
ExecReload=/bin/kill -HUP $MAINPID
PIDFile=/run/tbot-db-proxy.pid
LimitNOFILE=8192
[Install]
WantedBy=multi-user.target
이렇게 하면 example-server 데이터베이스 서버에 연결하는 데 사용할 수 있는 포트 12345의 로컬 프록시가 시작됩니다. 로컬 설정에 맞게 tbot 파라미터를 커스터마이즈하세요.
마지막으로 다음 명령을 실행하여 로컬 프록시 서비스를 활성화하고 시작합니다:
$ sudo systemctl enable tbot-db-proxy
$ sudo systemctl start tbot-db-proxy
$ sudo systemctl status tbot-db-proxy
인증된 터널#
기본 동작에서는 클라이언트가 클라이언트 인증서 인증을 사용해야 하지만, 인증된 터널을 설정할 수 있습니다. 이는 로컬 포트로의 모든 수신 연결에 자격 증명을 자동으로 첨부합니다. 덜 안전하지만 데이터베이스와 함께 사용하려는 클라이언트가 클라이언트 인증서 인증을 지원하지 않는 경우 필요할 수 있습니다.
인증된 터널 모드를 활성화하려면 tbot proxy db...와 함께 --tunnel 플래그를 사용합니다.
포그라운드에서 실행하는 경우 --tunnel 플래그를 제공하세요. systemd 서비스를 사용하는 경우 machine-id-proxy.service의 ExecStart에 --tunnel을 추가한 후 유닛을 다시 로드합니다.
이 기능이 활성화되면 설정된 포트에 연결할 때 클라이언트에서 비밀번호나 TLS 인증서 및 인증 기관을 지정할 필요가 없습니다.
4단계/4단계. 데이터베이스에 연결하도록 클라이언트 설정#
자격 증명이 생성되고 로컬 프록시가 실행 중이면 이제 자격 증명과 함께 로컬 프록시를 사용하도록 클라이언트를 설정할 수 있습니다.
자격 증명 사용에 대한 샘플 Go 프로그램을 참조하세요:
<!-- INCLUDE:ENGLISH:docs/pages/includes/machine-id/postgresql/postgresql.go -->
// This example program demonstrates how to connect to a Postgres database
// using certificates issued by Teleport Machine & Workload Identity.
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/jackc/pgx/v4/stdlib"
)
func main() {
// Open connection to database.
db, err := sql.Open("pgx", fmt.Sprint(
"host=localhost ",
"port=1234 ",
"dbname=example ",
"user=alice ",
// The next four options should be omitted if the local proxy has been
// placed in "authenticated tunnel" mode.
"sslmode=verify-full ",
"sslrootcert=/opt/machine-id/teleport-host-ca.crt ",
"sslkey=/opt/machine-id/key ",
"sslcert=/opt/machine-id/tlscert ",
))
if err != nil {
log.Fatalf("Failed to open database: %v.", err)
}
defer db.Close()
// Call "Ping" to test connectivity.
err = db.Ping()
if err != nil {
log.Fatalf("Failed to Ping database: %v.", err)
}
log.Printf("Successfully connected to PostgreSQL.")
}
<!-- /INCLUDE:ENGLISH -->
<!-- INCLUDE:ENGLISH:docs/pages/includes/machine-id/mongodb/mongodb.go -->
// This example program demonstrates how to connect to a MongoDB database
// using certificates issued by Teleport Machine & Workload Identity.
package main
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Create client and connect to MongoDB. Make sure to modify the host,
// port, and certificate paths.
uri := fmt.Sprintf(
"mongodb://localhost:1234/?tlsCAFile=%s&tlsCertificateKeyFile=%s",
"/opt/machine-id/mongo.cas",
"/opt/machine-id/mongo.crt",
)
client, err := mongo.NewClient(options.Client().ApplyURI(uri))
if err != nil {
log.Fatalf("Failed to create database client: %v.", err)
}
err = client.Connect(ctx)
if err != nil {
log.Fatalf("Failed to connect to database: %v.", err)
}
defer client.Disconnect(ctx)
log.Printf("Successfully connected to MongoDB.")
// List databases to test connectivity.
databases, err := client.ListDatabaseNames(ctx, bson.M{})
if err != nil {
log.Fatalf("Failed to list databases: %v.", err)
}
log.Println(databases)
}
<!-- /INCLUDE:ENGLISH -->
설정이 완료되었습니다. 애플리케이션에 순환, 감사, 익숙한 모든 Teleport 접근 제어로 제어할 수 있는 데이터베이스에 접근할 수 있는 머신 아이덴티티에 연결된 단기 인증서를 제공했습니다.
다음 단계#
- 사용 가능한 모든 설정 옵션을 확인하려면 설정 레퍼런스를 읽으세요.
