github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/storage/schema2manifesthandler.go (about)

     1  package storage
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"encoding/json"
     7  	"github.com/docker/distribution"
     8  	"github.com/docker/distribution/context"
     9  	"github.com/docker/distribution/digest"
    10  	"github.com/docker/distribution/manifest/schema2"
    11  )
    12  
    13  //schema2ManifestHandler is a ManifestHandler that covers schema2 manifests.
    14  type schema2ManifestHandler struct {
    15  	repository *repository
    16  	blobStore  *linkedBlobStore
    17  	ctx        context.Context
    18  }
    19  
    20  var _ ManifestHandler = &schema2ManifestHandler{}
    21  
    22  func (ms *schema2ManifestHandler) Unmarshal(ctx context.Context, dgst digest.Digest, content []byte) (distribution.Manifest, error) {
    23  	context.GetLogger(ms.ctx).Debug("(*schema2ManifestHandler).Unmarshal")
    24  
    25  	var m schema2.DeserializedManifest
    26  	if err := json.Unmarshal(content, &m); err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	return &m, nil
    31  }
    32  
    33  func (ms *schema2ManifestHandler) Put(ctx context.Context, manifest distribution.Manifest, skipDependencyVerification bool) (digest.Digest, error) {
    34  	context.GetLogger(ms.ctx).Debug("(*schema2ManifestHandler).Put")
    35  
    36  	m, ok := manifest.(*schema2.DeserializedManifest)
    37  	if !ok {
    38  		return "", fmt.Errorf("non-schema2 manifest put to schema2ManifestHandler: %T", manifest)
    39  	}
    40  
    41  	if err := ms.verifyManifest(ms.ctx, *m, skipDependencyVerification); err != nil {
    42  		return "", err
    43  	}
    44  
    45  	mt, payload, err := m.Payload()
    46  	if err != nil {
    47  		return "", err
    48  	}
    49  
    50  	revision, err := ms.blobStore.Put(ctx, mt, payload)
    51  	if err != nil {
    52  		context.GetLogger(ctx).Errorf("error putting payload into blobstore: %v", err)
    53  		return "", err
    54  	}
    55  
    56  	// Link the revision into the repository.
    57  	if err := ms.blobStore.linkBlob(ctx, revision); err != nil {
    58  		return "", err
    59  	}
    60  
    61  	return revision.Digest, nil
    62  }
    63  
    64  // verifyManifest ensures that the manifest content is valid from the
    65  // perspective of the registry. As a policy, the registry only tries to store
    66  // valid content, leaving trust policies of that content up to consumers.
    67  func (ms *schema2ManifestHandler) verifyManifest(ctx context.Context, mnfst schema2.DeserializedManifest, skipDependencyVerification bool) error {
    68  	var errs distribution.ErrManifestVerification
    69  
    70  	if !skipDependencyVerification {
    71  		target := mnfst.Target()
    72  		_, err := ms.repository.Blobs(ctx).Stat(ctx, target.Digest)
    73  		if err != nil {
    74  			if err != distribution.ErrBlobUnknown {
    75  				errs = append(errs, err)
    76  			}
    77  
    78  			// On error here, we always append unknown blob errors.
    79  			errs = append(errs, distribution.ErrManifestBlobUnknown{Digest: target.Digest})
    80  		}
    81  
    82  		for _, fsLayer := range mnfst.References() {
    83  			_, err := ms.repository.Blobs(ctx).Stat(ctx, fsLayer.Digest)
    84  			if err != nil {
    85  				if err != distribution.ErrBlobUnknown {
    86  					errs = append(errs, err)
    87  				}
    88  
    89  				// On error here, we always append unknown blob errors.
    90  				errs = append(errs, distribution.ErrManifestBlobUnknown{Digest: fsLayer.Digest})
    91  			}
    92  		}
    93  	}
    94  	if len(errs) != 0 {
    95  		return errs
    96  	}
    97  
    98  	return nil
    99  }