-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. generate previews in s3_worker and upload them to s3 storage (#362)
S3 + CF integration
- Loading branch information
Showing
16 changed files
with
447 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import typer | ||
from rich import print | ||
from typing_extensions import Annotated | ||
|
||
from papermerge.core import cloudfront | ||
|
||
app = typer.Typer(help="List various entities") | ||
|
||
ValidFor = Annotated[ | ||
int, | ||
typer.Argument(help='Number of seconds the url will be valid for') | ||
] | ||
|
||
|
||
@app.command() | ||
def cf_sign_url(url: str, valid_for: ValidFor = 600): | ||
"""Sign URL using AWS CloudFront signer""" | ||
result = cloudfront.sign_url(url, valid_for) | ||
print(f"Signed URL: {result}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from datetime import datetime, timedelta | ||
from pathlib import Path | ||
|
||
from botocore.signers import CloudFrontSigner | ||
from cryptography.hazmat.backends import default_backend | ||
from cryptography.hazmat.primitives import hashes, serialization | ||
from cryptography.hazmat.primitives.asymmetric import padding | ||
from django.conf import settings | ||
|
||
|
||
def rsa_signer(message): | ||
_kpath = getattr(settings, 'CF_SIGN_URL_PRIVATE_KEY', None) | ||
if _kpath is None: | ||
raise ValueError( | ||
"Missing CF_SIGN_URL_PRIVATE_KEY setting" | ||
) | ||
|
||
key_path = Path(_kpath) | ||
if not key_path.exists(): | ||
raise ValueError( | ||
f"{key_path} does not exist" | ||
) | ||
|
||
with open(key_path, 'rb') as key_file: | ||
private_key = serialization.load_pem_private_key( | ||
key_file.read(), | ||
password=None, | ||
backend=default_backend() | ||
) | ||
return private_key.sign(message, padding.PKCS1v15(), hashes.SHA1()) | ||
|
||
|
||
def sign_url(url: str, valid_for: int = 600): | ||
""" | ||
:type url: str | ||
:param url: The URL of the protected object | ||
:type valid_for: int | ||
:param valid_for: number of seconds the url will be valid for, defaults | ||
to 600 (i.e. 10 minutes) | ||
""" | ||
key_id = getattr(settings, 'CF_SIGN_URL_KEY_ID', None) | ||
if key_id is None: | ||
raise ValueError( | ||
"CF_SIGN_URL_KEY_ID is empty" | ||
) | ||
cf_signer = CloudFrontSigner(key_id, rsa_signer) | ||
date_less_than = datetime.now() + timedelta(seconds=valid_for) | ||
signed_url = cf_signer.generate_presigned_url( | ||
url, | ||
date_less_than=date_less_than | ||
) | ||
return signed_url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.