MySQL 자동 사용자 프로비저닝
Teleport는 Teleport 사용자가 데이터베이스에 연결할 때 MySQL 데이터베이스에 자동으로 계정을 생성할 수 있으며, 이를 통해 사전에 개별 사용자 계정을 생성하거나 모든 사용자에 대해 동일한 공유 데이터베이스 계정 집합을 사용할 필요가 없습니다.
Teleport는 Teleport 사용자가 데이터베이스에 연결할 때 MySQL 데이터베이스에 자동으로 계정을 생성할 수 있으며, 이를 통해 사전에 개별 사용자 계정을 생성하거나 모든 사용자에 대해 동일한 공유 데이터베이스 계정 집합을 사용할 필요가 없습니다. 이 가이드는 MySQL에 대한 자동 사용자 프로비저닝을 설정하는 방법을 보여줍니다.
작동 방식#
MySQL 데이터베이스를 Teleport에 등록하기 전에, 사용자를 관리할 권한을 가진 관리자 사용자를 데이터베이스에 설정합니다. 그런 다음 관리자 사용자의 이름을 포함하는 구성 필드와 함께 데이터베이스를 Teleport 클러스터에 등록합니다.
자동 사용자 프로비저닝을 활성화하는 역할로 Teleport 사용자가 데이터베이스에 연결하면, Teleport 데이터베이스 서비스는 먼저 관리자 사용자로 데이터베이스에 연결하고, 데이터베이스에 임시 사용자를 생성한 다음, 임시 데이터베이스 사용자로 Teleport 사용자를 위한 데이터베이스 세션을 시작합니다.
Teleport 사용자가 세션을 종료하면, Teleport 데이터베이스 서비스는 구성에 따라 데이터베이스 사용자를 제거하거나 비활성화하고 권한을 제거하여 임시 데이터베이스 사용자가 데이터베이스에 접근하지 못하도록 합니다.
전제 조건#
- 셀프 호스팅 MySQL 또는 RDS MySQL 데이터베이스가 구성된 Teleport 클러스터.
- 대상 데이터베이스에 연결하고 사용자 계정을 생성할 수 있는 권한.
- 자동 사용자 프로비저닝은 MySQL 버전 8.0 미만과는 호환되지 않습니다.
- 자동 사용자 프로비저닝은 RDS Aurora 리더 엔드포인트와 호환되지 않습니다.
1/3단계. 데이터베이스 관리자 구성#
Teleport should be able to connect to the database as a user that can create
other users and assign them roles. We recommend creating a separate user
designated specifically for Teleport automatic user provisioning. Let's call it
teleport-admin.
Teleport는 관리자 사용자로 연결할 때 일반 사용자 연결과 동일한 인증 메커니즘을 사용합니다: 셀프 호스팅 데이터베이스의 경우 X.509, RDS의 경우 AWS IAM.
관리자 사용자는 데이터베이스 내에서 사용자를 생성하고 권한을 부여할 수 있는 권한이 있어야 합니다. 또한 사용자 프로세스 및 역할 할당을 모니터링할 수 있는 권한도 있어야 합니다.
또한, 관리자 사용자가 기본적으로 로그인할 데이터베이스가 필요합니다. 저장 프로시저도 이 데이터베이스에서 생성되고 실행됩니다.
RDS MySQL 관리자 사용자는 IAM 인증을 허용하기 위해 AWSAuthenticationPlugin을 사용해야 합니다:
CREATE USER 'teleport-admin' IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS';
GRANT SELECT ON mysql.role_edges TO 'teleport-admin' ;
GRANT PROCESS, ROLE_ADMIN, CREATE USER ON *.* TO 'teleport-admin' ;
CREATE DATABASE IF NOT EXISTS `teleport`;
GRANT ALTER ROUTINE, CREATE ROUTINE, EXECUTE ON `teleport`.* TO 'teleport-admin' ;
Note that Teleport uses teleport as the name of the default database but the
name is configurable in the Teleport database definition. Replace the database
name in the last two lines if you wish to use another database name.
셀프 호스팅 MySQL 관리자 사용자는 X.509 인증이 구성되어 있어야 합니다:
CREATE USER "teleport-admin" REQUIRE SUBJECT "/CN=teleport-admin";
GRANT SELECT ON mysql.role_edges TO 'teleport-admin' ;
GRANT PROCESS, ROLE_ADMIN, CREATE USER ON *.* TO 'teleport-admin' ;
CREATE DATABASE IF NOT EXISTS `teleport`;
GRANT ALTER ROUTINE, CREATE ROUTINE, EXECUTE ON `teleport`.* TO 'teleport-admin' ;
Note that Teleport uses teleport as the name of the default database but the
name is configurable in the Teleport database definition. Replace the database
name in the last two lines if you wish to use another database name.
Teleport가 생성한 사용자는 데이터베이스에서 teleport-auto-user 역할이 할당되며, 해당 역할이 존재하지 않으면 자동으로 생성됩니다.
Next, configure the database admin user in the Teleport database configuration:
kind: db
version: v3
metadata:
name: example
spec:
protocol: "mysql"
uri: "localhost:3306"
admin_user:
name: "teleport-admin"
# Optional default database the admin user logs into. Default is
# 'teleport', if not specified.
# default_database: teleport
This example assumes that you have configured the database as a dynamic
resource. If you have configured your database using a static Teleport Database
Service configuration, edit the entry in your db_service.databases
configuration.
For auto-discovered cloud databases, the name of the admin user is taken from
the teleport.dev/db-admin label, and the default database is taken from the
teleport.dev/db-admin-default-database label.
2/3단계. Teleport 역할 구성#
To specify the database roles a user should be assigned within the database,
use the db_roles role option:
kind: role
version: v7
metadata:
name: auto-db-users
spec:
options:
# create_db_user_mode enables automatic user provisioning for matching databases
create_db_user_mode: keep
allow:
db_labels:
"*": "*"
db_names:
- "*"
# db_roles is a list of roles the database user will be assigned
db_roles:
- reader
- "{{internal.db_roles}}"
- "{{external.db_roles}}"
(!docs/pages/includes/database-access/auto-user-provisioning/db_users_ignored.mdx!)
(!docs/pages/includes/database-access/auto-user-provisioning/modes.mdx!)
데이터베이스 내에서 생성된 사용자는 다음과 같이 설정됩니다:
teleport-auto-user역할이 할당됩니다.- 데이터베이스와 일치하는 Teleport 사용자의 역할 집합에서 모든 역할이 할당됩니다. 역할 이름은 유효하고 데이터베이스에 존재해야 합니다.
MySQL limits usernames to 32 characters. When the Teleport
username is within this limit, the user created within the database will have
the same name as the Teleport username. When the Teleport username is over the
32 character limit, the user created within the database will have the
name in the format of tp-<base64-sha1-teleport-username>.
이름 매핑 추적
원래 Teleport 사용자 이름은 데이터베이스 내 사용자 속성으로 저장됩니다.사용자는 자동 프로비저닝된 데이터베이스 세션에서 다음과 같이 자신의 속성을 찾을 수 있습니다:
SELECT * FROM INFORMATION_SCHEMA.USER_ATTRIBUTES WHERE CONCAT(USER, '@', HOST) = current_user();
데이터베이스 관리자는 다음과 같이 특정 Teleport 사용자 이름을 검색할 수 있습니다:
SELECT * FROM INFORMATION_SCHEMA.USER_ATTRIBUTES WHERE ATTRIBUTE->"$.user" = "teleport-user-name";
또한, Teleport 사용자 이름이 32자를 초과하는 경우 "해시된" 데이터베이스 내 이름이 Teleport 감사 로그에서 데이터베이스 쿼리의 db_user로 기록됩니다.
Note that in case of a name conflict where a user with the same name already
exists in the database and is not managed by Teleport (i.e. not assigned the
teleport-auto-user role), the connection will be aborted.
3/3단계. 데이터베이스에 연결#
Now, log into your Teleport cluster and connect to the database:
$ tsh login --proxy=teleport.example.com
$ tsh db connect --db-name <database> example
To view the list of database roles that are allowed for each database, you can
use the command tsh db ls -v. By default, all database roles will be assigned
to your auto-provisioned database user. You can optionally select a subset of
the database roles with --db-roles:
$ tsh db connect --db-name <database> --db-roles reader example
문제 해결#
데이터베이스 접근 거부 오류#
기본적으로 새로 생성된 사용자는 특정 데이터베이스에 접근할 수 있는 권한이 없습니다. 이러한 권한은 reader와 같은 데이터베이스 특정 역할을 통해 부여되어야 합니다.
그렇지 않으면 다음과 같은 오류가 발생할 수 있습니다:
$ tsh db connect --db-name <database> example
ERROR 1105 (HY000): ERROR 1044 (42000): Access denied for user '<your-teleport-username>'@'%' to database '<database>'
테이블이 읽기 전용 오류#
Amazon RDS Aurora 리더 엔드포인트에 연결할 때 다음과 같은 오류가 발생할 수 있습니다:
$ tsh db connect --db-name <database> example
ERROR 3501 (HY000): The ACL operation failed due to the following error from SE: errcode 165 - Table is read only
데이터베이스 자동 사용자 프로비저닝은 RDS Aurora 리더 엔드포인트와 호환되지 않습니다. 기본 엔드포인트에서 자동 사용자 프로비저닝을 사용하세요.
매핑된 원격 사용자 이름 오류 사용#
You may encounter the following error when connecting to a database in a remote cluster:
> tsh db connect --db-name <database> example
ERROR: please use your mapped remote username ("remote-<your-teleport-username>-<root-cluster-name>") to connect instead of "<database-user>"
When you access resources in a remote cluster, the remote cluster will receive
the name remote-<your-teleport-username>-<root-cluster-name> from the local
cluster. This is to prevent any naming collisions with users in the remote
cluster. Please use the username from the error message as the database
username for when connecting through tsh or GUI clients.
다음 단계#
- GUI 데이터베이스 클라이언트를 사용하여 연결합니다.
- 역할 템플릿에 대해 알아봅니다.
- 자동 사용자 프로비저닝 RFD를 읽어봅니다.
