셀프 호스팅 MongoDB를 사용한 데이터베이스 접근
Teleport can provide secure access to MongoDB via the Teleport Database Service. The Teleport Database Service proxies traffic from database clients to self-hosted databases in your infrastructure.
Teleport can provide secure access to MongoDB via the Teleport Database Service. This allows for fine-grained access control through the Teleport RBAC system.
The Teleport Database Service proxies traffic from database clients to self-hosted databases in your infrastructure. Teleport maintains a certificate authority (CA) for database clients. You configure your database to trust the Teleport database client CA, and the Teleport Database Service presents certificates signed by this CA when proxying user traffic. With this setup, there is no need to store long-lived credentials for self-hosted databases.
Meanwhile, the Teleport Database Service verifies self-hosted databases by checking their TLS certificates against either the Teleport database CA or a custom CA used with the database.
In this guide, you will:
- Configure your MongoDB database for Teleport access.
- Add the database to your Teleport cluster.
- Connect to the database via Teleport.
작동 방식#
The Teleport Database Service authenticates to your self-hosted MongoDB database using mutual TLS. MongoDB trusts the Teleport certificate authority for database clients, and presents a certificate signed by either the Teleport database CA or a custom CA. When a user initiates a database session, the Teleport Database Service presents a certificate signed by Teleport. The authenticated connection then proxies client traffic from the user.


사전 조건#
-
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:
-
-
MongoDB 클러스터(스탠드얼론 또는 레플리카 셋) 버전
<code>[mongodb.min_version]</code>이상.NoteTeleport 데이터베이스 접근은 MongoDB
<code>[mongodb.min_version]</code>이상을 지원합니다. 이전 버전은 테스트되지 않았으며 작동이 보장되지 않습니다. MongoDB<code>[mongodb.min_version]</code>은 2017년 11월에 출시되었으며 2021년 4월에 EOL에 도달했으므로 이전 버전을 사용 중인 경우 업그레이드를 고려하세요.To check that you can connect to your Teleport cluster, sign in with
tsh login, then verify that you can runtctlcommands 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 statuscommand, you can use your current credentials to run subsequenttctlcommands from your workstation. If you host your own Teleport cluster, you can also runtctlcommands on the computer that hosts the Teleport Auth Service for full permissions.- MongoDB 레플리카 셋용 인증 기관 및 해당 CA의 공개 인증서(PEM 형식):
. 스탠드얼론 MongoDB 인스턴스에 대해서도 이 CA를 신뢰하도록 Teleport를 구성할 수 있습니다.
Why do I need my own CA?
Distributed databases like MongoDB Replica Set use mTLS for node-to-node communication. Teleport requires that you have your own CA to issue certificates for node-to-node mTLS communication.Teleport uses a split-CA architecture for database access. The Teleport
dbCA issues server certificates and thedb_clientCA issues client certificates.Databases are configured to trust the Teleport
db_clientCA for client authentication, but not thedbCA. Additionally, Teleport only issues ephemeraldb_clientCA certificates.When a MongoDB Replica Set node connects to another MongoDB Replica Set node, it must present a certificate that the other node trusts for client authentication. Since Teleport does not issue long-lived
db_clientcertificates, the node needs to have a long-lived certificate issued by another CA that its peer node trusts.The split
dbanddb_clientCA architecture was introduced as a security fix in Teleport versions (= db_client_ca.released_version.v14 =) and (= db_client_ca.released_version.v15 =).See Database CA Migrations for more information.
1/3단계. Teleport 설치 및 구성#
Teleport 데이터베이스 서비스 설정#
The Database Service requires a valid join token to join your Teleport cluster. Run the following
tctlcommand and save the token output in/tmp/tokenon the server that will run the Database Service:$ tctl tokens add --type=db --format=text (=presets.tokens.first=)Teleport 데이터베이스 서비스를 실행할 위치에 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
On the host where you will run the Teleport Database Service, start Teleport with the appropriate configuration.
Note that a single Teleport process can run multiple different services, for example multiple Database Service agents as well as the SSH Service or Application Service. The step below will overwrite an existing configuration file, so if you're running multiple services add
--output=stdoutto print the config in your terminal, and manually adjust/etc/teleport.yaml.Generate a configuration file at
/etc/teleport.yamlfor the Database Service:(!docs/pages/includes/start-teleport.mdx service="the Teleport Database Service"!)
Teleport는 Kubernetes 클러스터에 Teleport 데이터베이스 서비스를 설치하기 위한 Helm 차트를 제공합니다.
Configure Helm to fetch Teleport charts from the Teleport Helm repository:
$ helm repo add teleport (=teleport.helm_repo_url=)Refresh the local Helm cache by fetching the latest charts:
$ helm repo updateMake sure that the Teleport Agent pod is running. You should see one
teleport-kube-agentpod with a single ready container:$ kubectl -n teleport-agent get pods NAME READY STATUS RESTARTS AGE teleport-kube-agent-0 1/1 Running 0 32s(!docs/pages/includes/database-access/multiple-instances-tip.mdx !)
단일 연결 주소나 MongoDB 연결 문자열을 URI로 지정할 수 있습니다. 예를 들어 레플리카 셋에 연결할 때:
$ --uri="mongodb://mongo1.example.com:27017,mongo2.example.com:27017/?replicaSet=rs0"기본적으로 Teleport는 기본 레플리카 셋 멤버에 연결합니다. 대신 세컨더리에 연결하려면 Teleport가
readPreference연결 문자열 설정을 따릅니다:$ --uri="mongodb://mongo1.example.com:27017,mongo2.example.com:27017/?replicaSet=rs0&readPreference=secondary"Teleport 사용자 생성#
Flag Description --rolesList of roles to assign to the user. The builtin accessrole 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. For more detailed information about database access controls and how to restrict access see RBAC documentation.
If you opt for a stricter selection of database names for your user, which differs from the wildcard approach illustrated in this guide, it is essential to include the
admindatabase. This ensures MongoDB clients won't have issues while connecting and executing operations such as retrieving server information, listing databases, and aborting transactions.2/3단계. MongoDB 구성#
MongoDB 사용자 생성#
Teleport는 MongoDB 인스턴스에 연결할 때 X.509 인증을 사용합니다. 클라이언트 인증서로 인증하는 사용자는
$externalMongoDB 인증 데이터베이스에 생성되어야 합니다.MongoDB는 클라이언트 인증서의 전체
Subject줄을 사용자 이름으로 처리합니다. MongoDB 서버에 연결할 때, 예를 들어 사용자alice로 연결할 때, Teleport는CN=alice주체로 임시 인증서를 서명합니다.데이터베이스에서 이 사용자를 생성하려면
mongosh또는mongo쉘을 사용하여 연결하고 다음 명령을 실행합니다:db.getSiblingDB("$external").runCommand( { createUser: "CN=alice", roles: [ { role: "readWriteAnyDatabase", db: "admin" } ] } )사용자에게 적절한 데이터베이스 권한을 부여하도록 역할을 업데이트합니다.
상호 TLS 설정#
Teleport uses mutual TLS authentication with self-hosted databases. These databases must be configured with Teleport's certificate authority to be able to verify client certificates. They also need a certificate/key pair that Teleport can verify.
To use issue certificates from your workstation with
tctl, your Teleport user must be allowed to impersonate the system roleDb.Include the following
allowrule in in your Teleport user's role:allow: impersonate: users: ["Db"] roles: ["Db"]스탠드얼론 MongoDB에 연결할 때, Teleport가 연결할 호스트명에 대한 인증서를 서명합니다.
예를 들어 MongoDB 서버가
mongo.example.com호스트명에서 접근 가능한 경우 다음을 실행합니다:$ tctl auth sign --format=mongodb --host=mongo.example.com --out=mongo --ttl=2190h이 명령은 두 개의 파일을 생성합니다: Teleport의 인증 기관이 포함된
mongo.cas와 생성된 인증서 및 키 쌍이 포함된mongo.crt. MongoDB 서버에서 상호 TLS를 활성화하려면 이 파일들이 필요합니다.MongoDB 인스턴스에 이미 인증서를 서명하는 데 사용하는 CA가 있는 경우, Teleport 데이터베이스 서비스에서 오는 트래픽을 MongoDB가 인증하기 위한 Teleport CA 인증서만 내보내면 됩니다.
-
클러스터의 Teleport Proxy 서비스의 호스트와 웹 포트로 을 교체합니다. 워크스테이션에서 다음 명령을 실행합니다:
$ tctl auth export --type=db-client --auth-server= > server.cas이 명령은
server.cas파일 1개를 생성합니다. -
MongoDB가 Teleport에 제시할 인증서와 키 쌍을 MongoDB에 사용하는 CA에서 가져와 하나의 파일
mongo.crt에 추가합니다.
Modify the Teleport Database Service to trust your MongoDB CA, assigning to the path to your CA certificate:
databases: - name: "example-mongodb" protocol: "mongodb" uri: "mongodb.example.com:27017" static_labels: "env": "example" tls: ca_cert_file: ""Now the Teleport Database Service will trust certificates presented by your MongoDB.
Teleport에서 Teleport 데이터베이스 클라이언트 CA를 내보낸 다음, CA의 인증서와 연결하여 추가 신뢰 CA로 추가합니다:
$ tctl auth export --type=db-client > db-client-ca.crt $ cat db-client-ca.crt > /etc/certs/mongo.cas이렇게 구성하면 MongoDB가 이 CA들을 신뢰하도록 구성되어 Teleport 데이터베이스 클라이언트 CA를 신뢰하고 Teleport를 통한 접근을 허용하면서, 피어 검증에 CA의 인증서를 사용하는 TLS를 통한 MongoDB 복제도 계속 허용합니다.
Modify the Teleport Database Service to trust your MongoDB Replica Set CA, assigning to the path to your CA certificate:
databases: - name: "example-mongodb" protocol: "mongodb" uri: "mongodb.example.com:27017" static_labels: "env": "example" tls: ca_cert_file: ""Now the Teleport Database Service will trust certificates presented by your MongoDB Replica Set.
생성된 시크릿을 사용하여
mongod.conf구성 파일에서 상호 TLS를 활성화하고 데이터베이스를 재시작합니다:net: ssl: mode: requireSSL PEMKeyFile: /etc/certs/mongo.crt CAFile: /etc/certs/mongo.casnet: tls: mode: requireTLS certificateKeyFile: /etc/certs/mongo.crt CAFile: /etc/certs/mongo.cas레플리카 셋을 구성할 때는 각 멤버에 대해 구성하고 특정 서버에 대해 생성된 시크릿을 사용해야 합니다.
상호 TLS가 활성화되면 유효한 클라이언트 인증서 없이는 클러스터에 연결할 수 없습니다.
net.tls.allowConnectionsWithoutCertificates설정을 사용하여 인증서를 제시하지 않는 클라이언트의 연결을 허용할 수 있습니다.자세한 내용은 MongoDB 문서의 TLS/SSL 구성을 참조하세요.
3/3단계. 연결#
Teleport 클러스터에 로그인하고 사용 가능한 데이터베이스를 확인합니다:
$ tsh login --proxy=teleport.example.com --user=alice $ tsh db ls # Name Description Labels # ------------- --------------- -------- # example-mongo Example MongoDB env=dev$ tsh login --proxy=mytenant.teleport.sh --user=alice $ tsh db ls # Name Description Labels # ------------- --------------- -------- # example-mongo Example MongoDB env=dev데이터베이스의 자격 증명을 가져와서 연결하려면:
$ tsh db connect --db-user=alice --db-name dev example-mongoNote이 섹션의 내용은 원문 문서를 참조하세요. (
proxy-db-tunnel.mdx)지원되는 MongoDB 클라이언트연결하려면
PATH에mongosh또는mongo커맨드라인 클라이언트가 있어야 합니다. 데이터베이스 서비스는 먼저mongosh를 실행하려고 시도하고,mongosh가PATH에 없으면mongo를 실행합니다.데이터베이스에서 로그아웃하고 자격 증명을 제거하려면:
# 특정 데이터베이스 인스턴스의 자격 증명 제거. $ tsh db logout example-mongo # 모든 데이터베이스 인스턴스의 자격 증명 제거. $ tsh db logout다음 단계#
-
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.
- MongoDB 레플리카 셋용 인증 기관 및 해당 CA의 공개 인증서(PEM 형식):
