...

Source file src/github.com/sigstore/cosign/v2/pkg/oci/static/file.go

Documentation: github.com/sigstore/cosign/v2/pkg/oci/static

     1  //
     2  // Copyright 2021 The Sigstore Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package static
    17  
    18  import (
    19  	"io"
    20  
    21  	v1 "github.com/google/go-containerregistry/pkg/v1"
    22  	"github.com/google/go-containerregistry/pkg/v1/empty"
    23  	"github.com/google/go-containerregistry/pkg/v1/mutate"
    24  	"github.com/google/go-containerregistry/pkg/v1/types"
    25  	payloadsize "github.com/sigstore/cosign/v2/internal/pkg/cosign/payload/size"
    26  	"github.com/sigstore/cosign/v2/internal/pkg/now"
    27  	"github.com/sigstore/cosign/v2/pkg/oci"
    28  	"github.com/sigstore/cosign/v2/pkg/oci/signed"
    29  )
    30  
    31  // NewFile constructs a new v1.Image with the provided payload.
    32  func NewFile(payload []byte, opts ...Option) (oci.File, error) {
    33  	o, err := makeOptions(opts...)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	base := mutate.MediaType(empty.Image, types.OCIManifestSchema1)
    38  	base = mutate.ConfigMediaType(base, o.ConfigMediaType)
    39  	layer := &staticLayer{
    40  		b:    payload,
    41  		opts: o,
    42  	}
    43  	img, err := mutate.Append(base, mutate.Addendum{
    44  		Layer: layer,
    45  	})
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	// Add annotations from options
    51  	img = mutate.Annotations(img, o.Annotations).(v1.Image)
    52  
    53  	if o.RecordCreationTimestamp {
    54  		t, err := now.Now()
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  
    59  		// Set the Created date to time of execution
    60  		img, err = mutate.CreatedAt(img, v1.Time{Time: t})
    61  		if err != nil {
    62  			return nil, err
    63  		}
    64  	}
    65  
    66  	return &file{
    67  		SignedImage: signed.Image(img),
    68  		layer:       layer,
    69  	}, nil
    70  }
    71  
    72  type file struct {
    73  	oci.SignedImage
    74  	layer v1.Layer
    75  }
    76  
    77  var _ oci.File = (*file)(nil)
    78  
    79  // FileMediaType implements oci.File
    80  func (f *file) FileMediaType() (types.MediaType, error) {
    81  	return f.layer.MediaType()
    82  }
    83  
    84  // Payload implements oci.File
    85  func (f *file) Payload() ([]byte, error) {
    86  	size, err := f.layer.Size()
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	err = payloadsize.CheckSize(uint64(size))
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	rc, err := f.layer.Uncompressed()
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	defer rc.Close()
    99  	return io.ReadAll(rc)
   100  }
   101  

View as plain text