Skip to content

Commit

Permalink
Configurable lifetime of secure urls
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Sep 16, 2022
1 parent 2dc4273 commit 5d1475f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ will be aborted. You can configure this lifetime, example:

ckanext.cloudstorage.max_multipart_lifetime = 7

One-time URLs generated by CKAN are expired in an hour. This behaviour can be
changed by setting expected lifetime(in seconds) as `ckanext.cloudstorage.secure_ttl` option:

# make one-time links valid only for 1 minute
ckanext.cloudstorage.secure_ttl = 60

# Migrating From FileStorage

If you already have resources that have been uploaded and saved using CKAN's
Expand Down
14 changes: 12 additions & 2 deletions ckanext/cloudstorage/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
AWS_UPLOAD_PART_SIZE = 5 * 1024 * 1024


CONFIG_SECURE_TTL = "ckanext.cloudstorage.secure_ttl"
DEFAULT_SECURE_TTL = 3600


def config_secure_ttl():
return p.toolkit.asint(p.toolkit.config.get(
CONFIG_SECURE_TTL, DEFAULT_SECURE_TTL
))


def _get_underlying_file(wrapper):
if isinstance(wrapper, FlaskFileStorage):
return wrapper.stream
Expand Down Expand Up @@ -409,7 +419,7 @@ def get_url_by_path(self, path, content_type=None):
sas_token=blob_service.generate_blob_shared_access_signature(
container_name=self.container_name,
blob_name=path,
expiry=datetime.utcnow() + timedelta(hours=1),
expiry=datetime.utcnow() + timedelta(seconds=config_secure_ttl()),
permission=azure_blob.BlobPermissions.READ,
),
)
Expand All @@ -429,7 +439,7 @@ def get_url_by_path(self, path, content_type=None):
]

generate_url_params = {
"expires_in": 60 * 60,
"expires_in": config_secure_ttl(),
"method": "GET",
"bucket": self.container_name,
"key": path,
Expand Down

0 comments on commit 5d1475f

Please sign in to comment.