

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 LDAP 身分驗證 Presto on Amazon EMR
<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 會透過連接埠 636，使用 LDAPS 與 LDAP 伺服器進行通訊。如果您的 LDAP 實作需要自訂連接埠，您可以使用 `ldap.url` 屬性搭配 Amazon EMR 5.16.0 或更高版本來指定，或使用 `authentication.ldap.url` 搭配更早版本。將自訂連接埠換成 `636`，如 `presto-config` 中的 [步驟 3：為 LDAP 建立使用 Presto 屬性的組態 JSON](#emr-presto-ldap-prestoconfig) 組態分類範例中所示。確定任何防火牆和安全群組都允許連接埠 636 (或是您的自訂連接埠) 傳入及傳出流量，而且用於內部叢集通訊的連接埠 8446 (或是您的自訂連接埠) 也允許這些流量。

### LDAP 伺服器憑證
<a name="w2aac60c39c13b7b7"></a>

您必須將憑證檔案上傳至 Amazon S3 內的安全位置。如需詳細資訊，請參閱《Amazon Simple Storage Service 使用者指南》**中的[我該如何上傳檔案與資料夾至 S3 儲存貯體](https://docs.aws.amazon.com/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 上停用，則您必須使用具備許可之帳戶的使用者 ID (UID) 和密碼，才能繫結至 LDAP 伺服器，如此 PrestoDB 伺服器才能建立連線。您要使用 `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` 名稱/值對。

### 適用於 Presto 使用者的 LDAP 辨別名稱 (DN)
<a name="w2aac60c39c13b7c11"></a>

當您為 Presto 指定 LDAP 組態時，您指定的繫結模式要由 `${USER}` 加上組織單位 (OU)，以及其他網域元件 (DC) 所組成。進行密碼身分驗證時，Presto 會將 `${USER}` 更換成每位使用者的實際使用者 ID (UID)，以便與這個繫結模式所指定的辨別名稱 (DN) 進行匹配。您需要合格使用者所隸屬的 OU 及其 DC。例如，若要允許 `admins` OU 內使用者能在 `corp.example.com` 網域中驗證 Presto，則您必須指定其使用者繫結模式為 `${USER},ou=admins,dc=corp,dc=example,dc=com`。

**注意**  
使用 時 AWS CloudFormation，您需要使用 Fn：：Sub 函數，以實際使用者 ID (UID) `${USER}`取代 。如需詳細資訊，請參閱《AWS CloudFormation 使用者指南》**中的 [Fn::Sub 主題](https://docs.aws.amazon.com/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.aws.amazon.com/emr/latest/ManagementGuide/emr-create-security-configuration.html)。在您設定傳輸中加密時所提供的加密成品，將會用來加密 Presto 節點之間的內部通訊。如需詳細資訊，請參閱[提供傳輸中資料加密的憑證](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-encryption-enable.html#emr-encryption-certificates)。當用戶端連線至 Presto 伺服器時，將會使用 LDAP 伺服器憑證來進行驗證。

## 步驟 3：為 LDAP 建立使用 Presto 屬性的組態 JSON
<a name="emr-presto-ldap-prestoconfig"></a>

您要使用 `presto-config` 組態分類來設定 LDAP 的 Presto 屬性。`presto-config` 的格式和內容略有不同，取決於 Amazon EMR 發行版本和 Presto 安裝 (PrestoDB 或 Trino)。本章節稍後會提供有關組態差異的範例。如需詳細資訊，請參閱[設定應用程式](emr-configure-apps.md)。

下列步驟會假設您已將 JSON 資料存為檔案 {{MyPrestoConfig.json}}。如果您是使用主控台，則該檔案應上傳到 Amazon S3 中的安全位置，以便您在建立叢集時可以進行參考。如果您使用 AWS 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，請使用 `trino-config` 取代以下組態分類中的 {{`prestosql-config`}}，並且使用 `trino-password-authenticator` 取代 `{{prestosql-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 主控台，然後從側邊導覽選取**切換至舊主控台**。如需有關切換至舊主控台時預期情況的詳細資訊，請參閱[使用舊主控台](https://docs.aws.amazon.com/emr/latest/ManagementGuide/whats-new-in-console.html#console-opt-in)。

1. 選擇 **Create cluster (建立叢集)**，然後選擇 **Go to advanced options (前往進階選項)**。

1. 選擇 **Presto** 和 Amazon EMR 要安裝的其他應用程式，並在 **軟體組態**下方，選擇要使用的 Amazon EMR **版本**。只有在使用 Amazon EMR 5.10.0 和更高版本時，LDAP 身分驗證才能受到支援。

1. 在**編輯軟體設定**中，選擇**從 S3 載入 JSON**，然後在您於 [步驟 3：為 LDAP 建立使用 Presto 屬性的組態 JSON](#emr-presto-ldap-prestoconfig) 中所建立 JSON 組態檔案的 Amazon S3 中輸入位置，最後選擇**下一步**。

1. 設定叢集硬體和聯網，然後選擇 **Next (下一步)**。

1. 選擇 **Bootstrap Actions (引導操作)**。在 **Add bootstrap action (新增引導操作)** 中，依序選取 **Custom action (自訂動作)**、**Configure and add (設定和新增)**。

1. 輸入引導操作**的名稱**，輸入您在 中建立的**指令碼位置**[步驟 4：建立指令碼，並透過其複製 LDAP 伺服器憑證，以及上傳至 Amazon S3](#emr-presto-ldap-servercert)，例如 **s3：//amzn-s3-demo-bucket/LoadLDAPCert.sh**，然後選擇**新增**。

1. 依序在 **General Options (一般選項)**、**Tags (標記)** 和 **Additional Options (其他選項)** 中，選擇適合您的應用程式的設定，然後選擇 **Next (下一步)**。

1. 選擇 **Authentication and encryption (身分驗證和加密)**，接著選擇您在 ** 中所建立的 **Security configuration (安全組態)[步驟 2：設定安全組態](#emr-presto-ldap-seccfg)。

1. 選擇適用於應用程式的其他安全選項，然後選擇 **Create cluster (建立叢集)**。

**使用 建立具有 LDAP 身分驗證的 Presto 叢集 AWS 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
  ```