|
| 1 | +# Copyright Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing perepo_managerissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from enum import Enum |
| 16 | +from pathlib import Path |
| 17 | +from typing import IO |
| 18 | + |
| 19 | +import in_toto_attestation.v1.resource_descriptor_pb2 as rdpb |
| 20 | +from in_toto_attestation.v1.statement import Statement |
| 21 | + |
| 22 | +class DigestAlgorithm(Enum): |
| 23 | + SHA256_P1 = 1 |
| 24 | + |
| 25 | + def __str__(self): |
| 26 | + return str(self.name).replace("_", "-").lower() |
| 27 | + @staticmethod |
| 28 | + def from_string(s: str): |
| 29 | + return DigestAlgorithm[s.replace("-", "_")] |
| 30 | + |
| 31 | +class Hashed: |
| 32 | + algorithm: DigestAlgorithm |
| 33 | + digest: bytes |
| 34 | + def __init__(self, algorithm_: DigestAlgorithm, digest_: bytes): |
| 35 | + self.algorithm = algorithm_ |
| 36 | + self.digest = digest_ |
| 37 | + |
| 38 | +class PathMetadata: |
| 39 | + hashed: Hashed |
| 40 | + path: str |
| 41 | + def __init__(self, path_: str, hashed_: Hashed): |
| 42 | + self.path = path_ |
| 43 | + self.hashed = hashed_ |
| 44 | + |
| 45 | +class Manifest: |
| 46 | + paths: PathMetadata |
| 47 | + predicate_type: str |
| 48 | + def __init__(self, paths: [PathMetadata]): |
| 49 | + self.paths = paths |
| 50 | + self.predicate_type = "https://github.com/google/model-transparency/manifest/v1" |
| 51 | + |
| 52 | + def to_intoto_statement(self) -> Statement: |
| 53 | + subjects: [rdpb.ResourceDescriptor] = [] |
| 54 | + for _, p in enumerate(self.paths): |
| 55 | + sub = rdpb.ResourceDescriptor() |
| 56 | + sub.download_location = p.path |
| 57 | + sub.digest[str(p.hashed.algorithm)] = p.hashed.digest.hex() |
| 58 | + subjects += [sub] |
| 59 | + # See example at https://github.com/in-toto/attestation/blob/main/python/tests/test_statement.py. |
| 60 | + return Statement(subjects, self.predicate_type, {}) |
0 commit comments