-
Notifications
You must be signed in to change notification settings - Fork 33
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
WIP feat: Use manifest #112
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
391dca0
backup
laurentsimon 7b611bd
Missing file
laurentsimon 20c6c94
add comment
laurentsimon 70679fa
Fix
laurentsimon 5af662e
patch for upstream
laurentsimon 621283b
update API
laurentsimon 005c461
temporarily disable printing of identity-provider sigstore-python #970
laurentsimon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Copyright Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing perepo_managerissions and | ||
# limitations under the License. | ||
|
||
from enum import Enum | ||
from typing import List | ||
|
||
from sigstore import dsse | ||
|
||
class DigestAlgorithm(Enum): | ||
SHA256_P1 = 1 | ||
|
||
def __str__(self): | ||
return str(self.name).replace("_", "-").lower() | ||
@staticmethod | ||
def from_string(s: str): | ||
return DigestAlgorithm[s.replace("-", "_")] | ||
|
||
class Hashed: | ||
algorithm: DigestAlgorithm | ||
digest: bytes | ||
def __init__(self, algorithm_: DigestAlgorithm, digest_: bytes): | ||
self.algorithm = algorithm_ | ||
self.digest = digest_ | ||
|
||
class PathMetadata: | ||
hashed: Hashed | ||
path: str | ||
def __init__(self, path_: str, hashed_: Hashed): | ||
self.path = path_ | ||
self.hashed = hashed_ | ||
|
||
class Manifest: | ||
paths: PathMetadata | ||
predicate_type: str | ||
def __init__(self, paths: [PathMetadata]): | ||
self.paths = paths | ||
self.predicate_type = "sigstore.dev/model-transparency/manifest/v1" | ||
|
||
def verify(self, verified_manifest: any) -> None: | ||
# The manifest is the one constructed from disk and is untrusted. | ||
# The statement is from the verified bundle and is trusted. | ||
# Verify the type and version. | ||
predicateType = verified_manifest["predicateType"] | ||
if predicateType != self.predicate_type: | ||
raise ValueError(f"invalid predicate type: {predicateType}") | ||
files = verified_manifest["predicate"]["files"] | ||
if len(self.paths) != len(files): | ||
raise ValueError(f"mismatch number of files: expected {len(files)}, got {len(self.paths)}") | ||
for i in range(len(self.paths)): | ||
actual_path = self.paths[i] | ||
verified_path = files[i] | ||
# Verify the path. | ||
if actual_path.path != verified_path["path"]: | ||
raise ValueError(f"mismatch path name: expected '{verified_path['path']}'. Got '{actual_path.path}'") | ||
# Verify the hash name in verified manifest. | ||
if str(DigestAlgorithm.SHA256_P1) not in verified_path["digest"]: | ||
raise ValueError(f"unrecognized hash algorithm: {set(verified_path['digest'].keys())}") | ||
# Verify the hash name in actual path. | ||
if actual_path.hashed.algorithm != DigestAlgorithm.SHA256_P1: | ||
raise ValueError(f"internal error: algorithm {str(actual_path.hashed.algorithm)}") | ||
# Verify the hash value. | ||
verified_digest = verified_path["digest"][str(actual_path.hashed.algorithm)] | ||
if actual_path.hashed.digest.hex() != verified_digest: | ||
raise ValueError(f"mismatch hash for file '{actual_path.path}': expected '{verified_digest}'. Got '{actual_path.hashed.digest.hex()}'") | ||
|
||
|
||
def to_intoto_statement(self) -> dsse.Statement: | ||
# See example at https://github.com/in-toto/attestation/blob/main/python/tests/test_statement.py. | ||
files: [any] = [] | ||
for _, p in enumerate(self.paths): | ||
f = { | ||
"path": p.path, | ||
"digest": { | ||
str(p.hashed.algorithm): p.hashed.digest.hex(), | ||
}, | ||
} | ||
files += [f] | ||
stmt = ( | ||
dsse._StatementBuilder() | ||
.subjects( | ||
[dsse._Subject(name="-", digest={"sha256": "-"})] | ||
) | ||
.predicate_type(self.predicate_type) | ||
.predicate( | ||
{ | ||
"files": files, | ||
} | ||
) | ||
).build() | ||
return stmt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the subjects be the files?
My mental model had the statement subjects as the artifacts we want to sign so you get
subject[n].name = path
andsubject[n].digest = file_hash
. So we would only set thepredicate_type
field assigstore.dev/model-transparency/manifest/v1
and that describes everything we need to know about the subjects. Furthermore, the predicate could then hold metadata like{"hash_algorithm": "abc"}
.PLMK if there is something I don't get here.
See https://github.com/in-toto/attestation/blob/main/spec/v1/statement.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're correct that's how intoto works, but there is some discussion here about whether we want to use intoto or not #111
For this PR, I could not pack files in the subject due to some current limitation on the hash type accepted by sigstore-python library (they only accept known hashes, but we use a parallel hash). And I could not use a non-intoto payload for this PoC, because sigstore-python does not support it yet (WIP).