...

Source file src/github.com/sigstore/cosign/v2/pkg/oci/static/options.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  	"encoding/json"
    20  
    21  	"github.com/google/go-containerregistry/pkg/v1/types"
    22  	"github.com/sigstore/cosign/v2/pkg/cosign/bundle"
    23  	ctypes "github.com/sigstore/cosign/v2/pkg/types"
    24  )
    25  
    26  // Option is a functional option for customizing static signatures.
    27  type Option func(*options)
    28  
    29  type options struct {
    30  	LayerMediaType          types.MediaType
    31  	ConfigMediaType         types.MediaType
    32  	Bundle                  *bundle.RekorBundle
    33  	RFC3161Timestamp        *bundle.RFC3161Timestamp
    34  	Cert                    []byte
    35  	Chain                   []byte
    36  	Annotations             map[string]string
    37  	RecordCreationTimestamp bool
    38  }
    39  
    40  func makeOptions(opts ...Option) (*options, error) {
    41  	o := &options{
    42  		LayerMediaType:  ctypes.SimpleSigningMediaType,
    43  		ConfigMediaType: types.OCIConfigJSON,
    44  		Annotations:     make(map[string]string),
    45  	}
    46  
    47  	for _, opt := range opts {
    48  		opt(o)
    49  	}
    50  
    51  	if o.Cert != nil {
    52  		o.Annotations[CertificateAnnotationKey] = string(o.Cert)
    53  		o.Annotations[ChainAnnotationKey] = string(o.Chain)
    54  	}
    55  
    56  	if o.Bundle != nil {
    57  		b, err := json.Marshal(o.Bundle)
    58  		if err != nil {
    59  			return nil, err
    60  		}
    61  		o.Annotations[BundleAnnotationKey] = string(b)
    62  	}
    63  
    64  	if o.RFC3161Timestamp != nil {
    65  		b, err := json.Marshal(o.RFC3161Timestamp)
    66  		if err != nil {
    67  			return nil, err
    68  		}
    69  		o.Annotations[RFC3161TimestampAnnotationKey] = string(b)
    70  	}
    71  	return o, nil
    72  }
    73  
    74  // WithLayerMediaType sets the media type of the signature.
    75  func WithLayerMediaType(mt types.MediaType) Option {
    76  	return func(o *options) {
    77  		o.LayerMediaType = mt
    78  	}
    79  }
    80  
    81  // WithConfigMediaType sets the media type of the signature.
    82  func WithConfigMediaType(mt types.MediaType) Option {
    83  	return func(o *options) {
    84  		o.ConfigMediaType = mt
    85  	}
    86  }
    87  
    88  // WithAnnotations sets the annotations that will be associated.
    89  func WithAnnotations(ann map[string]string) Option {
    90  	return func(o *options) {
    91  		o.Annotations = ann
    92  	}
    93  }
    94  
    95  // WithBundle sets the bundle to attach to the signature
    96  func WithBundle(b *bundle.RekorBundle) Option {
    97  	return func(o *options) {
    98  		o.Bundle = b
    99  	}
   100  }
   101  
   102  // WithRFC3161Timestamp sets the time-stamping bundle to attach to the signature
   103  func WithRFC3161Timestamp(b *bundle.RFC3161Timestamp) Option {
   104  	return func(o *options) {
   105  		o.RFC3161Timestamp = b
   106  	}
   107  }
   108  
   109  // WithCertChain sets the certificate chain for this signature.
   110  func WithCertChain(cert, chain []byte) Option {
   111  	return func(o *options) {
   112  		o.Cert = cert
   113  		o.Chain = chain
   114  	}
   115  }
   116  
   117  // WithRecordCreationTimestamp sets the feature flag to honor the creation timestamp to time of running
   118  func WithRecordCreationTimestamp(rct bool) Option {
   119  	return func(o *options) {
   120  		o.RecordCreationTimestamp = rct
   121  	}
   122  }
   123  

View as plain text