EventBridge Scheduler で Inspector の検出結果レポートを定期出力する

2023.04.04

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Amazon Inspectorは脆弱性スキャンサービスです。 EC2インスタンスやECRイメージ、Lambda関数などの脆弱性を継続的にスキャンします。

スキャンされた各種脆弱性の情報は「 検出結果(Findings) 」として保存されます。 Inspector には検出結果のレポート機能が備わっています。CSVもしくはJSONでS3バケットへ出力できます。

今回はこのレポート出力を EventBridge Scheduler を使って定期実行してみます。

作ってみる

前提条件(S3バケットとKMSキーの準備)

レポート出力のために「出力先のS3バケット」および「出力の際に使うKMSキー」が必要です。 以下ブログで詳細が書かれているので、本ブログでは割愛します。

EventBridge Scheduler 用のIAMロール作成

EventBridge Scheduler 用のIAMロールを事前に作成しておきます。

信頼ポリシーは以下のとおりです。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "scheduler.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

インラインポリシーとして、 inspector2:CreateFindingsReport を許可するステートメントを記載します。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "inspector2:CreateFindingsReport",
      "Resource": "*"
    }
  ]
}

スケジュールの作成

EventBridgeマネジメントコンソールにて [スケジューラ > スケジュール] を選択します。

img

スケジュール名は適当に入力します(例: schedule-for-inspector-report )。

スケジュールのパターンは以下( cron(0 6 * * ? *) )のようにしました。 毎日 06:00 に実行されるスケジュールです。

img

ターゲットは [すべてのAPI > Amazon Inspector2 > CreateFindingsReport] を選択します。

img

入力は以下のようにします。 ${} 部分を適宜置き換えてください。

{
  "ReportFormat": "JSON",
  "S3Destination": {
    "BucketName": "${S3バケット名}",
    "KeyPrefix": "<aws.scheduler.scheduled-time>",
    "KmsKeyArn": "${KMSキーARN}"
  }
}

ReportFormatCSV でもOKです。

※ "aws.scheduler.scheduled-time" は入力に使えるキーワードです(詳細は以下参照)。

aws.scheduler.scheduled-time – The time you specified for the schedule to invoke its target, for example, 2022-03-22T18:59:43Z.

Adding context attributes - EventBridge Scheduler

アクセス許可には「先程作成したIAMロール」を選択します。

img

[スケジュールを作成] を選択して完了です。

試してみる

すぐに結果を確認するために実行頻度を cron(*/2 * * * ? *) (2分毎)に設定変更して、確認します。

設定変更後、しばらくしてからS3バケットを確認します。 以下のようにオブジェクトが作成されていることを確認できました。

aws s3api list-objects --bucket ${S3BucketName} \
   --query "Contents[].Key" --output yaml \
# 2023-03-25T23:30:00Z/563db7c3-c4cc-41fd-ae07-627example.json
# 2023-03-25T23:32:00Z/51620922-15df-4b57-94b6-84dexample.json
# 2023-03-25T23:34:00Z/5e3c5965-7f19-43fd-a6fc-c45example.json

ちゃんとレポート出力もできていますね。

aws s3 cp s3://${S3BucketName}/2023-03-25T23:30:00Z/563db7c3-c4cc-41fd-ae07-627example.json ./report.json
head -n 3 ./report.json
# {"findings":[
# {"awsAccountId":"123456789012","description":"In the Linux kernel before 6.1.6, a NULL (略)","exploitAvailable":"NO","findingArn":(略)
# {"awsAccountId":"123456789012","description":"An issue in the urllib.parse component of(略)","exploitAvailable":"NO","findingArn":(略)

CloudFormationテンプレート化してみる

これまで作成したEventBridgeスケジューラー(およびそのIAMロール)を CloudFomationテンプレートにしました。 サクッと展開したい方はぜひ活用ください。

AWSTemplateFormatVersion: 2010-09-09

Parameters:
  # IAMロール名
  RoleName:
    Type: String
    Default: "scheduler-role-for-inspector-report"
  # スケジュール名
  ScheduleName:
    Type: String
    Default: "schedule-for-inspector-report"
  # S3バケット名
  S3BucketName:
    Type: String
  # KMSキーARN
  KmsKeyARN:
    Type: String
  # ReportFormat
  ReportFormat:
    Type: String
    Default: "JSON"
    AllowedValues:
      - "CSV"
      - "JSON"

Resources:
  # #####
  # IAMロール
  # #####
  IamRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Ref RoleName
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Principal:
              Service: "scheduler.amazonaws.com"
            Action: "sts:AssumeRole"
      Policies:
        - PolicyName: CreateFindingsReport
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: "Allow"
                Action: "inspector2:CreateFindingsReport"
                Resource: "*"

  # #####
  # EventBridge Scheduler
  # #####
  Schedule:
    Type: AWS::Scheduler::Schedule
    Properties:
      Name: !Ref ScheduleName
      FlexibleTimeWindow:
        Mode: "OFF"
      ScheduleExpression: "cron(0 6 * * ? *)"
      ScheduleExpressionTimezone: "Asia/Tokyo"
      Target:
        Arn: "arn:aws:scheduler:::aws-sdk:inspector2:createFindingsReport"
        Input: !Sub |
          {
            "ReportFormat": "JSON",
            "S3Destination": {
              "BucketName": "${S3BucketName}",
              "KeyPrefix": "<aws.scheduler.scheduled-time>",
              "KmsKeyArn": "${KmsKeyARN}"
            }
          }
        RoleArn: !GetAtt IamRole.Arn

おわりに

以上 Inspector のレポート出力をなるべく楽ちんに自動化してみました。

各種分析ツールで継続的に可視化・レポートしたい際に役に立つかも知れません。

参考