Skip to content

dsse: add Envelope._from_json #1039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion sigstore/dsse.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class Envelope:
"""
Represents a DSSE envelope.

This class cannot be constructed directly; you must use `sign`.
This class cannot be constructed directly; you must use `sign` or `from_json`.

See: <https://github.com/secure-systems-lab/dsse/blob/v1.0.0/envelope.md>
"""
Expand All @@ -204,12 +204,26 @@ def __init__(self, inner: _Envelope) -> None:

self._inner = inner

@classmethod
def _from_json(cls, contents: bytes | str) -> Envelope:
"""Return a DSSE envelope from the given JSON representation."""
inner = _Envelope().from_json(contents)
return cls(inner)

def to_json(self) -> str:
"""
Return a JSON string with this DSSE envelope's contents.
"""
return self._inner.to_json()

def __eq__(self, other: object) -> bool:
"""Equality for DSSE envelopes."""

if not isinstance(other, Envelope):
return NotImplemented

return self._inner == other._inner


def _pae(type_: str, body: bytes) -> bytes:
"""
Expand Down
41 changes: 41 additions & 0 deletions test/unit/test_dsse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2022 The Sigstore Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import base64
import json

from sigstore import dsse


class TestEnvelope:
def test_roundtrip(self):
raw = json.dumps(
{
"payload": base64.b64encode(b"foo").decode(),
"payloadType": dsse.Envelope._TYPE,
"signatures": [
{"sig": base64.b64encode(b"lol").decode()},
{"sig": base64.b64encode(b"lmao").decode()},
],
}
)
evp = dsse.Envelope._from_json(raw)

assert evp._inner.payload == b"foo"
assert evp._inner.payload_type == dsse.Envelope._TYPE
assert [b"lol", b"lmao"] == [s.sig for s in evp._inner.signatures]

serialized = evp.to_json()
assert serialized == raw
assert dsse.Envelope._from_json(serialized) == evp
Loading