How-To: Impersonation | Tutorial
HDFS file system support — Trino 452 Documentation
什么是用户冒充
配置 hive.metastore.thrift.impersonation.enabled=true 是在 Trino 与 Hive Metastore 进行交互时启用用户冒充(impersonation)的设置。让我们深入解释这个配置项及其用途。
什么是用户冒充(Impersonation)?
用户冒充是一种机制,允许服务在与其他服务交互时,代表实际用户执行操作。在 Hadoop 和 Hive 环境中,用户冒充非常重要,因为它确保了权限和审计的一致性。例如,Trino 代表实际的用户访问 Hive Metastore,以确保数据访问权限和操作都能正确地记录和控制。
配置项解释
hive.metastore.thrift.impersonation.enabled=true
- hive.metastore.thrift.impersonation.enabled:这是一个布尔值配置项,用于启用或禁用 Trino 在访问 Hive Metastore 时的用户冒充功能。
- true:启用用户冒充。Trino 在与 Hive Metastore 交互时,会以实际用户的身份进行操作。
- false:禁用用户冒充。Trino 将以自己的身份与 Hive Metastore 交互,不会代表实际用户。
配置文件
此配置通常位于 Trino 的 Hive catalog 配置文件中,例如 hive.properties 文件。
connector.name=hive
hive.metastore.uri=thrift://<metastore-host>:<metastore-port>
hive.metastore.authentication.type=KERBEROS
hive.metastore.thrift.impersonation.enabled=true
hive.metastore.service.principal=hive/<hive-metastore-host>@<REALM>
hive.metastore.client.principal=trino/<trino-host>@<REALM>
hive.metastore.client.keytab=/path/to/trino.keytab
# 其他相关配置
hive.s3.aws-access-key=<your-access-key>
hive.s3.aws-secret-key=<your-secret-key>
hive.s3.endpoint=http://<s3-endpoint>
hive.s3.path-style-access=true
hive.s3.ssl.enabled=false
hive.security=allow-all
hive.non-managed-table-writes-enabled=true
如何工作
- 身份验证:Trino 使用 Kerberos 对 Hive Metastore 进行身份验证。
- 用户冒充:启用用户冒充后,Trino 在访问 Hive Metastore 时,将使用实际用户的身份。这意味着 Hive Metastore 将认为请求来自实际用户,而不是 Trino 服务本身。
- 权限控制:Hive Metastore 根据实际用户的权限进行访问控制。只有实际用户有权限访问的数据,才能被 Trino 访问。
使用场景
- 多用户环境:在具有多个用户的环境中,这种设置确保每个用户的操作都能被正确审计和控制。
- 安全合规:启用用户冒充有助于满足数据访问的安全和合规要求,因为每个操作都是由实际用户执行的,而不是通过一个共享的服务账户。
总结
通过设置 hive.metastore.thrift.impersonation.enabled=true,您可以确保 Trino 在访问 Hive Metastore 时,能够代表实际用户执行操作。这对于确保权限控制和审计一致性非常重要,特别是在多用户和高安全性要求的环境中。这一设置结合 Kerberos 认证机制,提供了强大的安全性和灵活性。
实际用户是什么
在上述解释中,“实际用户”是指发起查询或操作的真实用户,而不是中间代理服务或系统用户。实际用户代表在系统中拥有身份和权限的个体或应用程序用户。
实际用户的概念
- 实际用户:执行查询或操作的终端用户,例如一个数据分析师、数据工程师,或运行查询的应用程序的用户。
- 中间服务用户:代表实际用户执行操作的服务账户,例如 trino 用户。
为什么需要实际用户?
当启用用户冒充(Impersonation)时,Trino 代表实际用户向 Hive Metastore 发起请求,这样可以确保:
- 权限控制:Hive Metastore 将基于实际用户的权限来处理请求,而不是基于 Trino 服务账户的权限。
- 审计和日志记录:所有操作都会记录为实际用户的操作,便于追踪和审计。
举例说明
假设有一个数据分析师 alice 通过 Trino 查询 Hive 数据:
- 没有启用用户冒充:
- Trino 服务账户 trino 直接与 Hive Metastore 交互。
- Hive Metastore 只知道是 trino 用户发起的请求,无法知道实际是谁在使用这些数据。
- 权限检查基于 trino 用户的权限,而不是 alice 的权限。
- 启用用户冒充:
- alice 通过 Trino 发起查询请求。
- Trino 在与 Hive Metastore 交互时,代表 alice 发起请求。
- Hive Metastore 看到的请求来自 alice,而不是 trino。
- 权限检查基于 alice 的权限。
- 操作日志记录为 alice 的操作。
配置和操作流程
配置
Trino 和 Hive Metastore 的配置文件设置用户冒充:
Trino Hive Catalog 配置 (hive.properties):
connector.name=hive
hive.metastore.uri=thrift://<metastore-host>:<metastore-port>
hive.metastore.authentication.type=KERBEROS
hive.metastore.thrift.impersonation.enabled=true
hive.metastore.service.principal=hive/<hive-metastore-host>@<REALM>
hive.metastore.client.principal=trino/<trino-host>@<REALM>
hive.metastore.client.keytab=/path/to/trino.keytab
Hadoop Core-site 配置 (core-site.xml):
<configuration>
<property>
<name>hadoop.security.authentication</name>
<value>kerberos</value>
</property>
<property>
<name>hadoop.proxyuser.trino.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.trino.groups</name>
<value>*</value>
</property>
</configuration>
操作流程
- Alice 登录 Trino:
- Alice 使用 Kerberos 认证登录 Trino。
- Trino 发起查询:
- Alice 发起一个查询请求。
- Trino 代理 Alice 向 Hive Metastore 发起请求。
- Hive Metastore 处理请求:
- Hive Metastore 接收到来自 Trino 的请求,但请求中包含 Alice 的身份信息。
- Hive Metastore 基于 Alice 的权限处理请求,并将结果返回给 Trino。
- 结果返回:
- Trino 将查询结果返回给 Alice。
总结
实际用户是指在系统中执行查询或操作的终端用户。在启用用户冒充的配置下,Trino 可以代表实际用户与 Hive Metastore 交互,这样可以确保基于实际用户的权限进行访问控制和审计记录。这对于权限管理和合规性至关重要。
用户冒充工作流程启用用户冒充时,Hive Metastore 看到请求来自 alice,而不是 trino,这是通过 Kerberos 认证和用户冒充机制共同实现的。以下是详细的工作原理:
用户冒充工作原理
- 用户认证:实际用户 alice 通过 Kerberos 认证登录 Trino。
- 用户冒充配置:Trino 配置了 Hive Metastore 的用户冒充,并启用了 Kerberos 认证。
- 代理请求:Trino 代表 alice 发起对 Hive Metastore 的请求,并在请求中包含 alice 的身份信息。
详细步骤
- Alice 登录 Trino:
- alice 使用 Kerberos 认证登录 Trino,获得 Kerberos 票据。
- 例如,alice 登录命令:
kinit alice
- Trino 配置:
- Trino 的配置中启用了 Kerberos 认证和用户冒充。
- Trino 使用服务票据(trino 用户的 Kerberos 票据)与 Hive Metastore 通信。
- Trino 的 hive.properties 配置文件:
connector.name=hive
hive.metastore.uri=thrift://<metastore-host>:<metastore-port>
hive.metastore.authentication.type=KERBEROS
hive.metastore.thrift.impersonation.enabled=true
hive.metastore.service.principal=hive/<hive-metastore-host>@<REALM>
hive.metastore.client.principal=trino/<trino-host>@<REALM>
hive.metastore.client.keytab=/path/to/trino.keytab
- 代理请求:
- alice 在 Trino 中执行查询。
- Trino 在执行查询时,使用 alice 的身份信息生成对 Hive Metastore 的请求。
- Hive Metastore 认证和授权:
- Hive Metastore 收到来自 Trino 的请求,但请求中包含 alice 的 Kerberos 票据。
- Hive Metastore 验证 alice 的 Kerberos 票据,并确定请求是由 alice 发起的。
- Hive Metastore 根据 alice 的权限处理请求。
- 返回结果:
- Hive Metastore 将查询结果返回给 Trino。
- Trino 将结果返回给 alice。
配置和示例
Trino Hive Catalog 配置 (hive.properties)
connector.name=hive
hive.metastore.uri=thrift://<metastore-host>:<metastore-port>
hive.metastore.authentication.type=KERBEROS
hive.metastore.thrift.impersonation.enabled=true
hive.metastore.service.principal=hive/<hive-metastore-host>@<REALM>
hive.metastore.client.principal=trino/<trino-host>@<REALM>
hive.metastore.client.keytab=/path/to/trino.keytab
Hadoop Core-site 配置 (core-site.xml)
<configuration>
<property>
<name>hadoop.security.authentication</name>
<value>kerberos</value>
</property>
<property>
<name>hadoop.proxyuser.trino.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.trino.groups</name>
<value>*</value>
</property>
</configuration>
总结
通过配置 Kerberos 认证和启用用户冒充,Trino 在与 Hive Metastore 交互时,可以代表实际用户(如 alice)发起请求。Hive Metastore 通过验证请求中的用户身份(Kerberos 票据)来确认请求是由实际用户发起的,而不是由中间服务用户(如 trino)发起的。这样可以确保权限控制和审计记录的准确性。
hive.metastore.service.principal
配置项 hive.metastore.service.principal=hive/<hive-metastore-host>@<REALM> 指定了 Hive Metastore 服务的 Kerberos 主要身份(Principal)。这个配置项的作用是确保 Trino 客户端能够正确地与 Hive Metastore 进行 Kerberos 认证通信。让我们详细解释它的作用和工作原理。
Kerberos 主要身份(Principal)
在 Kerberos 认证系统中,每个参与通信的实体(包括用户和服务)都有一个唯一的 Kerberos 主要身份(Principal)。一个典型的 Kerberos Principal 的格式如下:
primary/instance@REALM
- primary:主要部分,通常是服务的名称。例如,对于 Hive Metastore 服务,通常是 hive。
- instance:实例部分,通常是服务运行的主机名。例如,如果 Hive Metastore 运行在 hive-metastore-host 上,实例部分就是 hive-metastore-host。
- REALM:Kerberos 域,通常是大写的 DNS 域名,例如 EXAMPLE.COM。
配置项解释
hive.metastore.service.principal
hive.metastore.service.principal=hive/<hive-metastore-host>@<REALM>
- hive:这是服务名称部分,表示 Hive Metastore 服务。
- :这是实例部分,表示 Hive Metastore 服务运行的具体主机名。例如,如果 Hive Metastore 运行在 hive.example.com,这里就应该配置为 hive.example.com。
- @:这是 Kerberos 域,表示 Hive Metastore 服务所在的 Kerberos 域。例如,如果域是 EXAMPLE.COM,这里就配置为 @EXAMPLE.COM。
作用
- 服务认证:这个配置项告诉 Trino 客户端,Hive Metastore 服务的 Kerberos 主要身份是什么。这样,Trino 客户端在与 Hive Metastore 服务进行 Kerberos 认证时,可以正确地识别并认证 Hive Metastore 服务。
- 安全通信:Kerberos 认证确保了 Trino 与 Hive Metastore 之间的通信是安全的,并且双方都能够验证对方的身份。这防止了中间人攻击(MITM)和其他安全风险。
- 访问控制:通过指定服务的 Kerberos Principal,确保只有合法的 Trino 客户端能够访问 Hive Metastore 服务。这是基于 Kerberos 强认证机制的。
配置示例
假设 Hive Metastore 运行在 hive.example.com,Kerberos 域是 EXAMPLE.COM,配置项将如下:
hive.metastore.service.principal=hive/hive.example.com@EXAMPLE.COM
工作流程
- Trino 客户端初始化:
- Trino 客户端读取配置文件,知道 Hive Metastore 服务的 Kerberos 主要身份是 hive/hive.example.com@EXAMPLE.COM。
- Kerberos 票据请求:
- Trino 客户端使用其自己的 Kerberos 票据(如 trino/trino-host@EXAMPLE.COM)向 Kerberos 服务器请求服务票据,以便访问 hive/hive.example.com@EXAMPLE.COM。
- 认证请求:
- Trino 客户端使用获得的服务票据与 Hive Metastore 服务建立连接,并进行 Kerberos 认证。
- 认证成功:
- 如果认证成功,Hive Metastore 服务接受 Trino 客户端的请求,并允许进行后续的操作。
- 用户冒充(Impersonation):
- 如果启用了用户冒充,Trino 客户端还会在请求中包含实际用户的身份信息(例如,alice),Hive Metastore 服务将根据实际用户的权限处理请求。
总结
配置项 hive.metastore.service.principal=hive/<hive-metastore-host>@<REALM> 的主要作用是指定 Hive Metastore 服务的 Kerberos 主要身份,以便 Trino 客户端能够正确地与 Hive Metastore 进行 Kerberos 认证通信。这确保了通信的安全性和正确的访问控制。