松田です!
Codeシリーズ周りの嬉しいアップデートがあったので、こちらで共有させていただきます!
アップデート情報
以下、抜粋の和訳です。
AWS CodePipeline のアクションカタログに、ECRBuildAndPublish アクションと AWS InspectorScan アクションが導入されました。
ECRBuildAndPublish アクションを使用すると、パイプライン実行の一環として、Docker イメージを簡単に構築して ECR に公開できます。InspectorScan アクションを使用すると、パイプライン実行の一環として、ソースコードリポジトリまたは Docker イメージをスキャンできます。
何が嬉しいの?
ECSでアプリケーションを動かしており、CodeシリーズでCI/CDパイプラインを実装するケースを例にします。
以前まではCodeBuildプロバイダーのステージにてDockerイメージをビルドして、ECRへのPushを行う必要がありました。
CodeBuildを利用するので、buildspec.ymlに以下のようにビルド処理とECRへのPush処理をコマンドで定義していく必要がありました。
version: 0.2
phases:
pre_build:
commands:
- AWS_ACCOUNT_ID=$(echo ${CODEBUILD_BUILD_ARN} | cut -f 5 -d :)
- aws --version
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.ap-northeast-1.amazonaws.com
- REPOSITORY_URI=${AWS_ACCOUNT_ID}.dkr.ecr.ap-northeast-1.amazonaws.com/httpd
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_TAG=${COMMIT_HASH}
build:
commands:
- docker build -t $REPOSITORY_URI:$IMAGE_TAG .
post_build:
commands:
- docker push $REPOSITORY_URI:$IMAGE_TAG
参考 : Tutorial: Amazon ECS Standard Deployment with CodePipeline
ECRBuildAndPublishアクションを利用すると、上記の処理をbuidspecの作成不要で簡単に実装することが可能になります!
またECRのレポジトリにてプッシュ時にスキャンを有効にすることでInspectorによるスキャンが実行されるようになります。ただし、これだとパイプラインの中にスキャンを組み込むことができませんでした。(パイプライン外でスキャン結果を確認する必要がありました。)
Amazon InspectorScan アクションを利用することで、Inspectorによるスキャンを簡単にパイプラインへ組み込むことができるようになります!
やってみた
ECRBuildAndPublishアクション
CodePipelineで簡単なCI/CDパイプラインを実装します。
ソースステージを作成します。CodeCommitレポジトリをソースプロバイダーに設定します。(Dockerfileだけのシンプルなレポジトリです)

ビルドステージを作成します。プロバイダーに「AWS ECRBuildAndPublish」を指定します。Push先のECRレポジトリを指定します。

デプロイステージの導入は割愛します。
パイプライン作成後に、パイプラインが実行されました。無事に成功しています。

ECRにもlatestタグがついて、Pushされていることが確認できました。

Amazon InspectorScan アクション
先ほどのパイプラインを編集して、Scanステージ/Scanアクションを追加します。
ここでアクションプロバイダーに「AWS InspectorScan」を指定します。

Inspector Run modeからソースコードスキャンかECRイメージスキャンかを選択できるようです。(ソースコードスキャンってLambdaだけに対応していたような気もするのですが、input artifactとして渡せるものであれば対応可能になったような気がします。もう少し深掘りは必要なところです)
今回は「SourceCodeScan」を選択します。
パイプラインの更新をしてから、再びパイプラインを実行します。
Scanステージも正常に完了したようです。

Scanステージのログを見てみます。
[Container] 2024/11/26 04:30:52.202185 Running command curl "https://d33ue1ndcnyy34.cloudfront.net/inspectorscan-invoke-aws-1/0.1.0.tgz" -o /tmp/cp-action-source/action-archive.tgz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 7758k 100 7758k 0 0 78.9M 0 --:--:-- --:--:-- --:--:-- 79.7M
[Container] 2024/11/26 04:30:52.969920 Running command tar -xvzf /tmp/cp-action-source/action-archive.tgz --strip-components=1 -C /tmp/cp-action-source
package/dist/inspector-sbomgen-1.5.0/linux/amd64/inspector-sbomgen
package/dist/inspector-sbomgen-1.5.0/linux/amd64/licenses/3rd-party-licenses.csv
package/dist/index.js
package/dist/inspectorscan.js
package/package.json
package/dist/inspector-sbomgen-1.5.0/linux/amd64/sbom.json
package/dist/index.d.ts.map
package/dist/index.js.map
package/dist/inspectorscan.d.ts.map
package/dist/inspectorscan.js.map
package/dist/index.d.ts
package/dist/inspectorscan.d.ts
package/dist/inspector-sbomgen-1.5.0/linux/amd64/licenses/Apache-License-2.0.txt
package/dist/inspector-sbomgen-1.5.0/linux/amd64/licenses/bsd-2-clause.txt
package/dist/inspector-sbomgen-1.5.0/linux/amd64/licenses/bsd-3-clause.txt
package/dist/inspector-sbomgen-1.5.0/linux/amd64/checksum.txt
package/dist/inspector-sbomgen-1.5.0/linux/amd64/LICENSE.txt
package/dist/inspector-sbomgen-1.5.0/linux/amd64/licenses/MIT.txt
package/dist/inspector-sbomgen-1.5.0/linux/amd64/README.txt
package/dist/inspector-sbomgen-1.5.0/linux/amd64/WhatsNew.txt
[Container] 2024/11/26 04:30:53.273168 Running command node $CODEPIPELINE_INPUT_ACTION_SOURCE_PATH/dist/index.js
Running Inspector Scan...
Running source code scan...
Running command: /tmp/cp-action-source/dist/inspector-sbomgen-1.5.0/linux/amd64/inspector-sbomgen directory --path . --scan-sbom --disable-progress-bar -o /tmp/cp-action-source/sbom.json
time="2024-11-26 04:30:56" level=info msg="Amazon Inspector SBOM Generator v1.5.0 - linux amd64 - Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved" file="cli.go:157:"
time="2024-11-26 04:30:56" level=info msg="[/tmp/cp-action-source/dist/inspector-sbomgen-1.5.0/linux/amd64/inspector-sbomgen directory --path . --scan-sbom --disable-progress-bar -o /tmp/cp-action-source/sbom.json]" file="cli.go:158:"
time="2024-11-26 04:30:56" level=info msg="writing log file to: /root/.inspector-sbomgen/logs/inspector-sbomgen-log_2024-11-26_04-30-56.txt" file="cli.go:159:"
time="2024-11-26 04:30:56" level=info msg="initializing target artifact" file="coreV1.go:77:"
time="2024-11-26 04:30:56" level=info msg="executing pre-processors" file="coreV1.go:82:"
time="2024-11-26 04:30:56" level=info msg="walking the artifact" file="directory.go:198:"
time="2024-11-26 04:30:56" level=info msg="analyzing artifact" file="coreV1.go:87:"
time="2024-11-26 04:30:56" level=info msg="executing post-processors" file="coreV1.go:92:"
time="2024-11-26 04:30:56" level=info msg="encoding findings" file="coreV1.go:100:"
time="2024-11-26 04:30:56" level=info msg="encoded 1 components" file="directories.go:251:"
time="2024-11-26 04:30:56" level=info msg="cleaning up file system artifacts" file="directory.go:279:"
time="2024-11-26 04:30:56" level=info msg="scanning SBOM contents for vulnerable packages with Inspector Scan service" file="clientv1.go:333:"
time="2024-11-26 04:30:56" level=info msg="Elapsed time: 393ms" file="cli.go:60:"
---------Vulnerability analysis --------
Critical severity vulnerabilities found: 0
High severity vulnerabilities found: 0
Medium severity vulnerabilities found: 0
Low severity vulnerabilities found: 0
------------------------------------------
No vulnerabilities found.
Highest severity vulnerability: {}
Inspector scan complete.
[Container] 2024/11/26 04:30:56.847728 Running command [ -f /tmp/cp-action-source/action-output-variables.sh ] && chmod 755 /tmp/cp-action-source/action-output-variables.sh && source /tmp/cp-action-source/action-output-variables.sh || true
[Container] 2024/11/26 04:30:56.853336 Running command if [[ ! -z $CodePipeline_ErrorCode || ! -z $CodePipeline_ErrorSummary ]]; then exit 1; fi
ソースコードの中身はシンプルなDockerfileしかないので、もちろん脆弱性は検出されていません。
もし脆弱性があれば、ステージ(アクション)が失敗するようになっています。
またOutputアーティファクトの中身も確認してみます。
{
"bomFormat": "CycloneDX",
"metadata": {
"properties": [
{
"name": "amazon:inspector:sbom_scanner:critical_vulnerabilities",
"value": "0"
},
{
"name": "amazon:inspector:sbom_scanner:high_vulnerabilities",
"value": "0"
},
{
"name": "amazon:inspector:sbom_scanner:medium_vulnerabilities",
"value": "0"
},
{
"name": "amazon:inspector:sbom_scanner:low_vulnerabilities",
"value": "0"
},
{
"name": "amazon:inspector:sbom_scanner:other_vulnerabilities",
"value": "0"
}
],
"timestamp": "2024-11-26T04:30:56.821Z",
"tools": {
"services": [
{
"name": "Amazon Inspector Scan SBOM API",
"version": "56008c36+5ce64b41+1f1b76d5"
}
]
}
},
"serialNumber": "urn:uuid:992f4554-5f28-4fce-913d-e65aac8c2285",
"specVersion": "1.5"
}
脆弱性の検出件数やどのスキャンツールを利用したかがJsonで記述されています。
注意点
ECRBuildAndPublishアクションでPushするイメージのタグはstatic(Latestもしくは指定したタグ)になってしまうことには注意が必要です。
コミットハッシュタグなどの動的なタグ付けをしてタグはイミュータブルにしておくのがプラクティスですが、それができなさそうです。
ここはアップデートを待ちたいところですね。
最後に
色々不安でしたが、今回のアップデートでCodeシリーズへのモチベーションが高まりました!
今後のアップデートも心待ちにしたいと思います!