22
22
import dev .sigstore .encryption .Keys ;
23
23
import dev .sigstore .encryption .signers .Verifiers ;
24
24
import dev .sigstore .tuf .model .*;
25
+ import dev .sigstore .tuf .model .TargetMeta .TargetData ;
25
26
import java .io .IOException ;
26
27
import java .nio .charset .StandardCharsets ;
27
28
import java .security .InvalidKeyException ;
@@ -91,13 +92,15 @@ public static Builder builder() {
91
92
return new Builder ();
92
93
}
93
94
95
+ /** Update metadata and download all targets. */
94
96
public void update ()
95
97
throws IOException , NoSuchAlgorithmException , InvalidKeySpecException , InvalidKeyException {
96
98
updateMeta ();
97
99
downloadTargets (trustedMetaStore .getTargets ());
98
100
}
99
101
100
- void updateMeta () throws IOException , NoSuchAlgorithmException , InvalidKeySpecException {
102
+ /** Update just metadata but do not download targets. */
103
+ public void updateMeta () throws IOException , NoSuchAlgorithmException , InvalidKeySpecException {
101
104
updateRoot ();
102
105
var oldTimestamp = trustedMetaStore .findTimestamp ();
103
106
updateTimestamp ();
@@ -112,6 +115,16 @@ void updateMeta() throws IOException, NoSuchAlgorithmException, InvalidKeySpecEx
112
115
updateTargets ();
113
116
}
114
117
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
+
115
128
// https://theupdateframework.github.io/specification/latest/#detailed-client-workflow
116
129
void updateRoot ()
117
130
throws IOException , RoleExpiredException , NoSuchAlgorithmException , InvalidKeySpecException ,
@@ -304,7 +317,6 @@ void updateTimestamp()
304
317
localTimestamp .getSignedMeta ().getVersion (), timestamp .getSignedMeta ().getVersion ());
305
318
}
306
319
if (localTimestamp .getSignedMeta ().getVersion () == timestamp .getSignedMeta ().getVersion ()) {
307
- trustedMetaStore .setTimestamp (localTimestamp );
308
320
return ;
309
321
}
310
322
}
@@ -459,24 +471,28 @@ void downloadTargets(Targets targets)
459
471
throw new TargetMetadataMissingException (targetName );
460
472
}
461
473
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
+ }
469
477
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
+ }
475
486
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 () );
479
490
}
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 );
480
496
}
481
497
482
498
@ VisibleForTesting
0 commit comments