Skip to content

Commit 7128496

Browse files
authored
Merge pull request #962 from sigstore/targetinputstream
Allow targetStore to return input streams
2 parents dd78f82 + 69c4add commit 7128496

File tree

7 files changed

+52
-43
lines changed

7 files changed

+52
-43
lines changed

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

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

1818
import com.google.common.base.Preconditions;
19-
import com.google.protobuf.util.JsonFormat;
20-
import dev.sigstore.proto.trustroot.v1.TrustedRoot;
2119
import dev.sigstore.trustroot.SigstoreTrustedRoot;
2220
import dev.sigstore.tuf.SigstoreTufClient;
2321
import java.io.IOException;
24-
import java.nio.charset.StandardCharsets;
2522
import java.nio.file.Files;
2623
import java.nio.file.Path;
2724
import java.security.InvalidAlgorithmParameterException;
@@ -53,10 +50,9 @@ static TrustedRootProvider from(SigstoreTufClient.Builder tufClientBuilder) {
5350
static TrustedRootProvider from(Path trustedRoot) {
5451
Preconditions.checkNotNull(trustedRoot);
5552
return () -> {
56-
var trustedRootBuilder = TrustedRoot.newBuilder();
57-
JsonFormat.parser()
58-
.merge(Files.readString(trustedRoot, StandardCharsets.UTF_8), trustedRootBuilder);
59-
return SigstoreTrustedRoot.from(trustedRootBuilder.build());
53+
try (var is = Files.newInputStream(trustedRoot)) {
54+
return SigstoreTrustedRoot.from(is);
55+
}
6056
};
6157
}
6258
}

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
package dev.sigstore.trustroot;
1717

1818
import com.google.api.client.util.Lists;
19+
import com.google.protobuf.util.JsonFormat;
1920
import dev.sigstore.proto.trustroot.v1.TrustedRoot;
21+
import dev.sigstore.proto.trustroot.v1.TrustedRootOrBuilder;
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.InputStreamReader;
25+
import java.nio.charset.StandardCharsets;
2026
import java.security.cert.CertificateException;
2127
import java.util.List;
2228
import java.util.stream.Collectors;
@@ -25,17 +31,26 @@
2531
@Immutable
2632
public interface SigstoreTrustedRoot {
2733

28-
/** A list of certificate authorities associated with this trustroot. */
34+
/** A list of certificate authorities associated with this trustedroot. */
2935
List<CertificateAuthority> getCAs();
3036

31-
/** A list of binary transparency logs associated with this trustroot. */
37+
/** A list of binary transparency logs associated with this trustedroot. */
3238
List<TransparencyLog> getTLogs();
3339

34-
/** A list of certificate transparency logs associated with this trustroot. */
40+
/** A list of certificate transparency logs associated with this trustedroot. */
3541
List<TransparencyLog> getCTLogs();
3642

37-
/** Create an instance from a parsed proto definition of a trustroot. */
38-
static SigstoreTrustedRoot from(TrustedRoot proto) throws CertificateException {
43+
/** Create an instance from an input stream of a json representation of a trustedroot. */
44+
static SigstoreTrustedRoot from(InputStream json) throws IOException, CertificateException {
45+
var trustedRootBuilder = TrustedRoot.newBuilder();
46+
try (var reader = new InputStreamReader(json, StandardCharsets.UTF_8)) {
47+
JsonFormat.parser().merge(reader, trustedRootBuilder);
48+
}
49+
return from(trustedRootBuilder);
50+
}
51+
52+
/** Create an instance from a parsed proto definition of a trustedroot. */
53+
static SigstoreTrustedRoot from(TrustedRootOrBuilder proto) throws CertificateException {
3954
List<CertificateAuthority> cas = Lists.newArrayList();
4055
for (var certAuthority : proto.getCertificateAuthoritiesList()) {
4156
cas.add(CertificateAuthority.from(certAuthority));

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import dev.sigstore.tuf.model.*;
2222
import java.io.BufferedWriter;
2323
import java.io.IOException;
24+
import java.io.InputStream;
2425
import java.net.URLEncoder;
2526
import java.nio.charset.StandardCharsets;
2627
import java.nio.file.Files;
@@ -77,6 +78,12 @@ public byte[] readTarget(String targetName) throws IOException {
7778
return Files.readAllBytes(targetsDir.resolve(encoded));
7879
}
7980

81+
@Override
82+
public InputStream getTargetInputSteam(String targetName) throws IOException {
83+
var encoded = URLEncoder.encode(targetName, StandardCharsets.UTF_8);
84+
return Files.newInputStream(targetsDir.resolve(encoded));
85+
}
86+
8087
@Override
8188
public boolean hasTarget(String targetName) throws IOException {
8289
var encoded = URLEncoder.encode(targetName, StandardCharsets.UTF_8);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package dev.sigstore.tuf;
1717

1818
import java.io.IOException;
19+
import java.io.InputStream;
1920

2021
/** Interface that defines reading targets from local storage. */
2122
public interface TargetReader {
@@ -30,6 +31,18 @@ public interface TargetReader {
3031
*/
3132
byte[] readTarget(String targetName) throws IOException;
3233

34+
/**
35+
* Returns an input stream to a TUF target file in the local TUF store. Target names may include
36+
* path elements and the storage engine should be consistent when handling writing and reading
37+
* these.
38+
*
39+
* @param targetName the name of the target file to read (e.g. ctfe.pub)
40+
* @return an input steam to the target file in the local store, the consumer must close the input
41+
* stream
42+
* @throws IOException if an error occurs
43+
*/
44+
InputStream getTargetInputSteam(String targetName) throws IOException;
45+
3346
/**
3447
* Checks if the local TUF store actually contains a target file with name.
3548
*

sigstore-java/src/test/java/dev/sigstore/fulcio/client/FulcioVerifierTest.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
package dev.sigstore.fulcio.client;
1717

1818
import com.google.common.io.Resources;
19-
import com.google.protobuf.util.JsonFormat;
2019
import dev.sigstore.bundle.Bundle;
2120
import dev.sigstore.encryption.certificates.Certificates;
22-
import dev.sigstore.proto.trustroot.v1.TrustedRoot;
2321
import dev.sigstore.trustroot.ImmutableLogId;
2422
import dev.sigstore.trustroot.ImmutableTransparencyLog;
2523
import dev.sigstore.trustroot.SigstoreTrustedRoot;
@@ -59,14 +57,9 @@ public static void loadResources() throws IOException {
5957

6058
@BeforeAll
6159
public static void initTrustRoot() throws Exception {
62-
var json =
63-
Resources.toString(
64-
Resources.getResource("dev/sigstore/trustroot/trusted_root.json"),
65-
StandardCharsets.UTF_8);
66-
var builder = TrustedRoot.newBuilder();
67-
JsonFormat.parser().merge(json, builder);
68-
69-
trustRoot = SigstoreTrustedRoot.from(builder.build());
60+
trustRoot =
61+
SigstoreTrustedRoot.from(
62+
Resources.getResource("dev/sigstore/trustroot/trusted_root.json").openStream());
7063
}
7164

7265
@Test

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package dev.sigstore.rekor.client;
1717

1818
import com.google.common.io.Resources;
19-
import com.google.protobuf.util.JsonFormat;
20-
import dev.sigstore.proto.trustroot.v1.TrustedRoot;
2119
import dev.sigstore.trustroot.ImmutableLogId;
2220
import dev.sigstore.trustroot.ImmutablePublicKey;
2321
import dev.sigstore.trustroot.ImmutableTransparencyLog;
@@ -58,14 +56,9 @@ public void loadResources() throws IOException {
5856

5957
@BeforeAll
6058
public static void initTrustRoot() throws IOException, CertificateException {
61-
var json =
62-
Resources.toString(
63-
Resources.getResource("dev/sigstore/trustroot/staging_trusted_root.json"),
64-
StandardCharsets.UTF_8);
65-
var builder = TrustedRoot.newBuilder();
66-
JsonFormat.parser().merge(json, builder);
67-
68-
trustRoot = SigstoreTrustedRoot.from(builder.build());
59+
trustRoot =
60+
SigstoreTrustedRoot.from(
61+
Resources.getResource("dev/sigstore/trustroot/staging_trusted_root.json").openStream());
6962
}
7063

7164
@Test

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
import static org.junit.jupiter.api.Assertions.assertTrue;
2222

2323
import com.google.common.io.Resources;
24-
import com.google.protobuf.util.JsonFormat;
25-
import dev.sigstore.proto.trustroot.v1.TrustedRoot;
2624
import java.io.IOException;
27-
import java.nio.charset.StandardCharsets;
2825
import java.security.cert.CertificateException;
2926
import java.time.ZonedDateTime;
3027
import java.util.List;
@@ -38,14 +35,9 @@ class SigstoreTrustedRootTest {
3835

3936
@BeforeAll
4037
public static void initTrustRoot() throws IOException, CertificateException {
41-
var json =
42-
Resources.toString(
43-
Resources.getResource("dev/sigstore/trustroot/trusted_root.json"),
44-
StandardCharsets.UTF_8);
45-
var builder = TrustedRoot.newBuilder();
46-
JsonFormat.parser().merge(json, builder);
47-
48-
trustRoot = SigstoreTrustedRoot.from(builder.build());
38+
trustRoot =
39+
SigstoreTrustedRoot.from(
40+
Resources.getResource("dev/sigstore/trustroot/trusted_root.json").openStream());
4941
}
5042

5143
@Test

0 commit comments

Comments
 (0)