Skip to content

Commit 22530d1

Browse files
add test_sign
Signed-off-by: Ramon Petgrave <ramon.petgrave64@gmail.com>
1 parent 57181bc commit 22530d1

File tree

1 file changed

+63
-11
lines changed

1 file changed

+63
-11
lines changed

test/integration/cli/test_sign.py

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,32 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from functools import partial
1415
from pathlib import Path
1516
from typing import Optional
1617

1718
import pytest
1819

20+
from sigstore._internal.trust import ClientTrustConfig
1921
from sigstore.models import Bundle
2022
from sigstore.verify import Verifier
2123
from sigstore.verify.policy import UnsafeNoOp
2224

2325

24-
def get_cli_params(
26+
def _get_cli_params(
2527
artifact_paths: list[Path],
2628
overwrite: bool = False,
2729
no_default_files: bool = False,
2830
output_directory: Optional[Path] = None,
2931
bundle_path: Optional[Path] = None,
3032
signature_path: Optional[Path] = None,
3133
certificate_path: Optional[Path] = None,
34+
trust_config_path: Optional[Path] = None,
3235
) -> list[str]:
33-
cli_params = ["--staging", "sign"]
36+
if trust_config_path is not None:
37+
cli_params = ["--trust-config", str(trust_config_path), "sign"]
38+
else:
39+
cli_params = ["--staging", "sign"]
3440
if output_directory is not None:
3541
cli_params.extend(["--output-directory", str(output_directory)])
3642
if bundle_path is not None:
@@ -49,6 +55,52 @@ def get_cli_params(
4955
return cli_params
5056

5157

58+
@pytest.fixture(params=[
59+
True, False
60+
])
61+
def get_cli_params(request, asset) -> callable:
62+
"""
63+
"""
64+
if request.param:
65+
return _get_cli_params
66+
return partial(_get_cli_params, trust_config_path=asset("tsa/trust_config.rekorv2_alpha.json"))
67+
68+
69+
@pytest.mark.ambient_oidc
70+
def test_sign_success_default_output_bundle_with_trust_config(
71+
capsys, sigstore, asset_integration, asset
72+
):
73+
artifact = asset_integration("a.txt")
74+
expected_output_bundle = artifact.with_name("a.txt.sigstore.json")
75+
76+
trust_config = asset("tsa/trust_config.rekorv2_alpha.json")
77+
78+
assert not expected_output_bundle.exists()
79+
sigstore(
80+
*_get_cli_params(artifact_paths=[artifact], trust_config_path=trust_config))
81+
82+
assert expected_output_bundle.exists()
83+
verifier = Verifier(
84+
trusted_root=ClientTrustConfig.from_json(
85+
trust_config.read_text()).trusted_root
86+
)
87+
with (
88+
open(expected_output_bundle, "r") as bundle_file,
89+
open(artifact, "rb") as input_file,
90+
):
91+
bundle = Bundle.from_json(bundle_file.read())
92+
verifier.verify_artifact(
93+
input_=input_file.read(), bundle=bundle, policy=UnsafeNoOp()
94+
)
95+
96+
expected_output_bundle.unlink()
97+
98+
captures = capsys.readouterr()
99+
assert captures.out.endswith(
100+
f"Sigstore bundle written to {expected_output_bundle}\n"
101+
)
102+
103+
52104
@pytest.mark.staging
53105
@pytest.mark.ambient_oidc
54106
def test_sign_success_default_output_bundle(capsys, sigstore, asset_integration):
@@ -57,7 +109,7 @@ def test_sign_success_default_output_bundle(capsys, sigstore, asset_integration)
57109

58110
assert not expected_output_bundle.exists()
59111
sigstore(
60-
*get_cli_params(
112+
*_get_cli_params(
61113
artifact_paths=[artifact],
62114
)
63115
)
@@ -83,7 +135,7 @@ def test_sign_success_default_output_bundle(capsys, sigstore, asset_integration)
83135

84136
@pytest.mark.staging
85137
@pytest.mark.ambient_oidc
86-
def test_sign_success_custom_outputs(capsys, sigstore, asset_integration, tmp_path):
138+
def test_sign_success_custom_outputs(capsys, sigstore, asset_integration, tmp_path, get_cli_params):
87139
artifact = asset_integration("a.txt")
88140
output_bundle = tmp_path / "bundle.json"
89141
output_cert = tmp_path / "cert.cert"
@@ -110,7 +162,7 @@ def test_sign_success_custom_outputs(capsys, sigstore, asset_integration, tmp_pa
110162

111163
@pytest.mark.staging
112164
@pytest.mark.ambient_oidc
113-
def test_sign_success_custom_output_dir(capsys, sigstore, asset_integration, tmp_path):
165+
def test_sign_success_custom_output_dir(capsys, sigstore, asset_integration, tmp_path, get_cli_params):
114166
artifact = asset_integration("a.txt")
115167
expected_output_bundle = tmp_path / "a.txt.sigstore.json"
116168

@@ -131,7 +183,7 @@ def test_sign_success_custom_output_dir(capsys, sigstore, asset_integration, tmp
131183

132184
@pytest.mark.staging
133185
@pytest.mark.ambient_oidc
134-
def test_sign_success_no_default_files(capsys, sigstore, asset_integration, tmp_path):
186+
def test_sign_success_no_default_files(capsys, sigstore, asset_integration, tmp_path, get_cli_params):
135187
artifact = asset_integration("a.txt")
136188
default_output_bundle = tmp_path / "a.txt.sigstore.json"
137189
output_cert = tmp_path / "cert.cert"
@@ -157,7 +209,7 @@ def test_sign_success_no_default_files(capsys, sigstore, asset_integration, tmp_
157209

158210
@pytest.mark.staging
159211
@pytest.mark.ambient_oidc
160-
def test_sign_overwrite_existing_bundle(capsys, sigstore, asset_integration):
212+
def test_sign_overwrite_existing_bundle(capsys, sigstore, asset_integration, get_cli_params):
161213
artifact = asset_integration("a.txt")
162214
expected_output_bundle = artifact.with_name("a.txt.sigstore.json")
163215

@@ -196,7 +248,7 @@ def test_sign_overwrite_existing_bundle(capsys, sigstore, asset_integration):
196248

197249

198250
def test_sign_fails_with_default_files_and_bundle_options(
199-
capsys, sigstore, asset_integration
251+
capsys, sigstore, asset_integration, get_cli_params
200252
):
201253
artifact = asset_integration("a.txt")
202254
output_bundle = artifact.with_name("a.txt.sigstore.json")
@@ -218,7 +270,7 @@ def test_sign_fails_with_default_files_and_bundle_options(
218270

219271

220272
def test_sign_fails_with_multiple_inputs_and_custom_output(
221-
capsys, sigstore, asset_integration
273+
capsys, sigstore, asset_integration, get_cli_params
222274
):
223275
artifact = asset_integration("a.txt")
224276

@@ -263,7 +315,7 @@ def test_sign_fails_with_multiple_inputs_and_custom_output(
263315

264316

265317
def test_sign_fails_with_output_dir_and_custom_output_files(
266-
capsys, sigstore, asset_integration
318+
capsys, sigstore, asset_integration, get_cli_params
267319
):
268320
artifact = asset_integration("a.txt")
269321

@@ -311,7 +363,7 @@ def test_sign_fails_with_output_dir_and_custom_output_files(
311363

312364

313365
def test_sign_fails_without_both_output_cert_and_signature(
314-
capsys, sigstore, asset_integration
366+
capsys, sigstore, asset_integration, get_cli_params
315367
):
316368
artifact = asset_integration("a.txt")
317369

0 commit comments

Comments
 (0)