@@ -11,6 +11,7 @@ import (
1111 "runtime"
1212
1313 "github.com/Sirupsen/logrus"
14+ "github.com/containers/image/signature"
1415 "github.com/docker/distribution"
1516 "github.com/docker/distribution/digest"
1617 "github.com/docker/distribution/manifest/manifestlist"
@@ -57,6 +58,9 @@ type v2Puller struct {
5758 // confirmedV2 is set to true if we confirm we're talking to a v2
5859 // registry. This is used to limit fallbacks to the v1 protocol.
5960 confirmedV2 bool
61+
62+ policyContext * signature.PolicyContext
63+ originalRef reference.Named
6064}
6165
6266func (p * v2Puller ) Pull (ctx context.Context , ref reference.Named ) (err error ) {
@@ -91,16 +95,26 @@ func (p *v2Puller) Pull(ctx context.Context, ref reference.Named) (err error) {
9195func (p * v2Puller ) pullV2Repository (ctx context.Context , ref reference.Named ) (err error ) {
9296 var layersDownloaded bool
9397 if ! reference .IsNameOnly (ref ) {
98+ var err error
99+ if p .config .SignatureCheck {
100+ ref , err = p .checkTrusted (ctx , ref )
101+ if err != nil {
102+ // do not fallback to v1 is there was any error checking image's signatures
103+ return err
104+ }
105+ }
94106 layersDownloaded , err = p .pullV2Tag (ctx , ref )
95107 if err != nil {
96108 return err
97109 }
98110 } else {
99111 tags , err := p .repo .Tags (ctx ).All (ctx )
100112 if err != nil {
101- // If this repository doesn't exist on V2, we should
102- // permit a fallback to V1.
103- return allowV1Fallback (err )
113+ if p .config .SignatureCheck {
114+ return err
115+ } else {
116+ return allowV1Fallback (err )
117+ }
104118 }
105119
106120 // The v2 registry knows about this repository, so we will not
@@ -113,7 +127,17 @@ func (p *v2Puller) pullV2Repository(ctx context.Context, ref reference.Named) (e
113127 if err != nil {
114128 return err
115129 }
116- pulledNew , err := p .pullV2Tag (ctx , tagRef )
130+ var ref reference.Named
131+ ref = tagRef
132+ if p .config .SignatureCheck {
133+ trustedRef , err := p .checkTrusted (ctx , tagRef )
134+ if err != nil {
135+ p .originalRef = nil
136+ return err
137+ }
138+ ref = trustedRef
139+ }
140+ pulledNew , err := p .pullV2Tag (ctx , ref )
117141 if err != nil {
118142 // Since this is the pull-all-tags case, don't
119143 // allow an error pulling a particular tag to
@@ -129,7 +153,11 @@ func (p *v2Puller) pullV2Repository(ctx context.Context, ref reference.Named) (e
129153 }
130154 }
131155
132- writeStatus (ref .String (), p .config .ProgressOutput , layersDownloaded )
156+ if p .originalRef != nil {
157+ writeStatus (p .originalRef .String (), p .config .ProgressOutput , layersDownloaded )
158+ } else {
159+ writeStatus (ref .String (), p .config .ProgressOutput , layersDownloaded )
160+ }
133161
134162 return nil
135163}
@@ -341,7 +369,11 @@ func (p *v2Puller) pullV2Tag(ctx context.Context, ref reference.Named) (tagUpdat
341369 if tagged , isTagged := ref .(reference.NamedTagged ); isTagged {
342370 manifest , err = manSvc .Get (ctx , "" , distribution .WithTag (tagged .Tag ()))
343371 if err != nil {
344- return false , allowV1Fallback (err )
372+ if p .config .SignatureCheck {
373+ return false , err
374+ } else {
375+ return false , allowV1Fallback (err )
376+ }
345377 }
346378 tagOrDigest = tagged .Tag ()
347379 } else if digested , isDigested := ref .(reference.Canonical ); isDigested {
@@ -416,6 +448,11 @@ func (p *v2Puller) pullV2Tag(ctx context.Context, ref reference.Named) (tagUpdat
416448 oldTagID , err := p .config .ReferenceStore .Get (ref )
417449 if err == nil {
418450 if oldTagID == id {
451+ if p .config .SignatureCheck {
452+ if err := p .addTrustedTag (id ); err != nil {
453+ return false , err
454+ }
455+ }
419456 return false , addDigestReference (p .config .ReferenceStore , ref , manifestDigest , id )
420457 }
421458 } else if err != reference .ErrDoesNotExist {
@@ -426,6 +463,11 @@ func (p *v2Puller) pullV2Tag(ctx context.Context, ref reference.Named) (tagUpdat
426463 if err = p .config .ReferenceStore .AddDigest (canonical , id , true ); err != nil {
427464 return false , err
428465 }
466+ if p .config .SignatureCheck {
467+ if err := p .addTrustedTag (id ); err != nil {
468+ return false , err
469+ }
470+ }
429471 } else {
430472 if err = addDigestReference (p .config .ReferenceStore , ref , manifestDigest , id ); err != nil {
431473 return false , err
@@ -438,6 +480,17 @@ func (p *v2Puller) pullV2Tag(ctx context.Context, ref reference.Named) (tagUpdat
438480 return true , nil
439481}
440482
483+ func (p * v2Puller ) addTrustedTag (id digest.Digest ) error {
484+ if p .policyContext != nil {
485+ if _ , ok := p .originalRef .(reference.Canonical ); ! ok {
486+ if err := p .config .ReferenceStore .AddTag (p .originalRef , id , true ); err != nil {
487+ return err
488+ }
489+ }
490+ }
491+ return nil
492+ }
493+
441494func (p * v2Puller ) pullSchema1 (ctx context.Context , ref reference.Named , unverifiedManifest * schema1.SignedManifest ) (id digest.Digest , manifestDigest digest.Digest , err error ) {
442495 var verifiedManifest * schema1.Manifest
443496 verifiedManifest , err = verifySchema1Manifest (unverifiedManifest , ref )
0 commit comments