

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

# 使用 部署公有子網路的偵測屬性型存取控制 AWS Config
<a name="deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config"></a>

*Amazon Web Services，Alberto Menendez*

## 摘要
<a name="deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config-summary"></a>

分散式邊緣網路架構依賴於與虛擬私有雲端 (VPCs) 中的工作負載一起執行的網路邊緣安全性。相較於更常見、集中的方法，這提供了前所未有的可擴展性。雖然在工作負載帳戶中部署公有子網路可以帶來好處，但它也會帶來新的安全風險，因為它會增加攻擊面。我們建議您僅在這些 VPCs 的公有子網路中部署 Elastic Load Balancing 資源，例如 Application Load Balancer 或 NAT 閘道。在專用公有子網路中使用負載平衡器和 NAT 閘道，可協助您針對傳入和傳出流量實作精細的控制。

我們建議您同時實作預防性和偵測性控制，以限制可在公有子網路中部署的資源類型。如需使用屬性型存取控制 (ABAC) 部署公有子網路預防性控制的詳細資訊，請參閱[部署公有子網路的預防性屬性型存取控制](https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/deploy-preventative-attribute-based-access-controls-for-public-subnets.html)。雖然對大多數情況都有效，但這些預防性控制可能無法解決所有可能的使用案例。因此，此模式以 ABAC 方法為基礎，可協助您設定有關部署在公有子網路中不合規資源的提醒。解決方案會檢查彈性網路介面是否屬於公有子網路中不允許的資源。

為了達成此目的，此模式使用[AWS Config 自訂規則](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules.html)和 [ABAC](https://aws.amazon.com/identity/attribute-based-access-control/)。自訂規則會在建立或修改彈性網路界面時處理其組態。在高階，此規則會執行兩個動作來判斷網路界面是否合規：

1. 若要判斷網路界面是否在規則範圍內，規則會檢查子網路是否具有指出其為公有子網路的特定[AWS 標籤](https://docs.aws.amazon.com/tag-editor/latest/userguide/tagging.html)。例如，此標籤可能是 `IsPublicFacing=True`。

1. 如果網路界面部署在公有子網路中，則規則會檢查 AWS 服務 建立此資源的 。如果資源不是 Elastic Load Balancing 資源或 NAT 閘道，則會將該資源標記為不合規。

## 先決條件和限制
<a name="deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶
+ AWS Config，在工作負載帳戶中[設定](https://docs.aws.amazon.com/config/latest/developerguide/gs-console.html) 
+ 在工作負載帳戶中部署所需資源的許可
+ 具有公有子網路的 VPC
+ 正確套用標籤以識別目標公有子網路
+ （選用） 中的組織 AWS Organizations
+ （選用） 中央安全帳戶，其為 AWS Config 和 的委派管理員 AWS Security Hub CSPM

## Architecture
<a name="deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config-architecture"></a>

**目標架構**

![\[使用 AWS Config 自訂規則來偵測公有子網路中的不合規資源\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/85d54ead-7f00-4381-89fb-cffe307c4cfc/images/a8c19913-d260-4b70-96ba-732bb1b9881f.png)


此圖展示了以下要點：

1. 部署或修改彈性網路界面資源 (`AWS::EC2::NetworkInterface`) 時， 會 AWS Config 擷取事件和組態。

1. AWS Config 會將此事件與用來評估組態的自訂規則相符。

1. 系統會叫用與此自訂規則相關聯的 AWS Lambda 函數。函數會評估資源並套用指定的邏輯，以判斷資源組態是 `COMPLIANT`、 `NON_COMPLIANT`或 `NOT_APPLICABLE`。

1. 如果資源判斷為 `NON_COMPLIANT`， 會透過 Amazon Simple Notification Service (Amazon SNS) AWS Config 傳送提醒。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config.html)

**Lambda 函數評估邏輯**

下圖顯示 Lambda 函數套用的邏輯，以評估彈性網路界面的合規性。

![\[Lambda 函數邏輯的圖表\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/85d54ead-7f00-4381-89fb-cffe307c4cfc/images/9575e20f-142b-4eba-b34d-3b9bda163144.png)


**自動化和擴展**

此模式是一種偵測性解決方案。您也可以使用修補規則來補充它，以自動解決任何不合規的資源。如需詳細資訊，請參閱[使用 AWS Config 規則修復不合規資源](https://docs.aws.amazon.com/config/latest/developerguide/remediation.html)。

您可以透過以下方式擴展此解決方案：
+ 強制套用您建立的對應 AWS 標籤，以識別面向公有的子網路。如需詳細資訊，請參閱 AWS Organizations 文件中的[標記政策](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_tag-policies.html)。
+ 設定將 AWS Config 自訂規則套用至組織中每個工作負載帳戶的中央安全帳戶。如需詳細資訊，請參閱[大規模自動化組態合規 AWS](https://aws.amazon.com/blogs/mt/automate-configuration-compliance-at-scale-in-aws/) (AWS 部落格文章）。
+  AWS Config 與 整合， AWS Security Hub CSPM 以大規模擷取、集中和通知 。如需詳細資訊，請參閱 AWS Security Hub CSPM 文件中的[設定 AWS Config](https://docs.aws.amazon.com/securityhub/latest/userguide/securityhub-prereq-config.html)。

## 工具
<a name="deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config-tools"></a>
+ [AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html) 提供 中資源的詳細檢視 AWS 帳戶 及其設定方式。它可協助您識別資源彼此之間的關係，以及其組態如何隨著時間而改變。
+ [Elastic Load Balancing](https://docs.aws.amazon.com/elasticloadbalancing/latest/userguide/what-is-load-balancing.html) 會將傳入的應用程式或網路流量分散到多個目標。例如，您可以將流量分散到一或多個可用區域中的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體、容器和 IP 地址。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [Amazon Simple Notification Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) 可協助您協調和管理發佈者和用戶端之間的訊息交換，包括 Web 伺服器和電子郵件地址。 
+ [Amazon Virtual Private Cloud (Amazon VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) 可協助您在已定義的虛擬網路中啟動 AWS 資源。此虛擬網路與您在自己的資料中心中操作的傳統網路相似，且具備使用 AWS可擴展基礎設施的優勢。

## 最佳實務
<a name="deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config-best-practices"></a>

如需開發自訂 AWS Config 規則的更多範例和最佳實務，請參閱 GitHub 上的官方[AWS Config 規則儲存庫](https://github.com/awslabs/aws-config-rules)。

## 史詩
<a name="deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config-epics"></a>

### 部署解決方案
<a name="deploy-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 Lambda 函數。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config.html) | 一般 AWS | 
| 將許可新增至 Lambda 函數的執行角色。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config.html) | 一般 AWS | 
| 擷取 Lambda 函數 Amazon Resource Name (ARN)。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config.html) | 一般 AWS | 
| 建立 AWS Config 自訂規則。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config.html) | 一般 AWS | 
| 設定通知。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config.html) | 一般 AWS | 

### 測試解決方案
<a name="test-the-solution"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立合規資源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config.html) | 一般 AWS | 
| 建立不合規的資源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config.html) | 一般 AWS | 
| 建立不適用的資源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config.html) | 一般 AWS | 

## 相關資源
<a name="deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config-resources"></a>

**AWS 文件**
+ [設定 AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/gs-console.html)
+ [AWS Config 自訂規則](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules.html)
+ [的 ABAC AWS](https://aws.amazon.com/identity/attribute-based-access-control/)
+ [部署公有子網路的預防性屬性型存取控制](https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/deploy-preventative-attribute-based-access-controls-for-public-subnets.html)

**其他 AWS 資源**
+ [大規模自動化組態合規 AWS](https://aws.amazon.com/blogs/mt/automate-configuration-compliance-at-scale-in-aws/)
+ [具有 Gateway Load Balancer 的分散式檢查架構](https://d1.awsstatic.com/architecture-diagrams/ArchitectureDiagrams/distributed-inspection-architectures-gwlb-ra.pdf)

## 其他資訊
<a name="deploy-detective-attribute-based-access-controls-for-public-subnets-by-using-aws-config-additional"></a>

以下是為示範目的而提供的 Lambda 函數範例。

```
import boto3
import json
import os

# Init clients
config_client = boto3.client('config')
ec2_client = boto3.client('ec2')

def lambda_handler(event, context):

    # Init values
    compliance_value = 'NOT_APPLICABLE'
    invoking_event = json.loads(event['invokingEvent'])
    configuration_item = invoking_event['configurationItem']
    
    status = configuration_item['configurationItemStatus']
    eventLeftScope = event['eventLeftScope']

    # First check if the event configuration applies. Ex. resource event is not delete
    if (status == 'OK' or status == 'ResourceDiscovered') and not eventLeftScope:
        compliance_value = evaluate_change_notification_compliance(configuration_item)
    
    
    config_client.put_evaluations(
       Evaluations=[
           {
               'ComplianceResourceType': invoking_event['configurationItem']['resourceType'],
               'ComplianceResourceId': invoking_event['configurationItem']['resourceId'],
               'ComplianceType': compliance_value,
               'OrderingTimestamp': invoking_event['configurationItem']['configurationItemCaptureTime']
           },
       ],
       ResultToken=event['resultToken'])
    
# Function with the logs to evaluate the resource
def evaluate_change_notification_compliance(configuration_item):
    is_in_scope = is_in_scope_subnet(configuration_item['configuration']['subnetId'])
    
    if (configuration_item['resourceType'] != 'AWS::EC2::NetworkInterface') or not is_in_scope:
        return 'NOT_APPLICABLE'

    else:
        alb_condition = configuration_item['configuration']['requesterId'] in ['amazon-elb']
        nlb_condition = configuration_item['configuration']['interfaceType'] in ['network_load_balancer']
        nat_gateway_condition = configuration_item['configuration']['interfaceType'] in ['nat_gateway']

        if alb_condition or nlb_condition or nat_gateway_condition:
            return 'COMPLIANT'
    return 'NON_COMPLIANT'

# Function to check if elastic network interface is in public subnet
def is_in_scope_subnet(eni_subnet):

    subnet_description = ec2_client.describe_subnets(
        SubnetIds=[eni_subnet]
    )

    for subnet in subnet_description['Subnets']:
        for tag in subnet['Tags']:
            if tag['Key'] == os.environ.get('TAG_KEY') and tag['Value'] == os.environ.get('TAG_VALUE'):
                return True
    
    return False
```