-
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
Draft
ramonpetgrave64
wants to merge
21
commits into
sigstore:main
Choose a base branch
from
ramonpetgrave64:rekov2-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
21 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
# 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.v2_types.dev.sigstore.common.v1 import PublicKeyDetails | ||
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" | ||
|
||
DEFAULT_KEY_DETAILS = PublicKeyDetails.PKIX_ECDSA_P384_SHA_256 | ||
|
||
|
||
class RekorV2Client: | ||
"""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, request: v2.CreateEntryRequest) -> LogEntry: | ||
""" | ||
Submit a new entry for inclusion in the Rekor log. | ||
""" | ||
# TODO: There may be a bug in betterproto, where the V_0_0_2 is changed to V002, | ||
# Or it is an issue with the proto `json_value`. | ||
# See https://github.com/sigstore/rekor-tiles/blob/bd5893730de581629a5f475923c663f776793496/api/proto/rekor_service.proto#L66. | ||
payload = request.to_dict() | ||
if "hashedRekordRequestV002" in payload: | ||
payload["hashedRekordRequestV0_0_2"] = payload.pop( | ||
"hashedRekordRequestV002" | ||
) | ||
if "dsseRequestV002" in payload: | ||
payload["dsseRequestV0_0_2"] = payload.pop("dsseRequestV002") | ||
_logger.debug(f"request: {json.dumps(payload)}") | ||
resp = self.session.post(f"{self.url}/log/entries", json=payload) | ||
|
||
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_create_entry_request( | ||
cls, | ||
artifact_hashed_input: Hashed, | ||
artifact_signature: bytes, | ||
signining_certificate: Certificate, | ||
) -> v2.CreateEntryRequest: | ||
return v2.CreateEntryRequest( | ||
hashed_rekord_request_v0_0_2=v2.HashedRekordRequestV002( | ||
digest=artifact_hashed_input.digest, | ||
signature=v2.Signature( | ||
content=artifact_signature, | ||
verifier=v2.Verifier( | ||
public_key=v2.PublicKey( | ||
raw_bytes=signining_certificate.public_key().public_bytes( | ||
encoding=serialization.Encoding.DER, | ||
format=serialization.PublicFormat.SubjectPublicKeyInfo, | ||
) | ||
), | ||
key_details=DEFAULT_KEY_DETAILS, # type: ignore[arg-type] | ||
), | ||
), | ||
) | ||
) | ||
|
||
@classmethod | ||
def _build_dsse_create_entry_request( | ||
cls, envelope: Envelope, signing_certificate: Certificate | ||
) -> v2.CreateEntryRequest: | ||
return 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( | ||
public_key=v2.PublicKey( | ||
raw_bytes=signing_certificate.public_key().public_bytes( | ||
encoding=serialization.Encoding.DER, | ||
format=serialization.PublicFormat.SubjectPublicKeyInfo, | ||
) | ||
), | ||
key_details=DEFAULT_KEY_DETAILS, # type: ignore[arg-type] | ||
) | ||
], | ||
) | ||
) | ||
|
||
@classmethod | ||
def production(cls) -> RekorV2Client: | ||
""" | ||
Returns a `RekorV2Client` populated with the default Rekor production instance. | ||
""" | ||
return cls( | ||
DEFAULT_REKOR_URL, | ||
) | ||
|
||
@classmethod | ||
def staging(cls) -> RekorV2Client: | ||
""" | ||
Returns a `RekorV2Client` populated with the default Rekor staging instance. | ||
""" | ||
return cls(STAGING_REKOR_URL) | ||
|
||
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.