Skip to content

Commit 4014282

Browse files
committed
removing keys from rekor client
Signed-off-by: Javan lacerda <javanlacerda@google.com>
1 parent ee94ba2 commit 4014282

File tree

8 files changed

+36
-47
lines changed

8 files changed

+36
-47
lines changed

sigstore/_cli.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,9 @@ def _sign(args: argparse.Namespace) -> None:
649649
# Assume "production" trust root if no keys are given as arguments
650650
trusted_root = TrustedRoot.production(args=args, purpose=KeyringPurpose.SIGN)
651651

652-
ct_keyring = trusted_root.ct_keyring()
653-
rekor_keyring = trusted_root.rekor_keyring()
654-
655652
signing_ctx = SigningContext(
656653
fulcio=FulcioClient(args.fulcio_url),
657-
rekor=RekorClient(args.rekor_url, rekor_keyring, ct_keyring),
654+
rekor=RekorClient(args.rekor_url),
658655
trusted_root=trusted_root,
659656
)
660657

@@ -817,8 +814,6 @@ def _collect_verification_state(
817814
verifier = Verifier(
818815
rekor=RekorClient(
819816
url=args.rekor_url,
820-
rekor_keyring=trusted_root.rekor_keyring(),
821-
ct_keyring=trusted_root.ct_keyring(),
822817
),
823818
trusted_root=trusted_root,
824819
)

sigstore/_internal/rekor/checkpoint.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626

2727
from pydantic import BaseModel, Field, StrictStr
2828

29-
from sigstore._internal.rekor.client import RekorClient
30-
from sigstore._internal.trustroot import KeyringSignatureError
29+
from sigstore._internal.trustroot import KeyringSignatureError, RekorKeyring
3130
from sigstore._utils import KeyID
3231
from sigstore.transparency import LogEntry
3332

@@ -163,7 +162,7 @@ def from_text(cls, text: str) -> SignedNote:
163162

164163
return cls(note=header, signatures=signatures)
165164

166-
def verify(self, client: RekorClient, key_id: KeyID) -> None:
165+
def verify(self, rekor_keyring: RekorKeyring, key_id: KeyID) -> None:
167166
"""
168167
Verify the `SignedNote` with using the given RekorClient by verifying each contained signature.
169168
"""
@@ -175,7 +174,7 @@ def verify(self, client: RekorClient, key_id: KeyID) -> None:
175174
raise CheckpointError("sig_hash hint does not match expected key_id")
176175

177176
try:
178-
client._rekor_keyring.verify(
177+
rekor_keyring.verify(
179178
key_id=key_id, signature=base64.b64decode(sig.signature), data=note
180179
)
181180
except KeyringSignatureError as sig_err:
@@ -202,7 +201,7 @@ def from_text(cls, text: str) -> SignedCheckpoint:
202201
return cls(signed_note=signed_note, checkpoint=checkpoint)
203202

204203

205-
def verify_checkpoint(client: RekorClient, entry: LogEntry) -> None:
204+
def verify_checkpoint(rekor_keyring: RekorKeyring, entry: LogEntry) -> None:
206205
"""
207206
Verify the inclusion proof's checkpoint.
208207
"""
@@ -215,7 +214,9 @@ def verify_checkpoint(client: RekorClient, entry: LogEntry) -> None:
215214
# 1) verify the signature on the checkpoint
216215
# 2) verify the root hash in the checkpoint matches the root hash from the inclusion proof.
217216
signed_checkpoint = SignedCheckpoint.from_text(inclusion_proof.checkpoint)
218-
signed_checkpoint.signed_note.verify(client, KeyID(bytes.fromhex(entry.log_id)))
217+
signed_checkpoint.signed_note.verify(
218+
rekor_keyring, KeyID(bytes.fromhex(entry.log_id))
219+
)
219220

220221
checkpoint_hash = signed_checkpoint.checkpoint.log_hash
221222
root_hash = inclusion_proof.root_hash

sigstore/_internal/rekor/client.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import rekor_types
2929
import requests
3030

31-
from sigstore._internal.trustroot import CTKeyring, RekorKeyring, TrustedRoot
3231
from sigstore.transparency import LogEntry
3332

3433
logger = logging.getLogger(__name__)
@@ -222,9 +221,7 @@ def post(
222221
class RekorClient:
223222
"""The internal Rekor client"""
224223

225-
def __init__(
226-
self, url: str, rekor_keyring: RekorKeyring, ct_keyring: CTKeyring
227-
) -> None:
224+
def __init__(self, url: str) -> None:
228225
"""
229226
Create a new `RekorClient` from the given URL.
230227
"""
@@ -234,43 +231,31 @@ def __init__(
234231
{"Content-Type": "application/json", "Accept": "application/json"}
235232
)
236233

237-
self._ct_keyring = ct_keyring
238-
self._rekor_keyring = rekor_keyring
239-
240234
def __del__(self) -> None:
241235
"""
242236
Terminates the underlying network session.
243237
"""
244238
self.session.close()
245239

246240
@classmethod
247-
def production(cls, trust_root: TrustedRoot) -> RekorClient:
241+
def production(cls) -> RekorClient:
248242
"""
249243
Returns a `RekorClient` populated with the default Rekor production instance.
250244
251245
trust_root must be a `TrustedRoot` for the production TUF repository.
252246
"""
253247
return cls(
254248
DEFAULT_REKOR_URL,
255-
rekor_keyring=trust_root.rekor_keyring(),
256-
ct_keyring=trust_root.ct_keyring(),
257249
)
258250

259251
@classmethod
260-
def staging(cls, trust_root: TrustedRoot) -> RekorClient:
252+
def staging(cls) -> RekorClient:
261253
"""
262254
Returns a `RekorClient` populated with the default Rekor staging instance.
263255
264256
trust_root must be a `TrustedRoot` for the staging TUF repository.
265257
"""
266-
rekor_keyring = trust_root.rekor_keyring()
267-
ctfe_keys = trust_root.ct_keyring()
268-
269-
return cls(
270-
STAGING_REKOR_URL,
271-
rekor_keyring,
272-
ctfe_keys,
273-
)
258+
return cls(STAGING_REKOR_URL)
274259

275260
@property
276261
def log(self) -> RekorLog:

sigstore/_internal/set.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from cryptography.exceptions import InvalidSignature
2222

23-
from sigstore._internal.rekor import RekorClient
23+
from sigstore._internal.trustroot import RekorKeyring
2424
from sigstore._utils import KeyID
2525
from sigstore.transparency import LogEntry
2626

@@ -33,7 +33,7 @@ class InvalidSETError(Exception):
3333
pass
3434

3535

36-
def verify_set(client: RekorClient, entry: LogEntry) -> None:
36+
def verify_set(rekor_keyring: RekorKeyring, entry: LogEntry) -> None:
3737
"""
3838
Verify the inclusion promise (Signed Entry Timestamp) for a given transparency log
3939
`entry` using the given `client`.
@@ -46,7 +46,7 @@ def verify_set(client: RekorClient, entry: LogEntry) -> None:
4646
signed_entry_ts = base64.b64decode(entry.inclusion_promise)
4747

4848
try:
49-
client._rekor_keyring.verify(
49+
rekor_keyring.verify(
5050
key_id=KeyID(bytes.fromhex(entry.log_id)),
5151
signature=signed_entry_ts,
5252
data=entry.encode_canonical(),

sigstore/sign.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def sign(
203203
cert = certificate_response.cert
204204
chain = certificate_response.chain
205205

206-
verify_sct(sct, cert, chain, self._signing_ctx._rekor._ct_keyring)
206+
verify_sct(sct, cert, chain, self._signing_ctx._trusted_root.ct_keyring())
207207

208208
_logger.debug("Successfully verified SCT...")
209209

@@ -301,7 +301,7 @@ def production(cls) -> SigningContext:
301301
Return a `SigningContext` instance configured against Sigstore's production-level services.
302302
"""
303303
trusted_root = TrustedRoot.production(purpose=KeyringPurpose.SIGN)
304-
rekor = RekorClient.production(trusted_root)
304+
rekor = RekorClient.production()
305305
return cls(
306306
fulcio=FulcioClient.production(), rekor=rekor, trusted_root=trusted_root
307307
)
@@ -312,7 +312,7 @@ def staging(cls) -> SigningContext:
312312
Return a `SignerContext` instance configured against Sigstore's staging-level services.
313313
"""
314314
trusted_root = TrustedRoot.staging(purpose=KeyringPurpose.SIGN)
315-
rekor = RekorClient.staging(trusted_root)
315+
rekor = RekorClient.staging()
316316
return cls(
317317
fulcio=FulcioClient.staging(), rekor=rekor, trusted_root=trusted_root
318318
)

sigstore/verify/verifier.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def __init__(self, *, rekor: RekorClient, trusted_root: TrustedRoot):
124124
X509.from_cryptography(parent_cert)
125125
for parent_cert in trusted_root.get_fulcio_certs()
126126
]
127+
self.trusted_root = trusted_root
127128

128129
@classmethod
129130
def production(cls) -> Verifier:
@@ -132,7 +133,7 @@ def production(cls) -> Verifier:
132133
"""
133134
trusted_root = TrustedRoot.production(purpose=KeyringPurpose.VERIFY)
134135
return cls(
135-
rekor=RekorClient.production(trusted_root),
136+
rekor=RekorClient.production(),
136137
trusted_root=trusted_root,
137138
)
138139

@@ -143,7 +144,7 @@ def staging(cls) -> Verifier:
143144
"""
144145
trusted_root = TrustedRoot.staging(purpose=KeyringPurpose.VERIFY)
145146
return cls(
146-
rekor=RekorClient.staging(trusted_root),
147+
rekor=RekorClient.staging(),
147148
trusted_root=trusted_root,
148149
)
149150

@@ -225,7 +226,7 @@ def verify(
225226
sct,
226227
materials.certificate,
227228
[parent_cert.to_cryptography() for parent_cert in chain],
228-
self._rekor._ct_keyring,
229+
self.trusted_root.ct_keyring(),
229230
)
230231

231232
# 3) Check that the signing certificate contains the proof claim as the subject
@@ -293,7 +294,7 @@ def verify(
293294
)
294295

295296
try:
296-
verify_checkpoint(self._rekor, entry)
297+
verify_checkpoint(self.trusted_root.rekor_keyring(), entry)
297298
except CheckpointError as exc:
298299
return VerificationFailure(reason=f"invalid Rekor root hash: {exc}")
299300

@@ -313,7 +314,7 @@ def verify(
313314
# 7) Verify the Signed Entry Timestamp (SET) supplied by Rekor for this artifact
314315
if entry.inclusion_promise:
315316
try:
316-
verify_set(self._rekor, entry)
317+
verify_set(self.trusted_root.rekor_keyring(), entry)
317318
_logger.debug(
318319
f"successfully verified inclusion promise: index={entry.log_index}"
319320
)

test/unit/test_sign.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,18 @@ def test_sct_verify_keyring_lookup_error(signer_and_ident, monkeypatch):
6969

7070
# a signer whose keyring always fails to lookup a given key.
7171
ctx: SigningContext = ctx()
72-
ctx._rekor._ct_keyring = pretend.stub(verify=pretend.raiser(KeyringLookupError))
72+
mock = pretend.stub(
73+
ct_keyring=lambda: pretend.stub(verify=pretend.raiser(KeyringLookupError))
74+
)
75+
ctx._trusted_root = mock
7376
assert identity is not None
7477

7578
payload = secrets.token_bytes(32)
76-
7779
with pytest.raises(
7880
InvalidSCTError,
7981
) as excinfo:
8082
with ctx.signer(identity) as signer:
83+
print(signer.sign(payload))
8184
signer.sign(payload)
8285

8386
# The exception subclass is the one we expect.
@@ -91,6 +94,10 @@ def test_sct_verify_keyring_error(signer_and_ident, monkeypatch):
9194

9295
# a signer whose keyring throws an internal error.
9396
ctx: SigningContext = ctx()
97+
mock = pretend.stub(
98+
ct_keyring=lambda: pretend.stub(verify=pretend.raiser(KeyringLookupError))
99+
)
100+
ctx._trusted_root = mock
94101
ctx._rekor._ct_keyring = pretend.stub(verify=pretend.raiser(KeyringError))
95102
assert identity is not None
96103

@@ -132,6 +139,8 @@ def test_sign_prehashed(staging):
132139
sign_ctx: SigningContext = sign_ctx()
133140
verifier: Verifier = verifier()
134141

142+
# mock = pretend.stub(ct_keyring=lambda: pretend.stub(verify=pretend.raiser(KeyringLookupError)))
143+
# sign_ctx._trusted_root = mock
135144
input_ = secrets.token_bytes(32)
136145
hashed = Hashed(
137146
digest=hashlib.sha256(input_).digest(), algorithm=HashAlgorithm.SHA2_256

test/unit/verify/test_models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from sigstore_protobuf_specs.dev.sigstore.common.v1 import HashAlgorithm
1818

1919
from sigstore._internal.rekor.client import RekorClient
20-
from sigstore._internal.trustroot import KeyringPurpose, TrustedRoot
2120
from sigstore._utils import _sha256_streaming
2221
from sigstore.hashes import Hashed
2322
from sigstore.verify.models import (
@@ -52,8 +51,7 @@ def test_verification_materials_retrieves_rekor_entry(self, signing_materials):
5251
file, materials = signing_materials("a.txt")
5352
assert materials._rekor_entry is None
5453

55-
trust_root = TrustedRoot.staging(purpose=KeyringPurpose.VERIFY)
56-
client = RekorClient.staging(trust_root)
54+
client = RekorClient.staging()
5755

5856
with file.open(mode="rb", buffering=0) as input_:
5957
digest = _sha256_streaming(input_)

0 commit comments

Comments
 (0)