Skip to content

Commit

Permalink
Add SigstoreSignature for storing Sigstore signatures
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Maruseac <mihaimaruseac@google.com>
  • Loading branch information
mihaimaruseac committed Aug 5, 2024
1 parent e012e76 commit 149ba68
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions model_signing/signing/in_toto.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _convert_descriptors_to_hashed_statement(
*,
predicate_type: str,
predicate_top_level_name: str,
):
) -> statement.Statement:
"""Converts manifest descriptors to an in-toto statement with payload.
Args:
Expand Down Expand Up @@ -359,7 +359,7 @@ def from_manifest(cls, manifest: manifest_module.Manifest) -> Self:

def _convert_descriptors_to_direct_statement(
manifest: manifest_module.Manifest, predicate_type: str
):
) -> statement.Statement:
"""Converts manifest descriptors to an in-toto statement, as subjects.
Args:
Expand Down
68 changes: 68 additions & 0 deletions model_signing/signing/sigstore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright 2024 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.

"""Sigstore based signature, signers and verifiers."""

import pathlib
from typing import Self

from sigstore import models as sigstore_models
from typing_extensions import override

from model_signing.signing import signing


class SigstoreSignature(signing.Signature):
"""Sigstore signature support, wrapping around `sigstore_models.Bundle`."""

def __init__(self, bundle: sigstore_models.Bundle):
"""Builds an instance of this signature.
Args:
bundle: the Sigstore `Bundle` to wrap around.
"""
self.bundle = bundle

@override
def write(self, path: pathlib.Path) -> None:
"""Writes the signature to disk, to the given path.
The Sigstore `Bundle` is written in JSON format, per the
canonicalization defined by the `sigstore-python` library.
Args:
path: the path to write the signature to.
"""
path.write_text(self.bundle.to_json())

@classmethod
@override
def read(cls, path: pathlib.Path) -> Self:
"""Reads the signature from disk.
Does not perform any signature verification, except what is needed to
parse the signature file.
Args:
path: the path to read the signature from.
Returns:
A `SigstoreSignature` object wrapping a Sigstore `Bundle`.
Raises:
ValueError: If the Sigstore `Bundle` could not be deserialized from
the contents of the file pointed to by `path`.
"""
content = path.read_text()
return cls(sigstore_models.Bundle.from_json(content))

0 comments on commit 149ba68

Please sign in to comment.