Skip to content

Commit 5188f50

Browse files
committed
Add support for config file in S3.
1 parent d1cc944 commit 5188f50

8 files changed

Lines changed: 332 additions & 19 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ clean: ## Clean the temporary files.
1717

1818
.PHONY: format
1919
format: ## Format the code.
20-
poetry run black .
21-
poetry run ruff check . --fix
20+
poetry run black src
21+
poetry run ruff check src --fix
2222

2323
.PHONY: lint
2424
lint: ## Run all linters (black/ruff/pylint/mypy).

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ A Python utility used to archive old, unused GitHub repositories from an organis
1818
- [Storing the Container on AWS Elastic Container Registry (ECR)](#storing-the-container-on-aws-elastic-container-registry-ecr)
1919
- [Deploying the Lambda](#deploying-the-lambda)
2020
- [Destroying / Removing the Lambda](#destroying--removing-the-lambda)
21+
- [Deployments with Concourse](#deployments-with-concourse)
22+
- [Allowlisting your IP](#allowlisting-your-ip)
23+
- [Setting up a pipeline](#setting-up-a-pipeline)
24+
- [Triggering a pipeline](#triggering-a-pipeline)
2125
- [Linting and Testing](#linting-and-testing)
2226
- [GitHub Actions](#github-actions)
2327
- [Running Tests Locally](#running-tests-locally)
@@ -112,19 +116,21 @@ Before the doing the following, make sure your Daemon is running. If using Colim
112116
-e AWS_SECRET_NAME=<secret_name> \
113117
-e GITHUB_ORG=<org> \
114118
-e GITHUB_APP_CLIENT_ID=<client_id> \
119+
-e S3_BUCKET_NAME=<bucket_name>\
115120
-e AWS_LAMBDA_FUNCTION_TIMEOUT=300
116121
github-repository-archive-script
117122
```
118123

119124
When running the container, you are required to pass some environment variable.
120125

121-
| Variable | Description |
122-
|-----------------------------|-------------------------------------------------------------------------------------------|
123-
| GITHUB_ORG | The organisation you would like to run the tool in. |
124-
| GITHUB_APP_CLIENT_ID | The Client ID for the GitHub App which the tool uses to authenticate with the GitHub API. |
125-
| AWS_DEFAULT_REGION | The AWS Region which the Secret Manager Secret is in. |
126-
| AWS_SECRET_NAME | The name of the AWS Secret Manager Secret to get. |
127-
| AWS_LAMBDA_FUNCTION_TIMEOUT | The timeout time in seconds (Default: 300s / 5 minutes). |
126+
| Variable | Description |
127+
|-----------------------------|----------------------------------------------------------------------------------------------------|
128+
| GITHUB_ORG | The organisation you would like to run the tool in. |
129+
| GITHUB_APP_CLIENT_ID | The Client ID for the GitHub App which the tool uses to authenticate with the GitHub API. |
130+
| AWS_DEFAULT_REGION | The AWS Region which the Secret Manager Secret is in. |
131+
| AWS_SECRET_NAME | The name of the AWS Secret Manager Secret to get. |
132+
| AWS_BUCKET_NAME | The name of the S3 bucket which has the cloud config in (Only used when `use_local_config=False`). |
133+
| AWS_LAMBDA_FUNCTION_TIMEOUT | The timeout time in seconds (Default: 300s / 5 minutes). |
128134

129135
Once the container is running, a local endpoint is created at `localhost:9000/2015-03-31/functions/function/invocations`.
130136

@@ -177,6 +183,7 @@ To run the Lambda function outside of a container, we need to execute the `handl
177183
export AWS_SECRET_ACCESS_KEY=<secret_access_key>
178184
export AWS_DEFAULT_REGION=eu-west-2
179185
export AWS_SECRET_NAME=<secret_name>
186+
export S3_BUCKET_NAME=<bucket_name>
180187
export GITHUB_ORG=<org>
181188
export GITHUB_APP_CLIENT_ID=<client_id>
182189
```

config/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"features": {
3-
"show_log_locally": true
3+
"show_log_locally": false,
4+
"use_local_config": false
45
},
56
"archive_configuration": {
67
"archive_threshold": 365,

src/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(self, debug: bool) -> None:
1212
self.logger.setLevel(logging.INFO)
1313

1414
if debug:
15-
logging.basicConfig(level=logging.DEBUG, filename="debug.log", filemode="w")
15+
logging.basicConfig(filename="debug.log", filemode="w")
1616

1717
def log_info(self, message: str) -> None:
1818
"""Logs an info message to the logger.

src/main.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,39 @@ def handler(event, context) -> str: # type: ignore[no-untyped-def]
512512
features = get_dict_value(config, "features")
513513
archive_rules = get_dict_value(config, "archive_configuration")
514514

515+
# Create a Boto3 session
516+
517+
session = boto3.session.Session()
518+
519+
# Create Boto3 S3 client
520+
521+
s3 = session.client(service_name="s3")
522+
523+
# Check whether to use local config or cloud config
524+
525+
if not get_dict_value(features, "use_local_config"):
526+
527+
bucket_name = os.getenv("S3_BUCKET_NAME")
528+
529+
if not bucket_name:
530+
error_message = "S3_BUCKET_NAME environment variable not set. Please check your environment variables."
531+
raise Exception(error_message)
532+
533+
try:
534+
response = s3.get_object(
535+
Bucket=bucket_name,
536+
Key="config.json",
537+
)
538+
except s3.exceptions.NoSuchKey as e:
539+
error_message = (
540+
f"Configuration file not found in S3 bucket {bucket_name}. Please check the bucket contents."
541+
)
542+
raise Exception(error_message) from e
543+
544+
config = json.loads(response["Body"].read().decode("utf-8"))
545+
features = get_dict_value(config, "features")
546+
archive_rules = get_dict_value(config, "archive_configuration")
547+
515548
# Initialise logging
516549

517550
debug = get_dict_value(features, "show_log_locally")
@@ -528,9 +561,7 @@ def handler(event, context) -> str: # type: ignore[no-untyped-def]
528561

529562
# Create Boto3 Secret Manager client
530563

531-
session = boto3.session.Session()
532564
secret_manager = session.client(service_name="secretsmanager", region_name=aws_default_region)
533-
534565
logger.log_info("Boto3 Secret Manager client created.")
535566

536567
# Create GitHub API interfaces (GraphQL and REST)
@@ -567,8 +598,8 @@ def handler(event, context) -> str: # type: ignore[no-untyped-def]
567598
"## Important Notice \n\n",
568599
f"This repository has not been updated in over {archive_threshold} days and will be archived in {notification_period} days if no action is taken. \n",
569600
"## Actions Required to Prevent Archive \n\n",
570-
f"1. Update the repository by creating/updating an exemption file. \n",
571-
f" - The exemption file should be named one of the following: \n",
601+
"1. Update the repository by creating/updating an exemption file. \n",
602+
" - The exemption file should be named one of the following: \n",
572603
f"{''.join(formatted_filenames)}\n",
573604
" - This file should contain the reason why the repository should not be archived. \n",
574605
" - If the file already exists, please update it with the latest information. \n",

terraform/service/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ resource "aws_lambda_function" "lambda_function" {
5252
GITHUB_APP_CLIENT_ID = var.github_app_client_id
5353
AWS_SECRET_NAME = var.aws_secret_name
5454
AWS_ACCOUNT_NAME = var.env_name
55+
AWS_BUCKET_NAME = var.aws_bucket_name
5556
}
5657
}
5758
}

terraform/service/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ variable "aws_secret_name" {
1818
type = string
1919
}
2020

21+
variable "aws_bucket_name" {
22+
description = "The name of the S3 bucket which the cloud config is stored in"
23+
type = string
24+
default = "${env_name}-github-repository-archive-script"
25+
}
26+
2127
variable "env_name" {
2228
description = "AWS environment"
2329
type = string

0 commit comments

Comments
 (0)