

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

# 將 Amazon API Gateway 方法設定為自訂身分提供者
<a name="gateway-api-tutorial"></a>

本教學課程說明如何設定 Amazon API Gateway 方法，並將其用作將檔案上傳至 AWS Transfer Family 伺服器的自訂身分提供者。本教學課程僅使用[基本堆疊範本](https://s3.amazonaws.com/aws-transfer-resources/custom-idp-templates/aws-transfer-custom-idp-basic-apig.template.yml)和其他基本功能做為範例。

**Topics**
+ [先決條件](#prereq-api-gateway-tutorial)
+ [步驟 1：建立 CloudFormation 堆疊](#create-cf-stack)
+ [步驟 2：檢查伺服器的 API Gateway 方法組態](#deploy-api-gateway)
+ [步驟 3：檢視 Transfer Family 伺服器詳細資訊](#create-transfer-server)
+ [步驟 4：測試您的使用者是否可以連線到伺服器](#test-user-server-connect)
+ [步驟 5：測試 SFTP 連線和檔案傳輸](#test-sftp-connection)
+ [步驟 6：限制對儲存貯體的存取](#example-bucket)
+ [如果使用 Amazon EFS，請更新 Lambda](#lambda-posix)

## 先決條件
<a name="prereq-api-gateway-tutorial"></a>

在 中建立 Transfer Family 資源之前 CloudFormation，請先建立您的儲存體和使用者角色。

**指定儲存體並建立使用者角色**

1. 視您使用的儲存體而定，請參閱下列文件：
   + 若要建立 Amazon S3 儲存貯體，請參閱[如何建立 S3 儲存貯體？](https://docs.aws.amazon.com/AmazonS3/latest/user-guide/create-bucket-overview.html) *Amazon Simple Storage Service 使用者指南*中的 。
   + 若要建立 Amazon EFS 檔案系統，請參閱 [設定 Amazon EFS 檔案系統](configure-storage.md#requirements-efs)。

1. 若要建立使用者角色，請參閱 [建立 IAM 角色和政策](requirements-roles.md)

您可以在下一節中建立 CloudFormation 堆疊時，輸入儲存體和使用者角色的詳細資訊。

## 步驟 1：建立 CloudFormation 堆疊
<a name="create-cf-stack"></a>

**從提供的範本建立 AWS CloudFormation 堆疊**

1. 在 https：//[https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/) 開啟 CloudFormation 主控台。

1. 選取**建立堆疊**，然後選擇**使用新資源 （標準）**。

1. 在**先決條件 - 準備範本**窗格中，選擇**選擇現有範本**。

1. 複製此連結、[基本堆疊範本](https://s3.amazonaws.com/aws-transfer-resources/custom-idp-templates/aws-transfer-custom-idp-basic-apig.template.yml)，並將其貼入 **Amazon S3 URL** 欄位。

1. 按一下 **Next (下一步)**。

1. 指定參數，包括堆疊的名稱。請務必執行下列動作：
   + 取代 **UserName** 和 **UserPassword** 的預設值。
   + 針對 **UserHomeDirectory**，輸入您先前建立之儲存體 (Amazon S3 儲存貯體或 Amazon EFS 檔案系統） 的詳細資訊。
   + 將預設 **UserRoleArn** 取代為您先前建立的使用者角色。 AWS Identity and Access Management (IAM) 角色必須具有適當的許可。如需 IAM 角色和儲存貯體政策的範例，請參閱 [步驟 6：限制對儲存貯體的存取](#example-bucket)。
   + 如果您想要使用公有金鑰而非密碼進行身分驗證，請在 **UserPublicKey1** 欄位中輸入公有金鑰。第一次使用 SFTP 連線到伺服器時，您會提供私有金鑰而非密碼。

1. 選擇**下一步**，然後在**設定堆疊選項**頁面上再次選擇**下一步**。

1. 檢閱您要建立之堆疊的詳細資訊，然後選擇**建立堆疊**。
**注意**  
在頁面底部**的功能**下，您必須確認 CloudFormation 可能會建立 IAM 資源。

## 步驟 2：檢查伺服器的 API Gateway 方法組態
<a name="deploy-api-gateway"></a>

**注意**  
為了提高安全性，您可以設定 Web 應用程式防火牆。 AWS WAF 是一種 Web 應用程式防火牆，可讓您監控轉送至 Amazon API Gateway 的 HTTP 和 HTTPS 請求。如需詳細資訊，請參閱[新增 Web 應用程式防火牆](web-application-firewall.md)。

**請勿啟用 API Gateway 快取**  
將 API Gateway 方法用作 Transfer Family 的自訂身分提供者時，請勿啟用 API Gateway 方法的快取。快取對身分驗證請求不適當且無效，因為：  
每個身分驗證請求都是唯一的，並且需要即時回應，而不是快取的回應
快取沒有好處，因為 Transfer Family 永遠不會將重複或重複的請求傳送至 API Gateway
啟用快取會導致 API Gateway 以不相符的資料回應，導致對身分驗證請求的回應無效

**檢查伺服器的 API Gateway 方法組態並進行部署**

1. <a name="step_invoke_url"></a>

   在以下網址開啟 API Gateway 主控台：[https://console.aws.amazon.com/apigateway/](https://console.aws.amazon.com/apigateway/)。

1. 選擇 CloudFormation 範本產生的 **Transfer Custom Identity Provider 基本範本 API**。

1. 在**資源**窗格中，選擇 **GET**，然後選擇**方法請求**。

1. 針對**動作**，選擇**部署 API**。針對**部署階段**，選擇 **prod**，然後選擇**部署**。

   成功部署 API Gateway 方法後，請在**階段編輯器**區段中檢視其效能。
**注意**  
複製出現在頁面頂端的**調用 URL** 地址。下一個步驟將需要它。

## 步驟 3：檢視 Transfer Family 伺服器詳細資訊
<a name="create-transfer-server"></a>

當您使用 範本建立 CloudFormation 堆疊時，會自動建立 Transfer Family 伺服器。

**檢視 Transfer Family 伺服器詳細資訊**

1. 在 https：//[https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/) 開啟 CloudFormation 主控台。

1. 選擇您建立的堆疊。

1. 選擇 **Resources** (資源) 標籤。  
![畫面顯示建立 CloudFormation 堆疊期間建立的資源清單，並反白顯示 Transfer Family 伺服器的詳細資訊。](http://docs.aws.amazon.com/zh_tw/transfer/latest/userguide/images/apig-find-server-id.png)

   伺服器 ARN 會顯示在 **TransferServer** 資料列的**實體 ID** 欄中。伺服器 ID 包含在 ARN 中，例如 **s-11112222333344445**。

1. 在 https：//[https://console.aws.amazon.com/transfer/](https://console.aws.amazon.com/transfer/) 開啟 AWS Transfer Family 主控台，然後在**伺服器**頁面上選擇新的伺服器。

   伺服器 ID 符合 **TransferServer** 資源顯示的 ID CloudFormation。

## 步驟 4：測試您的使用者是否可以連線到伺服器
<a name="test-user-server-connect"></a>

**若要測試您的使用者是否可以連線至伺服器，請使用 Transfer Family 主控台**

1. 在 https：//[https://console.aws.amazon.com/transfer/](https://console.aws.amazon.com/transfer/) 開啟 AWS Transfer Family 主控台。

1. 在**伺服器**頁面上，選擇您的新伺服器，選擇**動作**，然後選擇**測試**。

1. 在**使用者名稱**欄位中輸入登入資料的文字，然後在**密碼**欄位中輸入。這些是您部署 CloudFormation 堆疊時設定的值。

1. 針對**伺服器通訊協定**，選取 **SFTP**，針對**來源 IP**，輸入 **127.0.0.1**。

1. 選擇**測試**。

   如果使用者身分驗證成功，測試會傳回 `StatusCode: 200` HTML 回應和 JSON 物件，其中包含使用者角色和許可的詳細資訊。例如：

   ```
   {
       "Response": "{\"Role\": \"arn:aws:iam::{{123456789012}}:role/{{my-user-role}}\",\"HomeDirectory\": \"/${transfer:HomeBucket}/\"}",
       "StatusCode": 200,
       "Message": "",
       "Url": "https://{{1a2b3c4d5e}}.execute-api.{{us-east-2}}.amazonaws.com/prod/servers/s-{{1234abcd5678efgh0}}/users/{{myuser}}/config"
   }
   ```

   如果測試失敗，請將其中一個 API Gateway AWS 受管政策新增至您用於 API 的角色。

## 步驟 5：測試 SFTP 連線和檔案傳輸
<a name="test-sftp-connection"></a>

**測試 SFTP 連線**

1. 在 Linux 或 macOS 裝置上，開啟命令終端機。

1. 輸入下列其中一個命令，取決於您使用密碼或金鑰對進行身分驗證。
   + 如果您使用的是密碼，請輸入此命令：

      ` sftp -o PubkeyAuthentication=no {{myuser}}@{{server-ID}}.server.transfer.{{region-code}}.amazonaws.com ` 

     出現提示時，輸入您的密碼。
   +  如果您使用的是金鑰對，請輸入此命令：

     ` sftp -i {{private-key-file}} {{myuser}}@{{server-ID}}.server.transfer.{{region-code}}.amazonaws.com `
**注意**  
對於這些`sftp`命令，請插入 Transfer Family 伺服器所在的 AWS 區域 程式碼。例如，如果您的伺服器位於美國東部 （俄亥俄），請輸入 **us-east-2**。

1.  出現`sftp>`提示時，請確定您可以上傳 (`put`)、下載 (`get`) 和檢視目錄和檔案 (`pwd` 和 `ls`)。

## 步驟 6：限制對儲存貯體的存取
<a name="example-bucket"></a>

您可以限制誰可以存取特定的 Amazon S3 儲存貯體。下列範例顯示要在 CloudFormation 堆疊和您為使用者選取的政策中使用的設定。

在此範例中，我們為 CloudFormation 堆疊設定下列參數：
+ **CreateServer**： `true`
+ **UserHomeDirectory**： `/amzn-s3-demo-bucket1`
+ **UserName**： `myuser`
+ **UserPassword**： `MySuperSecretPassword`
**重要**  
這是範例密碼。當您設定 API Gateway 方法時，請務必輸入強式密碼。
+ **UserPublicKey1**： `{{your-public-key}}`
+ **UserRoleArn**： `arn:aws:iam::{{role-id}}:role/myuser-api-gateway-role`

``**UserPublicKey1** 是您作為公有/私有金鑰對的一部分產生的公有金鑰。

對於您建立的使用者角色而言， `{{role-id}}` 是唯一的。連接到 的政策`myuser-api-gateway-role`如下：

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "s3:ListBucket",
            "Resource": "arn:aws:s3:::amzn-s3-demo-bucket1"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObjectAcl",
                "s3:GetObject",
                "s3:DeleteObjectVersion",
                "s3:DeleteObject",
                "s3:PutObjectAcl",
                "s3:GetObjectVersion"
            ],
            "Resource": "arn:aws:s3:::amzn-s3-demo-bucket1/*"
        }
    ]
}
```

 若要使用 SFTP 連線至伺服器，請在提示中輸入下列其中一個命令。
+ 如果您使用密碼進行身分驗證，請執行下列命令：

   ` sftp -o PubkeyAuthentication=no {{myuser}}@{{transfer-server-ID}}.server.transfer.{{region-id}}.amazonaws.com` 

  出現提示時，輸入您的密碼。
+  如果您使用金鑰對進行身分驗證，請執行下列命令：

  ` sftp -i {{private-key-file}} myuser@{{transfer-server-ID}}.server.transfer.{{region-id}}.amazonaws.com `

**注意**  
對於這些`sftp`命令，請使用 AWS 區域 Transfer Family 伺服器所在的 的 ID。例如，如果您的伺服器位於美國東部 （俄亥俄），請使用 `us-east-2`。

 出現`sftp`提示時，系統會將您導向主目錄，您可以透過執行 `pwd`命令來檢視該主目錄。例如：

```
sftp> pwd
Remote working directory: /amzn-s3-demo-bucket1
```

使用者無法檢視主目錄上方的任何目錄。例如：

```
sftp> pwd
Remote working directory: /amzn-s3-demo-bucket1
sftp> cd ..
sftp> ls
Couldn't read directory: Permission denied
```

## 如果使用 Amazon EFS，請更新 Lambda
<a name="lambda-posix"></a>

 如果您選取 Amazon EFS 做為 Transfer Family 伺服器的儲存選項，則需要編輯堆疊的 lambda 函數。

**將 Posix 設定檔新增至 Lambda 函數**

1. 開啟位於 https：//[https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/) 的 Lambda 主控台。

1.  選取您先前建立的 Lambda 函數。Lambda 函數的格式為 **{{stack-name}}-GetUserConfigLambda-{{lambda-identifier}}**，其中 {{stack-name}} 是 CloudFormation 堆疊名稱，{{lambda-identifier}} 是函數的識別符。

1.  在**程式碼**索引標籤中，選取 **index.js** 以顯示函數的程式碼。

1.  在 中`response`，在 `Policy`和 之間新增下列行`HomeDirectory`：

    ` PosixProfile: {"Uid": {{uid-value}}, "Gid": {{gid-value}}}, ` 

    其中 {{uid-value}} 和 {{gid-value}} 是整數 0 或更高，分別代表使用者 ID 和群組 ID。

   例如，在您新增 Posix 設定檔之後，回應欄位可能如下所示：

   ```
    response = {
         Role: 'arn:aws:iam::123456789012:role/api-gateway-transfer-efs-role', // The user will be authenticated if and only if the Role field is not blank
         Policy: '', // Optional JSON blob to further restrict this user's permissions
         PosixProfile: {"Gid": 65534, "Uid": 65534},
         HomeDirectory: '/fs-fab2c234' // Not required, defaults to '/'
       };
   ```