|
15 | 15 | */
|
16 | 16 | package dev.sigstore.tuf;
|
17 | 17 |
|
| 18 | +import static dev.sigstore.json.GsonSupplier.GSON; |
| 19 | + |
| 20 | +import com.google.common.base.Preconditions; |
18 | 21 | import dev.sigstore.tuf.model.Root;
|
19 | 22 | import dev.sigstore.tuf.model.SignedTufMeta;
|
20 | 23 | import dev.sigstore.tuf.model.TufMeta;
|
21 | 24 | import java.io.IOException;
|
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.util.Locale; |
22 | 27 | import java.util.Optional;
|
| 28 | +import javax.annotation.Nullable; |
23 | 29 |
|
24 |
| -/** Retrieves TUF metadata. */ |
25 |
| -public interface MetaFetcher { |
| 30 | +public class MetaFetcher { |
26 | 31 |
|
27 |
| - /** |
28 |
| - * Describes the source of the metadata being fetched from. e.g "http://mirror.bla/mirror", |
29 |
| - * "mock", "c:/tmp". |
30 |
| - */ |
31 |
| - String getSource(); |
| 32 | + private static final int MAX_META_BYTES = 99 * 1024; // 99 KB |
| 33 | + private final Fetcher fetcher; |
32 | 34 |
|
33 |
| - /** |
34 |
| - * Fetch the {@link Root} at the specified {@code version}. |
35 |
| - * |
36 |
| - * @throws FileExceedsMaxLengthException when the retrieved file is larger than the maximum |
37 |
| - * allowed by the client |
38 |
| - */ |
39 |
| - Optional<MetaFetchResult<Root>> getRootAtVersion(int version) |
40 |
| - throws IOException, FileExceedsMaxLengthException; |
| 35 | + private MetaFetcher(Fetcher fetcher) { |
| 36 | + this.fetcher = fetcher; |
| 37 | + } |
41 | 38 |
|
42 |
| - /** |
43 |
| - * Fetches the unversioned specified role meta from the source |
44 |
| - * |
45 |
| - * @param name TUF role name |
46 |
| - * @param roleType this should be the type you expect in return |
47 |
| - * @return the latest fully de-serialized role if it was present at the source |
48 |
| - * @throws IOException in case of IO errors |
49 |
| - * @throws FileExceedsMaxLengthException if the role meta at source exceeds client specified max |
50 |
| - * size |
51 |
| - */ |
52 |
| - <T extends SignedTufMeta<? extends TufMeta>> Optional<MetaFetchResult<T>> getMeta( |
53 |
| - String name, Class<T> roleType) throws IOException, FileExceedsMaxLengthException; |
| 39 | + public static MetaFetcher newFetcher(Fetcher fetcher) { |
| 40 | + return new MetaFetcher(fetcher); |
| 41 | + } |
54 | 42 |
|
55 |
| - /** |
56 |
| - * Fetches the specified role meta from the source |
57 |
| - * |
58 |
| - * @param name TUF role name |
59 |
| - * @param version the version of the file to download |
60 |
| - * @param roleType this should be the type you expect in return |
61 |
| - * @param maxSize max file size in bytes |
62 |
| - * @return the fully de-serialized role if it was present at the source |
63 |
| - * @throws IOException in case of IO errors |
64 |
| - * @throws FileExceedsMaxLengthException if the role meta at source exceeds client specified max |
65 |
| - * size |
66 |
| - */ |
67 |
| - <T extends SignedTufMeta<? extends TufMeta>> Optional<MetaFetchResult<T>> getMeta( |
68 |
| - String name, int version, Class<T> roleType, Integer maxSize) |
69 |
| - throws IOException, FileExceedsMaxLengthException; |
| 43 | + public String getSource() { |
| 44 | + return fetcher.getSource(); |
| 45 | + } |
| 46 | + |
| 47 | + public Optional<MetaFetchResult<Root>> getRootAtVersion(int version) |
| 48 | + throws IOException, FileExceedsMaxLengthException { |
| 49 | + String versionFileName = version + ".root.json"; |
| 50 | + return getMeta(versionFileName, Root.class, null); |
| 51 | + } |
70 | 52 |
|
71 |
| - byte[] fetchResource(String filename, int maxLength) |
72 |
| - throws IOException, FileExceedsMaxLengthException; |
| 53 | + public <T extends SignedTufMeta<? extends TufMeta>> Optional<MetaFetchResult<T>> getMeta( |
| 54 | + String role, Class<T> t) throws IOException, FileExceedsMaxLengthException { |
| 55 | + return getMeta(getFileName(role, null), t, null); |
| 56 | + } |
| 57 | + |
| 58 | + public <T extends SignedTufMeta<? extends TufMeta>> Optional<MetaFetchResult<T>> getMeta( |
| 59 | + String role, int version, Class<T> t, Integer maxSize) |
| 60 | + throws IOException, FileExceedsMaxLengthException { |
| 61 | + Preconditions.checkArgument(version > 0, "version should be positive, got: %s", version); |
| 62 | + return getMeta(getFileName(role, version), t, maxSize); |
| 63 | + } |
| 64 | + |
| 65 | + private static String getFileName(String role, @Nullable Integer version) { |
| 66 | + return version == null |
| 67 | + ? role + ".json" |
| 68 | + : String.format(Locale.ROOT, "%d.%s.json", version, role); |
| 69 | + } |
| 70 | + |
| 71 | + <T extends SignedTufMeta<? extends TufMeta>> Optional<MetaFetchResult<T>> getMeta( |
| 72 | + String filename, Class<T> t, Integer maxSize) |
| 73 | + throws IOException, FileExceedsMaxLengthException { |
| 74 | + byte[] roleBytes = fetcher.fetchResource(filename, maxSize == null ? MAX_META_BYTES : maxSize); |
| 75 | + if (roleBytes == null) { |
| 76 | + return Optional.empty(); |
| 77 | + } |
| 78 | + var result = |
| 79 | + new MetaFetchResult<T>( |
| 80 | + roleBytes, GSON.get().fromJson(new String(roleBytes, StandardCharsets.UTF_8), t)); |
| 81 | + return Optional.of(result); |
| 82 | + } |
73 | 83 | }
|
0 commit comments