From 56fbbcf7cf4f7fb7fc035fba3ce61c79c9c994c3 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Mon, 25 Sep 2017 17:14:28 -0400 Subject: [PATCH] Add request IP to get_direct_download_url method --- data/userfiles.py | 3 ++- endpoints/appr/cnr_backend.py | 3 ++- endpoints/v1/registry.py | 2 +- endpoints/v2/blob.py | 2 +- storage/cloud.py | 6 +++--- storage/distributedstorage.py | 4 ++-- storage/fakestorage.py | 2 +- storage/swift.py | 2 +- 8 files changed, 13 insertions(+), 11 deletions(-) diff --git a/data/userfiles.py b/data/userfiles.py index b45bbdf99..2a80bb8e5 100644 --- a/data/userfiles.py +++ b/data/userfiles.py @@ -107,7 +107,8 @@ class DelegateUserfiles(object): def get_file_url(self, file_id, expires_in=300, requires_cors=False): path = self.get_file_id_path(file_id) - url = self._storage.get_direct_download_url(self._locations, path, expires_in, requires_cors) + url = self._storage.get_direct_download_url(self._locations, path, request.remote_addr, expires_in, + requires_cors) if url is None: if self._handler_name is None: diff --git a/endpoints/appr/cnr_backend.py b/endpoints/appr/cnr_backend.py index 86a5195d8..3ba4820ee 100644 --- a/endpoints/appr/cnr_backend.py +++ b/endpoints/appr/cnr_backend.py @@ -6,6 +6,7 @@ from cnr.models.channel_base import ChannelBase from cnr.models.db_base import CnrDB from cnr.models.package_base import PackageBase, manifest_media_type +from flask import request from app import storage from endpoints.appr.models_oci import model @@ -36,7 +37,7 @@ class Blob(BlobBase): locations = model.get_blob_locations(digest) if not locations: raise_package_not_found(package_name, digest) - return storage.get_direct_download_url(locations, blobpath) + return storage.get_direct_download_url(locations, blobpath, request.remote_addr) class Channel(ChannelBase): diff --git a/endpoints/v1/registry.py b/endpoints/v1/registry.py index bbe28fe77..366bf1afa 100644 --- a/endpoints/v1/registry.py +++ b/endpoints/v1/registry.py @@ -132,7 +132,7 @@ def get_image_layer(namespace, repository, image_id, headers): abort(404, 'Image %(image_id)s not found', issue='unknown-image', image_id=image_id) try: logger.debug('Looking up the direct download URL for path: %s', path) - direct_download_url = store.get_direct_download_url(locations, path) + direct_download_url = store.get_direct_download_url(locations, path, request.remote_addr) if direct_download_url: logger.debug('Returning direct download URL') resp = redirect(direct_download_url) diff --git a/endpoints/v2/blob.py b/endpoints/v2/blob.py index c7bf52c34..7614e02ad 100644 --- a/endpoints/v2/blob.py +++ b/endpoints/v2/blob.py @@ -83,7 +83,7 @@ def download_blob(namespace_name, repo_name, digest): # Short-circuit by redirecting if the storage supports it. logger.debug('Looking up the direct download URL for path: %s', path) - direct_download_url = storage.get_direct_download_url(blob.locations, path) + direct_download_url = storage.get_direct_download_url(blob.locations, path, request.remote_addr) if direct_download_url: logger.debug('Returning direct download URL') resp = redirect(direct_download_url) diff --git a/storage/cloud.py b/storage/cloud.py index e05943c95..b25ee35d3 100644 --- a/storage/cloud.py +++ b/storage/cloud.py @@ -119,7 +119,7 @@ class _CloudStorage(BaseStorageV2): def get_supports_resumable_downloads(self): return True - def get_direct_download_url(self, path, expires_in=60, requires_cors=False, head=False): + def get_direct_download_url(self, path, request_ip=None, expires_in=60, requires_cors=False, head=False): self._initialize_cloud_conn() path = self._init_path(path) k = self._key_class(self._cloud_bucket, path) @@ -568,11 +568,11 @@ class RadosGWStorage(_CloudStorage): storage_path, bucket_name, access_key, secret_key) # TODO remove when radosgw supports cors: http://tracker.ceph.com/issues/8718#change-38624 - def get_direct_download_url(self, path, expires_in=60, requires_cors=False, head=False): + def get_direct_download_url(self, path, request_ip=None, expires_in=60, requires_cors=False, head=False): if requires_cors: return None - return super(RadosGWStorage, self).get_direct_download_url(path, expires_in, requires_cors, + return super(RadosGWStorage, self).get_direct_download_url(path, request_ip, expires_in, requires_cors, head) # TODO remove when radosgw supports cors: http://tracker.ceph.com/issues/8718#change-38624 diff --git a/storage/distributedstorage.py b/storage/distributedstorage.py index 070cac05b..83bba9cd3 100644 --- a/storage/distributedstorage.py +++ b/storage/distributedstorage.py @@ -56,9 +56,9 @@ class DistributedStorage(StoragePaths): cancel_chunked_upload = _location_aware(BaseStorageV2.cancel_chunked_upload) - def get_direct_download_url(self, locations, path, expires_in=600, requires_cors=False, + def get_direct_download_url(self, locations, path, request_ip=None, expires_in=600, requires_cors=False, head=False): - download_url = self._get_direct_download_url(locations, path, expires_in, requires_cors, head) + download_url = self._get_direct_download_url(locations, path, request_ip, expires_in, requires_cors, head) if download_url is None: return None diff --git a/storage/fakestorage.py b/storage/fakestorage.py index 649d4ccab..2127ccb7f 100644 --- a/storage/fakestorage.py +++ b/storage/fakestorage.py @@ -15,7 +15,7 @@ class FakeStorage(BaseStorageV2): def _init_path(self, path=None, create=False): return path - def get_direct_download_url(self, path, expires_in=60, requires_cors=False, head=False): + def get_direct_download_url(self, path, request_ip=None, expires_in=60, requires_cors=False, head=False): try: if self.get_content('supports_direct_download') == 'true': return 'http://somefakeurl?goes=here' diff --git a/storage/swift.py b/storage/swift.py index 84a359c0d..bde838cf6 100644 --- a/storage/swift.py +++ b/storage/swift.py @@ -147,7 +147,7 @@ class SwiftStorage(BaseStorage): logger.exception('Could not head object at path %s: %s', path, ex) return None - def get_direct_download_url(self, object_path, expires_in=60, requires_cors=False, head=False): + def get_direct_download_url(self, object_path, request_ip=None, expires_in=60, requires_cors=False, head=False): if requires_cors: return None