

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

# 使用 AWS CloudFormation 和 AWS Config 監控 Amazon ECR 儲存庫是否有萬用字元許可
<a name="monitor-amazon-ecr-repositories-for-wildcard-permissions-using-aws-cloudformation-and-aws-config"></a>

*Vikrant Telkar、Wassim Benhallam 和 Sajid Momin，Amazon Web Services*

## 總結
<a name="monitor-amazon-ecr-repositories-for-wildcard-permissions-using-aws-cloudformation-and-aws-config-summary"></a>

在 Amazon Web Services (AWS) 雲端上，Amazon Elastic Container Registry (Amazon ECR) 是一種受管容器映像登錄服務，使用 AWS Identity and Access Management (IAM) 支援具有資源型許可的私有儲存庫。

IAM 在資源和動作屬性中都支援「`*`」萬用字元，這可讓您更輕鬆地自動選擇多個相符項目。在您的測試環境中，您可以在儲存庫政策陳述式的主體元素中使用`ecr:*`[萬用字元許可](https://docs.aws.amazon.com/lambda/latest/operatorguide/wildcard-permissions-iam.html)，允許所有已驗證的 AWS 使用者存取 Amazon ECR 儲存庫。 [https://docs.aws.amazon.com/AmazonECR/latest/userguide/set-repository-policy.html](https://docs.aws.amazon.com/AmazonECR/latest/userguide/set-repository-policy.html)萬`ecr:*`用字元許可在無法存取生產資料的開發帳戶中開發和測試時非常有用。

不過，您必須確定您的生產環境中未使用`ecr:*`萬用字元許可，因為它可能會導致嚴重的安全漏洞。此模式的方法可協助您識別儲存庫政策陳述式中包含`ecr:*`萬用字元許可的 Amazon ECR 儲存庫。  模式提供步驟和 AWS CloudFormation 範本，以在 AWS Config 中建立自訂規則。然後AWS Lambda 函數會監控您的 Amazon ECR 儲存庫政策陳述式是否有`ecr:*`萬用字元許可。如果找到不合規的儲存庫政策陳述式，Lambda 會通知 AWS Config 將事件傳送至 Amazon EventBridge，然後 EventBridge 會啟動 Amazon Simple Notification Service (Amazon SNS) 主題。SNS 主題會透過電子郵件通知您不合規的儲存庫政策陳述式。

## 先決條件和限制
<a name="monitor-amazon-ecr-repositories-for-wildcard-permissions-using-aws-cloudformation-and-aws-config-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 安裝並設定 AWS Command Line Interface (AWS CLI)。如需詳細資訊，請參閱 [AWS CLI 文件中的安裝、更新和解除安裝](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) AWS CLI。
+ 具有附加政策陳述式的現有 Amazon ECR 儲存庫，在您的測試環境中安裝和設定。如需詳細資訊，請參閱 Amazon ECR 文件中的[建立私有儲存庫](https://docs.aws.amazon.com/AmazonECR/latest/userguide/repository-create.html)和[設定儲存庫政策陳述](https://docs.aws.amazon.com/AmazonECR/latest/userguide/set-repository-policy.html)式。
+ AWS Config，在您偏好的 AWS 區域中設定。如需詳細資訊，請參閱 [ AWS Config 文件中的 AWS Config 入門](https://docs.aws.amazon.com/config/latest/developerguide/getting-started.html)。 AWS Config 
+ `aws-config-cloudformation.template` 檔案 （已連接），下載到您的本機電腦。

 

**限制**
+ 此模式的解決方案為區域性，您的資源必須在相同的區域中建立。 

## Architecture
<a name="monitor-amazon-ecr-repositories-for-wildcard-permissions-using-aws-cloudformation-and-aws-config-architecture"></a>

下圖顯示 AWS Config 如何評估 Amazon ECR 儲存庫政策陳述式。 

![\[AWS Config workflow with Lambda, Amazon ECR, EventBridge, SNS, and email notification components.\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/01bbf5f8-27aa-4c64-9a03-7fcccc0955b8/images/49bbf14b-0a18-4d4a-86ab-162d37708e01.png)


該圖顯示以下工作流程：

1. AWS Config 會啟動自訂規則。 

1. 自訂規則會叫用 Lambda 函數，以評估 Amazon ECR 儲存庫政策陳述式的合規性。然後，Lambda 函數會識別不合規的儲存庫政策陳述式。

1. Lambda 函數會將不合規狀態傳送至 AWS Config。

1. AWS Config 會將事件傳送至 EventBridge。

1. EventBridge 會將不合規通知發佈至 SNS 主題。

1. Amazon SNS 會傳送電子郵件提醒給您或授權的使用者。

**自動化和擴展**

此模式的解決方案可以監控任意數量的 Amazon ECR 儲存庫政策陳述式，但您想要評估的所有資源都必須在相同區域中建立。

## 工具
<a name="monitor-amazon-ecr-repositories-for-wildcard-permissions-using-aws-cloudformation-and-aws-config-tools"></a>
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) – AWS CloudFormation 可協助您建立模型並設定 AWS 資源、快速一致地佈建資源，以及在整個生命週期中管理資源。您可以使用範本來描述資源及其相依性，並將它們一起啟動和設定為堆疊，而不是個別管理資源。您可以管理和佈建跨多個 AWS 帳戶和 AWS 區域的堆疊。
+ [AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html) – AWS Config 提供 AWS 帳戶中 AWS 資源組態的詳細檢視。這包含資源彼此之間的關係和之前的組態方式，所以您可以看到一段時間中組態和關係的變化。
+ [Amazon ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html)****–****Amazon Elastic Container Registry (Amazon ECR) 是一種 AWS 受管容器映像登錄服務，安全、可擴展且可靠。Amazon ECR 支援私有儲存庫，其具有使用 IAM 的資源型許可。                                
+ [Amazon EventBridge](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html) – Amazon EventBridge 是一種無伺服器事件匯流排服務，可用來將應用程式與來自各種來源的資料連線。EventBridge 會將即時資料從您的應用程式、軟體即服務 (SaaS) 應用程式和 AWS 服務串流傳送至目標，例如 AWS Lambda 函數、使用 API 目的地的 HTTP 調用端點，或其他帳戶中的事件匯流排。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) – AWS Lambda 是一種運算服務，支援執行程式碼，無需佈建或管理伺服器。Lambda 只有在需要時才會執行程式碼，可自動從每天數項請求擴展成每秒數千項請求。只需為使用的運算時間支付費用，一旦未執行程式碼，就會停止計費。
+ [Amazon SNS](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) – Amazon Simple Notification Service (Amazon SNS) 會協調和管理發佈者和用戶端之間的訊息傳遞或傳送，包括 Web 伺服器和電子郵件地址。訂閱者會收到發佈到所訂閱主題的所有訊息，且某一主題的所有訂閱者均會收到相同訊息。 

**Code**

此模式的程式碼可在 `aws-config-cloudformation.template` 檔案中取得 （已連接）。

## 史詩
<a name="monitor-amazon-ecr-repositories-for-wildcard-permissions-using-aws-cloudformation-and-aws-config-epics"></a>

### 建立 AWS CloudFormation 堆疊
<a name="create-the-aws-cloudformation-stack"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 AWS CloudFormation 堆疊。 | 在 AWS CLI 中執行下列命令來建立 AWS CloudFormation 堆疊：<pre>$ aws cloudformation create-stack --stack-name=AWSConfigECR \<br />    --template-body  file://aws-config-cloudformation.template \<br />    --parameters ParameterKey=<email>,ParameterValue=<myemail@example.com> \<br />    --capabilities CAPABILITY_NAMED_IAM</pre> | AWS DevOps | 

### 測試 AWS Config 自訂規則
<a name="test-the-aws-config-custom-rule"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 測試 AWS Config 自訂規則。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/monitor-amazon-ecr-repositories-for-wildcard-permissions-using-aws-cloudformation-and-aws-config.html) | AWS DevOps | 

## 附件
<a name="attachments-01bbf5f8-27aa-4c64-9a03-7fcccc0955b8"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/01bbf5f8-27aa-4c64-9a03-7fcccc0955b8/attachments/attachment.zip)