Skip to content

Commit b852235

Browse files
authored
Merge pull request #517 from sigstore/fix-root-source
Add RootProvider
2 parents fcad127 + fbc57e5 commit b852235

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2023 The Sigstore Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package dev.sigstore.tuf;
17+
18+
import com.google.common.io.Resources;
19+
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
24+
/** An interface for providing the tuf root to a client. */
25+
@FunctionalInterface
26+
public interface RootProvider {
27+
String get() throws IOException;
28+
29+
static RootProvider fromResource(String resourceName) {
30+
return () -> Resources.toString(Resources.getResource(resourceName), StandardCharsets.UTF_8);
31+
}
32+
33+
static RootProvider fromFile(Path path) {
34+
return () -> Files.readString(path);
35+
}
36+
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.google.common.annotations.VisibleForTesting;
1919
import com.google.common.base.Preconditions;
20-
import com.google.common.io.Resources;
2120
import com.google.protobuf.util.JsonFormat;
2221
import dev.sigstore.proto.trustroot.v1.TrustedRoot;
2322
import dev.sigstore.trustroot.SigstoreTrustedRoot;
@@ -63,7 +62,7 @@ public static class Builder {
6362
Path.of(System.getProperty("user.home")).resolve(".sigstore-java").resolve("root");
6463

6564
URL remoteMirror;
66-
Path trustedRoot;
65+
RootProvider trustedRoot;
6766

6867
public Builder usePublicGoodInstance() {
6968
if (remoteMirror != null || trustedRoot != null) {
@@ -73,8 +72,7 @@ public Builder usePublicGoodInstance() {
7372
try {
7473
tufMirror(
7574
new URL("https://tuf-repo-cdn.sigstore.dev"),
76-
Path.of(
77-
Resources.getResource("dev/sigstore/tuf/sigstore-tuf-root/root.json").getPath()));
75+
RootProvider.fromResource("dev/sigstore/tuf/sigstore-tuf-root/root.json"));
7876
} catch (MalformedURLException e) {
7977
throw new AssertionError(e);
8078
}
@@ -89,8 +87,7 @@ public Builder useStagingInstance() {
8987
try {
9088
tufMirror(
9189
new URL("https://tuf-repo-cdn.sigstage.dev"),
92-
Path.of(
93-
Resources.getResource("dev/sigstore/tuf/tuf-root-staging/root.json").getPath()));
90+
RootProvider.fromResource("dev/sigstore/tuf/tuf-root-staging/root.json"));
9491
} catch (MalformedURLException e) {
9592
throw new AssertionError(e);
9693
}
@@ -102,7 +99,7 @@ public Builder useStagingInstance() {
10299
return this;
103100
}
104101

105-
public Builder tufMirror(URL mirror, Path trustedRoot) {
102+
public Builder tufMirror(URL mirror, RootProvider trustedRoot) {
106103
this.remoteMirror = mirror;
107104
this.trustedRoot = trustedRoot;
108105
return this;

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import dev.sigstore.tuf.model.*;
2525
import java.io.IOException;
2626
import java.nio.charset.StandardCharsets;
27-
import java.nio.file.Files;
28-
import java.nio.file.Path;
2927
import java.security.InvalidKeyException;
3028
import java.security.NoSuchAlgorithmException;
3129
import java.security.PublicKey;
@@ -58,14 +56,14 @@ public class Updater {
5856
private Verifiers.Supplier verifiers;
5957
private MetaFetcher fetcher;
6058
private ZonedDateTime updateStartTime;
61-
private Path trustedRootPath;
59+
private RootProvider trustedRootPath;
6260
private MutableTufStore localStore;
6361

6462
Updater(
6563
Clock clock,
6664
Verifiers.Supplier verifiers,
6765
MetaFetcher fetcher,
68-
Path trustedRootPath,
66+
RootProvider trustedRootPath,
6967
MutableTufStore localStore) {
7068
this.clock = clock;
7169
this.verifiers = verifiers;
@@ -106,7 +104,7 @@ Root updateRoot()
106104
if (localRoot.isPresent()) {
107105
trustedRoot = localRoot.get();
108106
} else {
109-
trustedRoot = GSON.get().fromJson(Files.readString(trustedRootPath), Root.class);
107+
trustedRoot = GSON.get().fromJson(trustedRootPath.get(), Root.class);
110108
}
111109
int baseVersion = trustedRoot.getSignedMeta().getVersion();
112110
int nextVersion = baseVersion + 1;
@@ -437,7 +435,7 @@ public static class Builder {
437435
private Verifiers.Supplier verifiers = Verifiers::newVerifier;
438436

439437
private MetaFetcher fetcher;
440-
private Path trustedRootPath;
438+
private RootProvider trustedRootPath;
441439
private MutableTufStore localStore;
442440

443441
public Builder setClock(Clock clock) {
@@ -455,7 +453,7 @@ public Builder setLocalStore(MutableTufStore store) {
455453
return this;
456454
}
457455

458-
public Builder setTrustedRootPath(Path trustedRootPath) {
456+
public Builder setTrustedRootPath(RootProvider trustedRootPath) {
459457
this.trustedRootPath = trustedRootPath;
460458
return this;
461459
}

sigstore-java/src/test/java/dev/sigstore/tuf/UpdaterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ private static Updater createTimeStaticUpdater(Path localStore, Path trustedRoot
937937
.setClock(Clock.fixed(Instant.parse(time), ZoneOffset.UTC))
938938
.setVerifiers(Verifiers::newVerifier)
939939
.setFetcher(HttpMetaFetcher.newFetcher(new URL(remoteUrl)))
940-
.setTrustedRootPath(trustedRootFile)
940+
.setTrustedRootPath(RootProvider.fromFile(trustedRootFile))
941941
.setLocalStore(FileSystemTufStore.newFileSystemStore(localStore))
942942
.build();
943943
}

0 commit comments

Comments
 (0)