

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

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

在 Amazon EMR 7.7 及更高版本中，您可以利用 AWS Lake Formation 对由 Amazon S3 支持的数据目录表应用精细的访问控制。此功能允许您为 Amazon EMR on EKS Spark Jobs 中的读取查询配置表、行、列和单元格级访问控制。

本节介绍如何创建安全配置和设置 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_cn/emr/latest/EMR-on-EKS-DevelopmentGuide/images/application_integration_settings_fgac.png)


请注意，此处传递的**LakeFormationAuthorizedCaller**会话标签**SecurityConfiguration**将在稍后设置 IAM 角色时在第 3 节中传递。

## 步骤 2：设置 EKS RBAC 权限
<a name="security_iam_fgac-lf-enable-rbac"></a>

第二，设置基于角色的访问控制权限。

### 向 Amazon EMR on EKS 服务提供 EKS 集群权限
<a name="security_iam_fgac-lf-enable-rbac-cluster"></a>

Amazon EMR on EKS 服务必须具有 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 作业包含两个组件：用户和系统。用户驱动程序和执行器在 User 命名空间中运行，并与 StartJobRun API 中传递的 JobExecutionRole 命名空间相关联。系统驱动程序和执行器在 System 命名空间中运行，并与**QueryEngine**角色相关联。

### 配置查询引擎角色
<a name="security_iam_fgac-lf-system-profile-configure-query"></a>

该 QueryEngine 角色与系统空间组件相关联，并且有权担任 with Sess **JobExecutionRole**i **LakeFormationAuthorizedCaller**on 标签。查询引擎角色的 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 For AWS mation 权限控制对 Glue 数据目录资源、Amazon S3 位置以及这些位置的基础数据的访问权限。IAM 权限控制对 Lake Formation 和 AWS Glue APIs 以及资源的访问。虽然您可能拥有 Lake Formation 权限来访问 Data Catalog (SELECT) 中的表，但如果没有对 `glue:Get*` API 操作的 IAM 权限，操作就会失败。

IAM 权限策略**JobExecutionRole**为：**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": [
        "*"
      ]
    }
  ]
}
```

------

适用于以下内容的 IAM 信任政策 **JobExecutionRole**：

------
#### [ 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"
            }
        }
    }'
```

确保在 “**authorizedSessionTag值**” 字段中传递的会话标签可以授权 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 中提交 Job 已启用 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 存储桶中以进行调试。我们建议在 Job Run 中配置客户管理的 KMS 密钥，以加密存储在 AWS 服务存储桶中的所有日志。有关启用日志加密的更多信息，请参阅[加密 Amazon EMR on EKS 日志](https://docs.aws.amazon.com/emr/latest/EMR-on-EKS-DevelopmentGuide/security_iam_fgac-logging-kms.html)。