Skip to content

Commit

Permalink
Merge pull request #828 from sigstore/non-inferred-file-names
Browse files Browse the repository at this point in the history
Non inferred file names
  • Loading branch information
loosebazooka authored Oct 18, 2024
2 parents 1072947 + a94177a commit 1ce04c9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public Optional<Targets> loadTargets() throws IOException {
return loadRole(RootRole.TARGETS, Targets.class);
}

@Override
public Optional<Targets> 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);
Expand All @@ -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);
}

<T extends SignedTufMeta<?>> Optional<T> loadRole(String roleName, Class<T> tClass)
Expand All @@ -112,9 +117,9 @@ <T extends SignedTufMeta<?>> Optional<T> loadRole(String roleName, Class<T> tCla
return Optional.of(GSON.get().fromJson(Files.readString(roleFile), tClass));
}

<T extends SignedTufMeta<?>> void storeRole(T role) throws IOException {
<T extends SignedTufMeta<?>> 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);
}
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions sigstore-java/src/main/java/dev/sigstore/tuf/TufStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public interface TufStore {
/** Return the local trusted targets metadata if there is any. */
Optional<Targets> loadTargets() throws IOException;

/** Return a named local delegated targets metadata if there is any. */
Optional<Targets> loadDelegatedTargets(String roleName) throws IOException;

/**
* Reads a TUF target file from the local TUF store
*
Expand Down
6 changes: 3 additions & 3 deletions sigstore-java/src/main/java/dev/sigstore/tuf/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ Optional<Timestamp> 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);
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 1ce04c9

Please sign in to comment.