Skip to content

Commit

Permalink
Handle targets with path elements
Browse files Browse the repository at this point in the history
filesystem store stores them down as urlencoded, this
mirrors behavior of the go-tuf client

Signed-off-by: Appu Goundan <[email protected]>
  • Loading branch information
loosebazooka committed Nov 18, 2024
1 parent 7402c14 commit 6570e4a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import dev.sigstore.tuf.model.*;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
Expand Down Expand Up @@ -65,12 +67,14 @@ public String getIdentifier() {

@Override
public void writeTarget(String targetName, byte[] targetContents) throws IOException {
Files.write(targetsCache.resolve(targetName), targetContents);
var encoded = URLEncoder.encode(targetName, StandardCharsets.UTF_8);
Files.write(targetsCache.resolve(encoded), targetContents);
}

@Override
public byte[] readTarget(String targetName) throws IOException {
return Files.readAllBytes(targetsCache.resolve(targetName));
var encoded = URLEncoder.encode(targetName, StandardCharsets.UTF_8);
return Files.readAllBytes(targetsCache.resolve(encoded));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public String getSource() {
public byte[] fetchResource(String filename, int maxLength)
throws IOException, FileExceedsMaxLengthException {
GenericUrl fileUrl = new GenericUrl(mirror + filename);
System.out.println(fileUrl.toString());
var req =
HttpClients.newHttpTransport(ImmutableHttpParams.builder().build())
.createRequestFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
public interface TargetReader {

/**
* Reads a TUF target file from the local TUF store
* Reads a TUF target file from the local TUF store. Target names may include path elements and
* the storage engine should be consistent when handling writing and reading these.
*
* @param targetName the name of the target file to read (e.g. ctfe.pub)
* @return the content of the file as bytes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public interface TargetStore extends TargetReader {
String getIdentifier();

/**
* Writes a TUF target to the local target store.
* Writes a TUF target to the local target store. Target names may include path elements and the
* storage engine should be consistent when handling writing and reading these.
*
* @param targetName the name of the target file to write (e.g. ctfe.pub)
* @param targetContents the content of the target file as bytes
Expand Down
18 changes: 16 additions & 2 deletions sigstore-java/src/main/java/dev/sigstore/tuf/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import dev.sigstore.tuf.model.Timestamp;
import dev.sigstore.tuf.model.TufMeta;
import java.io.IOException;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
Expand Down Expand Up @@ -478,12 +479,25 @@ void downloadTargets(Targets targets)
}

void downloadTarget(String targetName, TargetData targetData) throws IOException {
var calculatedName = targetName;
var calculatedPath = "";
// if target name includes directories then we have to process the path
if (targetName.contains("/")) {
var targetPath = Paths.get(targetName);
calculatedName = targetPath.getFileName().toString();
calculatedPath = targetPath.getParent().toString();
if (!calculatedPath.endsWith("/")) {
calculatedPath = calculatedPath + "/";
}
}
// 9) Download target up to length specified in bytes. verify against hash.
String versionedTargetName;
if (targetData.getHashes().getSha512() != null) {
versionedTargetName = targetData.getHashes().getSha512() + "." + targetName;
versionedTargetName =
calculatedPath + targetData.getHashes().getSha512() + "." + calculatedName;
} else {
versionedTargetName = targetData.getHashes().getSha256() + "." + targetName;
versionedTargetName =
calculatedPath + targetData.getHashes().getSha256() + "." + calculatedName;
}

var targetBytes = targetFetcher.fetchResource(versionedTargetName, targetData.getLength());
Expand Down
1 change: 0 additions & 1 deletion tuf-cli/tuf-cli.xfails
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
test_metadata_bytes_match
test_client_downloads_expected_file_in_sub_dir
test_duplicate_sig_keyids
test_unusual_role_name[?]
test_unusual_role_name[#]
Expand Down

0 comments on commit 6570e4a

Please sign in to comment.