Skip to content

Commit

Permalink
Add payload parsing capability
Browse files Browse the repository at this point in the history
payload is a byte sequence of serialized body, stored in Envelope, that
need to be parsed according to given payload_type in Envelope.

Generic Parser is added, so that a type of Parser can be created
according to the requirements of in-toto/tuf.

In form of an example JSONParser is added which parse serialized JSON
payloads and return them in form of dict. It will be used to test the
Payload parsing capabilities of Generic Parser.

Signed-off-by: Pradyumna Krishna <[email protected]>
  • Loading branch information
PradyumnaKrishna committed Jul 8, 2022
1 parent 4413c01 commit 4d1db17
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
10 changes: 10 additions & 0 deletions securesystemslib/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,13 @@ class StorageError(Error):
"""Indicate an error occured during interaction with an abstracted storage
backend."""
pass


class PayloadParseError(Error):
"""Indicate an error during parsing of payload."""
pass


class UnsupportedPayloadType(Error):
"""If a payload parser does not support the given payload type."""
pass
77 changes: 77 additions & 0 deletions securesystemslib/metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Dead Simple Signing Envelope
"""

import abc
import json
from typing import Any, List

from securesystemslib import exceptions, formats
Expand Down Expand Up @@ -97,3 +99,78 @@ def pae(self) -> bytes:
len(self.payload),
self.payload,
)


class Parser:
"""A Generic Payload Parser.
Attributes:
_supported_payload_types: A list of supported payload types in lowercase.
Methods:
check_type(cls, payload_type: str):
Checks the parser support given payload type.
parse(envelope: Envelope):
Parse the envelope's payload.
"""

__metaclass__ = abc.ABCMeta
_supported_payload_types: List[str] = []

@classmethod
def check_type(cls, payload_type: str) -> None:
"""Checks the parser support given payload type.
Arguments:
payload_type: The payload_type to be checked.
Raises:
UnsupportedPayloadType: if payload_type is not a supported payload type.
"""

if payload_type not in cls._supported_payload_types:
raise exceptions.UnsupportedPayloadType

@classmethod
@abc.abstractmethod
def parse(cls, envelope: Envelope) -> Any:
"""Parse the envelope's payload.
Arguments:
envelope: The DSSE "Envelope" class instance.
Returns:
Returns the serialized body.
"""
raise NotImplementedError # pragma: no cover


class SSlibParser(Parser):
"""An example parser for securesystemslib.
This parser is created to test the capabilities of the Generic Parser class
and provide reference to implement a parser according to the application
and type of payload.
"""

_supported_payload_types: List[str] = ["application/vnd.sslib+json"]

@classmethod
def parse(cls, envelope: Envelope) -> dict:
"""Parse the envelope's payload into dict.
Arguments:
envelope: The DSSE "Envelope" class instance.
Returns:
Returns the parsed body.
"""

cls.check_type(envelope.payload_type)
try:
return json.loads(envelope.payload)
except json.JSONDecodeError as exc:
raise exceptions.PayloadParseError(
"error during payload parsing, please check the payload"
) from exc

0 comments on commit 4d1db17

Please sign in to comment.