

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

# 使用 Amazon EMR on EKS 啟用 Lake Formation
<a name="security_iam_fgac-lf-enable"></a>

透過 Amazon EMR 7.7 版和更新版本，您可以利用 AWS Lake Formation 對 Amazon S3 支援的 Data Catalog 資料表套用精細存取控制。此功能可讓您設定 Amazon EMR on EKS Spark 任務中讀取查詢的資料表、資料列、資料欄和儲存格層級存取控制。

本節說明如何建立安全組態，並設定 Lake Formation 以使用 Amazon EMR。它還說明如何使用您為 Lake Formation 建立的安全組態來建立虛擬叢集。這些區段應依序完成。

## 步驟 1：設定 Lake Formation 型資料欄、資料列或儲存格層級許可
<a name="security_iam_fgac-lf-enable-permissions"></a>

首先，若要使用 Lake Formation 套用資料列和資料欄層級許可，Lake Formation 的資料湖管理員必須設定 **LakeFormationAuthorizedCaller** 工作階段標籤。Lake Formation 使用此工作階段標籤來授權呼叫者並提供對資料湖的存取權。

導覽至 AWS Lake Formation 主控台，然後從側邊列的**管理**區段中選取**應用程式整合設定**選項。然後，勾選**允許外部引擎篩選向 Lake Formation 註冊的 Amazon S3 位置中的資料**方塊。新增執行 Spark 任務**AWS 的帳戶 IDs**，以及**工作階段標籤值**。

![\[應用程式整合設定\]](http://docs.aws.amazon.com/zh_tw/emr/latest/EMR-on-EKS-DevelopmentGuide/images/application_integration_settings_fgac.png)


請注意，稍後當您設定 IAM 角色時，在此傳遞的 **LakeFormationAuthorizedCaller** 工作階段標籤會在 **SecurityConfiguration** 中傳遞，如第 3 節所述。

## 步驟 2：設定 EKS RBAC 許可
<a name="security_iam_fgac-lf-enable-rbac"></a>

其次，您可以設定角色型存取控制的許可。

### 將 EKS 叢集許可提供給 Amazon EMR on EKS 服務
<a name="security_iam_fgac-lf-enable-rbac-cluster"></a>

Amazon EMR on EKS Service 必須具有 EKS 叢集角色許可，才能建立跨命名空間許可，讓系統驅動程式在使用者命名空間中分割使用者執行器。

**建立叢集角色**

此範例定義資源集合的許可。

```
vim emr-containers-cluster-role.yaml
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: emr-containers
rules:
  - apiGroups: [""]
    resources: ["namespaces"]
    verbs: ["get"]
  - apiGroups: [""]
    resources: ["serviceaccounts", "services", "configmaps", "events", "pods", "pods/log"]
    verbs: ["get", "list", "watch", "describe", "create", "edit", "delete", "deletecollection", "annotate", "patch", "label"]
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["create", "patch", "delete", "watch"]
  - apiGroups: ["apps"]
    resources: ["statefulsets", "deployments"]
    verbs: ["get", "list", "watch", "describe", "create", "edit", "delete", "annotate", "patch", "label"]
  - apiGroups: ["batch"]
    resources: ["jobs"]
    verbs: ["get", "list", "watch", "describe", "create", "edit", "delete", "annotate", "patch", "label"]
  - apiGroups: ["extensions", "networking.k8s.io"]
    resources: ["ingresses"]
    verbs: ["get", "list", "watch", "describe", "create", "edit", "delete", "annotate", "patch", "label"]
  - apiGroups: ["rbac.authorization.k8s.io"]
    resources: ["clusterroles","clusterrolebindings","roles", "rolebindings"]
    verbs: ["get", "list", "watch", "describe", "create", "edit", "delete", "deletecollection", "annotate", "patch", "label"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "describe", "create", "edit", "delete",  "deletecollection", "annotate", "patch", "label"]
  - apiGroups: ["kyverno.io"]
    resources: ["clusterpolicies"]
    verbs: ["create", "delete"]
---
```

```
kubectl apply -f emr-containers-cluster-role.yaml
```

**建立叢集角色繫結**

```
vim emr-containers-cluster-role-binding.yaml
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: emr-containers
subjects:
- kind: User
  name: emr-containers
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: emr-containers
  apiGroup: rbac.authorization.k8s.io
---
```

```
kubectl apply -f emr-containers-cluster-role-binding.yaml
```

### 提供 Amazon EMR on EKS 服務的命名空間存取權
<a name="security_iam_fgac-lf-enable-rbac-cluster"></a>

建立兩個 Kubernetes 命名空間，一個用於使用者驅動程式和執行器，另一個用於系統驅動程式和執行器，並啟用 Amazon EMR on EKS 服務存取，以在使用者和系統命名空間中提交任務。請遵循現有指南，為每個命名空間提供存取權，可在[使用 啟用叢集存取中`aws-auth`](https://docs.aws.amazon.com/emr/latest/EMR-on-EKS-DevelopmentGuide/setting-up-cluster-access.html#setting-up-cluster-access-aws-auth)取得。

## 步驟 3：設定使用者和系統設定檔元件的 IAM 角色
<a name="security_iam_fgac-lf-system-profile-configure"></a>

第三，您可以為特定元件設定角色。啟用 Lake Formation 的 Spark 任務有兩個元件：使用者和系統。使用者驅動程式和執行器會在使用者命名空間中執行，並與 StartJobRun API 中傳遞的 JobExecutionRole 繫結。系統驅動程式和執行器會在系統命名空間中執行，並與 **QueryEngine** 角色繫結。

### 設定查詢引擎角色
<a name="security_iam_fgac-lf-system-profile-configure-query"></a>

QueryEngine 角色與系統空間元件繫結，並具有使用 **LakeFormationAuthorizedCaller** 工作階段標籤擔任 **JobExecutionRole** 的許可。查詢引擎角色的 IAM 許可政策如下：

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "AssumeJobRoleWithSessionTagAccessForSystemDriver",
      "Effect": "Allow",
      "Action": [
        "sts:AssumeRole",
        "sts:TagSession"
      ],
      "Resource": [
        "arn:aws:iam::*:role/JobExecutionRole"
      ],
      "Condition": {
        "StringLike": {
          "aws:RequestTag/LakeFormationAuthorizedCaller": "EMR on EKS Engine"
        }
      }
    },
    {
      "Sid": "AssumeJobRoleWithSessionTagAccessForSystemExecutor",
      "Effect": "Allow",
      "Action": [
        "sts:AssumeRole"
      ],
      "Resource": [
        "arn:aws:iam::*:role/JobExecutionRole"
      ]
    },
    {
      "Sid": "CreateCertificateAccessForTLS",
      "Effect": "Allow",
      "Action": [
        "emr-containers:CreateCertificate"
      ],
      "Resource": [
        "*"
      ]
    }
  ]
}
```

------

設定查詢引擎角色的信任政策，以信任 Kubernetes 系統命名空間。

```
aws emr-containers update-role-trust-policy \ 
    --cluster-name eks cluster \ 
    --namespace eks system namespace \ 
    --role-name query_engine_iam_role_name
```

如需詳細資訊，請參閱[更新角色信任政策](https://docs.aws.amazon.com/emr/latest/EMR-on-EKS-DevelopmentGuide/setting-up-trust-policy.html)。

### 設定任務執行角色
<a name="security_iam_fgac-lf-system-profile-job"></a>

Lake Formation 許可控制對 AWS Glue Data Catalog 資源、Amazon S3 位置和這些位置基礎資料的存取。IAM 許可控制對 Lake Formation 和 AWS Glue APIs存取。雖然您可能擁有 Lake Formation 許可來存取 Data Catalog (SELECT) 中的資料表，但如果您沒有 `glue:Get*` API 操作的 IAM 許可，您的操作會失敗。

**JobExecutionRole** 的 IAM 許可政策：**JobExecution** 角色應在其許可政策中具有政策陳述式。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "GlueCatalogAccess",
      "Effect": "Allow",
      "Action": [
        "glue:Get*",
        "glue:Create*",
        "glue:Update*"
      ],
      "Resource": [
        "*"
      ]
    },
    {
      "Sid": "LakeFormationAccess",
      "Effect": "Allow",
      "Action": [
        "lakeformation:GetDataAccess"
      ],
      "Resource": [
        "*"
      ]
    },
    {
      "Sid": "CreateCertificateAccessForTLS",
      "Effect": "Allow",
      "Action": [
        "emr-containers:CreateCertificate"
      ],
      "Resource": [
        "*"
      ]
    }
  ]
}
```

------

**JobExecutionRole** 的 IAM 信任政策：

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "TrustQueryEngineRoleForSystemDriver",
      "Effect": "Allow",
      "Action": [
        "sts:AssumeRole",
        "sts:TagSession"
      ],
      "Resource": [
        "arn:aws:iam::*:role/QueryExecutionRole"
      ],
      "Condition": {
        "StringLike": {
          "aws:RequestTag/LakeFormationAuthorizedCaller": "EMR on EKS Engine"
        }
      }
    },
    {
      "Sid": "TrustQueryEngineRoleForSystemExecutor",
      "Effect": "Allow",
      "Action": [
        "sts:AssumeRole"
      ],
      "Resource": [
        "arn:aws:iam::*:role/QueryEngineRole"
      ]
    }
  ]
}
```

------

設定任務執行角色的信任政策，以信任 Kubernetes 使用者命名空間：

```
aws emr-containers update-role-trust-policy \ 
    --cluster-name eks cluster \ 
    --namespace eks User namespace \ 
    --role-name job_execution_role_name
```

如需詳細資訊，請參閱[更新任務執行角色的信任政策](https://docs.aws.amazon.com/emr/latest/EMR-on-EKS-DevelopmentGuide/setting-up-trust-policy.html)。

## 步驟 4：設定安全組態
<a name="security_iam_fgac-lf-security-config"></a>

若要執行已啟用 Lake Formation 的任務，您必須建立安全組態。

```
aws emr-containers create-security-configuration \
    --name 'security-configuration-name' \
    --security-configuration '{
        "authorizationConfiguration": {
            "lakeFormationConfiguration": {
                "authorizedSessionTagValue": "SessionTag configured in LakeFormation",
                "secureNamespaceInfo": {
                    "clusterId": "eks-cluster-name",
                    "namespace": "system-namespace-name"
                },
                "queryEngineRoleArn": "query-engine-IAM-role-ARN"
            }
        }
    }'
```

確保在 **authorizedSessionTagValue** 欄位中傳遞的工作階段標籤可以授權 Lake Formation。將 值設定為 中 Lake Formation 設定的值[步驟 1：設定 Lake Formation 型資料欄、資料列或儲存格層級許可](#security_iam_fgac-lf-enable-permissions)。

## 步驟 5：建立虛擬叢集
<a name="security_iam_fgac-lf-virtual-cluster"></a>

使用安全組態建立 Amazon EMR on EKS 虛擬叢集。

```
aws emr-containers create-virtual-cluster \
--name my-lf-enabled-vc \
--container-provider '{
    "id": "eks-cluster",
    "type": "EKS",
    "info": {
        "eksInfo": {
            "namespace": "user-namespace"
        }
    }
}' \
--security-configuration-id SecurityConfiguraionId
```

確保傳遞上一個步驟的 **SecurityConfiguration** ID，以便將 Lake Formation 授權組態套用至虛擬叢集上執行的所有任務。如需詳細資訊，請參閱[向 Amazon EMR 註冊 Amazon EKS 叢集]()。

## 步驟 6：在啟用 FGAC 的 VirtualCluster 中提交任務
<a name="security_iam_fgac-enabled-cluster"></a>

非 Lake Formation 和 Lake Formation 任務的任務提交程序都相同。如需詳細資訊，請參閱[使用 提交任務執行`StartJobRun`](https://docs.aws.amazon.com/emr/latest/EMR-on-EKS-DevelopmentGuide/emr-eks-jobs-submit.html)。

系統驅動程式的 Spark 驅動程式、執行器和事件日誌存放在 AWS 服務帳戶的 S3 儲存貯體中以進行偵錯。我們建議在任務執行中設定客戶管理的 KMS 金鑰，以加密存放在 AWS 服務儲存貯體中的所有日誌。如需啟用日誌加密的詳細資訊，請參閱[加密 Amazon EMR on EKS 日誌](https://docs.aws.amazon.com/emr/latest/EMR-on-EKS-DevelopmentGuide/security_iam_fgac-logging-kms.html)。