5454import dev .sigstore .timestamp .client .TimestampResponse ;
5555import dev .sigstore .timestamp .client .TimestampVerificationException ;
5656import dev .sigstore .timestamp .client .TimestampVerifier ;
57+ import dev .sigstore .trustroot .LegacySigningConfig ;
58+ import dev .sigstore .trustroot .Service ;
5759import dev .sigstore .trustroot .SigstoreConfigurationException ;
5860import dev .sigstore .tuf .SigstoreTufClient ;
5961import java .io .IOException ;
60- import java .net .URI ;
6162import java .nio .charset .StandardCharsets ;
6263import java .nio .file .Path ;
6364import java .security .InvalidAlgorithmParameterException ;
@@ -160,40 +161,39 @@ public static Builder builder() {
160161
161162 public static class Builder {
162163 private TrustedRootProvider trustedRootProvider ;
164+ private SigningConfigProvider signingConfigProvider ;
163165 private OidcClients oidcClients ;
164166 private List <OidcTokenMatcher > oidcIdentities = Collections .emptyList ();
165167 private Signer signer ;
166168 private Duration minSigningCertificateLifetime = DEFAULT_MIN_SIGNING_CERTIFICATE_LIFETIME ;
167- private URI fulcioUri ;
168- private URI rekorUri ;
169- private URI timestampUri ;
170169
171170 @ CanIgnoreReturnValue
172- public Builder fulcioUrl (URI uri ) {
173- this .fulcioUri = uri ;
174- return this ;
175- }
176-
177- @ CanIgnoreReturnValue
178- public Builder rekorUrl (URI uri ) {
179- this .rekorUri = uri ;
171+ public Builder trustedRootProvider (TrustedRootProvider trustedRootProvider ) {
172+ this .trustedRootProvider = trustedRootProvider ;
180173 return this ;
181174 }
182175
183176 @ CanIgnoreReturnValue
184- public Builder timestampUrl ( URI uri ) {
185- this .timestampUri = uri ;
177+ public Builder signingConfigProvider ( SigningConfigProvider signingConfigProvider ) {
178+ this .signingConfigProvider = signingConfigProvider ;
186179 return this ;
187180 }
188181
189- @ CanIgnoreReturnValue
190- public Builder trustedRootProvider (TrustedRootProvider trustedRootProvider ) {
191- this .trustedRootProvider = trustedRootProvider ;
192- return this ;
182+ /**
183+ * Deprecated, use {@link #forceCredentialProviders}. sigstore-gradle requires a one version
184+ * deprecation window, so keep this in here until we've done another release.
185+ */
186+ @ Deprecated (forRemoval = true )
187+ public Builder oidcClients (OidcClients oidcClients ) {
188+ return forceCredentialProviders (oidcClients );
193189 }
194190
191+ /**
192+ * Override the default set of credential providers (ambient + signingConfig). It should be very
193+ * unusual for anyone to override this outside of testing scenarios.
194+ */
195195 @ CanIgnoreReturnValue
196- public Builder oidcClients (OidcClients oidcClients ) {
196+ public Builder forceCredentialProviders (OidcClients oidcClients ) {
197197 this .oidcClients = oidcClients ;
198198 return this ;
199199 }
@@ -244,22 +244,51 @@ public KeylessSigner build()
244244 SigstoreConfigurationException {
245245 Preconditions .checkNotNull (trustedRootProvider );
246246 var trustedRoot = trustedRootProvider .get ();
247- Preconditions .checkNotNull (fulcioUri );
248- Preconditions .checkNotNull (rekorUri );
249- Preconditions .checkNotNull (oidcClients );
247+ Preconditions .checkNotNull (signingConfigProvider );
248+ var signingConfig = signingConfigProvider .get ();
250249 Preconditions .checkNotNull (oidcIdentities );
251250 Preconditions .checkNotNull (signer );
252251 Preconditions .checkNotNull (minSigningCertificateLifetime );
253- var fulcioClient = FulcioClientGrpc .builder ().setUri (fulcioUri ).build ();
252+ var fulcioService = Service .select (signingConfig .getCas (), List .of (1 ));
253+ if (fulcioService .isEmpty ()) {
254+ throw new SigstoreConfigurationException (
255+ "No suitable fulcio target was found in signing config" );
256+ }
257+ var fulcioClient = FulcioClientGrpc .builder ().setService (fulcioService .get ()).build ();
254258 var fulcioVerifier = FulcioVerifier .newFulcioVerifier (trustedRoot );
255- var rekorClient = RekorClientHttp .builder ().setUri (rekorUri ).build ();
259+
260+ var rekorService = Service .select (signingConfig .getTLogs (), List .of (1 ));
261+ if (rekorService .isEmpty ()) {
262+ throw new SigstoreConfigurationException (
263+ "No suitable rekor target was found in signing config" );
264+ }
265+ var rekorClient = RekorClientHttp .builder ().setService (rekorService .get ()).build ();
256266 var rekorVerifier = RekorVerifier .newRekorVerifier (trustedRoot );
267+
257268 TimestampClient timestampClient = null ;
258269 TimestampVerifier timestampVerifier = null ;
259- if (timestampUri != null ) { // Building with staging defaults
260- timestampClient = TimestampClientHttp .builder ().setUri (timestampUri ).build ();
270+ var timestampService = Service .select (signingConfig .getTsas (), List .of (1 ));
271+ if (timestampService .isEmpty ()) {
272+ if (rekorService .get ().getApiVersion () != 1 ) {
273+ // only throw exception for rekor v2+ which will require time
274+ throw new SigstoreConfigurationException (
275+ "No suitable tsa target was found in signing config" );
276+ }
277+ } else {
278+ timestampClient = TimestampClientHttp .builder ().setService (timestampService .get ()).build ();
261279 timestampVerifier = TimestampVerifier .newTimestampVerifier (trustedRoot );
262280 }
281+
282+ // if the client hasn't overridden the oidc provider, determine it from the service config
283+ if (oidcClients == null ) {
284+ var oidcService = Service .select (signingConfig .getOidcProviders (), List .of (1 ));
285+ if (oidcService .isEmpty ()) {
286+ throw new SigstoreConfigurationException (
287+ "No suitable oidc target was found in signing config" );
288+ }
289+ oidcClients = OidcClients .from (oidcService .get ());
290+ }
291+
263292 return new KeylessSigner (
264293 fulcioClient ,
265294 fulcioVerifier ,
@@ -281,9 +310,10 @@ public KeylessSigner build()
281310 public Builder sigstorePublicDefaults () {
282311 var sigstoreTufClientBuilder = SigstoreTufClient .builder ().usePublicGoodInstance ();
283312 trustedRootProvider = TrustedRootProvider .from (sigstoreTufClientBuilder );
284- fulcioUri = FulcioClient .PUBLIC_GOOD_URI ;
285- rekorUri = RekorClient .PUBLIC_GOOD_URI ;
286- oidcClients (OidcClients .PUBLIC_GOOD );
313+ // TODO: signing config is not pushed to prod yet
314+ signingConfigProvider =
315+ SigningConfigProvider .fromOrDefault (
316+ sigstoreTufClientBuilder , LegacySigningConfig .PUBLIC_GOOD );
287317 signer (Signers .newEcdsaSigner ());
288318 minSigningCertificateLifetime (DEFAULT_MIN_SIGNING_CERTIFICATE_LIFETIME );
289319 return this ;
@@ -297,10 +327,7 @@ public Builder sigstorePublicDefaults() {
297327 public Builder sigstoreStagingDefaults () {
298328 var sigstoreTufClientBuilder = SigstoreTufClient .builder ().useStagingInstance ();
299329 trustedRootProvider = TrustedRootProvider .from (sigstoreTufClientBuilder );
300- fulcioUri = FulcioClient .STAGING_URI ;
301- rekorUri = RekorClient .STAGING_URI ;
302- timestampUri = TimestampClient .STAGING_URI ;
303- oidcClients (OidcClients .STAGING );
330+ signingConfigProvider = SigningConfigProvider .from (sigstoreTufClientBuilder );
304331 signer (Signers .newEcdsaSigner ());
305332 minSigningCertificateLifetime (DEFAULT_MIN_SIGNING_CERTIFICATE_LIFETIME );
306333 return this ;
0 commit comments