Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure out how to collect CloudTrail logs using the Elastic Agent (aws-s3 input) #79

Open
zmoog opened this issue Mar 3, 2024 · 7 comments
Assignees
Labels
documentation Improvements or additions to documentation research

Comments

@zmoog
Copy link
Owner

zmoog commented Mar 3, 2024

I want to collect Cloudtrail logs from a bucket named aws-cloudtrail-logs-1234-2761c2fa using the Elastic Agent, and send them to Elasticsearch.

@zmoog zmoog self-assigned this Mar 3, 2024
@zmoog zmoog added documentation Improvements or additions to documentation research labels Mar 3, 2024
@zmoog zmoog added this to Notes Mar 3, 2024
@zmoog zmoog moved this to In Progress in Notes Mar 3, 2024
@zmoog zmoog changed the title Figure out how to collect Cloudtrail logs using the Elastic Agent (with aws-s3 input) Figure out how to collect CloudTrail logs using the Elastic Agent (with aws-s3 input) Mar 3, 2024
@zmoog zmoog changed the title Figure out how to collect CloudTrail logs using the Elastic Agent (with aws-s3 input) Figure out how to collect CloudTrail logs using the Elastic Agent (aws-s3 input) Mar 3, 2024
@zmoog
Copy link
Owner Author

zmoog commented Mar 3, 2024

Overview

Here is the overview of the solution.

Architecture

┌─────────────────┐  (1) sends      ┌─────────────────┐               (4)
│   CloudTrail    │────events──────▶│    S3 bucket    │◀────────────fetches ──────────────┐
└─────────────────┘                 └─────────────────┘             new logs              │
                                             │                                            │
                                         (2) send                                         │
                                       notifications                          ┌ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─
                                             │                                            │          │
                                             ▼                                │  ┌─────────────────┐
                                    ┌─────────────────┐      (3) fetches         │     aws-s3      │ │ (5) sends    ┌─────────────────┐
                                    │    SQS queue    │◀─────────new ─────────┼──│    <<input>>    │──────logs─────▶│  Elasticsearch  │
                                    └─────────────────┘     notifications        └─────────────────┘ │              └─────────────────┘
                                                                              │
                                                                               ─Elastic Agent─ ─ ─ ─ ┘
  1. CloudTrail sends audit events to an S3 bucket in .json.gz files.
  2. On each file creation, S3 sends a notification to the SQS queue
  3. The Elastic Agent receives the notification
  4. The Elastic Agent fetches the S3 object using the information from the notification.
  5. The Elastic Agent sends the log events to Elasticsearch

Steps

Here are the required steps to implement the solution.

  • Create a trail to export events to an S3 bucket.
  • Create an SQS queue
  • Allow the S3 service to publish message to the SQS queue
  • Set up the S3 bucket to send notifications to an SQS queue.

@zmoog
Copy link
Owner Author

zmoog commented Mar 3, 2024

Create a trail to export events to an S3 bucket

Visit the AWS web console and create a trail, storing the trail the event in a S3 bucket.

In this example, we create a trail named my-trail that stores events in a S3 bucket named aws-cloudtrail-logs-1234-2761c2fa.

CleanShot 2024-03-03 at 12 52 37@2x

@zmoog
Copy link
Owner Author

zmoog commented Mar 3, 2024

Create an SQS queue

We need an SQS queue where we will send the S3 object creation notifications for the aws-cloudtrail-logs-1234-2761c2fa bucket.

Create a new SQS queue named mbranca-cloudtrail-logs-notifications-queue.

$ aws sqs create-queue --queue-name mbranca-cloudtrail-logs-notifications-queue
{
    "QueueUrl": "https://sqs.eu-west-1.amazonaws.com/123/mbranca-cloudtrail-logs-notifications-queue"
}

@zmoog
Copy link
Owner Author

zmoog commented Mar 3, 2024

Allow the S3 service to publish message to the SQS queue

Allow S3 to send object creation notifications from aws-cloudtrail-logs-1234-2761c2fa to mbranca-cloudtrail-logs-notifications-queue:

$ cat policy.json
{
  "Version": "2008-10-17",
  "Id": "__default_policy_ID",
  "Statement": [
    {
      "Sid": "__owner_statement",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::123:root"
      },
      "Action": "SQS:*",
      "Resource": "aws-cloudtrail-logs-123-2761c2fa"
    },
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "s3.amazonaws.com"
      },
      "Action": "SQS:SendMessage",
      "Resource": "arn:aws:sqs:eu-west-1:123:mbranca-cloudtrail-logs-notifications-queue"
    }
  ]
}


# Set the SQS access policy
- Visit Amazon SQS > Queues > mbranca-cloudtrail-logs-notifications-queue > Access policy > Access policy (Permissions) 
- Edit, paste the content of `policy.json`, and save.

@zmoog
Copy link
Owner Author

zmoog commented Mar 3, 2024

Set up the S3 bucket to send notification to an SQS queue

The S3 bucket will notify the SQS queue whenever any s3:ObjectCreated:* event occurs.

# Enable notifications
$ cat notifications.json
{
    "QueueConfigurations": [
        {
            "Id": "Creations",
            "QueueArn": "arn:aws:sqs:eu-west-1:123:mbranca-cloudtrail-logs-notifications-queue",
            "Events": [
                "s3:ObjectCreated:*"
            ],
            "Filter": {
                "Key": {
                    "FilterRules": [
                        {
                            "Name": "Prefix",
                            "Value": ""
                        },
                        {
                            "Name": "Suffix",
                            "Value": ""
                        }
                    ]
                }
            }
        }
    ]
}

aws s3api put-bucket-notification-configuration \
    --bucket aws-cloudtrail-logs-1234-2761c2fa \
    --notification-configuration file://notifications.json

@zmoog
Copy link
Owner Author

zmoog commented Mar 3, 2024

Checkpoint

After this step, visit the AWS console and click on "Poll for messages" and you should be able to see a few messages with notification sent from S3.

CleanShot 2024-03-03 at 13 19 18@2x

Here's a sample notification:

{
  "Records": [
    {
      "eventVersion": "2.1",
      "eventSource": "aws:s3",
      "awsRegion": "eu-west-1",
      "eventTime": "2024-03-03T12:17:09.882Z",
      "eventName": "ObjectCreated:Put",
      "userIdentity": {
        "principalId": "<redacted>"
      },
      "requestParameters": {
        "sourceIPAddress": "<redacted>"
      },
      "s3": {
        "s3SchemaVersion": "1.0",
        "configurationId": "Creations",
        "bucket": {
          "name": "aws-cloudtrail-logs-1234-2761c2fa",
          "ownerIdentity": {
            "principalId": "<redacted>"
          },
          "arn": "arn:aws:s3:::aws-cloudtrail-logs-1234-2761c2fa"
        },
        "object": {
          "key": "AWSLogs/<redacted>/CloudTrail/eu-west-1/2024/03/03/<redacted>_CloudTrail_eu-west-1_20240303T1215Z_wf92tb6UZ23lB814.json.gz",
          "size": 1206,
          "eTag": "f19ba3a4761c6598231c456bdb698d80",
          "sequencer": "0065E46A45D5F39177"
        }
      }
    }
  ]
}

@zmoog
Copy link
Owner Author

zmoog commented Mar 3, 2024

Set up the AWS integration

We must set up the integration to receive notifications from the SQS queue.

  1. The "Collect logs via S3 Bucket" toggle must be off.
  2. Set the SQS queue URL in the "[SQS] Queue URL" field.

CleanShot 2024-03-03 at 18 21 55@2x

After you assign the policy to an Agent, it will start collecting CloudTrails logs:

CleanShot 2024-03-03 at 18 28 05@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation research
Projects
Status: In Review
Development

No branches or pull requests

1 participant