Skip to content

Commit 372b25b

Browse files
authored
Add GitHub Actions workflow for managing GCP Service Account keys (#35911)
* Add GitHub Actions workflow for managing GCP Service Account keys * Update GCP Service Account keys workflow to allow queueing and run the workflow on Mondays * Refactor key rotation service to remove GCS logging and add dry run option for cron job
1 parent 4bcef0b commit 372b25b

6 files changed

Lines changed: 128 additions & 214 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 modifies the GCP Service Account keys and manages the
19+
# storage, saving them onto Google Cloud Secret Manager. It also handles
20+
# the rotation of the keys.
21+
22+
name: Service Account Keys Management
23+
24+
on:
25+
workflow_dispatch:
26+
# Trigger when the keys.yaml file is modified on the main branch
27+
push:
28+
branches:
29+
- main
30+
paths:
31+
- 'infra/keys/keys.yaml'
32+
schedule:
33+
# Once a week at 9:00 AM on Monday
34+
- cron: '0 9 * * 1'
35+
36+
# This ensures that only one workflow run is running at a time, and others are queued.
37+
concurrency:
38+
group: ${{ github.workflow }}
39+
cancel-in-progress: false
40+
41+
#Setting explicit permissions for the action to avoid the default permissions which are `write-all` in case of pull_request_target event
42+
permissions:
43+
contents: read
44+
45+
jobs:
46+
beam_UserRoles:
47+
name: Apply user roles changes
48+
runs-on: [self-hosted, ubuntu-20.04, main]
49+
timeout-minutes: 30
50+
steps:
51+
- uses: actions/checkout@v4
52+
- name: Setup gcloud
53+
uses: google-github-actions/setup-gcloud@v2
54+
55+
- name: Setup Python
56+
uses: actions/setup-python@v4
57+
with:
58+
python-version: '3.13'
59+
60+
- name: Install Python dependencies
61+
working-directory: ./infra/keys
62+
run: |
63+
python -m pip install --upgrade pip
64+
pip install -r requirements.txt
65+
66+
- name: Run Service Account Key Management
67+
working-directory: ./infra/keys
68+
run: python keys.py --cron-dry-run

infra/keys/README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,17 @@ This section is intended for developers who need to manage service accounts and
4444

4545
### How it works
4646

47-
This module provide a script `keys.py` that allows you to manage the service accounts and their keys. This script is run as a cron job daily to ensure that service account keys are rotated regularly and that the latest keys are available for authorized users. It is also run every time a PR is merged over the `keys.yaml` file to ensure that the service accounts, their keys and authorized users are up to date.
47+
This module provide a script `keys.py` that allows you to manage the service accounts and their keys. This script is run automatically by a GitHub Action to ensure that service account keys are rotated regularly and that the latest keys are available for authorized users. It is also run every time a PR is merged over the `keys.yaml` file to ensure that the service accounts, their keys and authorized users are up to date.
48+
49+
### Automation with GitHub Actions
50+
51+
A GitHub Actions workflow is set up to automate the execution of the `keys.py` script. This workflow is defined in `.github/workflows/beam_Infrastructure_ServiceAccountKeys.yml`.
52+
53+
The workflow is triggered automatically on the following events:
54+
- A push to the `main` branch that includes changes to the `infra/keys/keys.yaml` file.
55+
- A manual trigger (`workflow_dispatch`) by a developer.
56+
57+
When triggered, the workflow runs the `python keys.py --cron` command, which handles the creation and rotation of service account keys based on the configuration in `keys.yaml` and `config.yaml`.
4858

4959
### Files
5060

@@ -88,6 +98,6 @@ This will rotate keys for all service accounts defined in the `keys.yaml` file t
8898
To retrieve the latest service account key, use the `--get-key` flag:
8999

90100
```bash
91-
python main.py --get-key my-service-account
101+
python keys.py --get-key my-service-account
92102
```
93103

infra/keys/config.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,5 @@ grace_period: 2
3030

3131
# LOGGING
3232

33-
# The secret rotation logs will be stored in a GCP bucket.
34-
bucket_name: "beam-terraform-infra-state"
35-
# The log file will be stored in the bucket with the following prefix
36-
log_file_prefix: "beam-secrets-rotation"
3733
# Logging level for the secrets rotation service
3834
logging_level: "DEBUG" # Options: DEBUG, INFO, WARNING, ERROR, CRITICAL

infra/keys/gcp_logger.py

Lines changed: 0 additions & 153 deletions
This file was deleted.

0 commit comments

Comments
 (0)