-
Notifications
You must be signed in to change notification settings - Fork 58
[DRAFT] Add RekorV2Client #1400
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
Closed
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
376361a
add RekorV2Client and types
ramonpetgrave64 2983b4a
add rekorv2 client + tests
ramonpetgrave64 3f94e7a
add lots of docstrings
ramonpetgrave64 d043256
xfail on local and staging
ramonpetgrave64 1a55117
add cahngelog
ramonpetgrave64 65ecf4e
Merge branch 'main' into rekov2-client
ramonpetgrave64 3730364
remove staging and production methods
ramonpetgrave64 1e78607
add @pytest.mark.ambient_oidc
ramonpetgrave64 53c9113
send the cert, not only the public key
ramonpetgrave64 79f967c
abstract the signer fixture
ramonpetgrave64 832f87d
reorganize fixtures
ramonpetgrave64 e6a6fe3
add tiemout
ramonpetgrave64 c546aea
Merge branch 'main' into rekov2-client
ramonpetgrave64 5baeb8f
no V002 workaround
ramonpetgrave64 4b2a03a
merge updates
ramonpetgrave64 73eaf0e
no V002 workaround
ramonpetgrave64 34043a2
regorganize tests
ramonpetgrave64 a733d5a
use new methods for building requests
ramonpetgrave64 90d8244
changelog
ramonpetgrave64 b49d8a7
cleanup comment
ramonpetgrave64 80422f2
future import
ramonpetgrave64 9b60fb6
Rekor: Tweak the log submitter abstraction
jku d80c25c
tests: Simplify rekorv2 tests
jku File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,176 @@ | ||
# Copyright 2025 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. | ||
|
||
""" | ||
Client implementation for interacting with RekorV2. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import json | ||
import logging | ||
|
||
import rekor_types | ||
import requests | ||
from cryptography.hazmat.primitives import serialization | ||
from cryptography.x509 import Certificate | ||
|
||
from sigstore._internal import USER_AGENT | ||
from sigstore._internal.rekor import EntryRequest, RekorLogSubmitter | ||
from sigstore._internal.rekor.v2_types.dev.sigstore.common import v1 as common_v1 | ||
from sigstore._internal.rekor.v2_types.dev.sigstore.rekor import v2 | ||
from sigstore._internal.rekor.v2_types.io import intoto as v2_intoto | ||
from sigstore.dsse import Envelope | ||
from sigstore.hashes import Hashed | ||
from sigstore.models import LogEntry | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
DEFAULT_REKOR_URL = "https://rekor.sigstore.dev" | ||
STAGING_REKOR_URL = "https://rekor.sigstage.dev" | ||
|
||
# TODO: Link to merged documenation. | ||
# See https://github.com/sigstore/rekor-tiles/pull/255/files#diff-eb568acf84d583e4d3734b07773e96912277776bad39c560392aa33ea2cf2210R196 | ||
CREATE_ENTRIES_TIMEOUT_SECONDS = 20 | ||
|
||
DEFAULT_KEY_DETAILS = common_v1.PublicKeyDetails.PKIX_ECDSA_P384_SHA_256 | ||
|
||
|
||
class RekorV2Client(RekorLogSubmitter): | ||
"""The internal Rekor client for the v2 API""" | ||
|
||
# TODO: implement get_tile, get_entry_bundle, get_checkpoint. | ||
|
||
def __init__(self, base_url: str) -> None: | ||
""" | ||
Create a new `RekorV2Client` from the given URL. | ||
""" | ||
self.url = f"{base_url}/api/v2" | ||
self.session = requests.Session() | ||
self.session.headers.update( | ||
{ | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
"User-Agent": USER_AGENT, | ||
} | ||
) | ||
|
||
def __del__(self) -> None: | ||
""" | ||
Terminates the underlying network session. | ||
""" | ||
self.session.close() | ||
|
||
def create_entry(self, payload: EntryRequest) -> LogEntry: | ||
""" | ||
Submit a new entry for inclusion in the Rekor log. | ||
""" | ||
_logger.debug(f"proposed: {json.dumps(payload)}") | ||
resp = self.session.post( | ||
f"{self.url}/log/entries", | ||
json=payload, | ||
timeout=CREATE_ENTRIES_TIMEOUT_SECONDS, | ||
) | ||
|
||
try: | ||
resp.raise_for_status() | ||
except requests.HTTPError as http_error: | ||
raise RekorClientError(http_error) | ||
|
||
integrated_entry = resp.json() | ||
_logger.debug(f"integrated: {integrated_entry}") | ||
return LogEntry._from_dict_rekor(integrated_entry) | ||
|
||
@classmethod | ||
def _build_hashed_rekord_request( | ||
cls, | ||
hashed_input: Hashed, | ||
signature: bytes, | ||
certificate: Certificate, | ||
) -> EntryRequest: | ||
""" | ||
Construct a hashed rekord request to submit to Rekor. | ||
""" | ||
req = v2.CreateEntryRequest( | ||
hashed_rekord_request_v0_0_2=v2.HashedRekordRequestV002( | ||
digest=hashed_input.digest, | ||
signature=v2.Signature( | ||
content=signature, | ||
verifier=v2.Verifier( | ||
x509_certificate=common_v1.X509Certificate( | ||
raw_bytes=certificate.public_bytes( | ||
encoding=serialization.Encoding.DER | ||
) | ||
), | ||
key_details=DEFAULT_KEY_DETAILS, # type: ignore[arg-type] | ||
), | ||
), | ||
) | ||
) | ||
return EntryRequest(req.to_dict()) | ||
|
||
@classmethod | ||
def _build_dsse_request( | ||
cls, envelope: Envelope, certificate: Certificate | ||
) -> EntryRequest: | ||
""" | ||
Construct a dsse request to submit to Rekor. | ||
""" | ||
req = v2.CreateEntryRequest( | ||
dsse_request_v0_0_2=v2.DsseRequestV002( | ||
envelope=v2_intoto.Envelope( | ||
payload=envelope._inner.payload, | ||
payload_type=envelope._inner.payload_type, | ||
signatures=[ | ||
v2_intoto.Signature( | ||
keyid=signature.keyid, | ||
sig=signature.sig, | ||
) | ||
for signature in envelope._inner.signatures | ||
], | ||
), | ||
verifiers=[ | ||
v2.Verifier( | ||
x509_certificate=common_v1.X509Certificate( | ||
raw_bytes=certificate.public_bytes( | ||
encoding=serialization.Encoding.DER | ||
) | ||
), | ||
key_details=DEFAULT_KEY_DETAILS, # type: ignore[arg-type] | ||
) | ||
], | ||
) | ||
) | ||
return EntryRequest(req.to_dict()) | ||
|
||
|
||
class RekorClientError(Exception): | ||
""" | ||
A generic error in the Rekor client. | ||
""" | ||
|
||
def __init__(self, http_error: requests.HTTPError): | ||
""" | ||
Create a new `RekorClientError` from the given `requests.HTTPError`. | ||
""" | ||
if http_error.response is not None: | ||
try: | ||
error = rekor_types.Error.model_validate_json(http_error.response.text) | ||
super().__init__(f"{error.code}: {error.message}") | ||
except Exception: | ||
super().__init__( | ||
f"Rekor returned an unknown error with HTTP {http_error.response.status_code}" | ||
) | ||
else: | ||
super().__init__(f"Unexpected Rekor error: {http_error}") |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# V2 Types | ||
|
||
TODO: Eventually move these types to sigstore/protobuf-specs. | ||
|
||
These are types meant to be used with RekorV2. | ||
|
||
Generated from running `make python` in sigstore/rekor-tiles to generate (although not checked into git) and copied into here, **plus** formatting and lint fixes (lots of `noqa` comments). | ||
|
||
Linting is still not expected to pass yet, since `interrogate` docstrings for **all** modules and classes. | ||
|
||
Eventually, we will move these types into sigstore/protobuf-specs. |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Types for RekorV2 | ||
""" |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Types used for RekorV2 | ||
""" |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Types for RekorV2 | ||
""" |
3 changes: 3 additions & 0 deletions
3
sigstore/_internal/rekor/v2_types/dev/sigstore/common/__init__.py
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Common types used by Sigstore services | ||
""" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this does not really do what the rekor docs say... Session timeout is the maximum allowed time between any two bytes the server sends as response. So the effective total timeout could still be effectively infinite.
I don't think the setting here is harmful but it strongly suggests that we're enforcing the rekor documented overall timeout when we're really not... We could either not have a timeout (we don't in other places) or mention in a comment that this is not the same thing as the rekor 20 sec recommendation