Skip to content

Commit a80049c

Browse files
committed
Use SigstoreConfigurationException more widely
Signed-off-by: Appu Goundan <[email protected]>
1 parent 7128496 commit a80049c

File tree

8 files changed

+46
-63
lines changed

8 files changed

+46
-63
lines changed

sigstore-java/src/main/java/dev/sigstore/KeylessSigner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import dev.sigstore.rekor.client.RekorResponse;
4747
import dev.sigstore.rekor.client.RekorVerificationException;
4848
import dev.sigstore.rekor.client.RekorVerifier;
49+
import dev.sigstore.trustroot.SigstoreConfigurationException;
4950
import dev.sigstore.tuf.SigstoreTufClient;
5051
import java.io.IOException;
5152
import java.net.URI;
@@ -218,7 +219,8 @@ public KeylessSigner build()
218219
NoSuchAlgorithmException,
219220
InvalidKeySpecException,
220221
InvalidKeyException,
221-
InvalidAlgorithmParameterException {
222+
InvalidAlgorithmParameterException,
223+
SigstoreConfigurationException {
222224
Preconditions.checkNotNull(trustedRootProvider);
223225
var trustedRoot = trustedRootProvider.get();
224226
Preconditions.checkNotNull(fulcioUri);

sigstore-java/src/main/java/dev/sigstore/KeylessVerifier.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import dev.sigstore.rekor.client.RekorVerifier;
3838
import dev.sigstore.rekor.dsse.v0_0_1.Dsse;
3939
import dev.sigstore.rekor.dsse.v0_0_1.PayloadHash;
40+
import dev.sigstore.trustroot.SigstoreConfigurationException;
4041
import dev.sigstore.tuf.SigstoreTufClient;
4142
import java.io.IOException;
4243
import java.nio.charset.StandardCharsets;
@@ -79,12 +80,11 @@ public static class Builder {
7980
private TrustedRootProvider trustedRootProvider;
8081

8182
public KeylessVerifier build()
82-
throws InvalidAlgorithmParameterException,
83+
throws SigstoreConfigurationException,
84+
InvalidAlgorithmParameterException,
8385
CertificateException,
8486
InvalidKeySpecException,
85-
NoSuchAlgorithmException,
86-
IOException,
87-
InvalidKeyException {
87+
NoSuchAlgorithmException {
8888
Preconditions.checkNotNull(trustedRootProvider);
8989
var trustedRoot = trustedRootProvider.get();
9090
var fulcioVerifier = FulcioVerifier.newFulcioVerifier(trustedRoot);

sigstore-java/src/main/java/dev/sigstore/TrustedRootProvider.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,34 @@
1616
package dev.sigstore;
1717

1818
import com.google.common.base.Preconditions;
19+
import dev.sigstore.trustroot.SigstoreConfigurationException;
1920
import dev.sigstore.trustroot.SigstoreTrustedRoot;
2021
import dev.sigstore.tuf.SigstoreTufClient;
2122
import java.io.IOException;
2223
import java.nio.file.Files;
2324
import java.nio.file.Path;
24-
import java.security.InvalidAlgorithmParameterException;
2525
import java.security.InvalidKeyException;
2626
import java.security.NoSuchAlgorithmException;
27-
import java.security.cert.CertificateException;
2827
import java.security.spec.InvalidKeySpecException;
2928

3029
@FunctionalInterface
3130
public interface TrustedRootProvider {
3231

33-
SigstoreTrustedRoot get()
34-
throws InvalidAlgorithmParameterException,
35-
CertificateException,
36-
InvalidKeySpecException,
37-
NoSuchAlgorithmException,
38-
IOException,
39-
InvalidKeyException;
32+
SigstoreTrustedRoot get() throws SigstoreConfigurationException;
4033

4134
static TrustedRootProvider from(SigstoreTufClient.Builder tufClientBuilder) {
4235
Preconditions.checkNotNull(tufClientBuilder);
4336
return () -> {
44-
var tufClient = tufClientBuilder.build();
45-
tufClient.update();
46-
return tufClient.getSigstoreTrustedRoot();
37+
try {
38+
var tufClient = tufClientBuilder.build();
39+
tufClient.update();
40+
return tufClient.getSigstoreTrustedRoot();
41+
} catch (IOException
42+
| NoSuchAlgorithmException
43+
| InvalidKeySpecException
44+
| InvalidKeyException ex) {
45+
throw new SigstoreConfigurationException(ex);
46+
}
4747
};
4848
}
4949

@@ -52,6 +52,8 @@ static TrustedRootProvider from(Path trustedRoot) {
5252
return () -> {
5353
try (var is = Files.newInputStream(trustedRoot)) {
5454
return SigstoreTrustedRoot.from(is);
55+
} catch (IOException ex) {
56+
throw new SigstoreConfigurationException(ex);
5557
}
5658
};
5759
}

sigstore-java/src/main/java/dev/sigstore/rekor/client/RekorEntryFetcher.java

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@
1818
import dev.sigstore.KeylessVerificationException;
1919
import dev.sigstore.TrustedRootProvider;
2020
import dev.sigstore.encryption.certificates.Certificates;
21+
import dev.sigstore.trustroot.SigstoreConfigurationException;
2122
import dev.sigstore.trustroot.TransparencyLog;
2223
import dev.sigstore.tuf.SigstoreTufClient;
2324
import java.io.IOException;
2425
import java.nio.file.Path;
25-
import java.security.InvalidAlgorithmParameterException;
26-
import java.security.InvalidKeyException;
27-
import java.security.NoSuchAlgorithmException;
28-
import java.security.cert.CertificateException;
2926
import java.security.cert.CertificateExpiredException;
3027
import java.security.cert.CertificateNotYetValidException;
3128
import java.security.cert.X509Certificate;
32-
import java.security.spec.InvalidKeySpecException;
3329
import java.sql.Date;
3430
import java.util.List;
3531
import java.util.Optional;
@@ -44,45 +40,23 @@ public class RekorEntryFetcher {
4440
// a client per remote trusted log
4541
private final List<RekorClient> rekorClients;
4642

47-
public static RekorEntryFetcher sigstoreStaging()
48-
throws InvalidAlgorithmParameterException,
49-
CertificateException,
50-
InvalidKeySpecException,
51-
NoSuchAlgorithmException,
52-
IOException,
53-
InvalidKeyException {
43+
public static RekorEntryFetcher sigstoreStaging() throws SigstoreConfigurationException {
5444
var sigstoreTufClientBuilder = SigstoreTufClient.builder().useStagingInstance();
5545
return fromTrustedRoot(TrustedRootProvider.from(sigstoreTufClientBuilder));
5646
}
5747

58-
public static RekorEntryFetcher sigstorePublicGood()
59-
throws InvalidAlgorithmParameterException,
60-
CertificateException,
61-
InvalidKeySpecException,
62-
NoSuchAlgorithmException,
63-
IOException,
64-
InvalidKeyException {
48+
public static RekorEntryFetcher sigstorePublicGood() throws SigstoreConfigurationException {
6549
var sigstoreTufClientBuilder = SigstoreTufClient.builder().usePublicGoodInstance();
6650
return fromTrustedRoot(TrustedRootProvider.from(sigstoreTufClientBuilder));
6751
}
6852

6953
public static RekorEntryFetcher fromTrustedRoot(Path trustedRoot)
70-
throws InvalidAlgorithmParameterException,
71-
CertificateException,
72-
InvalidKeySpecException,
73-
NoSuchAlgorithmException,
74-
IOException,
75-
InvalidKeyException {
54+
throws SigstoreConfigurationException {
7655
return fromTrustedRoot(TrustedRootProvider.from(trustedRoot));
7756
}
7857

7958
public static RekorEntryFetcher fromTrustedRoot(TrustedRootProvider trustedRootProvider)
80-
throws InvalidAlgorithmParameterException,
81-
CertificateException,
82-
InvalidKeySpecException,
83-
NoSuchAlgorithmException,
84-
IOException,
85-
InvalidKeyException {
59+
throws SigstoreConfigurationException {
8660
var trustedRoot = trustedRootProvider.get();
8761
var rekorClients =
8862
trustedRoot.getTLogs().stream()

sigstore-java/src/main/java/dev/sigstore/trustroot/SigstoreTrustedRoot.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,27 @@ public interface SigstoreTrustedRoot {
4141
List<TransparencyLog> getCTLogs();
4242

4343
/** Create an instance from an input stream of a json representation of a trustedroot. */
44-
static SigstoreTrustedRoot from(InputStream json) throws IOException, CertificateException {
44+
static SigstoreTrustedRoot from(InputStream json) throws SigstoreConfigurationException {
4545
var trustedRootBuilder = TrustedRoot.newBuilder();
4646
try (var reader = new InputStreamReader(json, StandardCharsets.UTF_8)) {
4747
JsonFormat.parser().merge(reader, trustedRootBuilder);
48+
} catch (IOException ex) {
49+
throw new SigstoreConfigurationException("Could not parse trusted root", ex);
4850
}
4951
return from(trustedRootBuilder);
5052
}
5153

5254
/** Create an instance from a parsed proto definition of a trustedroot. */
53-
static SigstoreTrustedRoot from(TrustedRootOrBuilder proto) throws CertificateException {
55+
static SigstoreTrustedRoot from(TrustedRootOrBuilder proto)
56+
throws SigstoreConfigurationException {
5457
List<CertificateAuthority> cas = Lists.newArrayList();
5558
for (var certAuthority : proto.getCertificateAuthoritiesList()) {
56-
cas.add(CertificateAuthority.from(certAuthority));
59+
try {
60+
cas.add(CertificateAuthority.from(certAuthority));
61+
} catch (CertificateException ce) {
62+
throw new SigstoreConfigurationException(
63+
"Could not parse certificates in trusted root", ce);
64+
}
5765
}
5866

5967
List<TransparencyLog> tlogs =

sigstore-java/src/main/java/dev/sigstore/tuf/SigstoreTufClient.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.base.Preconditions;
2020
import com.google.protobuf.util.JsonFormat;
2121
import dev.sigstore.proto.trustroot.v1.TrustedRoot;
22+
import dev.sigstore.trustroot.SigstoreConfigurationException;
2223
import dev.sigstore.trustroot.SigstoreTrustedRoot;
2324
import java.io.IOException;
2425
import java.net.MalformedURLException;
@@ -28,7 +29,6 @@
2829
import java.nio.file.Path;
2930
import java.security.InvalidKeyException;
3031
import java.security.NoSuchAlgorithmException;
31-
import java.security.cert.CertificateException;
3232
import java.security.spec.InvalidKeySpecException;
3333
import java.time.Duration;
3434
import java.time.Instant;
@@ -152,11 +152,11 @@ public SigstoreTufClient build() throws IOException {
152152
* defined on the client.
153153
*/
154154
public void update()
155-
throws IOException,
155+
throws SigstoreConfigurationException,
156+
IOException,
156157
NoSuchAlgorithmException,
157158
InvalidKeySpecException,
158-
InvalidKeyException,
159-
CertificateException {
159+
InvalidKeyException {
160160
if (lastUpdate == null
161161
|| Duration.between(lastUpdate, Instant.now()).compareTo(cacheValidity) > 0) {
162162
this.forceUpdate();
@@ -165,11 +165,11 @@ public void update()
165165

166166
/** Force an update, ignoring any cache validity. */
167167
public void forceUpdate()
168-
throws IOException,
168+
throws SigstoreConfigurationException,
169+
IOException,
169170
NoSuchAlgorithmException,
170171
InvalidKeySpecException,
171-
InvalidKeyException,
172-
CertificateException {
172+
InvalidKeyException {
173173
updater.update();
174174
lastUpdate = Instant.now();
175175
var trustedRootBuilder = TrustedRoot.newBuilder();

sigstore-java/src/test/java/dev/sigstore/rekor/client/RekorVerifierTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.io.IOException;
2525
import java.net.URI;
2626
import java.nio.charset.StandardCharsets;
27-
import java.security.cert.CertificateException;
2827
import java.time.Instant;
2928
import java.time.temporal.ChronoUnit;
3029
import java.util.List;
@@ -55,7 +54,7 @@ public void loadResources() throws IOException {
5554
}
5655

5756
@BeforeAll
58-
public static void initTrustRoot() throws IOException, CertificateException {
57+
public static void initTrustRoot() throws Exception {
5958
trustRoot =
6059
SigstoreTrustedRoot.from(
6160
Resources.getResource("dev/sigstore/trustroot/staging_trusted_root.json").openStream());

sigstore-java/src/test/java/dev/sigstore/trustroot/SigstoreTrustedRootTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import static org.junit.jupiter.api.Assertions.assertTrue;
2222

2323
import com.google.common.io.Resources;
24-
import java.io.IOException;
25-
import java.security.cert.CertificateException;
2624
import java.time.ZonedDateTime;
2725
import java.util.List;
2826
import org.bouncycastle.util.encoders.Base64;
@@ -34,7 +32,7 @@ class SigstoreTrustedRootTest {
3432
private static SigstoreTrustedRoot trustRoot;
3533

3634
@BeforeAll
37-
public static void initTrustRoot() throws IOException, CertificateException {
35+
public static void initTrustRoot() throws Exception {
3836
trustRoot =
3937
SigstoreTrustedRoot.from(
4038
Resources.getResource("dev/sigstore/trustroot/trusted_root.json").openStream());

0 commit comments

Comments
 (0)