Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Sigstore signing and verification of models #276

Merged
merged 5 commits into from
Aug 5, 2024
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
26 changes: 26 additions & 0 deletions model_signing/manifest/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,32 @@ def __str__(self) -> str:
"""
return f"{str(self.path)}:{self.start}:{self.end}"

@classmethod
def from_str(cls, s: str) -> Self:
"""Builds a file shard from the string representation.

It is guaranteed that for a shard `shard` and a (valid) string `s` the
following two round-trip properties hold:

```
str(Shard.from_str(s)) == s
Shard.from_str(str(shard)) == shard
```

Raises:
ValueError: if the string argument does not represent a valid shard
serialization (is not in the format `path:start:end`).
"""
parts = s.split(":")
if len(parts) != 3:
raise ValueError(f"Expected 3 components separated by `:`, got {s}")

path = pathlib.PurePosixPath(parts[0])
start = int(parts[1])
end = int(parts[2])

return cls(path, start, end)


@dataclasses.dataclass
class ShardedFileManifestItem(ManifestItem):
Expand Down
37 changes: 37 additions & 0 deletions model_signing/manifest/manifest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,43 @@ def test_manifest_has_the_correct_resource_descriptors(self):
assert descriptors[1].digest.digest_value == b"hash2"


class TestShard:

def test_round_trip_from_shard(self):
shard = manifest.Shard(pathlib.PurePosixPath("file"), 0, 42)
shard_str = str(shard)
assert manifest.Shard.from_str(shard_str) == shard

def test_round_trip_from_string(self):
shard_str = "file:0:42"
shard = manifest.Shard.from_str(shard_str)
assert str(shard) == shard_str

def test_invalid_shard_str_too_few_components(self):
shard_str = "file"

with pytest.raises(ValueError, match="Expected 3 components"):
manifest.Shard.from_str(shard_str)

def test_invalid_shard_str_too_many_components(self):
shard_str = "file:0:1:2"

with pytest.raises(ValueError, match="Expected 3 components"):
manifest.Shard.from_str(shard_str)

def test_invalid_shard_bad_type_for_start_offset(self):
shard_str = "file:zero:4"

with pytest.raises(ValueError, match="invalid literal for int"):
manifest.Shard.from_str(shard_str)

def test_invalid_shard_bad_type_for_endart_offset(self):
shard_str = "file:0:four"

with pytest.raises(ValueError, match="invalid literal for int"):
manifest.Shard.from_str(shard_str)


class TestShardLevelManifest:

def test_insert_order_does_not_matter(self):
Expand Down
Loading
Loading