Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## ?.?.? - Unreleased

* Removed support for file system credentials caching.
* S3 download URLs are now validated.

## 4.0.0 - 2026-01-22

Expand Down
2 changes: 2 additions & 0 deletions okdata/sdk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"keycloakServerUrl": "https://login-test.oslo.kommune.no/auth",
"pipelineUrl": "https://api.data-dev.oslo.systems/pipeline",
"s3BucketUrl": "https://s3.eu-west-1.amazonaws.com/ok-origo-dataplatform-dev",
"s3DownloadBaseUrl": "https://ok-origo-dataplatform-dev.s3.amazonaws.com",
"tokenService": "https://api.data-dev.oslo.systems/token-service/token",
"uploadUrl": "https://api.data-dev.oslo.systems/data-uploader",
"statusApiUrl": "https://api.data-dev.oslo.systems/status-api/status",
Expand All @@ -33,6 +34,7 @@
"keycloakServerUrl": "https://login.oslo.kommune.no/auth",
"pipelineUrl": "https://api.data.oslo.systems/pipeline",
"s3BucketUrl": "https://s3.eu-west-1.amazonaws.com/ok-origo-dataplatform-prod",
"s3DownloadBaseUrl": "https://ok-origo-dataplatform-prod.s3.amazonaws.com",
"tokenService": "https://api.data.oslo.systems/token-service/token",
"uploadUrl": "https://api.data.oslo.systems/data-uploader",
"statusApiUrl": "https://api.data.oslo.systems/status-api/status",
Expand Down
16 changes: 15 additions & 1 deletion okdata/sdk/data/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
log = logging.getLogger()


class DownloadURLAssertionError(Exception):
def __init__(self, url, expected_prefix):
super().__init__(
f"Aborting download because of an unexpected S3 download URL "
f"(should start with {expected_prefix}): {url}"
)


class Download(SDK):
def __init__(self, config=None, auth=None, env=None):
self.__name__ = "download"
Expand All @@ -27,7 +35,13 @@ def download(self, dataset_id, version, edition, output_path, retries=0):
downloaded_files = []
for file in self.get_files(dataset_id, version, edition, retries=retries):
file_name = file["key"].split("/")[-1]
file_content_response = requests.get(file["url"], stream=True)
file_url = file["url"]
base_url = self.config.get("s3DownloadBaseUrl")

if not file_url.startswith(base_url):
raise DownloadURLAssertionError(file_url, base_url)

file_content_response = requests.get(file_url, stream=True)
file_content_response.raise_for_status()

write_file_content(file_name, output_path, file_content_response.raw.read())
Expand Down
2 changes: 1 addition & 1 deletion okdata/sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
log = logging.getLogger()


class SDK(object):
class SDK:
def __init__(self, config=None, auth=None, env=None):
self.config = config
if self.config is None:
Expand Down
5 changes: 4 additions & 1 deletion tests/data/download_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os

import pytest
import json

from okdata.sdk.data.download import Download

Expand All @@ -17,6 +18,7 @@

def test_download(mock_home_dir, mock_http_calls):
data_downloader = Download()
data_downloader.config.config["s3DownloadBaseUrl"] = download_url
output_path = f"{os.environ['HOME']}/my/path"
result = data_downloader.download(
dataset_id, version, edition, output_path=output_path
Expand All @@ -29,6 +31,7 @@ def test_download(mock_home_dir, mock_http_calls):

def test_download_public(mock_home_dir, mock_http_calls_public):
data_downloader = Download()
data_downloader.config.config["s3DownloadBaseUrl"] = download_url
data_downloader.auth.token_provider = None
output_path = f"{os.environ['HOME']}/my/path"
result = data_downloader.download(
Expand Down
Loading