|
20 | 20 | from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat
|
21 | 21 | from cryptography.x509 import load_pem_x509_certificate
|
22 | 22 | from sigstore_protobuf_specs.dev.sigstore.common.v1 import TimeRange
|
| 23 | +from sigstore_protobuf_specs.dev.sigstore.trustroot.v1 import ( |
| 24 | + Service, |
| 25 | + ServiceConfiguration, |
| 26 | + ServiceSelector, |
| 27 | +) |
23 | 28 |
|
24 | 29 | from sigstore._internal.fulcio.client import FulcioClient
|
25 | 30 | from sigstore._internal.rekor.client import RekorClient
|
|
35 | 40 | from sigstore._utils import load_pem_public_key
|
36 | 41 | from sigstore.errors import Error
|
37 | 42 |
|
| 43 | +# Test data for TestSigningcconfig |
| 44 | +_service_v1_op1 = Service("url1", major_api_version=1, operator="op1") |
| 45 | +_service2_v1_op1 = Service("url2", major_api_version=1, operator="op1") |
| 46 | +_service_v2_op1 = Service("url3", major_api_version=2, operator="op1") |
| 47 | +_service_v1_op2 = Service("url4", major_api_version=1, operator="op2") |
| 48 | +_service_v1_op3 = Service("url5", major_api_version=1, operator="op3") |
| 49 | +_service_v1_op4 = Service( |
| 50 | + "url6", |
| 51 | + major_api_version=1, |
| 52 | + operator="op4", |
| 53 | + valid_for=TimeRange(datetime(3000, 1, 1, tzinfo=timezone.utc)), |
| 54 | +) |
| 55 | + |
38 | 56 |
|
39 | 57 | class TestCertificateAuthority:
|
40 | 58 | def test_good(self, asset):
|
@@ -75,6 +93,102 @@ def test_good(self, asset):
|
75 | 93 | assert isinstance(tsas[0], TimestampAuthorityClient)
|
76 | 94 | assert tsas[0].url == "https://timestamp.example.com/api/v1/timestamp"
|
77 | 95 |
|
| 96 | + @pytest.mark.parametrize( |
| 97 | + "services, versions, config, expected_result", |
| 98 | + [ |
| 99 | + ( |
| 100 | + [_service_v1_op1], |
| 101 | + [1], |
| 102 | + ServiceConfiguration(ServiceSelector.ALL), |
| 103 | + [_service_v1_op1], |
| 104 | + ), |
| 105 | + ( # multiple services, same operator: expect 1 service in result |
| 106 | + [_service_v1_op1, _service2_v1_op1], |
| 107 | + [1], |
| 108 | + ServiceConfiguration(ServiceSelector.ALL), |
| 109 | + [_service2_v1_op1], |
| 110 | + ), |
| 111 | + ( # 2 services, different operator: expect 2 services in result |
| 112 | + [_service_v1_op1, _service_v1_op2], |
| 113 | + [1], |
| 114 | + ServiceConfiguration(ServiceSelector.ALL), |
| 115 | + [_service_v1_op1, _service_v1_op2], |
| 116 | + ), |
| 117 | + ( # 3 services, one is not yet valid: expect 2 services in result |
| 118 | + [_service_v1_op1, _service_v1_op2, _service_v1_op4], |
| 119 | + [1], |
| 120 | + ServiceConfiguration(ServiceSelector.ALL), |
| 121 | + [_service_v1_op1, _service_v1_op2], |
| 122 | + ), |
| 123 | + ( # ANY selector: expect 1 service only in result |
| 124 | + [_service_v1_op1, _service_v1_op2], |
| 125 | + [1], |
| 126 | + ServiceConfiguration(ServiceSelector.ANY), |
| 127 | + [_service_v1_op1], |
| 128 | + ), |
| 129 | + ( # EXACT selector: expect configured number of services in result |
| 130 | + [_service_v1_op1, _service_v1_op2, _service_v1_op3], |
| 131 | + [1], |
| 132 | + ServiceConfiguration(ServiceSelector.EXACT, 2), |
| 133 | + [_service_v1_op1, _service_v1_op2], |
| 134 | + ), |
| 135 | + ( # services with different version: expect highest version |
| 136 | + [_service_v1_op1, _service_v2_op1], |
| 137 | + [1, 2], |
| 138 | + ServiceConfiguration(ServiceSelector.ALL), |
| 139 | + [_service_v2_op1], |
| 140 | + ), |
| 141 | + ( # services with different version: expect the supported version |
| 142 | + [_service_v1_op1, _service_v2_op1], |
| 143 | + [1], |
| 144 | + ServiceConfiguration(ServiceSelector.ALL), |
| 145 | + [_service_v1_op1], |
| 146 | + ), |
| 147 | + ( # No supported versions: expect no results |
| 148 | + [_service_v1_op1, _service_v1_op2], |
| 149 | + [2], |
| 150 | + ServiceConfiguration(ServiceSelector.ALL), |
| 151 | + [], |
| 152 | + ), |
| 153 | + ( # services without ServiceConfiguration: expect all supported |
| 154 | + [_service_v1_op1, _service_v2_op1, _service_v1_op2], |
| 155 | + [1], |
| 156 | + None, |
| 157 | + [_service_v1_op1, _service_v1_op2], |
| 158 | + ), |
| 159 | + ], |
| 160 | + ) |
| 161 | + def test_get_valid_services(self, services, versions, config, expected_result): |
| 162 | + result = SigningConfig._get_valid_services(services, versions, config) |
| 163 | + |
| 164 | + assert len(result) == len(expected_result) |
| 165 | + for s1, s2 in zip(result, expected_result): |
| 166 | + assert s1.url == s2.url |
| 167 | + |
| 168 | + @pytest.mark.parametrize( |
| 169 | + "services, versions, config", |
| 170 | + [ |
| 171 | + ( # ANY selector without services |
| 172 | + [], |
| 173 | + [1], |
| 174 | + ServiceConfiguration(ServiceSelector.ANY), |
| 175 | + ), |
| 176 | + ( # EXACT selector without enough services |
| 177 | + [_service_v1_op1], |
| 178 | + [1], |
| 179 | + ServiceConfiguration(ServiceSelector.EXACT, 2), |
| 180 | + ), |
| 181 | + ( # UNDEFINED selector |
| 182 | + [_service_v1_op1], |
| 183 | + [1], |
| 184 | + ServiceConfiguration(ServiceSelector.UNDEFINED, 1), |
| 185 | + ), |
| 186 | + ], |
| 187 | + ) |
| 188 | + def test_get_valid_services_fail(self, services, versions, config): |
| 189 | + with pytest.raises(ValueError): |
| 190 | + SigningConfig._get_valid_services(services, versions, config) |
| 191 | + |
78 | 192 |
|
79 | 193 | class TestTrustedRoot:
|
80 | 194 | @pytest.mark.parametrize(
|
|
0 commit comments