Skip to content

Commit 90d958b

Browse files
authored
Merge pull request #839 from sigstore/minor-tuf-updates
Update tuf updater api surface
2 parents 8e91247 + 775e0a0 commit 90d958b

File tree

7 files changed

+39
-17
lines changed

7 files changed

+39
-17
lines changed

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import dev.sigstore.encryption.Keys;
2323
import dev.sigstore.encryption.signers.Verifiers;
2424
import dev.sigstore.tuf.model.*;
25+
import dev.sigstore.tuf.model.TargetMeta.TargetData;
2526
import java.io.IOException;
2627
import java.nio.charset.StandardCharsets;
2728
import java.security.InvalidKeyException;
@@ -91,13 +92,15 @@ public static Builder builder() {
9192
return new Builder();
9293
}
9394

95+
/** Update metadata and download all targets. */
9496
public void update()
9597
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
9698
updateMeta();
9799
downloadTargets(trustedMetaStore.getTargets());
98100
}
99101

100-
void updateMeta() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
102+
/** Update just metadata but do not download targets. */
103+
public void updateMeta() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
101104
updateRoot();
102105
var oldTimestamp = trustedMetaStore.findTimestamp();
103106
updateTimestamp();
@@ -112,6 +115,16 @@ void updateMeta() throws IOException, NoSuchAlgorithmException, InvalidKeySpecEx
112115
updateTargets();
113116
}
114117

118+
/** Download a single target defined in targets. Does not handle delegated targets. */
119+
public void downloadTarget(String targetName)
120+
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
121+
var targetData = trustedMetaStore.getTargets().getSignedMeta().getTargets().get(targetName);
122+
if (targetData == null) {
123+
throw new TargetMetadataMissingException(targetName);
124+
}
125+
downloadTarget(targetName, targetData);
126+
}
127+
115128
// https://theupdateframework.github.io/specification/latest/#detailed-client-workflow
116129
void updateRoot()
117130
throws IOException, RoleExpiredException, NoSuchAlgorithmException, InvalidKeySpecException,
@@ -304,7 +317,6 @@ void updateTimestamp()
304317
localTimestamp.getSignedMeta().getVersion(), timestamp.getSignedMeta().getVersion());
305318
}
306319
if (localTimestamp.getSignedMeta().getVersion() == timestamp.getSignedMeta().getVersion()) {
307-
trustedMetaStore.setTimestamp(localTimestamp);
308320
return;
309321
}
310322
}
@@ -459,24 +471,28 @@ void downloadTargets(Targets targets)
459471
throw new TargetMetadataMissingException(targetName);
460472
}
461473
TargetMeta.TargetData targetData = entry.getValue();
462-
// 9) Download target up to length specified in bytes. verify against hash.
463-
String versionedTargetName;
464-
if (targetData.getHashes().getSha512() != null) {
465-
versionedTargetName = targetData.getHashes().getSha512() + "." + targetName;
466-
} else {
467-
versionedTargetName = targetData.getHashes().getSha256() + "." + targetName;
468-
}
474+
downloadTarget(targetName, targetData);
475+
}
476+
}
469477

470-
var targetBytes = targetFetcher.fetchResource(versionedTargetName, targetData.getLength());
471-
if (targetBytes == null) {
472-
throw new FileNotFoundException(targetName, targetFetcher.getSource());
473-
}
474-
verifyHashes(entry.getKey(), targetBytes, targetData.getHashes());
478+
void downloadTarget(String targetName, TargetData targetData) throws IOException {
479+
// 9) Download target up to length specified in bytes. verify against hash.
480+
String versionedTargetName;
481+
if (targetData.getHashes().getSha512() != null) {
482+
versionedTargetName = targetData.getHashes().getSha512() + "." + targetName;
483+
} else {
484+
versionedTargetName = targetData.getHashes().getSha256() + "." + targetName;
485+
}
475486

476-
// when persisting targets use the targetname without sha512 prefix
477-
// https://theupdateframework.github.io/specification/latest/index.html#fetch-target
478-
targetStore.writeTarget(targetName, targetBytes);
487+
var targetBytes = targetFetcher.fetchResource(versionedTargetName, targetData.getLength());
488+
if (targetBytes == null) {
489+
throw new FileNotFoundException(targetName, targetFetcher.getSource());
479490
}
491+
verifyHashes(targetName, targetBytes, targetData.getHashes());
492+
493+
// when persisting targets use the targetname without sha512 prefix
494+
// https://theupdateframework.github.io/specification/latest/index.html#fetch-target
495+
targetStore.writeTarget(targetName, targetBytes);
480496
}
481497

482498
@VisibleForTesting

sigstore-java/src/main/java/dev/sigstore/tuf/model/Root.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
@Value.Immutable
2525
public interface Root extends SignedTufMeta<RootMeta> {
2626
@Override
27+
@Gson.Ignore
2728
@Derived
2829
default RootMeta getSignedMeta() {
2930
return getSignedMeta(RootMeta.class);

sigstore-java/src/main/java/dev/sigstore/tuf/model/SignedTufMeta.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public interface SignedTufMeta<T extends TufMeta> {
3838

3939
/** An internal helper to translate raw signed json to a useable type. */
4040
@Derived
41+
@Gson.Ignore
4142
default T getSignedMeta(Class<T> type) {
4243
return GsonSupplier.GSON.get().fromJson(getRawSignedMeta(), type);
4344
}

sigstore-java/src/main/java/dev/sigstore/tuf/model/Snapshot.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
public interface Snapshot extends SignedTufMeta<SnapshotMeta> {
2626
@Override
2727
@Derived
28+
@Gson.Ignore
2829
default SnapshotMeta getSignedMeta() {
2930
return getSignedMeta(SnapshotMeta.class);
3031
}

sigstore-java/src/main/java/dev/sigstore/tuf/model/SnapshotMeta.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ interface SnapshotTarget {
5959

6060
/** The length in bytes of the given target's metadata, or a default if not present */
6161
@Derived
62+
@Gson.Ignore
6263
default Integer getLengthOrDefault() {
6364
return getLength().orElse(DEFAULT_MAX_LENGTH);
6465
}

sigstore-java/src/main/java/dev/sigstore/tuf/model/Targets.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
public interface Targets extends SignedTufMeta<TargetMeta> {
2626
@Override
2727
@Derived
28+
@Gson.Ignore
2829
default TargetMeta getSignedMeta() {
2930
return getSignedMeta(TargetMeta.class);
3031
}

sigstore-java/src/main/java/dev/sigstore/tuf/model/Timestamp.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public interface Timestamp extends SignedTufMeta<TimestampMeta> {
2626

2727
@Override
2828
@Derived
29+
@Gson.Ignore
2930
default TimestampMeta getSignedMeta() {
3031
return getSignedMeta(TimestampMeta.class);
3132
}

0 commit comments

Comments
 (0)