Add freezegun to runtime dependencies, generate cachable s3 urls (#2264)

* Add freezegun to application dependencies
* Generate cachable S3 URLs by rounding time down to the previous hour to generate a consistent URL
This commit is contained in:
Kevin Chung
2023-02-19 15:01:28 -05:00
committed by GitHub
parent c8dbfa6050
commit 68da00900a
4 changed files with 24 additions and 11 deletions

View File

@@ -1,6 +1,8 @@
import datetime
import os import os
import posixpath import posixpath
import string import string
import time
from pathlib import PurePath from pathlib import PurePath
from shutil import copyfileobj, rmtree from shutil import copyfileobj, rmtree
@@ -8,6 +10,7 @@ import boto3
from botocore.client import Config from botocore.client import Config
from flask import current_app, redirect, send_file from flask import current_app, redirect, send_file
from flask.helpers import safe_join from flask.helpers import safe_join
from freezegun import freeze_time
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from CTFd.utils import get_app_config from CTFd.utils import get_app_config
@@ -119,8 +122,13 @@ class S3Uploader(BaseUploader):
return dst return dst
def download(self, filename): def download(self, filename):
# S3 URLs by default are valid for one hour.
# We round the timestamp down to the previous hour and generate the link at that time
current_timestamp = int(time.time())
truncated_timestamp = current_timestamp - (current_timestamp % 3600)
key = filename key = filename
filename = filename.split("/").pop() filename = filename.split("/").pop()
with freeze_time(datetime.datetime.fromtimestamp(truncated_timestamp)):
url = self.s3.generate_presigned_url( url = self.s3.generate_presigned_url(
"get_object", "get_object",
Params={ Params={
@@ -129,7 +137,9 @@ class S3Uploader(BaseUploader):
"ResponseContentDisposition": "attachment; filename={}".format( "ResponseContentDisposition": "attachment; filename={}".format(
filename filename
), ),
"ResponseCacheControl": "max-age=3600",
}, },
ExpiresIn=3600,
) )
return redirect(url) return redirect(url)

View File

@@ -4,7 +4,6 @@ pytest==5.4.2
pytest-randomly==3.4.0 pytest-randomly==3.4.0
coverage==5.1 coverage==5.1
flake8==3.8.2 flake8==3.8.2
freezegun==0.3.15
psycopg2-binary==2.8.6 psycopg2-binary==2.8.6
codecov==2.1.7 codecov==2.1.7
moto==1.3.16 moto==1.3.16

View File

@@ -29,3 +29,4 @@ python-geoacumen-city==2023.1.15
maxminddb==1.5.4 maxminddb==1.5.4
tenacity==6.2.0 tenacity==6.2.0
pybluemonday==0.0.9 pybluemonday==0.0.9
freezegun==1.2.2

View File

@@ -59,6 +59,8 @@ flask-sqlalchemy==2.4.3
# via # via
# -r requirements.in # -r requirements.in
# flask-migrate # flask-migrate
freezegun==1.2.2
# via -r requirements.in
gevent==22.10.2 gevent==22.10.2
# via -r requirements.in # via -r requirements.in
greenlet==2.0.1 greenlet==2.0.1
@@ -115,6 +117,7 @@ python-dateutil==2.8.1
# via # via
# alembic # alembic
# botocore # botocore
# freezegun
python-dotenv==0.13.0 python-dotenv==0.13.0
# via -r requirements.in # via -r requirements.in
python-editor==1.0.4 python-editor==1.0.4