Skip to content

Commit 9c63913

Browse files
authored
Merge pull request #1 from javanlacerda/javan.refactor.ct-key-management
move ct keyring responsibilities to trustedroot
2 parents 2922262 + 8134ad1 commit 9c63913

File tree

7 files changed

+29
-57
lines changed

7 files changed

+29
-57
lines changed

sigstore/_cli.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from sigstore_protobuf_specs.dev.sigstore.bundle.v1 import Bundle
2828

2929
from sigstore import __version__
30-
from sigstore._internal.ctfe import CTKeyring
3130
from sigstore._internal.fulcio.client import (
3231
DEFAULT_FULCIO_URL,
3332
ExpiredCertificate,
@@ -37,7 +36,7 @@
3736
DEFAULT_REKOR_URL,
3837
RekorClient,
3938
)
40-
from sigstore._internal.trustroot import Keyring, KeyringPurpose, TrustedRoot
39+
from sigstore._internal.trustroot import KeyringPurpose, TrustedRoot
4140
from sigstore._utils import PEMCert, cert_der_to_pem, sha256_digest
4241
from sigstore.errors import Error
4342
from sigstore.oidc import (
@@ -649,15 +648,10 @@ def _sign(args: argparse.Namespace) -> None:
649648
else:
650649
# Assume "production" trust root if no keys are given as arguments
651650
trusted_root = TrustedRoot.production(args=args, purpose=KeyringPurpose.SIGN)
652-
if args.ctfe_pem is not None:
653-
ctfe_keys = [args.ctfe_pem.read()]
654-
else:
655-
ctfe_keys = trusted_root.get_ctfe_keys()
656651

652+
ct_keyring = trusted_root.ct_keyring()
657653
rekor_keyring = trusted_root.rekor_keyring()
658654

659-
ct_keyring = CTKeyring(Keyring(ctfe_keys))
660-
661655
signing_ctx = SigningContext(
662656
fulcio=FulcioClient(args.fulcio_url),
663657
rekor=RekorClient(args.rekor_url, rekor_keyring, ct_keyring),
@@ -819,13 +813,12 @@ def _collect_verification_state(
819813
_die(args, "Custom Rekor URL used without specifying --certificate-chain")
820814

821815
trusted_root = TrustedRoot.production(args=args, purpose=KeyringPurpose.VERIFY)
822-
ct_keys = trusted_root.get_ctfe_keys()
823816

824817
verifier = Verifier(
825818
rekor=RekorClient(
826819
url=args.rekor_url,
827820
rekor_keyring=trusted_root.rekor_keyring(),
828-
ct_keyring=CTKeyring(Keyring(ct_keys)),
821+
ct_keyring=trusted_root.ct_keyring(),
829822
),
830823
trusted_root=trusted_root,
831824
)

sigstore/_internal/ctfe.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

sigstore/_internal/rekor/client.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
import rekor_types
2929
import requests
3030

31-
from sigstore._internal.ctfe import CTKeyring
32-
from sigstore._internal.trustroot import Keyring, RekorKeyring, TrustedRoot
31+
from sigstore._internal.trustroot import CTKeyring, RekorKeyring, TrustedRoot
3332
from sigstore.transparency import LogEntry
3433

3534
logger = logging.getLogger(__name__)
@@ -251,13 +250,10 @@ def production(cls, trust_root: TrustedRoot) -> RekorClient:
251250
252251
trust_root must be a `TrustedRoot` for the production TUF repository.
253252
"""
254-
rekor_keyring = trust_root.rekor_keyring()
255-
ctfe_keys = trust_root.get_ctfe_keys()
256-
257253
return cls(
258254
DEFAULT_REKOR_URL,
259-
rekor_keyring,
260-
CTKeyring(Keyring(ctfe_keys)),
255+
rekor_keyring=trust_root.rekor_keyring(),
256+
ct_keyring=trust_root.ct_keyring(),
261257
)
262258

263259
@classmethod
@@ -268,12 +264,12 @@ def staging(cls, trust_root: TrustedRoot) -> RekorClient:
268264
trust_root must be a `TrustedRoot` for the staging TUF repository.
269265
"""
270266
rekor_keyring = trust_root.rekor_keyring()
271-
ctfe_keys = trust_root.get_ctfe_keys()
267+
ctfe_keys = trust_root.ct_keyring()
272268

273269
return cls(
274270
STAGING_REKOR_URL,
275271
rekor_keyring,
276-
CTKeyring(Keyring(ctfe_keys)),
272+
ctfe_keys,
277273
)
278274

279275
@property

sigstore/_internal/sct.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
)
3737
from cryptography.x509.oid import ExtendedKeyUsageOID
3838

39-
from sigstore._internal.ctfe import CTKeyring
4039
from sigstore._internal.trustroot import (
40+
CTKeyring,
4141
KeyringError,
4242
KeyringLookupError,
4343
KeyringSignatureError,

sigstore/_internal/trustroot.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ def verify(self, *, key_id: KeyID, signature: bytes, data: bytes) -> None:
173173

174174

175175
RekorKeyring = NewType("RekorKeyring", Keyring)
176+
CTKeyring = NewType("CTKeyring", Keyring)
176177

177178

178179
class KeyringPurpose(str, Enum):
@@ -292,14 +293,20 @@ def rekor_keyring(self) -> RekorKeyring:
292293

293294
return RekorKeyring(self._get_rekor_keys())
294295

295-
def get_ctfe_keys(self) -> list[bytes]:
296+
def ct_keyring(self) -> CTKeyring:
297+
"""Return public key contents given certificate authorities."""
298+
299+
return CTKeyring(self._get_ctfe_keys())
300+
301+
def _get_ctfe_keys(self) -> Keyring:
296302
"""Return the CTFE public keys contents."""
297-
# TODO: get purpose as argument
298-
purpose = KeyringPurpose.VERIFY
299-
ctfes: list[bytes] = list(self._get_tlog_keys(self.ctlogs, purpose))
303+
if self.args and self.args.ctfe_pem:
304+
ctfes = [self.args.ctfe_pem.read()]
305+
else:
306+
ctfes = list(self._get_tlog_keys(self.ctlogs, self.purpose))
300307
if not ctfes:
301308
raise MetadataError("CTFE keys not found in trusted root")
302-
return ctfes
309+
return Keyring(ctfes)
303310

304311
def _get_rekor_keys(self) -> Keyring:
305312
"""Return the rekor public key content."""

test/unit/internal/test_ctfe.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
import pretend
1616
import pytest
1717

18-
from sigstore._internal.ctfe import CTKeyring
19-
from sigstore._internal.trustroot import Keyring, KeyringLookupError
18+
from sigstore._internal.trustroot import CTKeyring, Keyring, KeyringLookupError
2019

2120

2221
class TestCTKeyring:

test/unit/internal/test_trust_root.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_trust_root_tuf_caches_and_requests(mock_staging_tuf, tuf_dirs):
5353
assert reqs == expected_requests
5454
assert fail_reqs == expected_fail_reqs
5555

56-
trust_root.get_ctfe_keys()
56+
trust_root.ct_keyring()
5757
trust_root.rekor_keyring()
5858

5959
# no new requests
@@ -69,7 +69,7 @@ def test_trust_root_tuf_caches_and_requests(mock_staging_tuf, tuf_dirs):
6969
assert reqs == expected_requests
7070
assert fail_reqs == expected_fail_reqs
7171

72-
trust_root.get_ctfe_keys()
72+
trust_root.ct_keyring()
7373
trust_root.rekor_keyring()
7474
# Expect no requests
7575
assert reqs == expected_requests
@@ -91,7 +91,7 @@ def test_trust_root_tuf_offline(mock_staging_tuf, tuf_dirs):
9191
assert reqs == {}
9292
assert fail_reqs == {}
9393

94-
trust_root.get_ctfe_keys()
94+
trust_root.ct_keyring()
9595
trust_root.rekor_keyring()
9696

9797
# Still no requests
@@ -159,20 +159,20 @@ def _pem_keys(keys):
159159

160160
# Assert that trust root from TUF contains the expected keys/certs
161161
trust_root = TrustedRoot.staging(purpose=KeyringPurpose.VERIFY)
162-
assert ctfe_keys[0] in _der_keys(trust_root.get_ctfe_keys())
162+
assert ctfe_keys[0] in get_public_bytes(trust_root.ct_keyring()._keyring.values())
163163
assert get_public_bytes(trust_root.rekor_keyring()._keyring.values()) == rekor_keys
164164
assert trust_root.get_fulcio_certs() == fulcio_certs
165165

166166
# Assert that trust root from offline TUF contains the expected keys/certs
167167
trust_root = TrustedRoot.staging(offline=True, purpose=KeyringPurpose.VERIFY)
168-
assert ctfe_keys[0] in _der_keys(trust_root.get_ctfe_keys())
168+
assert ctfe_keys[0] in get_public_bytes(trust_root.ct_keyring()._keyring.values())
169169
assert get_public_bytes(trust_root.rekor_keyring()._keyring.values()) == rekor_keys
170170
assert trust_root.get_fulcio_certs() == fulcio_certs
171171

172172
# Assert that trust root from file contains the expected keys/certs
173173
path = tuf_asset.target_path("trusted_root.json")
174174
trust_root = TrustedRoot.from_file(path)
175-
assert ctfe_keys[0] in _der_keys(trust_root.get_ctfe_keys())
175+
assert ctfe_keys[0] in get_public_bytes(trust_root.ct_keyring()._keyring.values())
176176
assert get_public_bytes(trust_root.rekor_keyring()._keyring.values()) == rekor_keys
177177
assert trust_root.get_fulcio_certs() == fulcio_certs
178178

@@ -186,7 +186,7 @@ def test_trust_root_tuf_ctfe_keys_error(monkeypatch):
186186
trust_root = TrustedRoot.staging(offline=True)
187187
monkeypatch.setattr(trust_root, "ctlogs", [])
188188
with pytest.raises(Exception, match="CTFE keys not found in trusted root"):
189-
trust_root.get_ctfe_keys()
189+
trust_root.ct_keyring()
190190

191191

192192
def test_trust_root_fulcio_certs_error(tuf_asset, monkeypatch):

0 commit comments

Comments
 (0)