diff --git a/sigstore/_internal/fulcio/__init__.py b/sigstore/_internal/fulcio/__init__.py index 57162df9..8a6ceec3 100644 --- a/sigstore/_internal/fulcio/__init__.py +++ b/sigstore/_internal/fulcio/__init__.py @@ -17,7 +17,7 @@ """ -from ._client import ( +from .client import ( FulcioCertificateSigningRequest, FulcioCertificateSigningResponse, FulcioClient, diff --git a/sigstore/_internal/fulcio/_client.py b/sigstore/_internal/fulcio/client.py similarity index 100% rename from sigstore/_internal/fulcio/_client.py rename to sigstore/_internal/fulcio/client.py diff --git a/sigstore/_internal/rekor/__init__.py b/sigstore/_internal/rekor/__init__.py index 50ef344e..e04f5732 100644 --- a/sigstore/_internal/rekor/__init__.py +++ b/sigstore/_internal/rekor/__init__.py @@ -16,6 +16,6 @@ APIs for interacting with Rekor. """ -from ._client import RekorClient, RekorEntry, RekorInclusionProof +from .client import RekorClient, RekorEntry, RekorInclusionProof __all__ = ["RekorClient", "RekorEntry", "RekorInclusionProof"] diff --git a/sigstore/_internal/rekor/_client.py b/sigstore/_internal/rekor/client.py similarity index 97% rename from sigstore/_internal/rekor/_client.py rename to sigstore/_internal/rekor/client.py index 55ee48dd..1c7c3563 100644 --- a/sigstore/_internal/rekor/_client.py +++ b/sigstore/_internal/rekor/client.py @@ -66,6 +66,9 @@ class RekorInclusionProof(BaseModel): tree_size: int = Field(..., alias="treeSize") hashes: List[str] = Field(..., alias="hashes") + class Config: + allow_population_by_field_name = True + @validator("log_index") def log_index_positive(cls, v: int) -> int: if v < 0: @@ -82,7 +85,7 @@ def tree_size_positive(cls, v: int) -> int: def log_index_within_tree_size( cls, v: int, values: Dict[str, Any], **kwargs: Any ) -> int: - if v <= values["log_index"]: + if "log_index" in values and v <= values["log_index"]: raise ValueError( "Inclusion proof has log index greater than or equal to tree size: " f"{v} <= {values['log_index']}" diff --git a/test/internal/rekor/__init__.py b/test/internal/rekor/__init__.py new file mode 100644 index 00000000..88cb71fa --- /dev/null +++ b/test/internal/rekor/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2022 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. diff --git a/test/internal/rekor/test_client.py b/test/internal/rekor/test_client.py new file mode 100644 index 00000000..95b13e7e --- /dev/null +++ b/test/internal/rekor/test_client.py @@ -0,0 +1,51 @@ +# Copyright 2022 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. + +import pytest +from pydantic import ValidationError + +from sigstore._internal.rekor import client + + +class TestRekorInclusionProof: + def test_valid(self): + proof = client.RekorInclusionProof( + log_index=1, root_hash="abcd", tree_size=2, hashes=[] + ) + assert proof is not None + + def test_negative_log_index(self): + with pytest.raises( + ValidationError, match="Inclusion proof has invalid log index" + ): + client.RekorInclusionProof( + log_index=-1, root_hash="abcd", tree_size=2, hashes=[] + ) + + def test_negative_tree_size(self): + with pytest.raises( + ValidationError, match="Inclusion proof has invalid tree size" + ): + client.RekorInclusionProof( + log_index=1, root_hash="abcd", tree_size=-1, hashes=[] + ) + + def test_log_index_outside_tree_size(self): + with pytest.raises( + ValidationError, + match="Inclusion proof has log index greater than or equal to tree size", + ): + client.RekorInclusionProof( + log_index=2, root_hash="abcd", tree_size=1, hashes=[] + )