Skip to content

Commit 98524a8

Browse files
committed
tuf: use cached targets when available
Signed-off-by: Appu Goundan <[email protected]>
1 parent 617ea59 commit 98524a8

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

build-logic/jvm/src/main/kotlin/build-logic.spotless-base.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ spotless {
1111
target("*.md", ".gitignore", "**/*.yaml")
1212

1313
trimTrailingWhitespace()
14-
indentWithSpaces()
14+
leadingTabsToSpaces()
1515
endWithNewline()
1616
}
1717
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public byte[] readTarget(String targetName) throws IOException {
7777
return Files.readAllBytes(targetsDir.resolve(encoded));
7878
}
7979

80+
@Override
81+
public boolean hasTarget(String targetName) throws IOException {
82+
var encoded = URLEncoder.encode(targetName, StandardCharsets.UTF_8);
83+
return Files.isRegularFile(targetsDir.resolve(encoded));
84+
}
85+
8086
@Override
8187
public void writeMeta(String roleName, SignedTufMeta<?> meta) throws IOException {
8288
storeRole(roleName, meta);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ public interface TargetReader {
2929
* @throws IOException if an error occurs
3030
*/
3131
byte[] readTarget(String targetName) throws IOException;
32+
33+
/**
34+
* Checks if the local TUF store actually contains a target file with name.
35+
*
36+
* @param targetName the name of the target file to read (e.g. ctfe.pub)
37+
* @return true if the target exists locally
38+
* @throws IOException if an error occurs
39+
*/
40+
boolean hasTarget(String targetName) throws IOException;
3241
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,27 @@ public void updateMeta() throws IOException, NoSuchAlgorithmException, InvalidKe
116116
}
117117
}
118118

119-
/** Download a single target defined in targets. Does not handle delegated targets. */
120-
public void downloadTarget(String targetName)
121-
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
119+
/**
120+
* Download a single target defined in targets. Will not re-download a target that is already
121+
* cached locally. Does not handle delegated targets.
122+
*/
123+
public void downloadTarget(String targetName) throws IOException {
122124
var targetData = trustedMetaStore.getTargets().getSignedMeta().getTargets().get(targetName);
123125
if (targetData == null) {
124126
throw new TargetMetadataMissingException(targetName);
125127
}
128+
if (targetStore.hasTarget(targetName)) {
129+
byte[] target = targetStore.readTarget(targetName);
130+
// TODO: Using exceptions for control flow here, we should have something that returns a true
131+
// TODO: or false on hashes, but requires reworking verifyHashes.
132+
try {
133+
verifyHashes(targetName, target, targetData.getHashes());
134+
// found a valid cached instance of the target
135+
return;
136+
} catch (InvalidHashesException ioe) {
137+
// continue to download targets
138+
}
139+
}
126140
downloadTarget(targetName, targetData);
127141
}
128142

0 commit comments

Comments
 (0)