

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 为 Presto on Amazon EMR 使用 LDAP 身份验证
<a name="emr-presto-ldap"></a>

可以按照本节中的步骤配置 LDAP。请参阅每个步骤以了解示例以及指向更多信息的链接。

**Topics**
+ [步骤 1：收集有关 LDAP 服务器的信息并将服务器证书复制到 Amazon S3](#emr-presto-ldap-server-prereq)
+ [步骤 2：设置安全配置](#emr-presto-ldap-seccfg)
+ [步骤 3：使用 LDAP 的 Presto 属性创建配置 JSON](#emr-presto-ldap-prestoconfig)
+ [步骤 4：创建脚本以复制 LDAP 服务器证书并将其上传到 Amazon S3](#emr-presto-ldap-servercert)
+ [步骤 5：创建集群](#emr-presto-ldap-createcluster)

## 步骤 1：收集有关 LDAP 服务器的信息并将服务器证书复制到 Amazon S3
<a name="emr-presto-ldap-server-prereq"></a>

您需要在下一节中使用这些信息和内容，以便从 LDAP 服务器中配置 LDAP 身份验证。

### LDAP 服务器的 IP 地址或主机名
<a name="w2aac60c39c13b7b5"></a>

Amazon EMR 主节点上的 Presto 协调器必须能够访问具有指定的 IP 地址或主机名的 LDAP 服务器。默认情况下，Presto 使用 LDAPS 通过端口 636 与 LDAP 服务器通信。如果您的 LDAP 实施需要使用自定义端口，您可以使用 `ldap.url` 属性（Amazon EMR 5.16.0 或更高版本）或者 `authentication.ldap.url`（早期版本）指定该端口。将自定义端口替换为 `636`，如 [步骤 3：使用 LDAP 的 Presto 属性创建配置 JSON](#emr-presto-ldap-prestoconfig) 中的 `presto-config` 配置分类示例所示。确保任何防火墙和安全组允许端口 636（或自定义端口）以及端口 8446（或自定义端口）上的入站和出站流量，端口 8446 用于内部集群通信。

### LDAP 服务器证书
<a name="w2aac60c39c13b7b7"></a>

您必须将证书文件上传到 Amazon S3 中的安全位置。有关更多信息，请参阅*《Amazon Simple Storage Service 用户指南》*中的[如何将文件和文件夹上传到 S3 存储桶](https://docs.amazonaws.cn/AmazonS3/latest/userguide/upload-objects.html)。您可以创建一个引导操作，以便在集群启动时将该证书从 Amazon S3 复制到集群中的每个节点。在[步骤 4：创建脚本以复制 LDAP 服务器证书并将其上传到 Amazon S3](#emr-presto-ldap-servercert)中。示例证书是{{s3://amzn-s3-demo-bucket/ldap\_server.crt}}。

### LDAP 服务器的匿名绑定设置
<a name="w2aac60c39c13b7b9"></a>

如果 PrestoDB 禁用了匿名绑定，您需要使用有权限绑定到 LDAP 服务器的账户的用户 ID（UID）和密码，以便 Presto 服务器建立连接。您可以使用 `internal-communication.authentication.ldap.user` 配置分类中的 `internal-communication.authentication.ldap.password` 和 `presto-config` 属性指定 UID 和密码。Amazon EMR 5.10.0 不支持这些设置，因此，在使用该发行版时，LDAP 服务器上必须支持匿名绑定。

请注意，Trino 不需要匿名绑定配置。

**获取 LDAP 服务器上的匿名绑定状态**
+ 从 Linux 客户端中使用 [ldapwhoami](https://linux.die.net/man/1/ldapwhoami) 命令，如以下示例所示：

  ```
  ldapwhoami -x -H ldaps://{{LDAPServerHostNameOrIPAddress}}
  ```

  如果不允许匿名绑定，该命令将返回以下内容：

  ```
  ldap_bind: Inappropriate authentication (48)
  additional info: anonymous bind disallowed
  ```

**验证账户是否具有使用简单身份验证的 LDAP 服务器的权限**
+ 从 Linux 客户端中使用 [ldapwhoami](https://linux.die.net/man/1/ldapwhoami) 命令，如以下示例所示。该示例使用了一个虚构的用户{{presto}}，该用户存储在 EC2 实例上运行的 Open LDAP 服务器中，其主机名是虚构的。{{ip-xxx-xxx-xxx-xxx.ec2.internal}}用户与组织单位 (OU) 相关联{{admins}}，密码{{123456}}为：

  ```
  ldapwhoami -x -w "{{123456}}" -D uid={{presto}},ou={{admins}},dc=ec2,dc=internal -H ldaps://{{ip-xxx-xxx-xxx-xxx.ec2.internal}} 
  ```

  如果该账户有效并具有相应的权限，该命令将返回：

  ```
  dn:uid=presto,ou=admins,dc=ec2,dc=internal
  ```

为了清楚起见，[步骤 3：使用 LDAP 的 Presto 属性创建配置 JSON](#emr-presto-ldap-prestoconfig) 中的示例配置包含该账户，但 5.10.0 示例除外，该发行版不支持该账户。如果 LDAP 服务器使用匿名绑定，请移除`internal-communication.authentication.ldap.user`和`internal-communication.authentication.ldap.password` name/value 对。

### Presto 用户的 LDAP 可分辨名称 (DN)
<a name="w2aac60c39c13b7c11"></a>

当您为 Presto 指定 LDAP 配置时，您可以指定一种绑定模式，该模式包括组织单位 (OU) 和其他域组件 (DCs)。`${USER}`在密码身份验证期间，Presto 将 `${USER}` 替换为每个用户的实际用户 ID（UID），以便与该绑定模式指定的可分辨名称（DN）相匹配。您需要符合条件 OUs 的用户所属的以及他们 DCs的。例如，要允许 `admins` 域上的 `corp.example.com` OU 中的用户在 Presto 中进行身份验证，您可以将 `${USER},ou=admins,dc=corp,dc=example,dc=com` 指定为用户绑定模式。

**注意**  
使用时 Amazon CloudFormation，需要使用 Fn:: Sub 函数才能替换为实际`${USER}`的用户 ID (UID)。有关更多信息，请参阅*《Amazon CloudFormation 用户指南》* 中的 [Fn::Sub](https://docs.amazonaws.cn/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html) 主题。

在使用 Amazon EMR 5.10.0 时，您只能指定一种此类模式。在使用 Amazon EMR 5.11.0 或更高版本时，您可以指定多种模式并以冒号（:）分隔。尝试在 Presto 中进行身份验证的用户先与第一种模式进行比较，然后与第二种模式进行比较，依此类推。有关示例，请参阅[步骤 3：使用 LDAP 的 Presto 属性创建配置 JSON](#emr-presto-ldap-prestoconfig)。

## 步骤 2：设置安全配置
<a name="emr-presto-ldap-seccfg"></a>

创建一个安全配置并启用传输中加密。有关更多信息，请参阅《Amazon EMR 管理指南》**中的[创建安全配置](https://docs.amazonaws.cn/emr/latest/ManagementGuide/emr-create-security-configuration.html)。在设置传输中加密时提供的加密构件用于加密 Presto 节点之间的内部通信。有关更多信息，请参阅[为传输中的数据加密提供证书](https://docs.amazonaws.cn/emr/latest/ManagementGuide/emr-encryption-enable.html#emr-encryption-certificates)。LDAP 服务器证书用于对到 Presto 服务器的客户端连接进行身份验证。

## 步骤 3：使用 LDAP 的 Presto 属性创建配置 JSON
<a name="emr-presto-ldap-prestoconfig"></a>

您可以使用 `presto-config` 配置分类为 LDAP 设置 Presto 属性。根据 Amazon EMR 发行版和安装的 Presto（PrestoDB 或 Trino）的不同，`presto-config` 的格式和内容稍有不同。在本节后面提供了配置差异示例。有关更多信息，请参阅 [配置应用程序](emr-configure-apps.md)。

以下步骤假设您将 JSON 数据保存到文件中{{MyPrestoConfig.json}}。如果使用控制台，请将该文件上传到 Amazon S3 中的安全位置，以便在创建集群时引用该文件。如果使用 Amazon CLI，则可以在本地引用该文件。

**Example 采用 PrestoSQL (Trino) 的 Amazon EMR 6.1.0 及更高版本**  
以下示例使用 [步骤 1：收集有关 LDAP 服务器的信息并将服务器证书复制到 Amazon S3](#emr-presto-ldap-server-prereq) 中的 LDAP 主机名，以便在 LDAP 服务器中验证身份以进行绑定。指定了两种用户绑定模式，它指示 LDAP 服务器上的 `admins` OU 和 `datascientists` OU 中的用户可以在 Trino 服务器上作为用户进行身份验证。绑定模式由冒号 (`:`) 分隔。  
Amazon EMR 6.4.0 及更高版本使用新名称 Trino 而不是 PrestoSQL。如果您使用 Trino，请在以下配置分类中将 {{`prestosql-config`}} 替换为 `trino-config`、`{{prestosql-password-authenticator}}` 和 `trino-password-authenticator`。  

```
[
   {
      "Classification":"{{prestosql-config}}",
      "Properties":{
         "http-server.authentication.type":"PASSWORD"
      }
   },
   {
      "Classification":"{{prestosql-password-authenticator}}",
      "Properties":{
         "password-authenticator.name":"ldap",
         "ldap.url":"ldaps://ip-xxx-xxx-xxx-xxx.ec2.internal:636",
         "ldap.user-bind-pattern": "uid=${USER},ou=admins,dc=ec2,dc=internal:uid=${USER},ou=datascientists,dc=ec2,dc=internal"
      }
   }
]
```

**Example Amazon EMR 5.16.0 及更高版本**  
以下示例使用[步骤 1：收集有关 LDAP 服务器的信息并将服务器证书复制到 Amazon S3](#emr-presto-ldap-server-prereq)中的 LDAP 用户 ID 和密码以及 LDAP 主机名，以便在 LDAP 服务器中验证身份以进行绑定。指定了两种用户绑定模式，它指示 LDAP 服务器上的 `admins` OU 和 `datascientists` OU 中的用户可以在 Presto 服务器上作为用户进行身份验证。绑定模式由冒号 (`:`) 分隔。  

```
[{
        "Classification": "presto-config",
                "Properties": {
                        "http-server.authentication.type": "PASSWORD"
                }
        },
        {
                "Classification": "presto-password-authenticator",
                "Properties": {
                        "password-authenticator.name": "ldap",
                        "ldap.url": "ldaps://{{ip-xxx-xxx-xxx-xxx.ec2.internal}}:636",
                        "ldap.user-bind-pattern": "uid=${USER},ou={{admins}},dc=ec2,dc=internal:uid=${USER},ou={{datascientists}},dc=ec2,dc=internal",
                        "internal-communication.authentication.ldap.user": "{{presto}}",
                        "internal-communication.authentication.ldap.password": "{{123456}}"
                }
        }]
```

**Example Amazon EMR 5.11.0 至 5.15.0**  
这些发行版的 `presto-config ` 配置分类的格式略有不同。以下示例指定与上一示例相同的参数。  

```
[{
        "Classification": "presto-config",
                "Properties": {
                        "http-server.authentication.type": "LDAP",
                        "authentication.ldap.url": "ldaps://{{ip-xxx-xxx-xxx-xxx.ec2.internal}}:636",
                        "authentication.ldap.user-bind-pattern": "uid=${USER},ou={{admins}},dc=ec2,dc=internal:uid=${USER},ou={{datascientists}},dc=ec2,dc=internal",
                        "internal-communication.authentication.ldap.user": "{{presto}}",
                        "internal-communication.authentication.ldap.password": "{{123456}}"
                }
        }]
```

**Example Amazon EMR 5.10.0**  
Amazon EMR 5.10.0 仅支持匿名绑定，因此，将省略这些条目。此外，只能指定一种绑定模式。  

```
[{
        "Classification": "presto-config",
                "Properties": {
                        "http-server.authentication.type": "LDAP",
                        "authentication.ldap.url": "ldaps://{{ip-xxx-xxx-xxx-xxx.ec2.internal}}:636",
                        "ldap.user-bind-pattern": "uid=${USER},ou={{prestousers}},dc=ec2,dc=internal"
                }
        }]
```

## 步骤 4：创建脚本以复制 LDAP 服务器证书并将其上传到 Amazon S3
<a name="emr-presto-ldap-servercert"></a>

创建一个脚本以将证书文件复制到集群中的每个节点，然后将其添加到密钥存储中。请使用文本编辑器创建脚本，保存该脚本，然后将其上传到 Amazon S3 中。在中[步骤 5：创建集群](#emr-presto-ldap-createcluster)，脚本文件被引用为{{s3://amzn-s3-demo-bucket/LoadLDAPCert.sh}}。

以下示例脚本使用默认的密钥库密码。{{changeit}}我们建议您在创建集群后连接到主节点，并使用 keytool 命令更改密钥存储密码。

```
#!/bin/bash
aws s3 cp s3://amzn-s3-demo-bucket/ldap_server.crt .
sudo keytool -import -keystore /usr/lib/jvm/jre-1.8.0-openjdk.x86_64/lib/security/cacerts -trustcacerts -alias ldap_server -file ./ldap_server.crt -storepass {{changeit}} -noprompt
```



## 步骤 5：创建集群
<a name="emr-presto-ldap-createcluster"></a>

在创建集群时，您可以指定 Presto 以及希望 Amazon EMR 安装的其他应用程序。以下示例还引用 JSON 中的配置分类属性，但也可以指定内联的配置分类。

**使用 Amazon EMR 控制台创建具有 LDAP 身份验证的 Presto 集群**

1. 导航到 Amazon EMR 新控制台，然后从侧面导航栏中选择**切换到旧控制台**。有关切换到旧控制台后预期情况的更多信息，请参阅 [Using the old console](https://docs.amazonaws.cn/emr/latest/ManagementGuide/whats-new-in-console.html#console-opt-in)。

1. 依次选择 **Create cluster (创建集群)**、**Go to advanced options (转到高级选项)**。

1. 选择 **Presto** 以及 Amazon EMR 安装的其他应用程序，然后在 **Software Configuration (软件配置)** 下面选择要使用的 Amazon EMR **发行版**。仅 Amazon EMR 5.10.0 和更高版本支持 LDAP 身份验证。

1. 在 **Edit software settings (编辑软件设置)** 下面，选择 **Load JSON from S3 (从 S3 加载 JSON)**，输入在[步骤 3：使用 LDAP 的 Presto 属性创建配置 JSON](#emr-presto-ldap-prestoconfig)中创建的 JSON 配置文件的 Amazon S3 位置，然后选择 **Next (下一步)**。

1. 配置集群硬件和网络，然后选择**下一步**。

1. 选择 **Bootstrap Actions (引导操作)**。对于**添加引导操作**，请选择**自定义操作**，然后选择**配置并添加**。

1. **输入引导操作的**名称**，输入您在中创建的**脚本位置**[步骤 4：创建脚本以复制 LDAP 服务器证书并将其上传到 Amazon S3](#emr-presto-ldap-servercert)，例如 **s3://amzn-s3-demo-bucket/Load LDAPCert .sh**，然后选择添加。**

1. 在**常规选项**、**标签**和**其他选项**下面，选择适合您的应用程序的设置，然后选择**下一步**。

1. 选择**身份验证和加密**，然后选择您在**中创建的**安全配置[步骤 2：设置安全配置](#emr-presto-ldap-seccfg)。

1. 选择适合您的应用程序的其他安全选项，然后选择**创建集群**。

**要创建采用 LDAP 身份验证的 Presto 集群，请使用 Amazon CLI**
+ 使用 `aws emr create-cluster` 命令。至少，指定 Presto 应用程序，以及在以前步骤中创建的 Presto 配置分类、引导脚本和安全配置。以下示例将配置文件作为在运行该命令的同一目录中保存的 JSON 文件引用。另一方面，引导脚本必须保存在 Amazon S3 中。下面的示例使用了 `s3://amzn-s3-demo-bucket/LoadLDAPCert.sh`。
**注意**  
为了便于读取，包含 Linux 行继续符（\\）。它们可以通过 Linux 命令删除或使用。对于 Windows，请将它们删除或替换为脱字号（^）。

  ```
  aws emr create-cluster  --applications Name=presto --release-label emr-5.16.0 \
  --use-default-roles --ec2-attributes KeyName={{MyKeyPair}},SubnetId={{subnet-1234ab5}} \ --instance-count 3 --instance-type m5.xlarge --region us-west-2 --name "MyPrestoWithLDAPAuth" \
  --bootstrap-actions Name="Distribute LDAP server cert",Path="s3://amzn-s3-demo-bucket/LoadLDAPCert.sh" \
  --security-configuration MyPrestoLDAPSecCfg --configurations file://MyPrestoConfig.json
  ```