

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

# Glue AWS 的身分型政策範例
<a name="security_iam_id-based-policy-examples"></a>

根據預設，使用者和角色沒有建立或修改 AWS Glue 資源的許可。若要授予使用者對其所需資源執行動作的許可，IAM 管理員可以建立 IAM 政策。

如需了解如何使用這些範例 JSON 政策文件建立 IAM 身分型政策，請參閱《*IAM 使用者指南*》中的[建立 IAM 政策 (主控台)](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create-console.html)。

如需 Glue AWS 定義的動作和資源類型的詳細資訊，包括每種資源類型的 ARNs 格式，請參閱*《服務授權參考*》中的 [Glue AWS 的動作、資源和條件索引鍵](https://docs.aws.amazon.com/service-authorization/latest/reference/list_awsglue.html)。

**注意**  
本節提供的範例全部使用 `us-west-2` 區域。您可以將此取代為您要使用的 AWS 區域。

**Topics**
+ [政策最佳實務](#security_iam_service-with-iam-policy-best-practices)
+ [資源層級許可僅適用特定的 AWS Glue 物件](#glue-identity-based-policy-limitations)
+ [使用 AWS Glue 主控台](#security_iam_id-based-policy-examples-console)
+ [允許使用者檢視他們自己的許可](#security_iam_id-based-policy-examples-view-own-permissions)
+ [授予資料表的唯讀許可](#security_iam_id-based-policy-examples-read-only-table-access)
+ [依據 GetTables 許可篩選資料表](#security_iam_id-based-policy-examples-filter-tables)
+ [授予完整存取資料表和所有分割區的許可](#security_iam_id-based-policy-examples-full-access-tables-partitions)
+ [透過名稱字首和明確拒絕的存取控制](#security_iam_id-based-policy-examples-deny-by-name-prefix)
+ [使用標籤授權存取](#tags-control-access-example-triggers-allow)
+ [使用標籤拒絕存取](#tags-control-access-example-triggers-deny)
+ [搭配使用標籤與清單和批次 API 操作](#tags-control-access-example-triggers-list-batch)
+ [使用條件索引鍵或內容索引鍵來控制設定](#glue-identity-based-policy-condition-keys)
+ [拒絕給予身分建立資料預覽工作階段的能力](#deny-data-preview-sessions-per-identity)

## 政策最佳實務
<a name="security_iam_service-with-iam-policy-best-practices"></a>

身分型政策會判斷您帳戶中的某個人員是否可以建立、存取或刪除 AWS Glue 資源。這些動作可能會讓您的 AWS 帳戶產生費用。當您建立或編輯身分型政策時，請遵循下列準則及建議事項：
+ **開始使用 AWS 受管政策並邁向最低權限許可** – 若要開始將許可授予您的使用者和工作負載，請使用將許可授予許多常見使用案例的 *AWS 受管政策*。它們可在您的 中使用 AWS 帳戶。我們建議您定義特定於使用案例 AWS 的客戶受管政策，以進一步減少許可。如需更多資訊，請參閱《*IAM 使用者指南*》中的 [AWS 受管政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html#aws-managed-policies)或[任務職能的AWS 受管政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_job-functions.html)。
+ **套用最低權限許可** – 設定 IAM 政策的許可時，請僅授予執行任務所需的許可。為實現此目的，您可以定義在特定條件下可以對特定資源採取的動作，這也稱為*最低權限許可*。如需使用 IAM 套用許可的更多相關資訊，請參閱《*IAM 使用者指南*》中的 [IAM 中的政策和許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html)。
+ **使用 IAM 政策中的條件進一步限制存取權** – 您可以將條件新增至政策，以限制動作和資源的存取。例如，您可以撰寫政策條件，指定必須使用 SSL 傳送所有請求。如果透過特定 例如 使用服務動作 AWS 服務，您也可以使用條件來授予其存取權 CloudFormation。如需詳細資訊，請參閱《*IAM 使用者指南*》中的 [IAM JSON 政策元素：條件](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html)。
+ **使用 IAM Access Analyzer 驗證 IAM 政策，確保許可安全且可正常運作** – IAM Access Analyzer 驗證新政策和現有政策，確保這些政策遵從 IAM 政策語言 (JSON) 和 IAM 最佳實務。IAM Access Analyzer 提供 100 多項政策檢查及切實可行的建議，可協助您撰寫安全且實用的政策。如需詳細資訊，請參閱《*IAM 使用者指南*》中的[使用 IAM Access Analyzer 驗證政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-policy-validation.html)。
+ **需要多重要素驗證 (MFA)** – 如果您的案例需要 IAM 使用者或 中的根使用者 AWS 帳戶，請開啟 MFA 以提高安全性。如需在呼叫 API 操作時請求 MFA，請將 MFA 條件新增至您的政策。如需詳細資訊，請參閱《*IAM 使用者指南*》中的[透過 MFA 的安全 API 存取](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_configure-api-require.html)。

如需 IAM 中最佳實務的相關資訊，請參閱《*IAM 使用者指南*》中的 [IAM 安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。

## 資源層級許可僅適用特定的 AWS Glue 物件
<a name="glue-identity-based-policy-limitations"></a>

您只能在 AWS Glue 中定義特定物件的精細控制。因此，您必須編寫您用戶端的 IAM 政策，讓允許 `Resource` 陳述式 Amazon Resource Name (ARN) 的 API 操作，不會與不允許 ARN 的 API 操作混合。

例如，以下 IAM 政策允許 `GetClassifier` 和 `GetJobRun` 的 API 操作。它將 `Resource` 定義為 `*`，因為 AWS Glue 不允許分類器和任務回合的 ARN。因為允許特定 API 操作的 ARN (例如 `GetDatabase` 和 `GetTable`)，所以可以在政策的下半部指定 ARN。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "glue:GetClassifier*",
        "glue:GetJobRun*"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "glue:Get*"
      ],
      "Resource": [
        "arn:aws:glue:us-east-1:111122223333:catalog",
        "arn:aws:glue:us-east-1:111122223333:database/default",
        "arn:aws:glue:us-east-1:111122223333:table/default/e*1*",
        "arn:aws:glue:us-east-1:111122223333:connection/connection2"
      ]
    }
  ]
}
```

------

如需允許 ARN 的 AWS Glue 物件清單，請參閱[指定 AWS Glue 資源 ARN](glue-specifying-resource-arns.md) 

## 使用 AWS Glue 主控台
<a name="security_iam_id-based-policy-examples-console"></a>

若要存取 AWS Glue 主控台，您必須擁有一組最低許可。這些許可必須允許您列出和檢視 AWS Glue 資源的詳細資訊 AWS 帳戶。如果您建立比最基本必要許可更嚴格的身分型政策，則對於具有該政策的實體 (使用者或角色) 而言，主控台就無法如預期運作。

對於僅呼叫 AWS CLI 或 AWS API 的使用者，您不需要允許最低主控台許可。反之，只需允許存取符合他們嘗試執行之 API 操作的動作就可以了。

為了確保使用者和角色仍然可以使用 AWS Glue 主控台，請將 AWS Glue `ConsoleAccess`或 `ReadOnly` AWS 受管政策連接到實體。如需詳細資訊，請參閱《*IAM 使用者指南*》中的[新增許可到使用者](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_change-permissions.html#users_change_permissions-add-console)。

若要讓使用者使用 AWS Glue 主控台，該使用者必須擁有一組最低許可，允許他們使用其 AWS 帳戶AWS Glue的資源。除了這些 AWS Glue 許可之外，主控台還需要以下服務的許可：
+ 顯示日誌的 Amazon CloudWatch Logs 許可。
+ AWS Identity and Access Management 列出和傳遞角色的 (IAM) 許可。
+ AWS CloudFormation 使用堆疊的許可。
+ 列出 VPC、子網路、安全群組、執行個體和其他物件的 Amazon Elastic Compute Cloud (Amazon EC2) 許可。
+ 列出儲存貯體和物件，以及擷取和儲存指令碼的 Amazon Simple Storage Service (Amazon S3) 許可。
+ 使用叢集的 Amazon Redshift 許可
+ 列出執行個體的 Amazon Relational Database Service (Amazon RDS) 許可。

如需使用者檢視和使用 AWS Glue 主控台所需許可的詳細資訊，請參閱[步驟 3：連接政策到存取 AWS Glue 的使用者或群組](attach-policy-iam-user.md)。

如果您建立比最基本必要許可更嚴格的 IAM 政策，則對於採取該 IAM 政策的使用者而言，主控台就無法如預期運作。為確保這些使用者仍可使用 AWS Glue 主控台，也請連接 [AWS 的 受管 （預先定義） 政策 AWS Glue](security-iam-awsmanpol.md#access-policy-examples-aws-managed) 中所述的 `AWSGlueConsoleFullAccess` 受管政策。

## 允許使用者檢視他們自己的許可
<a name="security_iam_id-based-policy-examples-view-own-permissions"></a>

此範例會示範如何建立政策，允許 IAM 使用者檢視附加到他們使用者身分的內嵌及受管政策。此政策包含在 主控台或使用 或 AWS CLI AWS API 以程式設計方式完成此動作的許可。

```
{
    "Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "ViewOwnUserInfo",
            "Effect": "Allow",
            "Action": [
                "iam:GetUserPolicy",
                "iam:ListGroupsForUser",
                "iam:ListAttachedUserPolicies",
                "iam:ListUserPolicies",
                "iam:GetUser"
            ],
            "Resource": ["arn:aws:iam::*:user/${aws:username}"]
        },
        {
            "Sid": "NavigateInConsole",
            "Effect": "Allow",
            "Action": [
                "iam:GetGroupPolicy",
                "iam:GetPolicyVersion",
                "iam:GetPolicy",
                "iam:ListAttachedGroupPolicies",
                "iam:ListGroupPolicies",
                "iam:ListPolicyVersions",
                "iam:ListPolicies",
                "iam:ListUsers"
            ],
            "Resource": "*"
        }
    ]
}
```

## 授予資料表的唯讀許可
<a name="security_iam_id-based-policy-examples-read-only-table-access"></a>

下列政策可授予 `db1` 資料庫中 `books` 資料表的唯讀許可。如需資源 Amazon Resource Name (ARN) 的詳細資訊，請參閱[Data Catalog ARN](glue-specifying-resource-arns.md#data-catalog-resource-arns)。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "GetTablesActionOnBooks",
      "Effect": "Allow",
      "Action": [
        "glue:GetTables",
        "glue:GetTable"
      ],
      "Resource": [
        "arn:aws:glue:us-east-1:111122223333:catalog",
        "arn:aws:glue:us-east-1:111122223333:database/db1",
        "arn:aws:glue:us-east-1:111122223333:table/db1/books"
      ]
    }
  ]
}
```

------

此政策會授予名為 `db1` 之資料庫中名為 `books` 之資料表的唯讀許可。若要將 `Get` 許可授予資料表，則也需要目錄和資料庫資源的該許可。

下列政策會授予在 `db1` 資料庫中建立 `tb1` 資料表的最低必要許可：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "glue:CreateTable"
      ],
      "Resource": [
        "arn:aws:glue:us-east-1:111122223333:table/db1/tbl1",
        "arn:aws:glue:us-east-1:111122223333:database/db1",
        "arn:aws:glue:us-east-1:111122223333:catalog"
      ]
    }
  ]
}
```

------

## 依據 GetTables 許可篩選資料表
<a name="security_iam_id-based-policy-examples-filter-tables"></a>

假設有三個資料表 `customers`、`stores` 以及 `store_sales` 在 `db1` 資料庫中。下列政策會授予 `GetTables` 許可給 `stores` 和 `store_sales`，但不會授予給 `customers`。當您使用此政策呼叫 `GetTables` 時，結果只會包含兩個授權的資料表 (不會傳回 `customers` 資料表)。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "GetTablesExample",
      "Effect": "Allow",
      "Action": [
        "glue:GetTables"
      ],
      "Resource": [
        "arn:aws:glue:us-east-1:111122223333:catalog",
        "arn:aws:glue:us-east-1:111122223333:database/db1",
        "arn:aws:glue:us-east-1:111122223333:table/db1/store_sales",
        "arn:aws:glue:us-east-1:111122223333:table/db1/stores"
      ]
    }
  ]
}
```

------

您可以使用 `store*` 來比對任何開頭為 `store` 的資料表名稱，藉此簡化上述政策。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "GetTablesExample2",
      "Effect": "Allow",
      "Action": [
        "glue:GetTables"
      ],
      "Resource": [
        "arn:aws:glue:us-east-1:111122223333:catalog",
        "arn:aws:glue:us-east-1:111122223333:database/db1",
        "arn:aws:glue:us-east-1:111122223333:table/db1/store*"
      ]
    }
  ]
}
```

------

同樣地，使用 `/db1/*` 比對 `db1` 中所有資料表時，下列政策可將 `GetTables` 存取授予 `db1` 中所有資料表。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "GetTablesReturnAll",
      "Effect": "Allow",
      "Action": [
        "glue:GetTables"
      ],
      "Resource": [
        "arn:aws:glue:us-east-1:111122223333:catalog",
        "arn:aws:glue:us-east-1:111122223333:database/db1",
        "arn:aws:glue:us-east-1:111122223333:table/db1/*"
      ]
    }
  ]
}
```

------

如未提供任何資料表 ARN，則可成功呼叫 `GetTables`，但會傳回空白清單。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "GetTablesEmptyResults",
      "Effect": "Allow",
      "Action": [
        "glue:GetTables"
      ],
      "Resource": [
        "arn:aws:glue:us-east-1:111122223333:catalog",
        "arn:aws:glue:us-east-1:111122223333:database/db1"
      ]
    }
  ]
}
```

------

如果政策中缺少資料庫 ARN，則呼叫 `GetTables` 會失敗，並顯示 `AccessDeniedException`。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "GetTablesAccessDeny",
      "Effect": "Allow",
      "Action": [
        "glue:GetTables"
      ],
      "Resource": [
        "arn:aws:glue:us-east-1:111122223333:catalog",
        "arn:aws:glue:us-east-1:111122223333:table/db1/*"
      ]
    }
  ]
}
```

------

## 授予完整存取資料表和所有分割區的許可
<a name="security_iam_id-based-policy-examples-full-access-tables-partitions"></a>

下列政策可授予 `db1` 資料庫中名稱為 `books` 之資料表的所有許可。這包括資料表本身、其存檔版本和其所有分割區的讀取和寫入許可。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "FullAccessOnTable",
      "Effect": "Allow",
      "Action": [
        "glue:CreateTable",
        "glue:GetTable",
        "glue:GetTables",
        "glue:UpdateTable",
        "glue:DeleteTable",
        "glue:BatchDeleteTable",
        "glue:GetTableVersion",
        "glue:GetTableVersions",
        "glue:DeleteTableVersion",
        "glue:BatchDeleteTableVersion",
        "glue:CreatePartition",
        "glue:BatchCreatePartition",
        "glue:GetPartition",
        "glue:GetPartitions",
        "glue:BatchGetPartition",
        "glue:UpdatePartition",
        "glue:DeletePartition",
        "glue:BatchDeletePartition"
      ],
      "Resource": [
        "arn:aws:glue:us-east-1:111122223333:catalog",
        "arn:aws:glue:us-east-1:111122223333:database/db1",
        "arn:aws:glue:us-east-1:111122223333:table/db1/books"
      ]
    }
  ]
}
```

------

上述政策在實務中可予以簡化。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "FullAccessOnTable",
      "Effect": "Allow",
      "Action": [
        "glue:*Table*",
        "glue:*Partition*"
      ],
      "Resource": [
        "arn:aws:glue:us-east-1:111122223333:catalog",
        "arn:aws:glue:us-east-1:111122223333:database/db1",
        "arn:aws:glue:us-east-1:111122223333:table/db1/books"
      ]
    }
  ]
}
```

------

請注意，精細定義存取控制的最低精細程度是資料表層級。這表示您無法授予使用者存取資料表中某些分割區而非其他分割區，或是存取某些資料表欄位而非其他欄位。使用者可以存取整個資料表，不然就是完全不能存取資料表。

## 透過名稱字首和明確拒絕的存取控制
<a name="security_iam_id-based-policy-examples-deny-by-name-prefix"></a>

在此範例中，假設 Glue Data Catalog AWS 中的資料庫和資料表是使用名稱字首進行組織。開發階段中的資料庫名稱字首為 `dev-`，而生產階段中的資料庫名稱字首為 `prod-`。您可以使用下列政策授予開發人員完整存取含 `dev-` 字首的所有資料庫、資料表、UDF 等。但是，若是含 `prod-` 字首的所有項目，您只授予唯讀存取。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "DevAndProdFullAccess",
      "Effect": "Allow",
      "Action": [
        "glue:*Database*",
        "glue:*Table*",
        "glue:*Partition*",
        "glue:*UserDefinedFunction*",
        "glue:*Connection*"
      ],
      "Resource": [
        "arn:aws:glue:us-east-1:111122223333:catalog",
        "arn:aws:glue:us-east-1:111122223333:database/dev-*",
        "arn:aws:glue:us-east-1:111122223333:database/prod-*",
        "arn:aws:glue:us-east-1:111122223333:table/dev-*/*",
        "arn:aws:glue:us-east-1:111122223333:table/*/dev-*",
        "arn:aws:glue:us-east-1:111122223333:table/prod-*/*",
        "arn:aws:glue:us-east-1:111122223333:table/*/prod-*",
        "arn:aws:glue:us-east-1:111122223333:userDefinedFunction/dev-*/*",
        "arn:aws:glue:us-east-1:111122223333:userDefinedFunction/*/dev-*",
        "arn:aws:glue:us-east-1:111122223333:userDefinedFunction/prod-*/*",
        "arn:aws:glue:us-east-1:111122223333:userDefinedFunction/*/prod-*",
        "arn:aws:glue:us-east-1:111122223333:connection/dev-*",
        "arn:aws:glue:us-east-1:111122223333:connection/prod-*"
      ]
    },
    {
      "Sid": "ProdWriteDeny",
      "Effect": "Deny",
      "Action": [
        "glue:*Create*",
        "glue:*Update*",
        "glue:*Delete*"
      ],
      "Resource": [
        "arn:aws:glue:us-east-1:111122223333:database/prod-*",
        "arn:aws:glue:us-east-1:111122223333:table/prod-*/*",
        "arn:aws:glue:us-east-1:111122223333:table/*/prod-*",
        "arn:aws:glue:us-east-1:111122223333:userDefinedFunction/prod-*/*",
        "arn:aws:glue:us-east-1:111122223333:userDefinedFunction/*/prod-*",
        "arn:aws:glue:us-east-1:111122223333:connection/prod-*"
      ]
    }
  ]
}
```

------

上述政策中的第二個陳述式會使用明確 `deny`。您可以使用明確 `deny` 來覆寫已授予委託人的任何 `allow` 許可。這可讓您鎖定關鍵資源的存取權，並防止其他政策意外授予這些資源的存取權。

在上述範例中，即使第一個陳述式授予 `prod-` 資源的完整存取，第二個陳述式仍明確撤銷其寫入存取，而只保留 `prod-` 資源的唯讀取存取。

## 使用標籤授權存取
<a name="tags-control-access-example-triggers-allow"></a>

例如，假設您想要限定只有您帳戶下名為 `Tom` 的特定使用者，才能存取觸發 `t2`。所有其他使用者，包括 `Sam`，都可以存取該觸發條件 `t1`。觸發條件 `t1` 和 `t2` 具備下列屬性。

```
aws glue get-triggers
{
    "Triggers": [
        {
            "State": "CREATED",
            "Type": "SCHEDULED",
            "Name": "t1",
            "Actions": [
                {
                    "JobName": "j1"
                }
            ],
            "Schedule": "cron(0 0/1 * * ? *)"
        },
        {
            "State": "CREATED",
            "Type": "SCHEDULED",
            "Name": "t2",
            "Actions": [
                {
                    "JobName": "j1"
                }
            ],
            "Schedule": "cron(0 0/1 * * ? *)"
        }
    ]
}
```

AWS Glue 管理員已將標籤值 `Tom` (`aws:ResourceTag/Name": "Tom"`) 連接到觸發條件 `t2`。AWS Glue 管理員也對 Tom 實施搭配以標籤為基礎之條件陳述式的 IAM 政策。因此，Tom 能使用的 AWS Glue 操作，將僅限於操作標籤值 `Tom` 的資源。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "glue:*",
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/Name": "Tom"
        }
      }
    }
  ]
}
```

------

當 Tom 嘗試存取觸發 `t1` 時，他會收到拒絕存取訊息。同時，他可以成功擷取觸發條件 `t2`。

```
aws glue get-trigger --name t1

An error occurred (AccessDeniedException) when calling the GetTrigger operation: User: Tom is not authorized to perform: glue:GetTrigger on resource: arn:aws:glue:us-east-1:123456789012:trigger/t1

aws glue get-trigger --name t2
{
    "Trigger": {
        "State": "CREATED",
        "Type": "SCHEDULED",
        "Name": "t2",
        "Actions": [
            {
                "JobName": "j1"
            }
        ],
        "Schedule": "cron(0 0/1 * * ? *)"
    }
}
```

Tom 無法使用複數 `GetTriggers` API 操作來列出觸發，因為此作業不支援篩選標籤。

為讓 Tom 存取 `GetTriggers`，AWS Glue 管理員要建立一項政策，將這些許可分為兩部分。其中一區可讓 Tom 使用 `GetTriggers` API 操作，存取所有觸發條件。第二區則讓 Tom 存取已標記值 `Tom` 的 API 操作。透過此政策，便可允許 Tom 同時擁有觸發條件 `t2` 的 `GetTriggers` 和 `GetTrigger` 存取權。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "glue:GetTriggers",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "glue:*",
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/Name": "Tom"
        }
      }
    }
  ]
}
```

------

## 使用標籤拒絕存取
<a name="tags-control-access-example-triggers-deny"></a>

另一種資源政策方法就是明確拒絕資源存取。

**重要**  
明確拒絕政策不適用於 `GetTriggers` 等複數 API 操作。

在下列範例政策中，允許所有 AWS Glue 任務操作。但是，第二個 `Effect` 陳述式*明確拒絕*存取標記為 `Team` 索引鍵和 `Special` 值的任務。

當管理員將以下政策連接至身分時，身分能存取除標記為 `Team` 索引鍵和 `Special` 值*以外*的所有任務。

## 搭配使用標籤與清單和批次 API 操作
<a name="tags-control-access-example-triggers-list-batch"></a>

撰寫資源政策的第三個方法，就是使用 `List` API 操作來列出符合標籤值的資源清單，進而允許資源的存取。然後，使用對應的 `Batch` API 操作，允許存取特定資源的詳細資訊。使用此方法時，管理員並不需要允許存取複數的 `GetCrawlers`、`GetDevEndpoints`、`GetJobs` 或 `GetTriggers` API 操作。相反地，您可以搭配下列 API 操作，允許列出資源的操作：
+ `ListCrawlers`
+ `ListDevEndpoints`
+ `ListJobs`
+ `ListTriggers`

此外，您可以搭配下列 API 操作，允許取得個別資源詳細資訊的操作：
+ `BatchGetCrawlers`
+ `BatchGetDevEndpoints`
+ `BatchGetJobs`
+ `BatchGetTriggers`

身為管理員，您可以使用此方法執行下列操作：

1. 新增標籤到您的爬蟲程式、開發端點、任務和觸發條件。

1. 拒絕使用者存取 `Get` API 操作，例如 `GetCrawlers`、`GetDevEndponts`、`GetJobs` 和 `GetTriggers`。

1. 為了讓使用者能夠找出其具備存取權的標記資源，這時要允許使用者存取 `List` API 操作，例如 `ListCrawlers`、`ListDevEndponts`、`ListJobs` 和 `ListTriggers`。

1. 拒絕使用者存取 AWS Glue 標記 API，例如 `TagResource` 和 `UntagResource`。

1. 允許使用者搭配 `BatchGet` API 操作來存取資源詳細資訊，例如 `BatchGetCrawlers`、`BatchGetDevEndponts`、`BatchGetJobs` 和 `BatchGetTriggers`。

例如，呼叫 `ListCrawlers` 操作時，提供標籤值以比對使用者名稱。然後，會得到符合所提供標籤值的爬蟲程式清單結果。將名稱清單提供給 `BatchGetCrawlers`，取得每個具備指定標籤之爬蟲程式的詳細資訊。

例如，如果 Tom 應該只能擷取已標記 `Tom` 之觸發條件的詳細資訊，則管理員可以將標籤新增至 `Tom` 的觸發條件，向所有使用者拒絕 `GetTriggers` API 操作的存取，而允許所有使用者對於 `ListTriggers` 和 `BatchGetTriggers` 的存取。

下面是 AWS Glue 管理員授予 Tom 的資源政策。在政策的第一區中，`GetTriggers` 已拒絕用於 AWS Glue API 操作。在政策的第二區中，允許 `ListTriggers` 用於所有資源。但在第三區中，標記 `Tom` 的這些資源就會允許 `BatchGetTriggers` 存取權進行存取。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Deny",
      "Action": "glue:GetTriggers",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "glue:ListTriggers"
      ],
      "Resource": [
        "*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "glue:BatchGetTriggers"
      ],
      "Resource": [
        "*"
      ],
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/Name": "Tom"
        }
      }
    }
  ]
}
```

------

使用與前例相同的觸發時，Tom 可以存取觸發 `t2`，但不觸發 `t1`。下面範例示範當 Tom 嘗試搭配 `BatchGetTriggers`，存取 `t1` 和 `t2` 時的結果。

```
aws glue batch-get-triggers --trigger-names t2
{
    "Triggers": {
        "State": "CREATED",
        "Type": "SCHEDULED",
        "Name": "t2",
        "Actions": [
            {
                "JobName": "j2"
            }
        ],
        "Schedule": "cron(0 0/1 * * ? *)"
    }
}

aws glue batch-get-triggers  --trigger-names t1

An error occurred (AccessDeniedException) when calling the BatchGetTriggers operation: No access to any requested resource.
```

下面範例示範當 Tom 嘗試使用同的 `BatchGetTriggers` 呼叫，同時存取觸發條件 `t2` 和觸發條件 `t3` (其實並不存在) 時的結果。請注意，因為 Tom 具備觸發條件 `t2` 的存取權，且只有當 `t2` 傳回時才具備此存取權限。雖然 Tom 獲允存取觸發條件 `t3`，但觸發條件 `t3` 並不存在，因此回應時傳回的 `t3` 會列在 `"TriggersNotFound": []` 清單中。

```
aws glue batch-get-triggers --trigger-names t2 t3
{
    "Triggers": {
        "State": "CREATED",
        "Type": "SCHEDULED",
        "Name": "t2",
        "Actions": [
            {
                "JobName": "j2"
            }
        ],
        "TriggersNotFound": ["t3"],
        "Schedule": "cron(0 0/1 * * ? *)"
    }
}
```

## 使用條件索引鍵或內容索引鍵來控制設定
<a name="glue-identity-based-policy-condition-keys"></a>

授予建立和更新任務的許可時，您可以使用條件索引鍵或內容索引鍵。以下章節討論索引鍵：
+ [控制使用條件索引鍵來控制設定的政策](#glue-identity-based-policy-condition-key-vpc)
+ [控制使用內容索引鍵來控制設定的政策](#glue-identity-based-policy-context-key-glue)

### 控制使用條件索引鍵來控制設定的政策
<a name="glue-identity-based-policy-condition-key-vpc"></a>

AWS Glue 提供三個 IAM 條件金鑰 `glue:VpcIds`、 `glue:SubnetIds`和 `glue:SecurityGroupIds`。授予建立和更新任務的許可時，您可以在 IAM 政策中使用條件索引鍵。您可以使用此設定來確保未建立 (或更新) 任務或工作階段，以在所需的 VPC 環境之外執行。VPC 設定資訊不是直接從 `CreateJob` 請求輸入，而是從任務「連線」欄位中推斷，該欄位指向 AWS Glue 連線。

**範例使用方式**  
建立名為 "traffic-monitored-connection" 的 AWS Glue 網路類型連線，其具有所需的 VpcId "vpc-id1234"、SubnetIds 和 SecurityGroupIds。

為 IAM 政策中的 `CreateJob` 和 `UpdateJob` 動作指定條件索引鍵。

```
{
  "Effect": "Allow",
  "Action": [
    "glue:CreateJob",
    "glue:UpdateJob"
  ],
  "Resource": [
    "*"
  ],
  "Condition": {
    "ForAnyValue:StringLike": {
      "glue:VpcIds": [
        "vpc-id1234"
      ]
    }
  }
}
```

 您可以建立類似的 IAM 政策，以禁止建立 AWS Glue 任務，而不指定連線資訊。

**限制 VPC 上的工作階段**

 若要強制建立的工作階段在指定的 VPC 內執行，您可以透過在 `glue:CreateSession` 動作上新增 `Deny` 效果來限制角色許可，條件是 glue:vpc-id 不等於 vpc-<123>。例如：

```
"Effect": "Deny",
"Action": [
    "glue:CreateSession"
 ],
"Condition": {
    "StringNotEquals" : {"glue:VpcIds" : ["vpc-123"]} 
}
```

 您也可以透過在 `glue:CreateSession` 動作上新增 `Deny` 效果來強制建立的工作階段在 VPC 內執行，條件是 `glue:vpc-id` 為 Null。例如：

```
{
    "Effect": "Deny",
    "Action": [
        "glue:CreateSession"
    ],
      "Condition": {
        "Null": {"glue:VpcIds": true}
    }
}, 
{
    "Effect": "Allow",
    "Action": [
        "glue:CreateSession"
    ],
    "Resource": ["*"]
}
```

### 控制使用內容索引鍵來控制設定的政策
<a name="glue-identity-based-policy-context-key-glue"></a>

AWS Glue 為每個角色工作階段提供內容索引鍵 (`glue:CredentialIssuingService= glue.amazonaws.com`)，AWS Glue可供任務和開發人員端點使用。這可讓您針對AWS Glue指令碼所採取的動作實作安全控制。 會為每個角色工作階段AWS Glue提供另一個內容金鑰 AWS (`glue:RoleAssumedBy=glue.amazonaws.com`)，其中 代表客戶 （而非任務/開發端點，而是直接由AWS Glue服務） AWS Glue呼叫其他服務。

**範例使用方式**  
在 IAM 政策中指定條件性許可，並將其連接至 AWS Glue 任務使用的角色。根據角色工作階段是否用於 AWS Glue 任務執行時間環境，這可確保某些動作被允許/拒絕。

```
{
    "Effect": "Allow",
    "Action": "s3:GetObject",
    "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*",
    "Condition": {
        "StringEquals": {
            "glue:CredentialIssuingService": "glue.amazonaws.com"
        }
    }
}
```

## 拒絕給予身分建立資料預覽工作階段的能力
<a name="deny-data-preview-sessions-per-identity"></a>

本節包含的 IAM 政策範例可用於拒絕給予身分建立資料預覽工作階段的能力。將此政策連接至身分，此身分與資料預覽工作階段在執行期間所使用的角色不同。

```
{
    "Sid": "DatapreviewDeny",
    "Effect": "Deny",
     "Action": [
           "glue:CreateSession"
     ],
     "Resource": [
          "arn:aws:glue:*:*:session/glue-studio-datapreview*"
      ]
 }
```