Skip to content

Commit a94177a

Browse files
committed
Allow storage of delegated targets
Signed-off-by: Appu Goundan <[email protected]>
1 parent 1072947 commit a94177a

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public Optional<Targets> loadTargets() throws IOException {
8888
return loadRole(RootRole.TARGETS, Targets.class);
8989
}
9090

91+
@Override
92+
public Optional<Targets> loadDelegatedTargets(String roleName) throws IOException {
93+
return loadRole(roleName, Targets.class);
94+
}
95+
9196
@Override
9297
public void storeTargetFile(String targetName, byte[] targetContents) throws IOException {
9398
Files.write(targetsCache.resolve(targetName), targetContents);
@@ -99,8 +104,8 @@ public byte[] getTargetFile(String targetName) throws IOException {
99104
}
100105

101106
@Override
102-
public void storeMeta(SignedTufMeta<?> timestamp) throws IOException {
103-
storeRole(timestamp);
107+
public void storeMeta(String roleName, SignedTufMeta<?> meta) throws IOException {
108+
storeRole(roleName, meta);
104109
}
105110

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

115-
<T extends SignedTufMeta<?>> void storeRole(T role) throws IOException {
120+
<T extends SignedTufMeta<?>> void storeRole(String roleName, T role) throws IOException {
116121
try (BufferedWriter fileWriter =
117-
Files.newBufferedWriter(repoBaseDir.resolve(role.getSignedMeta().getType() + ".json"))) {
122+
Files.newBufferedWriter(repoBaseDir.resolve(roleName + ".json"))) {
118123
GSON.get().toJson(role, fileWriter);
119124
}
120125
}
@@ -132,7 +137,7 @@ public void storeTrustedRoot(Root root) throws IOException {
132137
// The file is already backed-up. continue.
133138
}
134139
}
135-
storeRole(root);
140+
storeRole(RootRole.ROOT, root);
136141
}
137142

138143
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ public interface MutableTufStore extends TufStore {
3232
/**
3333
* Generic method to store one of the {@link SignedTufMeta} resources in the local tuf store.
3434
*
35+
* @param roleName the name of the role
3536
* @param meta the metadata to store
3637
* @throws IOException if writing the resource causes an IO error
3738
*/
38-
void storeMeta(SignedTufMeta<?> meta) throws IOException;
39+
void storeMeta(String roleName, SignedTufMeta<?> meta) throws IOException;
3940

4041
/**
4142
* Once you have ascertained that your root is trustworthy use this method to persist it to your

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public interface TufStore {
4242
/** Return the local trusted targets metadata if there is any. */
4343
Optional<Targets> loadTargets() throws IOException;
4444

45+
/** Return a named local delegated targets metadata if there is any. */
46+
Optional<Targets> loadDelegatedTargets(String roleName) throws IOException;
47+
4548
/**
4649
* Reads a TUF target file from the local TUF store
4750
*

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ Optional<Timestamp> updateTimestamp(Root root)
295295
// 4) check expiration timestamp is after tuf update start time, else fail.
296296
throwIfExpired(timestamp.getSignedMeta().getExpiresAsDate());
297297
// 5) persist timestamp.json
298-
localStore.storeMeta(timestamp);
298+
localStore.storeMeta(RootRole.TIMESTAMP, timestamp);
299299
return Optional.of(timestamp);
300300
}
301301

@@ -356,7 +356,7 @@ Snapshot updateSnapshot(Root root, Timestamp timestamp)
356356
// 6) Ensure expiration timestamp of snapshot is later than tuf update start time.
357357
throwIfExpired(snapshot.getMetaResource().getSignedMeta().getExpiresAsDate());
358358
// 7) persist snapshot.
359-
localStore.storeMeta(snapshot.getMetaResource());
359+
localStore.storeMeta(RootRole.SNAPSHOT, snapshot.getMetaResource());
360360
return snapshot.getMetaResource();
361361
}
362362

@@ -426,7 +426,7 @@ Targets updateTargets(Root root, Snapshot snapshot)
426426
throwIfExpired(targetsResult.getMetaResource().getSignedMeta().getExpiresAsDate());
427427
// 6) persist targets metadata
428428
// why do we persist the
429-
localStore.storeMeta(targetsResult.getMetaResource());
429+
localStore.storeMeta(RootRole.TARGETS, targetsResult.getMetaResource());
430430
return targetsResult.getMetaResource();
431431
}
432432

0 commit comments

Comments
 (0)