Skip to content

Commit e445f04

Browse files
authored
[CDF-27407] Skip library checksum validation for downloads (#2774)
## Description After careful consideration, decided to remove checksum validation. ## Bump - [x] Patch - [ ] Skip ## Changelog ### Changed - External module libraries are configured with a URL only; checksum fields and post-download SHA256 verification are removed.
1 parent 347fafe commit e445f04

File tree

11 files changed

+9
-135
lines changed

11 files changed

+9
-135
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ jobs:
125125
- name: Build the package
126126
run: uv build
127127
- name: Initialize project
128-
run: uv run cdf modules init demo_project --clean --all --library-url "https://github.com/cognitedata/toolkit-data/releases/download/latest/packages.zip" --library-checksum "sha256:b70c207a78d6c94e0977a733571420b250ca641ac8802d8310f1ffe05bf7b2eb"
128+
run: uv run cdf modules init demo_project --clean --all --library-url "https://github.com/cognitedata/toolkit-data/releases/download/latest/packages.zip"
129129
- name: "Pre-processing for demo environment"
130130
run: uv run python ./demo/preproc.py --modules all
131131
- name: "Build the templates"

.github/workflows/demo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: Build the package
3131
run: uv build
3232
- name: Initialize project
33-
run: uv run cdf modules init demo_project --clean --all --library-url "https://github.com/cognitedata/toolkit-data/releases/download/latest/packages.zip" --library-checksum "sha256:b70c207a78d6c94e0977a733571420b250ca641ac8802d8310f1ffe05bf7b2eb"
33+
run: uv run cdf modules init demo_project --clean --all --library-url "https://github.com/cognitedata/toolkit-data/releases/download/latest/packages.zip"
3434
- name: "Pre-processing for demo environment"
3535
run: uv run python ./demo/preproc.py
3636
- name: "Build the templates"

LIBRARIES.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ external-libraries = true
1313

1414
[library.package_1]
1515
url = "https://raw.githubusercontent.com/cognitedata/toolkit-data/librarian/builtins.zip"
16-
checksum = "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
1716

1817
```
1918

@@ -26,9 +25,6 @@ The library must be available over https. Authentication is not currently suppor
2625
To publish a library, create a repository that contains one or more downloadable zip files.
2726
The zip file must have the structure and content described below.
2827

29-
The checksum is mandatory. It is used to verify the integrity of the downloaded zip file.
30-
It must be a SHA-256 checksum of the zip file.
31-
3228
### <package_1>.zip
3329

3430
Zip file content must be structured like this:

cdf.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,4 @@ version = "0.0.0"
3535

3636

3737
[library.toolkit-data]
38-
url = "https://github.com/cognitedata/toolkit-data/releases/download/latest/packages.zip"
39-
checksum = "sha256:b70c207a78d6c94e0977a733571420b250ca641ac8802d8310f1ffe05bf7b2eb"
38+
url = "https://github.com/cognitedata/toolkit-data/releases/download/latest/packages.zip"

cognite_toolkit/_cdf_tk/apps/_modules_app.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def init(
7373
typer.Option(
7474
"--library-checksum",
7575
"-c",
76-
help="Checksum of the library to add to the project.",
76+
help="Library zip checksum (optional; accepted for compatibility, not verified).",
7777
),
7878
] = None,
7979
verbose: Annotated[
@@ -92,12 +92,6 @@ def init(
9292
# This is only used for logging purposes in the command.
9393
client = EnvironmentVariables.create_from_environment().get_client()
9494

95-
if library_url and not library_checksum:
96-
raise typer.BadParameter(
97-
"--library-checksum must be provided when --library-url is specified.",
98-
param_hint="--library-checksum",
99-
)
100-
10195
with ModulesCommand(client=client) as cmd:
10296
cmd.run(
10397
lambda: cmd.init(

cognite_toolkit/_cdf_tk/cdf_toml.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def load(cls, raw: dict[str, Any]) -> Self:
8080
@dataclass
8181
class Library:
8282
url: str
83-
checksum: str
8483

8584
def __post_init__(self) -> None:
8685
self._validate()
@@ -102,10 +101,7 @@ def load(cls, raw: dict[str, Any]) -> Self:
102101
if "url" not in raw:
103102
raise ValueError("Library configuration must contain 'url' field.")
104103

105-
if "checksum" not in raw:
106-
raise ValueError("Library configuration must contain 'checksum' field.")
107-
108-
return cls(**raw)
104+
return cls(url=raw["url"])
109105

110106

111107
@dataclass

cognite_toolkit/_cdf_tk/commands/modules.py

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import tempfile
66
import zipfile
77
from collections import Counter
8-
from hashlib import sha256
98
from pathlib import Path
109
from types import TracebackType
1110
from typing import Any, Literal
@@ -58,7 +57,6 @@
5857
from cognite_toolkit._cdf_tk.exceptions import ToolkitError, ToolkitRequiredValueError, ToolkitValueError
5958
from cognite_toolkit._cdf_tk.hints import verify_module_directory
6059
from cognite_toolkit._cdf_tk.tk_warnings import MediumSeverityWarning
61-
from cognite_toolkit._cdf_tk.tk_warnings.other import HighSeverityWarning
6260
from cognite_toolkit._cdf_tk.utils import humanize_collection, read_yaml_file
6361
from cognite_toolkit._cdf_tk.utils.file import safe_read, safe_rmtree, safe_write, yaml_safe_dump
6462
from cognite_toolkit._cdf_tk.utils.modules import module_directory_from_path
@@ -290,6 +288,8 @@ def init(
290288
library_url: str | None = None,
291289
library_checksum: str | None = None,
292290
) -> None:
291+
_ = library_checksum
292+
293293
if not organization_dir:
294294
organization_dir = ModulesCommand._prompt_organization_dir()
295295

@@ -298,11 +298,7 @@ def init(
298298
# Determine which library to use (if any)
299299
library: Library | None = None
300300
if library_url:
301-
if not library_checksum:
302-
raise ToolkitRequiredValueError(
303-
"The '--library-checksum' is required when '--library-url' is provided."
304-
)
305-
library = Library(url=library_url, checksum=library_checksum)
301+
library = Library(url=library_url)
306302
elif not (organization_dir / CDFToml.file_name).exists():
307303
# Load default library from resources when cdf.toml doesn't exist
308304
default_cdf_toml = CDFToml.load(cwd=RESOURCES_PATH, use_singleton=False)
@@ -887,7 +883,6 @@ def _get_available_packages(self, user_library: Library | None = None) -> tuple[
887883
)
888884
file_path = self._temp_download_dir / filename
889885
self._download(library.url, file_path)
890-
self._validate_checksum(library.checksum, file_path)
891886
self._unpack(file_path)
892887
packages = Packages().load(file_path.parent)
893888
if packages.warnings:
@@ -996,40 +991,6 @@ def _download(self, url: str, file_path: Path) -> None:
996991
except requests.exceptions.RequestException as e:
997992
raise ToolkitError(f"Error downloading file from {url}: {e}") from e
998993

999-
def _validate_checksum(self, checksum: str, file_path: Path) -> None:
1000-
"""
1001-
Compares the checksum of the downloaded file with the expected checksum.
1002-
"""
1003-
1004-
if checksum.lower().startswith("sha256:"):
1005-
checksum = checksum[7:]
1006-
else:
1007-
raise ToolkitValueError(f"Unsupported checksum format: {checksum}. Expected 'sha256:' prefix")
1008-
1009-
chunk_size: int = 8192
1010-
sha256_hash = sha256()
1011-
try:
1012-
with open(file_path, "rb") as f:
1013-
# Read the file in chunks to handle large files efficiently
1014-
for chunk in iter(lambda: f.read(chunk_size), b""):
1015-
sha256_hash.update(chunk)
1016-
calculated = sha256_hash.hexdigest()
1017-
except OSError as e:
1018-
raise ToolkitError(f"Failed to calculate checksum for {file_path}: {e}") from e
1019-
except Exception as e:
1020-
raise ToolkitError(f"Unexpected error during checksum calculation for {file_path}: {e}") from e
1021-
1022-
if calculated != checksum:
1023-
self.warn(
1024-
HighSeverityWarning(
1025-
f"The provided checksum sha256:{checksum} does not match downloaded file hash sha256:{calculated}.\n"
1026-
"Please verify the checksum with the source and update cdf.toml if needed.\n"
1027-
"This may indicate that the package content has changed."
1028-
)
1029-
)
1030-
else:
1031-
print("[green]✓ Checksum verified[/green]")
1032-
1033994
def _unpack(self, file_path: Path) -> None:
1034995
"""
1035996
Unzips the downloaded file to the specified output path.

cognite_toolkit/_resources/cdf.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ functions = false
1515

1616

1717
[library.toolkit-data]
18-
url = "https://github.com/cognitedata/toolkit-data/releases/download/latest/packages.zip"
19-
checksum = "sha256:b70c207a78d6c94e0977a733571420b250ca641ac8802d8310f1ffe05bf7b2eb"
18+
url = "https://github.com/cognitedata/toolkit-data/releases/download/latest/packages.zip"

tests/test_unit/test_cdf_tk/test_commands/test_about.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class TestAboutCommand:
5151
version = "0.0.0"
5252
[library.custom]
5353
url = "https://example.com/packages.zip"
54-
checksum = "abc123"
5554
""",
5655
[],
5756
),
@@ -68,7 +67,6 @@ class TestAboutCommand:
6867
graphql = true
6968
[library.my-lib]
7069
url = "https://example.com/lib.zip"
71-
checksum = "xyz789"
7270
""",
7371
[],
7472
),
@@ -89,10 +87,8 @@ class TestAboutCommand:
8987
version = "0.0.0"
9088
[library.lib1]
9189
url = "https://example.com/lib1.zip"
92-
checksum = "aaa"
9390
[library.lib2]
9491
url = "https://example.com/lib2.zip"
95-
checksum = "bbb"
9692
""",
9793
[],
9894
),

tests/test_unit/test_cdf_tk/test_commands/test_modules.py

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from __future__ import annotations
22

3-
import hashlib
43
import json
5-
import re
64
import zipfile
75
from pathlib import Path
86
from unittest.mock import MagicMock
@@ -17,7 +15,6 @@
1715
from cognite_toolkit._cdf_tk.constants import MODULES
1816
from cognite_toolkit._cdf_tk.data_classes import ModuleLocation, Package, Packages
1917
from cognite_toolkit._cdf_tk.exceptions import ToolkitError
20-
from cognite_toolkit._cdf_tk.tk_warnings.other import HighSeverityWarning
2118
from tests.data import COMPLETE_ORG, EXTERNAL_PACKAGE
2219
from tests.test_unit.utils import MockQuestionary
2320

@@ -307,31 +304,6 @@ def test_unpack_errors_os_error_during_write(self, tmp_path: Path, monkeypatch:
307304
assert isinstance(excinfo.value.__cause__, OSError)
308305
assert "No space left on device" in str(excinfo.value.__cause__)
309306

310-
def test_checksum_format(self, tmp_path: Path) -> None:
311-
invalid_checksum = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
312-
with pytest.raises(ToolkitError) as excinfo:
313-
ModulesCommand(module_source_dir=COMPLETE_ORG_MODULES)._validate_checksum(
314-
invalid_checksum, Path(tmp_path / "test_file.zip")
315-
)
316-
317-
assert "Unsupported checksum format" in str(excinfo.value)
318-
319-
def test_checksum_success(self, tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
320-
file_path = tmp_path / "test_file.zip"
321-
dummy_file_content = b"PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
322-
file_path.write_bytes(dummy_file_content)
323-
324-
expected_checksum = f"sha256:{hashlib.sha256(dummy_file_content).hexdigest()}"
325-
326-
cmd = ModulesCommand(print_warning=True, skip_tracking=True, module_source_dir=COMPLETE_ORG / MODULES)
327-
try:
328-
cmd._validate_checksum(
329-
checksum=expected_checksum,
330-
file_path=file_path, # Pass the correct Path object
331-
)
332-
except ToolkitError as e:
333-
pytest.fail(f"'_validate_checksum' raised an unexpected ToolkitError: {e}")
334-
335307
def test_download_deletes_existing_file(self, tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
336308
"""Test that _download method deletes existing zip files before downloading."""
337309
# Create a stale zip file that should be deleted
@@ -404,25 +376,6 @@ def test_iterate_modules_finds_modules_in_temp_download_dir(self, tmp_path: Path
404376

405377
shutil.rmtree(mock_module_dir)
406378

407-
def test_checksum_mismatch_prints_warning(self, tmp_path: Path, capsys) -> None:
408-
file_path = tmp_path / "mismatch.zip"
409-
# Write some bytes so we get a deterministic SHA256
410-
file_bytes = b"dummy-bytes-for-checksum-test"
411-
file_path.write_bytes(file_bytes)
412-
413-
# Intentionally use a different checksum than the file's actual hash
414-
wrong_checksum = "sha256:" + hashlib.sha256(b"some-other-content").hexdigest()
415-
416-
cmd = ModulesCommand(print_warning=True, skip_tracking=True, module_source_dir=COMPLETE_ORG / MODULES)
417-
cmd._validate_checksum(wrong_checksum, file_path)
418-
419-
assert len(cmd.warning_list) == 1
420-
warning = cmd.warning_list[0]
421-
assert isinstance(warning, HighSeverityWarning)
422-
# Expect: two SHA256 hex hashes in the message, one for provided and one for calculated
423-
pattern = r"^The provided checksum sha256:[0-9a-f]{64} does not match downloaded file hash sha256:[0-9a-f]{64}"
424-
assert re.search(pattern, warning.message_raw)
425-
426379
def test_list_json_output_is_parseable(self, tmp_path: Path, monkeypatch: MonkeyPatch, capsys) -> None:
427380
location = MagicMock()
428381
location.path = Path("modules/my_module")

0 commit comments

Comments
 (0)