Skip to content

Commit 158dc34

Browse files
authored
Eliminate deprecation warnings on 3.11 (#341)
* pyproject, sigstore: eliminate deprecation warnings on 3.11 This uses `importlib_resources` as a "polyfill" on older Python versions, allowing us to use the new `resources.files()` APIs. Signed-off-by: William Woodruff <[email protected]> * test_store: refactor in terms of resources helper Signed-off-by: William Woodruff <[email protected]> * workflows/ci: explicitly specify Python version Signed-off-by: William Woodruff <[email protected]> Signed-off-by: William Woodruff <[email protected]>
1 parent e0e2a05 commit 158dc34

File tree

8 files changed

+49
-40
lines changed

8 files changed

+49
-40
lines changed

.github/workflows/ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ jobs:
5858
steps:
5959
- uses: actions/checkout@d171c3b028d844f2bf14e9fdec0c58114451e4bf
6060
- uses: actions/setup-python@2c3dd9e7e29afd70cc0950079bde6c979d1f69f9
61+
with:
62+
python-version: "3.x"
6163
- name: deps
6264
run: make dev SIGSTORE_EXTRA=lint
6365
- name: lint

pyproject.toml

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ classifiers = [
2626
"Topic :: Security :: Cryptography",
2727
]
2828
dependencies = [
29-
"cryptography>=38",
29+
"cryptography >= 38",
30+
"importlib_resources ~= 5.7; python_version < '3.11'",
3031
"pydantic",
31-
"pyjwt>=2.1",
32-
"pyOpenSSL>=22.0.0",
32+
"pyjwt >= 2.1",
33+
"pyOpenSSL >= 22.0.0",
3334
"requests",
3435
"securesystemslib",
3536
]
@@ -66,7 +67,7 @@ lint = [
6667
]
6768
dev = [
6869
"build",
69-
"bump>=1.3.2",
70+
"bump >= 1.3.2",
7071
"pdoc3",
7172
"sigstore[test,lint]",
7273
]

sigstore/_cli.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import logging
1818
import os
1919
import sys
20-
from importlib import resources
2120
from pathlib import Path
2221
from textwrap import dedent
2322
from typing import Optional, TextIO, Union, cast
@@ -45,6 +44,7 @@
4544
from sigstore._utils import (
4645
SplitCertificateChainError,
4746
load_pem_public_key,
47+
read_embedded,
4848
split_certificate_chain,
4949
)
5050
from sigstore._verify import (
@@ -70,7 +70,7 @@ def __init__(self, name: str) -> None:
7070
self._name = name
7171

7272
def read(self) -> bytes:
73-
return resources.read_binary("sigstore._store", self._name)
73+
return read_embedded(self._name)
7474

7575
def __repr__(self) -> str:
7676
return f"{self._name} (embedded)"

sigstore/_internal/ctfe.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@
1818

1919
from __future__ import annotations
2020

21-
from importlib import resources
2221
from typing import List
2322

2423
import cryptography.hazmat.primitives.asymmetric.padding as padding
2524
from cryptography.exceptions import InvalidSignature
2625
from cryptography.hazmat.primitives import hashes
2726
from cryptography.hazmat.primitives.asymmetric import ec, rsa
2827

29-
from sigstore._utils import PublicKey, key_id, load_pem_public_key
28+
from sigstore._utils import (
29+
PublicKey,
30+
key_id,
31+
load_pem_public_key,
32+
read_embedded,
33+
)
3034

3135

3236
class CTKeyringError(Exception):
@@ -89,7 +93,7 @@ def _add_resource(self, name: str) -> None:
8993
Adds a key to the current keyring, as identified by its
9094
resource name under `sigstore._store`.
9195
"""
92-
key_pem = resources.read_binary("sigstore._store", name)
96+
key_pem = read_embedded(name)
9397
self.add(key_pem)
9498

9599
def add(self, key_pem: bytes) -> None:

sigstore/_internal/rekor/client.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import logging
2323
from abc import ABC
2424
from dataclasses import dataclass
25-
from importlib import resources
2625
from typing import Any, Dict, List, Optional
2726
from urllib.parse import urljoin
2827

@@ -34,22 +33,18 @@
3433
from securesystemslib.formats import encode_canonical
3534

3635
from sigstore._internal.ctfe import CTKeyring
37-
from sigstore._utils import base64_encode_pem_cert
36+
from sigstore._utils import base64_encode_pem_cert, read_embedded
3837

3938
logger = logging.getLogger(__name__)
4039

4140
DEFAULT_REKOR_URL = "https://rekor.sigstore.dev"
4241
STAGING_REKOR_URL = "https://rekor.sigstage.dev"
4342

44-
_DEFAULT_REKOR_ROOT_PUBKEY = resources.read_binary("sigstore._store", "rekor.pub")
45-
_STAGING_REKOR_ROOT_PUBKEY = resources.read_binary(
46-
"sigstore._store", "rekor.staging.pub"
47-
)
43+
_DEFAULT_REKOR_ROOT_PUBKEY = read_embedded("rekor.pub")
44+
_STAGING_REKOR_ROOT_PUBKEY = read_embedded("rekor.staging.pub")
4845

49-
_DEFAULT_REKOR_CTFE_PUBKEY = resources.read_binary("sigstore._store", "ctfe.pub")
50-
_STAGING_REKOR_CTFE_PUBKEY = resources.read_binary(
51-
"sigstore._store", "ctfe.staging.pub"
52-
)
46+
_DEFAULT_REKOR_CTFE_PUBKEY = read_embedded("ctfe.pub")
47+
_STAGING_REKOR_CTFE_PUBKEY = read_embedded("ctfe.staging.pub")
5348

5449

5550
class RekorBundle(BaseModel):

sigstore/_utils.py

+14
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@
2020

2121
import base64
2222
import hashlib
23+
import sys
2324
from typing import IO, Union
2425

2526
from cryptography.hazmat.primitives import serialization
2627
from cryptography.hazmat.primitives.asymmetric import ec, rsa
2728
from cryptography.x509 import Certificate
2829

30+
if sys.version_info < (3, 11):
31+
import importlib_resources as resources
32+
else:
33+
from importlib import resources
34+
2935
PublicKey = Union[rsa.RSAPublicKey, ec.EllipticCurvePublicKey]
3036

3137

@@ -137,3 +143,11 @@ def sha256_streaming(io: IO[bytes]) -> bytes:
137143
nbytes = io.readinto(view) # type: ignore
138144

139145
return sha256.digest()
146+
147+
148+
def read_embedded(name: str) -> bytes:
149+
"""
150+
Read a resource embedded in this distribution of sigstore-python,
151+
returning its contents as bytes.
152+
"""
153+
return resources.files("sigstore._store").joinpath(name).read_bytes()

sigstore/_verify/verifier.py

+5-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import datetime
2222
import logging
23-
from importlib import resources
2423
from typing import List, cast
2524

2625
from cryptography.exceptions import InvalidSignature
@@ -46,6 +45,7 @@
4645
)
4746
from sigstore._internal.rekor import RekorClient
4847
from sigstore._internal.set import InvalidSetError, verify_set
48+
from sigstore._utils import read_embedded
4949
from sigstore._verify.models import InvalidRekorEntry as InvalidRekorEntryError
5050
from sigstore._verify.models import RekorEntryMissing as RekorEntryMissingError
5151
from sigstore._verify.models import (
@@ -58,18 +58,11 @@
5858

5959
logger = logging.getLogger(__name__)
6060

61+
_DEFAULT_FULCIO_ROOT_CERT = read_embedded("fulcio.crt.pem")
62+
_DEFAULT_FULCIO_INTERMEDIATE_CERT = read_embedded("fulcio_intermediate.crt.pem")
6163

62-
_DEFAULT_FULCIO_ROOT_CERT = resources.read_binary("sigstore._store", "fulcio.crt.pem")
63-
_DEFAULT_FULCIO_INTERMEDIATE_CERT = resources.read_binary(
64-
"sigstore._store", "fulcio_intermediate.crt.pem"
65-
)
66-
67-
_STAGING_FULCIO_ROOT_CERT = resources.read_binary(
68-
"sigstore._store", "fulcio.crt.staging.pem"
69-
)
70-
_STAGING_FULCIO_INTERMEDIATE_CERT = resources.read_binary(
71-
"sigstore._store", "fulcio_intermediate.crt.staging.pem"
72-
)
64+
_STAGING_FULCIO_ROOT_CERT = read_embedded("fulcio.crt.staging.pem")
65+
_STAGING_FULCIO_INTERMEDIATE_CERT = read_embedded("fulcio_intermediate.crt.staging.pem")
7366

7467

7568
class RekorEntryMissing(VerificationFailure):

test/unit/test_store.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from importlib import resources
15+
from sigstore._utils import read_embedded
1616

1717

1818
def test_store_reads_fulcio_root_cert():
19-
fulcio_crt = resources.read_text("sigstore._store", "fulcio.crt.pem").strip()
20-
lines = fulcio_crt.split("\n")
19+
fulcio_crt = read_embedded("fulcio.crt.pem").strip()
20+
lines = fulcio_crt.split(b"\n")
2121

22-
assert lines[0].startswith("-----BEGIN CERTIFICATE-----")
23-
assert lines[-1].startswith("-----END CERTIFICATE-----")
22+
assert lines[0].startswith(b"-----BEGIN CERTIFICATE-----")
23+
assert lines[-1].startswith(b"-----END CERTIFICATE-----")
2424

2525

2626
def test_store_reads_ctfe_pub():
27-
ctfe_pub = resources.read_text("sigstore._store", "ctfe.pub").strip()
28-
lines = ctfe_pub.split("\n")
27+
ctfe_pub = read_embedded("ctfe.pub").strip()
28+
lines = ctfe_pub.split(b"\n")
2929

30-
assert lines[0].startswith("-----BEGIN PUBLIC KEY-----")
31-
assert lines[-1].startswith("-----END PUBLIC KEY-----")
30+
assert lines[0].startswith(b"-----BEGIN PUBLIC KEY-----")
31+
assert lines[-1].startswith(b"-----END PUBLIC KEY-----")

0 commit comments

Comments
 (0)