| 1 |
# Wrapper class for in-toto attestation ResourceDescriptor protos. |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import json |
| 6 |
|
| 7 |
import google.protobuf.json_format as pb_json |
| 8 |
from google.protobuf.struct_pb2 import Value |
| 9 |
|
| 10 |
import in_toto_attestation.v1.resource_descriptor_pb2 as rdpb |
| 11 |
|
| 12 |
|
| 13 |
class ResourceDescriptor: |
| 14 |
def __init__( |
| 15 |
self, |
| 16 |
name: str = "", |
| 17 |
uri: str = "", |
| 18 |
digest: dict | None = None, |
| 19 |
content: bytes = b"", |
| 20 |
download_location: str = "", |
| 21 |
media_type: str = "", |
| 22 |
annotations: dict | None = None, |
| 23 |
) -> None: |
| 24 |
self.pb = rdpb.ResourceDescriptor() # type: ignore[attr-defined] |
| 25 |
self.pb.name = name |
| 26 |
self.pb.uri = uri |
| 27 |
if digest: |
| 28 |
self.pb.digest.update(digest) |
| 29 |
self.pb.content = content |
| 30 |
self.pb.download_location = download_location |
| 31 |
self.pb.media_type = media_type |
| 32 |
if annotations: |
| 33 |
self.pb.annotations.update(annotations) |
| 34 |
|
| 35 |
@staticmethod |
| 36 |
def copy_from_pb(proto: rdpb.ResourceDescriptor) -> ResourceDescriptor: # type: ignore[name-defined] |
| 37 |
rd = ResourceDescriptor() |
| 38 |
rd.pb.CopyFrom(proto) |
| 39 |
return rd |
| 40 |
|
| 41 |
def validate(self) -> None: |
| 42 |
# at least one of name, URI or digest are required |
| 43 |
if self.pb.name == "" and self.pb.uri == "" and len(self.pb.digest) == 0: |
| 44 |
raise ValueError("At least one of name, URI, or digest need to be set") |