github.com/containerd/nerdctl@v1.7.7/pkg/signutil/signutil.go (about)

     1  /*
     2     Copyright The containerd 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  
    17  package signutil
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/containerd/log"
    24  	"github.com/containerd/nerdctl/pkg/api/types"
    25  )
    26  
    27  // Sign signs an image using a signer and options provided in options.
    28  func Sign(rawRef string, experimental bool, options types.ImageSignOptions) error {
    29  	switch options.Provider {
    30  	case "cosign":
    31  		if !experimental {
    32  			return fmt.Errorf("cosign only work with enable experimental feature")
    33  		}
    34  
    35  		if err := SignCosign(rawRef, options.CosignKey); err != nil {
    36  			return err
    37  		}
    38  	case "notation":
    39  		if !experimental {
    40  			return fmt.Errorf("notation only work with enable experimental feature")
    41  		}
    42  
    43  		if err := SignNotation(rawRef, options.NotationKeyName); err != nil {
    44  			return err
    45  		}
    46  	case "", "none":
    47  		log.L.Debugf("signing process skipped")
    48  	default:
    49  		return fmt.Errorf("no signers found: %s", options.Provider)
    50  	}
    51  	return nil
    52  }
    53  
    54  // Verify verifies an image using a verifier and options provided in options.
    55  func Verify(ctx context.Context, rawRef string, hostsDirs []string, experimental bool, options types.ImageVerifyOptions) (ref string, err error) {
    56  	switch options.Provider {
    57  	case "cosign":
    58  		if !experimental {
    59  			return "", fmt.Errorf("cosign only work with enable experimental feature")
    60  		}
    61  
    62  		if ref, err = VerifyCosign(ctx, rawRef, options.CosignKey, hostsDirs, options.CosignCertificateIdentity, options.CosignCertificateIdentityRegexp, options.CosignCertificateOidcIssuer, options.CosignCertificateOidcIssuerRegexp); err != nil {
    63  			return "", err
    64  		}
    65  	case "notation":
    66  		if !experimental {
    67  			return "", fmt.Errorf("notation only work with enable experimental feature")
    68  		}
    69  
    70  		if ref, err = VerifyNotation(ctx, rawRef, hostsDirs); err != nil {
    71  			return "", err
    72  		}
    73  	case "", "none":
    74  		ref = rawRef
    75  		log.G(ctx).Debugf("verifying process skipped")
    76  	default:
    77  		return "", fmt.Errorf("no verifiers found: %s", options.Provider)
    78  	}
    79  	return ref, nil
    80  }