...

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

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

     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 mutate
    17  
    18  import (
    19  	"github.com/google/go-containerregistry/pkg/v1/types"
    20  	"github.com/sigstore/cosign/v2/pkg/cosign/bundle"
    21  	"github.com/sigstore/cosign/v2/pkg/oci"
    22  )
    23  
    24  // DupeDetector scans a list of signatures looking for a duplicate.
    25  type DupeDetector interface {
    26  	Find(oci.Signatures, oci.Signature) (oci.Signature, error)
    27  }
    28  
    29  type ReplaceOp interface {
    30  	Replace(oci.Signatures, oci.Signature) (oci.Signatures, error)
    31  }
    32  
    33  type SignOption func(*signOpts)
    34  
    35  type signOpts struct {
    36  	dd  DupeDetector
    37  	ro  ReplaceOp
    38  	rct bool
    39  }
    40  
    41  func makeSignOpts(opts ...SignOption) *signOpts {
    42  	so := &signOpts{}
    43  	for _, opt := range opts {
    44  		opt(so)
    45  	}
    46  	return so
    47  }
    48  
    49  // WithDupeDetector configures Sign* to use the following DupeDetector
    50  // to avoid attaching duplicate signatures.
    51  func WithDupeDetector(dd DupeDetector) SignOption {
    52  	return func(so *signOpts) {
    53  		so.dd = dd
    54  	}
    55  }
    56  
    57  func WithReplaceOp(ro ReplaceOp) SignOption {
    58  	return func(so *signOpts) {
    59  		so.ro = ro
    60  	}
    61  }
    62  
    63  func WithRecordCreationTimestamp(rct bool) SignOption {
    64  	return func(so *signOpts) {
    65  		so.rct = rct
    66  	}
    67  }
    68  
    69  type signatureOpts struct {
    70  	annotations      map[string]string
    71  	bundle           *bundle.RekorBundle
    72  	rfc3161Timestamp *bundle.RFC3161Timestamp
    73  	cert             []byte
    74  	chain            []byte
    75  	mediaType        types.MediaType
    76  }
    77  
    78  type SignatureOption func(*signatureOpts)
    79  
    80  // WithAnnotations specifies the annotations the Signature should have.
    81  func WithAnnotations(annotations map[string]string) SignatureOption {
    82  	return func(so *signatureOpts) {
    83  		so.annotations = annotations
    84  	}
    85  }
    86  
    87  // WithBundle specifies the new Bundle the Signature should have.
    88  func WithBundle(b *bundle.RekorBundle) SignatureOption {
    89  	return func(so *signatureOpts) {
    90  		so.bundle = b
    91  	}
    92  }
    93  
    94  // WithRFC3161Timestamp specifies the new RFC3161Timestamp the Signature should have.
    95  func WithRFC3161Timestamp(b *bundle.RFC3161Timestamp) SignatureOption {
    96  	return func(so *signatureOpts) {
    97  		so.rfc3161Timestamp = b
    98  	}
    99  }
   100  
   101  // WithCertChain specifies the new cert and chain the Signature should have.
   102  func WithCertChain(cert, chain []byte) SignatureOption {
   103  	return func(so *signatureOpts) {
   104  		so.cert = cert
   105  		so.chain = chain
   106  	}
   107  }
   108  
   109  // WithMediaType specifies the new MediaType the Signature should have.
   110  func WithMediaType(mediaType types.MediaType) SignatureOption {
   111  	return func(so *signatureOpts) {
   112  		so.mediaType = mediaType
   113  	}
   114  }
   115  
   116  func makeSignatureOption(opts ...SignatureOption) *signatureOpts {
   117  	so := &signatureOpts{}
   118  	for _, opt := range opts {
   119  		opt(so)
   120  	}
   121  	return so
   122  }
   123  

View as plain text