-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add a security GCP log analyzer #35922
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
33f48cb
Add the base log_analyzer
ksobrenat32 4486544
Add github action for security logging
ksobrenat32 4f8a48e
Enhance LogAnalyzer to filter logs by time range and include file nam…
ksobrenat32 cc257b4
Add dry-run option for weekly email report generation in LogAnalyzer
ksobrenat32 97bc128
Better error handling for timezones and missing details
ksobrenat32 c4f5792
Refactor LogAnalyzer to use SinkCls for type consistency and enhance …
ksobrenat32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| # This workflow works with the GCP security log analyzer to | ||
| # generate weekly security reports and initialize log sinks | ||
|
|
||
| name: GCP Security Log Analyzer | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| schedule: | ||
| # Once a week at 9:00 AM on Monday | ||
| - cron: '0 9 * * 1' | ||
| push: | ||
| paths: | ||
| - 'infra/security/config.yml' | ||
|
|
||
| # This allows a subsequently queued workflow run to interrupt previous runs | ||
| concurrency: | ||
| group: '${{ github.workflow }} @ ${{ github.sha || github.head_ref || github.ref }}-${{ github.event.schedule || github.event.sender.login }}' | ||
| cancel-in-progress: true | ||
|
|
||
| #Setting explicit permissions for the action to avoid the default permissions which are `write-all` in case of pull_request_target event | ||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| beam_GCP_Security_LogAnalyzer: | ||
| name: GCP Security Log Analysis | ||
| runs-on: [self-hosted, ubuntu-20.04, main] | ||
| timeout-minutes: 30 | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup Python | ||
| uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: '3.13' | ||
|
|
||
| - name: Install Python dependencies | ||
| working-directory: ./infra/security | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install -r requirements.txt | ||
|
|
||
| - name: Setup gcloud | ||
| uses: google-github-actions/setup-gcloud@v2 | ||
|
|
||
| - name: Initialize Log Sinks | ||
| if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' | ||
| working-directory: ./infra/security | ||
| run: python log_analyzer.py --config config.yml initialize | ||
|
|
||
| - name: Generate Weekly Security Report | ||
| if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' | ||
| working-directory: ./infra/security | ||
| env: | ||
| SMTP_SERVER: smtp.gmail.com | ||
| SMTP_PORT: 465 | ||
| EMAIL_ADDRESS: ${{ secrets.ISSUE_REPORT_SENDER_EMAIL_ADDRESS }} | ||
| EMAIL_PASSWORD: ${{ secrets.ISSUE_REPORT_SENDER_EMAIL_PASSWORD }} | ||
| EMAIL_RECIPIENT: "dev@beam.apache.org" | ||
| run: python log_analyzer.py --config config.yml generate-report --dry-run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <!-- | ||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations | ||
| under the License. | ||
| --> | ||
|
|
||
| # GCP Security Analyzer | ||
|
|
||
| This document describes the implementation of a security analyzer for Google Cloud Platform (GCP) resources. The analyzer is designed to enhance security monitoring within our GCP environment by capturing critical events and generating alerts for specific security-sensitive actions. | ||
|
|
||
| ## How It Works | ||
|
|
||
| 1. **Log Sinks**: The system uses [GCP Log Sinks](https://cloud.google.com/logging/docs/export/configure_export_v2) to capture specific security-related log entries. These sinks are configured to filter for events like IAM policy changes or service account key creation. | ||
| 2. **Log Storage**: The filtered logs are routed to a dedicated Google Cloud Storage (GCS) bucket for persistence and analysis. | ||
| 3. **Report Generation**: A scheduled job runs weekly, executing the `log_analyzer.py` script. | ||
| 4. **Email Alerts**: The script analyzes the logs from the past week, compiles a summary of security events, and sends a report to a configured email address. | ||
|
|
||
| ## Configuration | ||
|
|
||
| The behavior of the log analyzer is controlled by a `config.yml` file. Here’s an overview of the configuration options: | ||
|
|
||
| - `project_id`: The GCP project ID where the resources are located. | ||
| - `bucket_name`: The name of the GCS bucket where logs will be stored. | ||
| - `logging`: Configures the logging level and format for the script. | ||
| - `sinks`: A list of log sinks to be created. Each sink has the following properties: | ||
| - `name`: A unique name for the sink. | ||
| - `description`: A brief description of what the sink monitors. | ||
| - `filter_methods`: A list of GCP API methods to include in the filter (e.g., `SetIamPolicy`). | ||
| - `excluded_principals`: A list of service accounts or user emails to exclude from monitoring, such as CI/CD service accounts. | ||
|
|
||
| ### Example Configuration (`config.yml`) | ||
|
|
||
| ```yaml | ||
| project_id: your-gcp-project-id | ||
| bucket_name: your-log-storage-bucket | ||
|
|
||
| sinks: | ||
| - name: iam-policy-changes | ||
| description: Monitors changes to IAM policies. | ||
| filter_methods: | ||
| - "SetIamPolicy" | ||
| excluded_principals: | ||
| - "ci-cd-account@your-project.iam.gserviceaccount.com" | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| The `log_analyzer.py` script provides two main commands for managing the security analyzer. | ||
|
|
||
| ### Initializing Sinks | ||
|
|
||
| To create or update the log sinks in GCP based on your `config.yml` file, run the following command: | ||
|
|
||
| ```bash | ||
| python log_analyzer.py --config config.yml initialize | ||
| ``` | ||
|
|
||
| This command ensures that the log sinks are correctly configured to capture the desired security events. | ||
|
|
||
| ### Generating Weekly Reports | ||
|
|
||
| To generate and send the weekly security report, run this command: | ||
|
|
||
| ```bash | ||
| python log_analyzer.py --config config.yml generate-report | ||
| ``` | ||
|
|
||
| This is typically run as a scheduled job (GitHub Action) to automate the delivery of weekly security reports. | ||
|
|
||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| project_id: testing-me-460223 | ||
|
|
||
| # Logging | ||
| logging: | ||
| level: DEBUG | ||
| format: "[%(asctime)s] %(levelname)s: %(message)s" | ||
|
|
||
| # gcloud storage bucket | ||
| bucket_name: "testing-me-460223-tfstate" | ||
|
|
||
| # GCP Log sinks | ||
| sinks: | ||
| - name: iam-policy-changes | ||
| description: Monitors changes to IAM policies, excluding approved CI/CD service accounts. | ||
| filter_methods: | ||
| - "SetIamPolicy" | ||
| excluded_principals: | ||
| - beam-github-actions@apache-beam-testing.iam.gserviceaccount.com | ||
| - github-self-hosted-runners@apache-beam-testing.iam.gserviceaccount.com | ||
|
|
||
| - name: sa-key-management | ||
| description: Monitors creation and deletion of service account keys. | ||
| filter_methods: | ||
| - "google.iam.admin.v1.IAM.CreateServiceAccountKey" | ||
| - "google.iam.admin.v1.IAM.DeleteServiceAccountKey" | ||
| excluded_principals: | ||
| - beam-github-actions@apache-beam-testing.iam.gserviceaccount.com | ||
| - github-self-hosted-runners@apache-beam-testing.iam.gserviceaccount.com | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.