diff --git a/sigstore-java/src/main/java/dev/sigstore/tuf/FileSystemTufStore.java b/sigstore-java/src/main/java/dev/sigstore/tuf/FileSystemTufStore.java index 22b870a4..bb86b47e 100644 --- a/sigstore-java/src/main/java/dev/sigstore/tuf/FileSystemTufStore.java +++ b/sigstore-java/src/main/java/dev/sigstore/tuf/FileSystemTufStore.java @@ -88,6 +88,11 @@ public Optional loadTargets() throws IOException { return loadRole(RootRole.TARGETS, Targets.class); } + @Override + public Optional loadDelegatedTargets(String roleName) throws IOException { + return loadRole(roleName, Targets.class); + } + @Override public void storeTargetFile(String targetName, byte[] targetContents) throws IOException { Files.write(targetsCache.resolve(targetName), targetContents); @@ -99,8 +104,8 @@ public byte[] getTargetFile(String targetName) throws IOException { } @Override - public void storeMeta(SignedTufMeta timestamp) throws IOException { - storeRole(timestamp); + public void storeMeta(String roleName, SignedTufMeta meta) throws IOException { + storeRole(roleName, meta); } > Optional loadRole(String roleName, Class tClass) @@ -112,9 +117,9 @@ > Optional loadRole(String roleName, Class tCla return Optional.of(GSON.get().fromJson(Files.readString(roleFile), tClass)); } - > void storeRole(T role) throws IOException { + > void storeRole(String roleName, T role) throws IOException { try (BufferedWriter fileWriter = - Files.newBufferedWriter(repoBaseDir.resolve(role.getSignedMeta().getType() + ".json"))) { + Files.newBufferedWriter(repoBaseDir.resolve(roleName + ".json"))) { GSON.get().toJson(role, fileWriter); } } @@ -132,7 +137,7 @@ public void storeTrustedRoot(Root root) throws IOException { // The file is already backed-up. continue. } } - storeRole(root); + storeRole(RootRole.ROOT, root); } @Override diff --git a/sigstore-java/src/main/java/dev/sigstore/tuf/MutableTufStore.java b/sigstore-java/src/main/java/dev/sigstore/tuf/MutableTufStore.java index 2d208a0f..319eb55d 100644 --- a/sigstore-java/src/main/java/dev/sigstore/tuf/MutableTufStore.java +++ b/sigstore-java/src/main/java/dev/sigstore/tuf/MutableTufStore.java @@ -32,10 +32,11 @@ public interface MutableTufStore extends TufStore { /** * Generic method to store one of the {@link SignedTufMeta} resources in the local tuf store. * + * @param roleName the name of the role * @param meta the metadata to store * @throws IOException if writing the resource causes an IO error */ - void storeMeta(SignedTufMeta meta) throws IOException; + void storeMeta(String roleName, SignedTufMeta meta) throws IOException; /** * Once you have ascertained that your root is trustworthy use this method to persist it to your diff --git a/sigstore-java/src/main/java/dev/sigstore/tuf/TufStore.java b/sigstore-java/src/main/java/dev/sigstore/tuf/TufStore.java index 6e831ccb..c990d32b 100644 --- a/sigstore-java/src/main/java/dev/sigstore/tuf/TufStore.java +++ b/sigstore-java/src/main/java/dev/sigstore/tuf/TufStore.java @@ -42,6 +42,9 @@ public interface TufStore { /** Return the local trusted targets metadata if there is any. */ Optional loadTargets() throws IOException; + /** Return a named local delegated targets metadata if there is any. */ + Optional loadDelegatedTargets(String roleName) throws IOException; + /** * Reads a TUF target file from the local TUF store * diff --git a/sigstore-java/src/main/java/dev/sigstore/tuf/Updater.java b/sigstore-java/src/main/java/dev/sigstore/tuf/Updater.java index 9d981c8a..05f622c9 100644 --- a/sigstore-java/src/main/java/dev/sigstore/tuf/Updater.java +++ b/sigstore-java/src/main/java/dev/sigstore/tuf/Updater.java @@ -295,7 +295,7 @@ Optional updateTimestamp(Root root) // 4) check expiration timestamp is after tuf update start time, else fail. throwIfExpired(timestamp.getSignedMeta().getExpiresAsDate()); // 5) persist timestamp.json - localStore.storeMeta(timestamp); + localStore.storeMeta(RootRole.TIMESTAMP, timestamp); return Optional.of(timestamp); } @@ -356,7 +356,7 @@ Snapshot updateSnapshot(Root root, Timestamp timestamp) // 6) Ensure expiration timestamp of snapshot is later than tuf update start time. throwIfExpired(snapshot.getMetaResource().getSignedMeta().getExpiresAsDate()); // 7) persist snapshot. - localStore.storeMeta(snapshot.getMetaResource()); + localStore.storeMeta(RootRole.SNAPSHOT, snapshot.getMetaResource()); return snapshot.getMetaResource(); } @@ -426,7 +426,7 @@ Targets updateTargets(Root root, Snapshot snapshot) throwIfExpired(targetsResult.getMetaResource().getSignedMeta().getExpiresAsDate()); // 6) persist targets metadata // why do we persist the - localStore.storeMeta(targetsResult.getMetaResource()); + localStore.storeMeta(RootRole.TARGETS, targetsResult.getMetaResource()); return targetsResult.getMetaResource(); }