-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add type annotations; use Value type in RD annotations
Signed-off-by: Marcela Melara <marcela.melara@intel.com>
- Loading branch information
1 parent
0dc6e30
commit 2a8a992
Showing
3 changed files
with
21 additions
and
12 deletions.
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 |
---|---|---|
@@ -1,28 +1,37 @@ | ||
# Wrapper class for in-toto attestation ResourceDescriptor protos. | ||
|
||
import in_toto_attestation.v1.resource_descriptor_pb2 as rdpb | ||
from google.protobuf.struct_pb2 import Value | ||
import google.protobuf.json_format as pb_json | ||
import json | ||
|
||
class ResourceDescriptor: | ||
def __init__(self, name='', uri='', digest={}, content=bytes(), download_location='', media_type='', annotations={}): | ||
def __init__(self, name='', uri='', digest=None, content=bytes(), download_location='', media_type='', annotations=None) -> None: | ||
self.pb = rdpb.ResourceDescriptor() | ||
self.pb.name = name | ||
self.pb.uri = uri | ||
self.pb.digest.update(digest) | ||
if digest: | ||
self.pb.digest.update(digest) | ||
self.pb.content = content | ||
self.pb.downloadLocation = download_location | ||
self.pb.mediaType = media_type | ||
|
||
# HACK | ||
for k,v in annotations.items(): | ||
self.pb.annotations[k].update(v) | ||
if annotations: | ||
# Value type fields are considered Messages in Python. | ||
# this is needed to set Value fields into the annotations map | ||
# without having to know its value type a priori. | ||
for k, v in annotations.items(): | ||
val_pb = pb_json.Parse(json.dumps(v), Value()) | ||
self.pb.annotations[k].CopyFrom(val_pb) | ||
|
||
@staticmethod | ||
def copy_from_pb(proto): | ||
def copy_from_pb(proto: type[rdpb.ResourceDescriptor]) -> 'ResourceDescriptor': | ||
rd = ResourceDescriptor() | ||
rd.pb.CopyFrom(proto) | ||
return rd | ||
|
||
def validate(self): | ||
def validate(self) -> None: | ||
# at least one of name, URI or digest are required | ||
if self.pb.name == '' and self.pb.uri == '' and len(self.pb.digest) == 0: | ||
raise ValueError("At least one of name, URI, or digest need to be set") |
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