0

🚀 Chapter 4 — Production: Phần C — Image Security với Amazon ECR & Amazon Inspector

Chapter 4B, chúng ta đã giải quyết vấn đề số lượng Docker Image tăng liên tục bằng Amazon ECR Lifecycle Policy.

Kiến trúc hiện tại:

Developer
    ↓
git push
    ↓
GitHub
    ↓
GitHub Actions
    ↓
Matrix
    ↓
Reusable Workflow
    ↓
Docker Build
    ↓
Amazon ECR
    ↓
Lifecycle Policy
    ↓
Old Images → Expire

Nhưng chúng ta vẫn còn một vấn đề:

Image trong ECR có thực sự an toàn không?

Một Docker Image có thể chứa:

OS package có CVE
Library có vulnerability
Runtime package cũ
Dependency có security issue

Vì vậy trong Chapter 4C, chúng ta sẽ thêm Amazon Inspector Enhanced Scanning vào ECR.

Kiến trúc sau Chapter 4C:

Developer
    ↓
git push
    ↓
GitHub
    ↓
GitHub Actions
    ↓
Matrix
    ↓
Reusable Workflow
    ↓
Docker Build
    ↓
Amazon ECR
    ↓
Amazon Inspector
    ↓
Vulnerability Scan
    ↓
Lifecycle Policy

Amazon ECR Enhanced Scanning sử dụng Amazon Inspector để tự động phát hiện vulnerability trong OS packages và programming-language packages của container image.


1. Mục tiêu

Sau Chapter 4C, chúng ta có:

✅ Docker Image được lưu trong ECR

✅ ECR tự động scan Image

✅ Phát hiện CVE

✅ Biết severity của vulnerability

✅ Biết package nào bị ảnh hưởng

✅ Biết cách xem kết quả bằng AWS Console

✅ Biết cách xem kết quả bằng AWS CLI

Ở Chapter 4C chúng ta chưa block deployment.

Phần:

Scan
 ↓
Critical?
 ↓
STOP

sẽ được làm ở Chapter 4E — Security Gate.


2. Kiểm tra trạng thái từ Chapter 4B

Trên máy local:

cd online-boutique-cicd

Kiểm tra Git:

git status

Kiểm tra workflow:

find .github/workflows -type f

Bạn sẽ thấy tương tự:

.github/workflows/
├── reusable-ci-cd.yml
└── scale-ci-cd.yml

Kiểm tra AWS Region:

aws configure get region

Kết quả:

ap-northeast-1

Nếu chưa có:

aws configure set region ap-northeast-1

3. Kiểm tra ECR Repository

Chạy:

aws ecr describe-repositories \
  --query 'repositories[?starts_with(repositoryName, `online-boutique-`)].repositoryName' \
  --output table

Kết quả ví dụ:

------------------------------------------------
|              DescribeRepositories             |
+----------------------------------------------+
| online-boutique-frontend                     |
| online-boutique-cartservice                  |
| online-boutique-productcatalogservice        |
| online-boutique-checkoutservice              |
+----------------------------------------------+

Các repository này đã được tạo ở Chapter 4A.


4. Kiểm tra Docker Image

Chúng ta sử dụng image đã được push ở Chapter 4A/4B.

Kiểm tra:

aws ecr list-images \
  --repository-name online-boutique-frontend \
  --output table

Ví dụ:

-----------------------------------------
|              ListImages               |
+----------------------+----------------+
| imageDigest          | imageTag       |
+----------------------+----------------+
| sha256:abc...        | a81c92f        |
| sha256:def...        | 92ab821        |
+----------------------+----------------+

Lấy image mới nhất:

aws ecr describe-images \
  --repository-name online-boutique-frontend \
  --query 'sort_by(imageDetails,&imagePushedAt)[-1].{Tag:imageTags[0],Digest:imageDigest,Pushed:imagePushedAt}' \
  --output table

Ví dụ:

---------------------------------------------------------
|                  DescribeImages                        |
+------------+----------------------+------------------+
| Tag        | Digest               | Pushed           |
+------------+----------------------+------------------+
| a81c92f    | sha256:abc...        | 2026-09-11...    |
+------------+----------------------+------------------+

Ghi lại:

Repository:
online-boutique-frontend

Tag:
a81c92f

Chúng ta sẽ dùng image này để test scanning.


5. Kiểm tra cấu hình ECR Scanning hiện tại

Chạy:

aws ecr get-registry-scanning-configuration \
  --region ap-northeast-1

Nếu chưa cấu hình Enhanced Scanning, bạn có thể thấy:

"BASIC"

hoặc cấu hình scanning khác.

AWS ECR hỗ trợ hai loại chính:

BASIC
ENHANCED

Trong Chapter 4C chúng ta sử dụng:

ENHANCED

Enhanced Scanning được cung cấp thông qua Amazon Inspector.


6. Enable Enhanced Scanning

Ở lab này, chúng ta sẽ cấu hình:

Enhanced Scanning
        ↓
SCAN_ON_PUSH
        ↓
online-boutique-* repositories

Chạy:

aws ecr put-registry-scanning-configuration \
  --scan-type ENHANCED \
  --rules '[{"repositoryFilters":[{"filter":"online-boutique-*","filterType":"WILDCARD"}],"scanFrequency":"SCAN_ON_PUSH"}]' \
  --region ap-northeast-1

Kết quả sẽ trả về cấu hình:

scanType:
ENHANCED

và rule:

online-boutique-*
        ↓
SCAN_ON_PUSH

AWS hỗ trợ SCAN_ON_PUSHCONTINUOUS_SCAN cho Enhanced Scanning.


7. Kiểm tra lại cấu hình

Chạy:

aws ecr get-registry-scanning-configuration \
  --region ap-northeast-1

Bạn cần thấy:

"scanType": "ENHANCED"

và:

"scanFrequency": "SCAN_ON_PUSH"

8. Amazon Inspector

Sau khi bật Enhanced Scanning, Amazon Inspector sẽ được sử dụng để scan các image phù hợp với filter.

AWS có thể tự tạo service-linked IAM role cần thiết cho Amazon Inspector khi Enhanced Scanning được bật.

Mở AWS Console.

Vào:

Amazon Inspector

Sau đó chọn:

Container images

Bạn sẽ thấy các image từ ECR được Inspector theo dõi.


9. Kiểm tra Image Coverage

Trong Amazon Inspector:

Amazon Inspector
    ↓
Coverage
    ↓
ECR container images

Tìm:

online-boutique-frontend

Bạn có thể thấy:

Repository
online-boutique-frontend

Image
a81c92f

Status
ACTIVE

Nếu image vừa được push và scan chưa hoàn thành, hãy chờ một chút rồi refresh.


10. Trigger một Image mới

Để chắc chắn chúng ta có một image mới được scan, tạo một commit nhỏ trong project.

Ví dụ:

git status

Sau đó sửa một file nhỏ hoặc README:

echo "" >> README.md

Commit:

git add README.md
git commit -m "test ecr image scanning"

Push:

git push

GitHub Actions sẽ chạy:

git push
    ↓
GitHub Actions
    ↓
Matrix
    ↓
Reusable Workflow
    ↓
Docker Build
    ↓
ECR Push

Vì repository có filter:

online-boutique-*

và:

SCAN_ON_PUSH

image mới được push sẽ được đưa vào scanning.


11. Kiểm tra Image mới trong ECR

Chạy:

aws ecr describe-images \
  --repository-name online-boutique-frontend \
  --query 'sort_by(imageDetails,&imagePushedAt)[-1].{Tag:imageTags[0],Digest:imageDigest,Pushed:imagePushedAt}' \
  --output table

Ví dụ:

---------------------------------------------------------
|                  DescribeImages                        |
+------------+----------------------+------------------+
| Tag        | Digest               | Pushed           |
+------------+----------------------+------------------+
| f72a921    | sha256:def...        | 2026-09-11...    |
+------------+----------------------+------------------+

Image mới:

f72a921

12. Kiểm tra Scan Findings

Lấy tag:

IMAGE_TAG=$(aws ecr describe-images \
  --repository-name online-boutique-frontend \
  --query 'sort_by(imageDetails,&imagePushedAt)[-1].imageTags[0]' \
  --output text)

Kiểm tra:

echo $IMAGE_TAG

Ví dụ:

f72a921

Sau đó:

aws ecr describe-image-scan-findings \
  --repository-name online-boutique-frontend \
  --image-id imageTag=$IMAGE_TAG \
  --region ap-northeast-1

AWS CLI describe-image-scan-findings trả về các findings của image, bao gồm severity và thông tin vulnerability; với Enhanced Scanning, kết quả nằm trong enhancedFindings.


13. Chỉ lấy danh sách Severity

Thay vì xem toàn bộ JSON:

aws ecr describe-image-scan-findings \
  --repository-name online-boutique-frontend \
  --image-id imageTag=$IMAGE_TAG \
  --region ap-northeast-1 \
  --query 'imageScanFindings.enhancedFindings[].severity' \
  --output text

Có thể nhận:

CRITICAL
HIGH
MEDIUM
LOW

Hoặc:

Nếu không có vulnerability tương ứng.


14. Đếm số lượng Vulnerability

Chạy:

aws ecr describe-image-scan-findings \
  --repository-name online-boutique-frontend \
  --image-id imageTag=$IMAGE_TAG \
  --region ap-northeast-1 \
  --query 'imageScanFindings.enhancedFindings[].severity' \
  --output text

Ví dụ:

HIGH MEDIUM MEDIUM LOW

Có thể hiểu:

CRITICAL = 0
HIGH     = 1
MEDIUM   = 2
LOW      = 1

Kết quả thực tế phụ thuộc vào version của base image và dependencies tại thời điểm scan.


15. Xem chi tiết Vulnerability

Lấy các field quan trọng:

aws ecr describe-image-scan-findings \
  --repository-name online-boutique-frontend \
  --image-id imageTag=$IMAGE_TAG \
  --region ap-northeast-1 \
  --query 'imageScanFindings.enhancedFindings[].{Severity:severity,Title:title,Package:packageVulnerabilityDetails.vulnerablePackages[0].name,CVE:packageVulnerabilityDetails.vulnerabilityId}' \
  --output table

Ví dụ:

----------------------------------------------------------------
|                   DescribeImageScanFindings                  |
+----------+------------+----------------------+---------------+
| Severity | CVE        | Package              | Title         |
+----------+------------+----------------------+---------------+
| HIGH     | CVE-XXXX   | package-x            | ...           |
| MEDIUM   | CVE-YYYY   | package-y            | ...           |
+----------+------------+----------------------+---------------+

16. Xem trên AWS Console

Mở:

Amazon ECR
    ↓
Repositories
    ↓
online-boutique-frontend
    ↓
Images
    ↓
Chọn image

Sau đó xem:

Image details

và phần:

Scan findings

Bạn có thể xem:

CRITICAL
HIGH
MEDIUM
LOW

và các vulnerability tương ứng.


17. Kiểm tra các Service còn lại

Không chỉ scan:

frontend

Chúng ta đã cấu hình:

online-boutique-*

nên các repository như:

online-boutique-frontend
online-boutique-cartservice
online-boutique-productcatalogservice
online-boutique-checkoutservice

đều thuộc phạm vi scan.

Kiểm tra:

for repo in \
  online-boutique-frontend \
  online-boutique-cartservice \
  online-boutique-productcatalogservice \
  online-boutique-checkoutservice
do
  echo "===== $repo ====="

  aws ecr describe-images \
    --repository-name "$repo" \
    --query 'sort_by(imageDetails,&imagePushedAt)[-1].{Tag:imageTags[0],Pushed:imagePushedAt}' \
    --output table
done

18. Kiểm tra Security Summary

Có thể tạo một command đơn giản:

for repo in \
  online-boutique-frontend \
  online-boutique-cartservice \
  online-boutique-productcatalogservice \
  online-boutique-checkoutservice
do
  echo "======================================"
  echo "$repo"
  echo "======================================"

  tag=$(aws ecr describe-images \
    --repository-name "$repo" \
    --query 'sort_by(imageDetails,&imagePushedAt)[-1].imageTags[0]' \
    --output text)

  echo "Image: $tag"

  aws ecr describe-image-scan-findings \
    --repository-name "$repo" \
    --image-id imageTag="$tag" \
    --region ap-northeast-1 \
    --query 'imageScanFindings.enhancedFindings[].severity' \
    --output text
done

Kết quả:

======================================
online-boutique-frontend
======================================
Image: f72a921
HIGH MEDIUM LOW

======================================
online-boutique-cartservice
======================================
Image: 83ab112
MEDIUM LOW

======================================
online-boutique-productcatalogservice
======================================
Image: 71cd821
HIGH HIGH MEDIUM

======================================
online-boutique-checkoutservice
======================================
Image: 91ac321
LOW

19. Điều quan trọng: Scan ≠ Block Deployment

Hiện tại pipeline của chúng ta:

GitHub
    ↓
Build
    ↓
ECR
    ↓
Scan
    ↓
Dev / Deploy

Nếu scan phát hiện:

CRITICAL

image vẫn tồn tại trong ECR.

Chúng ta chưa làm:

CRITICAL
   ↓
❌ Pipeline FAIL
   ↓
❌ Không Deploy

Đó sẽ là Chapter 4E — Security Gate.

Chapter 4C chỉ tập trung vào:

Detect
  ↓
Understand
  ↓
Review

20. Một điểm quan trọng về Scan

Enhanced Scanning không chỉ kiểm tra tại thời điểm image được push.

Amazon Inspector có thể tiếp tục theo dõi image để phát hiện vulnerability mới xuất hiện sau này. AWS cũng cho phép lựa chọn SCAN_ON_PUSH hoặc CONTINUOUS_SCAN.

Trong lab này chúng ta sử dụng:

SCAN_ON_PUSH

để dễ quan sát flow:

Push Image
    ↓
Scan
    ↓
Findings

Ở production, có thể cân nhắc:

CONTINUOUS_SCAN

để theo dõi vulnerability mới phát hiện sau khi image đã được push.


21. Optional — Chuyển sang Continuous Scanning

Nếu muốn thử:

SCAN_ON_PUSH

CONTINUOUS_SCAN

chạy:

aws ecr put-registry-scanning-configuration \
  --scan-type ENHANCED \
  --rules '[{"repositoryFilters":[{"filter":"online-boutique-*","filterType":"WILDCARD"}],"scanFrequency":"CONTINUOUS_SCAN"}]' \
  --region ap-northeast-1

Kiểm tra:

aws ecr get-registry-scanning-configuration \
  --region ap-northeast-1

Phải thấy:

"scanType": "ENHANCED"

và:

"scanFrequency": "CONTINUOUS_SCAN"

22. Kiểm tra toàn bộ Architecture

Sau Chapter 4C:

                         Developer
                             │
                             ▼
                           GitHub
                             │
                             ▼
                     GitHub Actions
                             │
                             ▼
                           Matrix
                             │
                             ▼
                     Reusable Workflow
                             │
                             ▼
                        Docker Build
                             │
                             ▼
                    ┌─────────────────┐
                    │   Amazon ECR    │
                    └─────────────────┘
                             │
                ┌────────────┼────────────┐
                │            │            │
                ▼            ▼            ▼
           Lifecycle     Inspector    Image Tags
             Policy        Scan
                │            │
                │            ▼
                │       Vulnerabilities
                │            │
                ▼            ▼
          Old Images      Findings
           → Expire

23. Kết quả sau Chapter 4C

Chúng ta đã có:

Chapter 4A
Docker Image
      ↓
Amazon ECR

Chapter 4B
Amazon ECR
      ↓
Lifecycle Policy
      ↓
Old Image → Expire

Chapter 4C
Amazon ECR
      ↓
Amazon Inspector
      ↓
Vulnerability Scan
      ↓
Security Findings

Vấn đề tiếp theo là:

Biết image có vulnerability rồi, nhưng CI/CD có nên cho phép image đó được deploy hay không?

Đó chính là bước tiếp theo:

Chapter 4D
Immutable Artifact
        ↓
Chapter 4E
Security Gate
        ↓
Chapter 4F
Artifact Promotion
        ↓
Chapter 4G
Production Approval

Chapter 4E, chúng ta sẽ biến kết quả của Amazon Inspector thành một quality/security gate:

Docker Build
     ↓
ECR
     ↓
Inspector Scan
     ↓
CRITICAL / HIGH ?
     │
     ├── YES → ❌ Pipeline FAIL
     │
     └── NO  → ✅ Continue
                    ↓
                  Deploy

Đây mới là lúc security scan thực sự trở thành một phần của Production CI/CD governance.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí