Skip to content

Commit

Permalink
Merge branch 'dev/s3-region-fix' into 'master'
Browse files Browse the repository at this point in the history
Add S3 region support

See merge request hackademint/ctfd!2
  • Loading branch information
SmylerMC committed Sep 2, 2022
2 parents a498dc2 + dbfcce6 commit a42a663
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CTFd/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ AWS_S3_BUCKET =
# A URL pointing to a custom S3 implementation. Only used under the s3 uploader.
AWS_S3_ENDPOINT_URL =

# AWS_S3_REGION
# The aws region that hosts your bucket. Only used in the s3 uploader.
AWS_S3_REGION =

[logs]
# LOG_FOLDER
# The location where logs are written. These are the logs for CTFd key submissions, registrations, and logins. The default location is the CTFd/logs folder.
Expand Down
2 changes: 2 additions & 0 deletions CTFd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ class ServerConfig(object):

AWS_S3_ENDPOINT_URL: str = empty_str_cast(config_ini["uploads"]["AWS_S3_ENDPOINT_URL"])

AWS_S3_REGION: str = empty_str_cast(config_ini["uploads"]["AWS_S3_REGION"])

# === OPTIONAL ===
REVERSE_PROXY: Union[str, bool] = empty_str_cast(config_ini["optional"]["REVERSE_PROXY"], default=False)

Expand Down
2 changes: 2 additions & 0 deletions CTFd/utils/uploads/uploaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ def _get_s3_connection(self):
access_key = get_app_config("AWS_ACCESS_KEY_ID")
secret_key = get_app_config("AWS_SECRET_ACCESS_KEY")
endpoint = get_app_config("AWS_S3_ENDPOINT_URL")
region = get_app_config("AWS_S3_REGION")
client = boto3.client(
"s3",
config=Config(signature_version="s3v4"),
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
endpoint_url=endpoint,
region_name=region
)
return client

Expand Down
6 changes: 4 additions & 2 deletions tests/utils/test_uploaders.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from io import BytesIO

import boto.s3.connection
import boto3
from moto import mock_s3

Expand All @@ -10,15 +11,16 @@

@mock_s3
def test_s3_uploader():
conn = boto3.resource("s3", region_name="us-east-1")
conn.create_bucket(Bucket="bucket")
conn = boto3.resource("s3", region_name="fr-evry-u1")
conn.create_bucket(Bucket="bucket", CreateBucketConfiguration={'LocationConstraint': "fr-evry-u1"})

app = create_ctfd()
with app.app_context():
app.config["UPLOAD_PROVIDER"] = "s3"
app.config["AWS_ACCESS_KEY_ID"] = "AKIAIOSFODNN7EXAMPLE"
app.config["AWS_SECRET_ACCESS_KEY"] = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
app.config["AWS_S3_BUCKET"] = "bucket"
app.config["AWS_S3_REGION"] = "country-city-id"

uploader = S3Uploader()

Expand Down

0 comments on commit a42a663

Please sign in to comment.