Skip to content

Commit 5b11920

Browse files
authored
Merge branch 'apache:master' into optional_schema
2 parents def91c0 + f3d10e3 commit 5b11920

File tree

5 files changed

+155
-11
lines changed

5 files changed

+155
-11
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# This workflow works with the infrastructure policy enforcer to
19+
# generate a report of IAM and Service Account Policies violations
20+
21+
name: Infrastructure Policy Enforcer
22+
23+
on:
24+
workflow_dispatch:
25+
schedule:
26+
# Once a week at 9:00 AM on Monday
27+
- cron: '0 9 * * 1'
28+
29+
# This allows a subsequently queued workflow run to interrupt previous runs
30+
concurrency:
31+
group: '${{ github.workflow }} @ ${{ github.event.issue.number || github.sha || github.head_ref || github.ref }}-${{ github.event.schedule || github.event.comment.id || github.event.sender.login }}'
32+
cancel-in-progress: true
33+
34+
#Setting explicit permissions for the action to avoid the default permissions which are `write-all` in case of pull_request_target event
35+
permissions:
36+
contents: read
37+
issues: write
38+
39+
jobs:
40+
beam_Infrastructure_PolicyEnforcer:
41+
name: Check and Report Infrastructure Policies Violations
42+
runs-on: [self-hosted, ubuntu-20.04, main]
43+
timeout-minutes: 30
44+
steps:
45+
- uses: actions/checkout@v4
46+
47+
- name: Setup Python
48+
uses: actions/setup-python@v4
49+
with:
50+
python-version: '3.13'
51+
52+
- name: Install Python dependencies
53+
working-directory: ./infra/enforcement
54+
run: |
55+
python -m pip install --upgrade pip
56+
pip install -r requirements.txt
57+
58+
- name: Setup gcloud
59+
uses: google-github-actions/setup-gcloud@v2
60+
61+
- name: Run IAM Policy Enforcement
62+
working-directory: ./infra/enforcement
63+
env:
64+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
65+
GITHUB_REPOSITORY: ${{ github.repository }}
66+
SMTP_SERVER: smtp.gmail.com
67+
SMTP_PORT: 465
68+
EMAIL_ADDRESS: ${{ secrets.ISSUE_REPORT_SENDER_EMAIL_ADDRESS }}
69+
EMAIL_PASSWORD: ${{ secrets.ISSUE_REPORT_SENDER_EMAIL_PASSWORD }}
70+
EMAIL_RECIPIENT: "[email protected]"
71+
run: python iam.py --action print
72+
73+
- name: Run Account Keys Policy Enforcement
74+
working-directory: ./infra/enforcement
75+
env:
76+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
77+
GITHUB_REPOSITORY: ${{ github.repository }}
78+
SMTP_SERVER: smtp.gmail.com
79+
SMTP_PORT: 465
80+
EMAIL_ADDRESS: ${{ secrets.ISSUE_REPORT_SENDER_EMAIL_ADDRESS }}
81+
EMAIL_PASSWORD: ${{ secrets.ISSUE_REPORT_SENDER_EMAIL_PASSWORD }}
82+
EMAIL_RECIPIENT: "[email protected]"
83+
run: python account_keys.py --action print

infra/enforcement/README.md

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919

2020
# Infrastructure rules enforcement
2121

22-
This module is used to check that the infrastructure rules are being used.
22+
This module is used to check that the infrastructure rules are being used and provides automated notifications for compliance violations.
23+
24+
The enforcement tools support multiple notification methods:
25+
- **GitHub Issues**: Automatically create GitHub issues with detailed compliance reports
26+
- **Email Notifications**: Send email alerts via SMTP for compliance violations
27+
- **Console Output**: Print detailed reports to console for manual review
2328

2429
## IAM Policies
2530

@@ -34,8 +39,11 @@ You can specify the action either through the configuration file (`config.yml`)
3439
# Check compliance and report issues (default)
3540
python iam.py --action check
3641

37-
# Create GitHub issue if compliance violations are found
38-
python iam.py --action issue
42+
# Create/update GitHub issue and send email if compliance violations are found
43+
python iam.py --action announce
44+
45+
# Print announcement details for testing purposes (no actual issue created)
46+
python iam.py --action print
3947

4048
# Generate new compliance file based on current IAM policy
4149
python iam.py --action generate
@@ -44,7 +52,8 @@ python iam.py --action generate
4452
### Actions
4553

4654
- **check**: Validates IAM policies against defined policies and reports any differences (default behavior)
47-
- **issue**: Creates a GitHub issue when IAM policies differ from the defined ones, including detailed permission discrepancies
55+
- **announce**: Creates or updates a GitHub issue and sends an email notification when IAM policies differ from the defined ones. If no open issue exists, creates a new one; if an open issue exists, updates the issue body with current violations
56+
- **print**: Prints announcement details for testing purposes without creating actual GitHub issues or sending emails
4857
- **generate**: Updates the compliance file to match the current GCP IAM policy, creating a new baseline from existing permissions
4958

5059
### Features
@@ -58,16 +67,31 @@ The IAM Policy enforcement tool provides the following capabilities:
5867
- **Sorted Output**: Provides consistent, sorted output for easy comparison and review
5968
- **Detailed Reporting**: Comprehensive reporting of permission differences with clear before/after comparisons
6069
- **GitHub Integration**: Automatic issue creation with detailed compliance violation reports
70+
- **Email Notifications**: Optional email notifications for compliance issues via SMTP
71+
- **Issue Management**: Smart issue handling - creates new issues when none exist, updates existing open issues with current violations
72+
- **Testing Support**: Print action allows testing notification content without actually sending
6173

6274
### Configuration
6375

6476
The `config.yml` file supports the following parameters for IAM policies:
6577

6678
- `project_id`: GCP project ID to check (default: `apache-beam-testing`)
6779
- `users_file`: Path to the YAML file containing expected IAM policies (default: `../iam/users.yml`)
68-
- `action`: Default action to perform (`check`, `issue`, or `generate`)
80+
- `action`: Default action to perform (`check`, `announce`, `print`, or `generate`)
6981
- `logging`: Logging configuration (level and format)
7082

83+
### Environment Variables (for announce action)
84+
85+
When using the `announce` action, the following environment variables are required:
86+
87+
- `GITHUB_TOKEN`: GitHub personal access token for creating issues
88+
- `GITHUB_REPOSITORY`: Repository in format `owner/repo` (default: `apache/beam`)
89+
- `SMTP_SERVER`: SMTP server for email notifications
90+
- `SMTP_PORT`: SMTP port (default: 587)
91+
- `EMAIL_ADDRESS`: Email address for sending notifications
92+
- `EMAIL_PASSWORD`: Email password for authentication
93+
- `EMAIL_RECIPIENT`: Email address to receive notifications
94+
7195
### IAM Policy File Format
7296

7397
The IAM policy file should follow this YAML structure:
@@ -98,9 +122,28 @@ Each user entry includes:
98122
3. **Role Processing**: Processes all roles while filtering out conditional bindings
99123
4. **Comparison**: Compares current permissions with expected permissions from the policy file
100124
5. **Reporting**: Generates detailed reports of any discrepancies found
125+
6. **Notification**: Sends notifications via GitHub issues and/or email when using announce action
126+
127+
The `print` action can be used for testing notification content without actually creating GitHub issues or sending emails.
101128

102129
Command-line arguments take precedence over configuration file settings.
103130

131+
## GitHub Actions Integration
132+
133+
The enforcement tools are integrated with GitHub Actions to provide automated compliance monitoring. The workflow is configured to run weekly and automatically create GitHub issues and send email notifications for any policy violations.
134+
135+
### Workflow Configuration
136+
137+
The GitHub Actions workflow (`.github/workflows/beam_Infrastructure_PolicyEnforcer.yml`) runs:
138+
- **Schedule**: Weekly on Mondays at 9:00 AM UTC
139+
- **Manual trigger**: Can be triggered manually via `workflow_dispatch`
140+
- **Actions**: Runs both IAM and Account Keys enforcement with the `announce` action
141+
142+
**Note**:
143+
- The email service is configured to use gmail
144+
- The recipient email is set to `[email protected]` for Apache Beam project notifications
145+
- The `GITHUB_TOKEN` is automatically provided by GitHub Actions and doesn't need to be configured manually
146+
104147
## Account Keys
105148

106149
The enforcement is also done by validating service account keys and their access permissions against the defined policies.
@@ -114,8 +157,11 @@ You can specify the action either through the configuration file (`config.yml`)
114157
# Check compliance and report issues (default)
115158
python account_keys.py --action check
116159
117-
# Create GitHub issue if compliance violations are found
118-
python account_keys.py --action issue
160+
# Create/update GitHub issue and send email if compliance violations are found
161+
python account_keys.py --action announce
162+
163+
# Print announcement details for testing purposes (no actual issue created)
164+
python account_keys.py --action print
119165
120166
# Generate new compliance file based on current service account keys policy
121167
python account_keys.py --action generate
@@ -124,7 +170,8 @@ python account_keys.py --action generate
124170
### Actions
125171

126172
- **check**: Validates service account keys and their permissions against defined policies and reports any differences (default behavior)
127-
- **issue**: Creates a GitHub issue when service account keys policies differ from the defined ones
173+
- **announce**: Creates or updates a GitHub issue and sends an email notification when service account keys policies differ from the defined ones. If no open issue exists, creates a new one; if an open issue exists, updates the issue body with current violations
174+
- **print**: Prints announcement details for testing purposes without creating actual GitHub issues or sending emails
128175
- **generate**: Updates the compliance file to match the current GCP service account keys and Secret Manager permissions
129176

130177
### Features
@@ -143,9 +190,21 @@ The `config.yml` file supports the following parameters for account keys:
143190

144191
- `project_id`: GCP project ID to check
145192
- `service_account_keys_file`: Path to the YAML file containing expected service account keys policies (default: `../keys/keys.yaml`)
146-
- `action`: Default action to perform (`check`, `issue`, or `generate`)
193+
- `action`: Default action to perform (`check`, `announce`, `print`, or `generate`)
147194
- `logging`: Logging configuration (level and format)
148195

196+
### Environment Variables (for announce action)
197+
198+
When using the `announce` action, the following environment variables are required:
199+
200+
- `GITHUB_TOKEN`: GitHub personal access token for creating issues
201+
- `GITHUB_REPOSITORY`: Repository in format `owner/repo` (default: `apache/beam`)
202+
- `SMTP_SERVER`: SMTP server for email notifications
203+
- `SMTP_PORT`: SMTP port (default: 587)
204+
- `EMAIL_ADDRESS`: Email address for sending notifications
205+
- `EMAIL_PASSWORD`: Email password for authentication
206+
- `EMAIL_RECIPIENT`: Email address to receive notifications
207+
149208
### Service Account Keys File Format
150209

151210
The service account keys file should follow this YAML structure:

infra/enforcement/config.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ service_account_keys_file: ../keys/keys.yaml
3232
# Action to perform when running the script
3333
# Options:
3434
# - check: Check compliance and report issues (default)
35-
# - issue: Create GitHub issue if compliance violations are found
35+
# - announce: Create/update GitHub issue and send email if compliance violations are found
36+
# - print: Print announcement details for testing purposes
3637
# - generate: Generate new compliance file based on current IAM policy
37-
action: check
38+
action: announce

infra/enforcement/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ google-cloud-iam==2.19.0
2121
google-cloud-resource-manager==1.14.1
2222
google-cloud-secret-manager==2.24.0
2323
google-crc32c==1.7.1
24+
requests==2.32.4

0 commit comments

Comments
 (0)