...

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

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

     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 empty
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  
    22  	"github.com/google/go-containerregistry/pkg/name"
    23  	v1 "github.com/google/go-containerregistry/pkg/v1"
    24  	"github.com/google/go-containerregistry/pkg/v1/empty"
    25  	"github.com/sigstore/cosign/v2/pkg/oci"
    26  )
    27  
    28  type signedImage struct {
    29  	v1.Image
    30  	digest       v1.Hash
    31  	signature    oci.Signatures
    32  	attestations oci.Signatures
    33  }
    34  
    35  func (se *signedImage) Signatures() (oci.Signatures, error) {
    36  	return se.signature, nil
    37  }
    38  
    39  func (se *signedImage) Attestations() (oci.Signatures, error) {
    40  	return se.attestations, nil
    41  }
    42  
    43  func (se *signedImage) Attachment(name string) (oci.File, error) { //nolint: revive
    44  	return nil, errors.New("no attachments")
    45  }
    46  
    47  func (se *signedImage) Digest() (v1.Hash, error) {
    48  	if se.digest.Hex == "" {
    49  		return v1.Hash{}, fmt.Errorf("digest not available")
    50  	}
    51  	return se.digest, nil
    52  }
    53  
    54  func SignedImage(ref name.Reference) (oci.SignedImage, error) {
    55  	var err error
    56  	d := v1.Hash{}
    57  	base := empty.Image
    58  	if digest, ok := ref.(name.Digest); ok {
    59  		d, err = v1.NewHash(digest.DigestStr())
    60  		if err != nil {
    61  			return nil, err
    62  		}
    63  	}
    64  	return &signedImage{
    65  		Image:        base,
    66  		digest:       d,
    67  		signature:    Signatures(),
    68  		attestations: Signatures(),
    69  	}, nil
    70  }
    71  

View as plain text