Skip to content

Commit

Permalink
storage: Add akamai storage provider (PROJQUAY-7238)
Browse files Browse the repository at this point in the history
Adds Akamai as another S3 backed storage provider
for CDN redundency
  • Loading branch information
syed committed May 21, 2024
1 parent 470141e commit ebec4e3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
akamai-edgeauth==0.3.2
alembic==1.3.3
aniso8601 @ git+https://github.com/DevTable/aniso8601-fake.git@bd7762c7dea0498706d3f57db60cd8a8af44ba90
APScheduler==3.10.4
Expand Down
2 changes: 2 additions & 0 deletions storage/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from storage.akamaistorage import AkamaiS3Storage
from storage.azurestorage import AzureStorage
from storage.cloud import (
CloudFrontedS3Storage,
Expand Down Expand Up @@ -33,6 +34,7 @@
"MultiCDNStorage": MultiCDNStorage,
"IBMCloudStorage": IBMCloudStorage,
"STSS3Storage": STSS3Storage,
"AkamaiS3Storage": AkamaiS3Storage,
}


Expand Down
87 changes: 87 additions & 0 deletions storage/akamaistorage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import logging
import urllib.parse
from akamai.edgeauth import EdgeAuth, EdgeAuthError

logger = logging.getLogger(__name__)

from storage.cloud import S3Storage

DEFAULT_SIGNED_URL_EXPIRY_SECONDS = 900 # 15 mins
TOKEN_QUERY_STRING = "akamai_signature"


class AkamaiS3Storage(S3Storage):
"""
Akamai CDN backed by S3 storage
"""

def __init__(
self,
context,
akamai_domain,
akamai_shared_secret,
storage_path,
s3_bucket,
s3_region,
*args,
**kwargs,
):
super(AkamaiS3Storage, self).__init__(
context, storage_path, s3_bucket, s3_region=s3_region, *args, **kwargs
)

self.akamai_domain = akamai_domain
self.akamai_shared_secret = akamai_shared_secret
self.region = s3_region
self.et = EdgeAuth(
token_name=TOKEN_QUERY_STRING,
key=self.akamai_shared_secret,
window_seconds=DEFAULT_SIGNED_URL_EXPIRY_SECONDS,
escape_early=True,
)

def get_direct_download_url(
self, path, request_ip=None, expires_in=60, requires_cors=False, head=False, **kwargs
):
# If CloudFront could not be loaded, fall back to normal S3.
s3_presigned_url = super(AkamaiS3Storage, self).get_direct_download_url(
path, request_ip, expires_in, requires_cors, head
)
s3_url_parsed = urllib.parse.urlparse(s3_presigned_url)

# replace s3 location with Akamai domain
akamai_url_parsed = s3_url_parsed._replace(netloc=self.akamai_domain)

# add akamai signed token
try:

# add region to the query string
akamai_url_parsed = akamai_url_parsed._replace(
query=f"{akamai_url_parsed.query}&region={self.region}"
)

# add additional params to the query string for metrics
additional_params = ["namespace", "username", "repo_name"]
for param in additional_params:
if param in kwargs and kwargs[param] is not None:
akamai_url_parsed = akamai_url_parsed._replace(
query=f"{akamai_url_parsed.query}&{param}={kwargs[param]}"
)

to_sign = f"{akamai_url_parsed.path}"
akamai_url_parsed = akamai_url_parsed._replace(
query=f"{akamai_url_parsed.query}&{TOKEN_QUERY_STRING}={self.et.generate_url_token(to_sign)}"
)


except EdgeAuthError as e:
logger.error(f"Failed to generate Akamai token: {e}")
return s3_presigned_url

logger.debug(
'Returning Akamai URL for path "%s" with IP "%s": %s',
path,
request_ip,
akamai_url_parsed,
)
return akamai_url_parsed.geturl()
2 changes: 2 additions & 0 deletions storage/multicdnstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from flask import has_request_context, request

from storage import AkamaiS3Storage
from storage.basestorage import BaseStorageV2, InvalidStorageConfigurationException
from storage.cloud import CloudFrontedS3Storage
from storage.cloudflarestorage import CloudFlareS3Storage
Expand All @@ -16,6 +17,7 @@
MULTICDN_STORAGE_PROVIDER_CLASSES = {
"CloudFrontedS3Storage": CloudFrontedS3Storage,
"CloudFlareStorage": CloudFlareS3Storage,
"AkamaiS3Storage": AkamaiS3Storage,
}


Expand Down

0 comments on commit ebec4e3

Please sign in to comment.