Skip to content

Serialization and Metadata API changes - #12

Merged
lukpueh merged 5 commits into
in-toto:masterfrom
PradyumnaKrishna:serialization_changes
Sep 6, 2022
Merged

lukpueh merged 5 commits into
in-toto:masterfrom
PradyumnaKrishna:serialization_changes

Conversation

@PradyumnaKrishna

Copy link
Copy Markdown

Description of the changes being introduced by the pull request:

BaseDeserializer requires a class type to deserialize the raw_data
into a class. This creates a problem with Paylaod Deserialization,
where we have to deserialize the same payload multiple time.

BaseDeserializer, JSONDeserializer returns json and that get
converted into the class object after that. Payload Deserialization
will be app specific and will be implemented at project level.

sign is renamed to create_sig and verify is renamed to verify_sigs,
so it does not clashes with other methods of current signature
wrappers.

Please verify and check that the pull request fulfils the following requirements:

  • The code follows the Code Style Guidelines
  • Tests have been added for the bug fix or new feature
  • Docs have been added for the bug fix or new feature

BaseDeserializer requires a class type to deserialize the raw_data
into a class. This creates a problem with Paylaod Deserialization,
where we have to deserialize the same payload multiple time.

BaseDeserializer, JSONDeserializer returns json and that get
converted into the class object after that. Payload Deserialization
will be app specific and will be implemented at project level.

Signed-off-by: Pradyumna Krishna <git@onpy.in>
sign is renamed to create_sig and verify is renamed to verify_sigs,
so it does not clashes with other methods of current signature
wrappers.

This has to be done to avoid any breaking change while integrating
DSSE in in-toto and tuf projects.

Signed-off-by: Pradyumna Krishna <git@onpy.in>
Tests was failing due to changes in serialization and metadata
module. Some methods of DSSE Envelope was renamed which was
failing the test cases.

Signed-off-by: Pradyumna Krishna <git@onpy.in>

@lukpueh lukpueh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

Comment thread securesystemslib/metadata.py Outdated
) -> "Envelope":

json = super().from_bytes(data, deserializer)
return cls.from_dict(json)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are mixing concerns here. Shouldn't we expect the deserializer passed to from_bytes to return an Envelope? IMO it's better and more flexible to move the logic here to a custom:

class EnvelopeJSONDeserializer: # could also inherit from `JsonDeserializer` and call `super()` below
    def deserialize(data):
        return Envelope.from_dict(
            JsonDeserializer().deserialize(data)
        )

Then you can return that from default_deserializer() and it will provide you with the from_bytes() you want without the need to override the one inherited from SerializationMixin

Note that in-toto won't use this EnvelopeJSONDeserializer because there we want something like an AnyMetadataJSONDeserializer, which can return either an Envelope or Metablock as described in ITE-5.

So for the purpose of ITE-5, you don't even have to create a EnvelopeJSONDeserializer. But I think it's okay to provide one in securesystemslib as blueprint for custom deserializers.

What do you think?

Comment thread securesystemslib/serialization.py Outdated
@abc.abstractmethod
def deserialize(self, raw_data: bytes, cls: Any) -> Any:
def deserialize(self, raw_data: bytes) -> Any:
"""Deserialize bytes to a specific object."""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Deserialize bytes to a specific object."""
"""Deserialize bytes."""

Comment thread securesystemslib/serialization.py Outdated

def deserialize(self, raw_data: bytes, cls: "JSONSerializable") -> Any:
"""Deserialize utf-8 encoded JSON bytes into an instance of cls.
def deserialize(self, raw_data: bytes) -> Any:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def deserialize(self, raw_data: bytes) -> Any:
def deserialize(self, raw_data: bytes) -> Dict:

needs to be imported from typing

Comment thread securesystemslib/serialization.py Outdated
cls: A "JSONSerializable" subclass.

Returns:
Object of the provided class type.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Object of the provided class type.
dict

Maybe the Returns section of the docstring can be omitted, given the type hint and oneliner on top.

deserialization.
to dict. It provides `to_dict` method, therefore, JSONSerializer requires
a subclass of this class for serialization.
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be shortened to something like...

    """Objects serialized with ``JSONSerializer`` must inherit from this class and
    implement its ``to_dict`` method.
    """

EnvelopeJSONDeserializer is a subclass of JSONDeserializer which
deserializes raw data into an object of Envelope class. This
deserializer is exclusive for DSSE Envelope.

To avoid circular imports EnvelopeJSONDeserializer is created in
metadata.py.

Signed-off-by: Pradyumna Krishna <git@onpy.in>
Comment thread securesystemslib/serialization.py Outdated
to dict. It provides `to_dict` method, therefore, JSONSerializer requires
a subclass of this class for serialization.
"""Objects serialized with ``JSONSerializer`` must inherit from this class
and implement its ``to_dict`` method..

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit pick:

Suggested change
and implement its ``to_dict`` method..
and implement its ``to_dict`` method.

Signed-off-by: Pradyumna Krishna <git@onpy.in>
@lukpueh
lukpueh merged commit 41e0fb1 into in-toto:master Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants